Thu, 28 Feb 2013 08:50:24 +0100
added license and copyright notice to all files
olaf@13 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@13 | 3 | * |
universe@103 | 4 | * Copyright 2013 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 | |
universe@69 | 29 | #include <stdint.h> |
olaf@30 | 30 | |
olaf@13 | 31 | #include "mpool_tests.h" |
olaf@13 | 32 | |
universe@33 | 33 | UCX_TEST_IMPLEMENT(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 |
universe@28 | 40 | ucx_mempool_free(pool); |
olaf@13 | 41 | } |
olaf@13 | 42 | |
universe@33 | 43 | UCX_TEST_IMPLEMENT(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 |
universe@28 | 59 | ucx_mempool_free(pool); |
olaf@13 | 60 | } |
olaf@13 | 61 | |
universe@33 | 62 | UCX_TEST_IMPLEMENT(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 |
universe@28 | 79 | ucx_mempool_free(pool); |
universe@28 | 80 | } |
universe@19 | 81 | |
universe@33 | 82 | UCX_TEST_IMPLEMENT(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 |
universe@28 | 93 | ucx_mempool_free(pool); |
universe@28 | 94 | } |
olaf@13 | 95 | |
universe@28 | 96 | void test_setdestr(void* elem) { |
olaf@30 | 97 | intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
universe@28 | 98 | *cb = 42; |
universe@28 | 99 | } |
olaf@13 | 100 | |
universe@33 | 101 | UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) { |
universe@28 | 102 | |
universe@33 | 103 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
universe@33 | 104 | UCX_TEST_BEGIN |
universe@28 | 105 | UcxMempool *pool = ucx_mempool_new(2); |
universe@28 | 106 | |
universe@32 | 107 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
olaf@30 | 108 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
universe@28 | 109 | |
universe@40 | 110 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
universe@28 | 111 | |
olaf@30 | 112 | test[0] = 5; test[1] = (intptr_t) cb; |
universe@28 | 113 | *cb = 13; |
universe@28 | 114 | |
universe@28 | 115 | ucx_mempool_set_destr(test, test_setdestr); |
universe@28 | 116 | UCX_TEST_ASSERT( |
universe@28 | 117 | *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") |
universe@28 | 118 | UCX_TEST_ASSERT( |
olaf@30 | 119 | test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
universe@28 | 120 | |
universe@28 | 121 | ucx_mempool_free(pool); |
universe@28 | 122 | |
universe@40 | 123 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
universe@28 | 124 | |
universe@28 | 125 | UCX_TEST_END |
universe@33 | 126 | if (cb != NULL) free(cb); |
universe@28 | 127 | } |
olaf@13 | 128 | |
olaf@13 | 129 | |
universe@33 | 130 | UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) { |
olaf@14 | 131 | |
universe@33 | 132 | intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
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(1); |
universe@28 | 136 | |
universe@40 | 137 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
universe@28 | 138 | |
olaf@30 | 139 | test[0] = 5; test[1] = (intptr_t) cb; |
universe@28 | 140 | *cb = 13; |
universe@28 | 141 | |
universe@28 | 142 | ucx_mempool_reg_destr(pool, test, test_setdestr); |
universe@28 | 143 | |
universe@28 | 144 | ucx_destructor *pooladdr = (ucx_destructor*) |
olaf@30 | 145 | ((char*)pool->data[0] + sizeof(ucx_destructor)); |
universe@28 | 146 | |
universe@40 | 147 | UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed"); |
universe@28 | 148 | |
universe@28 | 149 | ucx_mempool_free(pool); |
universe@40 | 150 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
universe@33 | 151 | UCX_TEST_END |
olaf@13 | 152 | |
universe@33 | 153 | if (test != NULL) free(test); |
universe@33 | 154 | if (cb != NULL) free(cb); |
universe@28 | 155 | } |
olaf@13 | 156 | |
universe@33 | 157 | UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) { |
universe@33 | 158 | |
universe@33 | 159 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
universe@33 | 160 | UCX_TEST_BEGIN |
universe@28 | 161 | UcxMempool *pool = ucx_mempool_new(2); |
universe@28 | 162 | |
universe@32 | 163 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
olaf@30 | 164 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
universe@33 | 165 | |
universe@40 | 166 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
universe@28 | 167 | |
olaf@30 | 168 | test[0] = 5; test[1] = (intptr_t) cb; |
universe@28 | 169 | *cb = 13; |
universe@28 | 170 | |
universe@28 | 171 | ucx_mempool_set_destr(test, test_setdestr); |
universe@28 | 172 | |
universe@69 | 173 | intptr_t *rtest, n = 2; |
universe@28 | 174 | do { |
universe@28 | 175 | n *= 2; |
universe@40 | 176 | UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc"); |
universe@69 | 177 | rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); |
universe@28 | 178 | } while (rtest == test); |
universe@28 | 179 | test = rtest; |
universe@28 | 180 | |
universe@28 | 181 | UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, |
universe@28 | 182 | "realloc killed destructor") |
universe@28 | 183 | UCX_TEST_ASSERT( |
olaf@30 | 184 | test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
universe@28 | 185 | |
universe@28 | 186 | ucx_mempool_free(pool); |
universe@28 | 187 | |
universe@40 | 188 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
universe@28 | 189 | |
universe@28 | 190 | UCX_TEST_END |
universe@33 | 191 | if (cb != NULL) free(cb); |
olaf@13 | 192 | } |