test/mpool_tests.c

Fri, 11 Mar 2016 18:06:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 11 Mar 2016 18:06:27 +0100
changeset 219
2df780a4482b
parent 192
1e51558b9d09
child 225
a1a068c2c4ef
permissions
-rw-r--r--

fixed misaligned memory access in test_ucx_buffer_write

olaf@13 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@13 3 *
universe@192 4 * Copyright 2015 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@13 27 */
olaf@13 28
olaf@219 29 #include <inttypes.h>
olaf@30 30
olaf@13 31 #include "mpool_tests.h"
olaf@13 32
universe@134 33 UCX_TEST(test_ucx_mempool_new) {
universe@28 34 UcxMempool *pool = ucx_mempool_new(16);
universe@33 35 UCX_TEST_BEGIN
universe@40 36 UCX_TEST_ASSERT(pool->size == 16, "wrong size");
universe@40 37 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
universe@40 38 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
universe@33 39 UCX_TEST_END
olaf@113 40 ucx_mempool_destroy(pool);
olaf@13 41 }
olaf@13 42
universe@134 43 UCX_TEST(test_ucx_mempool_malloc) {
universe@28 44
universe@28 45 UcxMempool *pool = ucx_mempool_new(1);
universe@33 46 UCX_TEST_BEGIN
universe@32 47 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
universe@28 48
universe@40 49 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
universe@40 50 UCX_TEST_ASSERT(pool->size == 1, "chcap called");
universe@28 51
universe@32 52 intptr_t *pooladdr =
universe@32 53 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
universe@28 54 *pooladdr = 5;
universe@28 55
universe@40 56 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
universe@28 57
universe@33 58 UCX_TEST_END
olaf@113 59 ucx_mempool_destroy(pool);
olaf@13 60 }
olaf@13 61
universe@134 62 UCX_TEST(test_ucx_mempool_malloc_with_chcap) {
universe@28 63
universe@28 64 UcxMempool *pool = ucx_mempool_new(1);
universe@33 65 UCX_TEST_BEGIN
universe@28 66 ucx_mempool_malloc(pool, sizeof(int));
universe@32 67 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
universe@28 68
universe@40 69 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
universe@40 70 UCX_TEST_ASSERT(pool->size == 17, "chcap not called");
universe@28 71
universe@32 72 intptr_t *pooladdr =
universe@32 73 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
universe@28 74 *pooladdr = 5;
universe@28 75
universe@40 76 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
universe@28 77
universe@33 78 UCX_TEST_END
olaf@113 79 ucx_mempool_destroy(pool);
universe@28 80 }
universe@19 81
universe@134 82 UCX_TEST(test_ucx_mempool_calloc) {
universe@28 83
universe@28 84 UcxMempool *pool = ucx_mempool_new(1);
universe@33 85 UCX_TEST_BEGIN
universe@28 86
universe@32 87 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@28 88
universe@40 89 UCX_TEST_ASSERT(test != NULL, "no memory for test data");
universe@40 90 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
universe@28 91
universe@33 92 UCX_TEST_END
olaf@113 93 ucx_mempool_destroy(pool);
olaf@113 94 }
olaf@113 95
universe@134 96 UCX_TEST(test_ucx_mempool_free) {
olaf@113 97 UcxMempool *pool = ucx_mempool_new(16);
olaf@113 98 void *mem1;
olaf@113 99 void *mem2;
olaf@113 100
olaf@113 101 UCX_TEST_BEGIN
olaf@113 102
olaf@113 103 mem1 = ucx_mempool_malloc(pool, 16);
olaf@113 104 ucx_mempool_free(pool, mem1);
olaf@113 105
olaf@113 106 UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
olaf@113 107
olaf@113 108 ucx_mempool_malloc(pool, 16);
olaf@113 109 ucx_mempool_malloc(pool, 16);
olaf@113 110 mem1 = ucx_mempool_malloc(pool, 16);
olaf@113 111 ucx_mempool_malloc(pool, 16);
olaf@113 112 mem2 = ucx_mempool_malloc(pool, 16);
olaf@113 113
olaf@113 114 ucx_mempool_free(pool, mem1);
olaf@113 115
olaf@113 116 UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
olaf@113 117
olaf@113 118 ucx_mempool_free(pool, mem2);
olaf@113 119
olaf@113 120 UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
olaf@113 121
olaf@113 122 UCX_TEST_END
olaf@113 123 ucx_mempool_destroy(pool);
universe@28 124 }
olaf@13 125
olaf@131 126 UCX_EXTERN void test_setdestr(void* elem) {
olaf@30 127 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
universe@28 128 *cb = 42;
universe@28 129 }
olaf@13 130
universe@134 131 UCX_TEST(test_ucx_mempool_set_destr) {
universe@28 132
universe@33 133 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 134 UCX_TEST_BEGIN
universe@28 135 UcxMempool *pool = ucx_mempool_new(2);
universe@28 136
universe@32 137 ucx_mempool_malloc(pool, sizeof(intptr_t));
olaf@30 138 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@28 139
universe@40 140 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 141
olaf@30 142 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 143 *cb = 13;
universe@28 144
universe@28 145 ucx_mempool_set_destr(test, test_setdestr);
universe@28 146 UCX_TEST_ASSERT(
universe@28 147 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
universe@28 148 UCX_TEST_ASSERT(
olaf@30 149 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
universe@28 150
olaf@113 151 ucx_mempool_destroy(pool);
universe@28 152
universe@40 153 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@28 154
universe@28 155 UCX_TEST_END
universe@33 156 if (cb != NULL) free(cb);
universe@28 157 }
olaf@13 158
olaf@13 159
universe@134 160 UCX_TEST(test_ucx_mempool_reg_destr) {
olaf@14 161
universe@33 162 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
universe@33 163 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 164 UCX_TEST_BEGIN
universe@28 165 UcxMempool *pool = ucx_mempool_new(1);
universe@28 166
universe@40 167 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 168
olaf@30 169 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 170 *cb = 13;
universe@28 171
universe@28 172 ucx_mempool_reg_destr(pool, test, test_setdestr);
universe@28 173
universe@28 174 ucx_destructor *pooladdr = (ucx_destructor*)
olaf@30 175 ((char*)pool->data[0] + sizeof(ucx_destructor));
universe@28 176
universe@40 177 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
universe@28 178
olaf@113 179 ucx_mempool_destroy(pool);
universe@40 180 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@33 181 UCX_TEST_END
olaf@13 182
universe@33 183 if (test != NULL) free(test);
universe@33 184 if (cb != NULL) free(cb);
universe@28 185 }
olaf@13 186
universe@134 187 UCX_TEST(test_ucx_mempool_realloc) {
universe@33 188
universe@33 189 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 190 UCX_TEST_BEGIN
universe@28 191 UcxMempool *pool = ucx_mempool_new(2);
universe@28 192
universe@32 193 ucx_mempool_malloc(pool, sizeof(intptr_t));
olaf@30 194 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@33 195
universe@40 196 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 197
olaf@30 198 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 199 *cb = 13;
universe@28 200
universe@28 201 ucx_mempool_set_destr(test, test_setdestr);
universe@28 202
universe@69 203 intptr_t *rtest, n = 2;
universe@28 204 do {
universe@28 205 n *= 2;
universe@40 206 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
universe@69 207 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
universe@28 208 } while (rtest == test);
universe@28 209 test = rtest;
universe@28 210
universe@28 211 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
universe@28 212 "realloc killed destructor")
universe@28 213 UCX_TEST_ASSERT(
olaf@30 214 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
universe@28 215
olaf@113 216 ucx_mempool_destroy(pool);
universe@28 217
universe@40 218 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@28 219
universe@28 220 UCX_TEST_END
universe@33 221 if (cb != NULL) free(cb);
olaf@13 222 }

mercurial