test/mpool_tests.c

Sat, 28 Oct 2017 15:43:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:43:51 +0200
changeset 259
2f5dea574a75
parent 253
e19825a1430a
child 270
3d80d425543b
permissions
-rw-r--r--

modules documentation

olaf@13 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@13 3 *
universe@259 4 * Copyright 2017 Mike Becker, 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@241 70 UCX_TEST_ASSERT(pool->size == 2, "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
universe@253 126 #ifdef __cplusplus
universe@253 127 extern "C"
universe@253 128 #endif
universe@253 129 void test_setdestr(void* elem) {
olaf@30 130 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
universe@28 131 *cb = 42;
universe@28 132 }
olaf@13 133
universe@134 134 UCX_TEST(test_ucx_mempool_set_destr) {
universe@28 135
universe@33 136 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 137 UCX_TEST_BEGIN
universe@28 138 UcxMempool *pool = ucx_mempool_new(2);
universe@28 139
universe@32 140 ucx_mempool_malloc(pool, sizeof(intptr_t));
olaf@30 141 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@28 142
universe@40 143 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 144
olaf@30 145 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 146 *cb = 13;
universe@28 147
universe@28 148 ucx_mempool_set_destr(test, test_setdestr);
universe@28 149 UCX_TEST_ASSERT(
universe@28 150 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
universe@28 151 UCX_TEST_ASSERT(
olaf@30 152 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
universe@28 153
olaf@113 154 ucx_mempool_destroy(pool);
universe@28 155
universe@40 156 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@28 157
universe@28 158 UCX_TEST_END
universe@33 159 if (cb != NULL) free(cb);
universe@28 160 }
olaf@13 161
olaf@13 162
universe@134 163 UCX_TEST(test_ucx_mempool_reg_destr) {
olaf@14 164
universe@33 165 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
universe@33 166 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 167 UCX_TEST_BEGIN
universe@28 168 UcxMempool *pool = ucx_mempool_new(1);
universe@28 169
universe@40 170 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 171
olaf@30 172 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 173 *cb = 13;
universe@28 174
universe@28 175 ucx_mempool_reg_destr(pool, test, test_setdestr);
universe@28 176
universe@28 177 ucx_destructor *pooladdr = (ucx_destructor*)
olaf@30 178 ((char*)pool->data[0] + sizeof(ucx_destructor));
universe@28 179
universe@40 180 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
universe@28 181
olaf@113 182 ucx_mempool_destroy(pool);
universe@40 183 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@33 184 UCX_TEST_END
olaf@13 185
universe@33 186 if (test != NULL) free(test);
universe@33 187 if (cb != NULL) free(cb);
universe@28 188 }
olaf@13 189
universe@134 190 UCX_TEST(test_ucx_mempool_realloc) {
universe@33 191
universe@33 192 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 193 UCX_TEST_BEGIN
universe@28 194 UcxMempool *pool = ucx_mempool_new(2);
universe@28 195
universe@32 196 ucx_mempool_malloc(pool, sizeof(intptr_t));
olaf@30 197 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@33 198
universe@40 199 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 200
olaf@30 201 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 202 *cb = 13;
universe@28 203
universe@28 204 ucx_mempool_set_destr(test, test_setdestr);
universe@28 205
universe@69 206 intptr_t *rtest, n = 2;
universe@28 207 do {
universe@28 208 n *= 2;
universe@40 209 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
universe@69 210 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
universe@28 211 } while (rtest == test);
universe@28 212 test = rtest;
universe@28 213
universe@28 214 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
universe@28 215 "realloc killed destructor")
universe@28 216 UCX_TEST_ASSERT(
olaf@30 217 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
universe@28 218
olaf@113 219 ucx_mempool_destroy(pool);
universe@28 220
universe@40 221 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@28 222
universe@28 223 UCX_TEST_END
universe@33 224 if (cb != NULL) free(cb);
olaf@13 225 }

mercurial