Fri, 11 May 2018 18:13:54 +0200
adds ucx_sstrcmp() compare function to utils.h
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 | |
olaf@270 | 78 | // overflow test |
olaf@270 | 79 | void *n0 = ucx_mempool_malloc(pool, (size_t)-1); |
olaf@270 | 80 | void *n1 = ucx_mempool_malloc(pool, ((size_t)-1) - sizeof(void*)/2); |
olaf@270 | 81 | |
olaf@270 | 82 | UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes"); |
olaf@270 | 83 | UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow"); |
olaf@270 | 84 | |
universe@33 | 85 | UCX_TEST_END |
olaf@113 | 86 | ucx_mempool_destroy(pool); |
universe@28 | 87 | } |
universe@19 | 88 | |
universe@134 | 89 | UCX_TEST(test_ucx_mempool_calloc) { |
universe@28 | 90 | |
universe@28 | 91 | UcxMempool *pool = ucx_mempool_new(1); |
universe@33 | 92 | UCX_TEST_BEGIN |
universe@28 | 93 | |
universe@32 | 94 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
universe@28 | 95 | |
universe@40 | 96 | UCX_TEST_ASSERT(test != NULL, "no memory for test data"); |
universe@40 | 97 | UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed"); |
universe@28 | 98 | |
olaf@270 | 99 | // overflow test |
olaf@270 | 100 | void *n0 = ucx_mempool_calloc(pool, (size_t)-1, 1); |
olaf@270 | 101 | void *n1 = ucx_mempool_calloc(pool, ((size_t)-1)/2, 3); |
olaf@270 | 102 | |
olaf@270 | 103 | UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes"); |
olaf@270 | 104 | UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow"); |
olaf@270 | 105 | |
universe@33 | 106 | UCX_TEST_END |
olaf@113 | 107 | ucx_mempool_destroy(pool); |
olaf@113 | 108 | } |
olaf@113 | 109 | |
universe@134 | 110 | UCX_TEST(test_ucx_mempool_free) { |
olaf@113 | 111 | UcxMempool *pool = ucx_mempool_new(16); |
olaf@113 | 112 | void *mem1; |
olaf@113 | 113 | void *mem2; |
olaf@113 | 114 | |
olaf@113 | 115 | UCX_TEST_BEGIN |
olaf@113 | 116 | |
olaf@113 | 117 | mem1 = ucx_mempool_malloc(pool, 16); |
olaf@113 | 118 | ucx_mempool_free(pool, mem1); |
olaf@113 | 119 | |
olaf@113 | 120 | UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty"); |
olaf@113 | 121 | |
olaf@113 | 122 | ucx_mempool_malloc(pool, 16); |
olaf@113 | 123 | ucx_mempool_malloc(pool, 16); |
olaf@113 | 124 | mem1 = ucx_mempool_malloc(pool, 16); |
olaf@113 | 125 | ucx_mempool_malloc(pool, 16); |
olaf@113 | 126 | mem2 = ucx_mempool_malloc(pool, 16); |
olaf@113 | 127 | |
olaf@113 | 128 | ucx_mempool_free(pool, mem1); |
olaf@113 | 129 | |
olaf@113 | 130 | UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size"); |
olaf@113 | 131 | |
olaf@113 | 132 | ucx_mempool_free(pool, mem2); |
olaf@113 | 133 | |
olaf@113 | 134 | UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size"); |
olaf@113 | 135 | |
olaf@113 | 136 | UCX_TEST_END |
olaf@113 | 137 | ucx_mempool_destroy(pool); |
universe@28 | 138 | } |
olaf@13 | 139 | |
universe@253 | 140 | #ifdef __cplusplus |
universe@253 | 141 | extern "C" |
universe@253 | 142 | #endif |
universe@253 | 143 | void test_setdestr(void* elem) { |
olaf@30 | 144 | intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
universe@28 | 145 | *cb = 42; |
universe@28 | 146 | } |
olaf@13 | 147 | |
universe@134 | 148 | UCX_TEST(test_ucx_mempool_set_destr) { |
universe@28 | 149 | |
universe@33 | 150 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
universe@33 | 151 | UCX_TEST_BEGIN |
universe@28 | 152 | UcxMempool *pool = ucx_mempool_new(2); |
universe@28 | 153 | |
universe@32 | 154 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
olaf@30 | 155 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
universe@28 | 156 | |
universe@40 | 157 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
universe@28 | 158 | |
olaf@30 | 159 | test[0] = 5; test[1] = (intptr_t) cb; |
universe@28 | 160 | *cb = 13; |
universe@28 | 161 | |
universe@28 | 162 | ucx_mempool_set_destr(test, test_setdestr); |
universe@28 | 163 | UCX_TEST_ASSERT( |
universe@28 | 164 | *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") |
universe@28 | 165 | UCX_TEST_ASSERT( |
olaf@30 | 166 | test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
universe@28 | 167 | |
olaf@113 | 168 | ucx_mempool_destroy(pool); |
universe@28 | 169 | |
universe@40 | 170 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
universe@28 | 171 | |
universe@28 | 172 | UCX_TEST_END |
universe@33 | 173 | if (cb != NULL) free(cb); |
universe@28 | 174 | } |
olaf@13 | 175 | |
olaf@13 | 176 | |
universe@134 | 177 | UCX_TEST(test_ucx_mempool_reg_destr) { |
olaf@14 | 178 | |
universe@33 | 179 | intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
universe@33 | 180 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
universe@33 | 181 | UCX_TEST_BEGIN |
universe@28 | 182 | UcxMempool *pool = ucx_mempool_new(1); |
universe@28 | 183 | |
universe@40 | 184 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
universe@28 | 185 | |
olaf@30 | 186 | test[0] = 5; test[1] = (intptr_t) cb; |
universe@28 | 187 | *cb = 13; |
universe@28 | 188 | |
universe@28 | 189 | ucx_mempool_reg_destr(pool, test, test_setdestr); |
universe@28 | 190 | |
universe@28 | 191 | ucx_destructor *pooladdr = (ucx_destructor*) |
olaf@30 | 192 | ((char*)pool->data[0] + sizeof(ucx_destructor)); |
universe@28 | 193 | |
universe@40 | 194 | UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed"); |
universe@28 | 195 | |
olaf@113 | 196 | ucx_mempool_destroy(pool); |
universe@40 | 197 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
universe@33 | 198 | UCX_TEST_END |
olaf@13 | 199 | |
universe@33 | 200 | if (test != NULL) free(test); |
universe@33 | 201 | if (cb != NULL) free(cb); |
universe@28 | 202 | } |
olaf@13 | 203 | |
universe@134 | 204 | UCX_TEST(test_ucx_mempool_realloc) { |
universe@33 | 205 | |
universe@33 | 206 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
universe@33 | 207 | UCX_TEST_BEGIN |
universe@28 | 208 | UcxMempool *pool = ucx_mempool_new(2); |
universe@28 | 209 | |
universe@32 | 210 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
olaf@30 | 211 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
universe@33 | 212 | |
universe@40 | 213 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
universe@28 | 214 | |
olaf@30 | 215 | test[0] = 5; test[1] = (intptr_t) cb; |
universe@28 | 216 | *cb = 13; |
universe@28 | 217 | |
universe@28 | 218 | ucx_mempool_set_destr(test, test_setdestr); |
universe@28 | 219 | |
universe@69 | 220 | intptr_t *rtest, n = 2; |
universe@28 | 221 | do { |
universe@28 | 222 | n *= 2; |
universe@40 | 223 | UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc"); |
universe@69 | 224 | rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); |
universe@28 | 225 | } while (rtest == test); |
universe@28 | 226 | test = rtest; |
universe@28 | 227 | |
universe@28 | 228 | UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, |
universe@28 | 229 | "realloc killed destructor") |
universe@28 | 230 | UCX_TEST_ASSERT( |
olaf@30 | 231 | test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
universe@28 | 232 | |
olaf@113 | 233 | ucx_mempool_destroy(pool); |
universe@28 | 234 | |
universe@40 | 235 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
universe@28 | 236 | |
universe@28 | 237 | UCX_TEST_END |
universe@33 | 238 | if (cb != NULL) free(cb); |
olaf@13 | 239 | } |