Tue, 21 Feb 2012 01:13:17 +0100
fixed map with the help of new tests
| 13 | 1 | /* |
| 2 | * | |
| 3 | */ | |
| 4 | ||
| 5 | #include "mpool_tests.h" | |
| 6 | ||
| 28 | 7 | UCX_TEST_BEGIN(test_ucx_mempool_new) { |
| 8 | UcxMempool *pool = ucx_mempool_new(16); | |
| 9 | ||
| 10 | UCX_TEST_ASSERT(pool->size == 16, "wrong size") | |
| 11 | UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter") | |
| 12 | UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed") | |
| 13 | ||
| 14 | ucx_mempool_free(pool); | |
| 15 | ||
| 16 | UCX_TEST_END | |
| 17 | } | |
| 13 | 18 | |
| 28 | 19 | UCX_TEST_BEGIN(test_ucx_mempool_malloc) { |
| 20 | ||
| 21 | UcxMempool *pool = ucx_mempool_new(1); | |
| 22 | ||
| 23 | int *test = (int*) ucx_mempool_malloc(pool, sizeof(int)); | |
| 24 | ||
| 25 | UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") | |
| 26 | UCX_TEST_ASSERT(pool->size == 1, "chcap called") | |
| 27 | ||
| 28 | int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor)); | |
| 29 | *pooladdr = 5; | |
| 30 | ||
| 31 | UCX_TEST_ASSERT(*test == 5, "wrong pointer") | |
| 32 | ||
| 33 | ucx_mempool_free(pool); | |
| 34 | ||
| 35 | UCX_TEST_END | |
| 13 | 36 | } |
| 37 | ||
| 28 | 38 | UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) { |
| 39 | ||
| 40 | UcxMempool *pool = ucx_mempool_new(1); | |
| 41 | ||
| 42 | ucx_mempool_malloc(pool, sizeof(int)); | |
| 43 | int *test = (int*) ucx_mempool_malloc(pool, sizeof(int)); | |
| 44 | ||
| 45 | UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented") | |
| 46 | UCX_TEST_ASSERT(pool->size == 17, "chcap not called") | |
| 47 | ||
| 48 | int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor)); | |
| 49 | *pooladdr = 5; | |
| 50 | ||
| 51 | UCX_TEST_ASSERT(*test == 5, "wrong pointer") | |
| 52 | ||
| 53 | ucx_mempool_free(pool); | |
| 54 | ||
| 55 | UCX_TEST_END | |
| 56 | } | |
| 57 | ||
| 58 | UCX_TEST_BEGIN(test_ucx_mempool_calloc) { | |
| 59 | ||
| 60 | UcxMempool *pool = ucx_mempool_new(1); | |
| 61 | ||
| 62 | int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); | |
| 63 | ||
| 64 | UCX_TEST_ASSERT(test != NULL, "no memory for test data") | |
| 65 | UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") | |
| 66 | ||
| 67 | ucx_mempool_free(pool); | |
| 68 | ||
| 69 | UCX_TEST_END | |
| 70 | } | |
| 71 | ||
| 72 | void test_setdestr(void* elem) { | |
| 73 | int *cb = (int*) ((int*) elem)[1]; | |
| 74 | *cb = 42; | |
| 13 | 75 | } |
| 76 | ||
| 28 | 77 | UCX_TEST_BEGIN(test_ucx_mempool_set_destr) { |
| 78 | ||
| 79 | UcxMempool *pool = ucx_mempool_new(2); | |
| 80 | ||
| 81 | ucx_mempool_malloc(pool, sizeof(int)); | |
| 82 | int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); | |
| 83 | ||
| 84 | int *cb = (int*) malloc(sizeof(int)); | |
| 85 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") | |
| 86 | ||
| 87 | test[0] = 5; test[1] = (int) cb; | |
| 88 | *cb = 13; | |
| 89 | ||
| 90 | ucx_mempool_set_destr(test, test_setdestr); | |
| 91 | ||
| 92 | UCX_TEST_ASSERT( | |
| 93 | *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") | |
| 94 | UCX_TEST_ASSERT( | |
| 95 | test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data") | |
| 96 | ||
| 97 | ucx_mempool_free(pool); | |
| 98 | ||
| 99 | UCX_TEST_ASSERT(*cb == 42, "destructor not called") | |
| 13 | 100 | |
| 28 | 101 | free(cb); |
| 102 | ||
| 103 | UCX_TEST_END | |
| 104 | } | |
| 13 | 105 | |
| 106 | ||
| 28 | 107 | UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) { |
| 108 | ||
| 109 | UcxMempool *pool = ucx_mempool_new(1); | |
| 110 | ||
| 111 | int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); | |
| 112 | ||
| 113 | int *cb = (int*) malloc(sizeof(int)); | |
| 114 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") | |
| 115 | ||
| 116 | test[0] = 5; test[1] = (int) cb; | |
| 117 | *cb = 13; | |
| 118 | ||
| 119 | ucx_mempool_reg_destr(pool, test, test_setdestr); | |
| 120 | ||
| 121 | ucx_destructor *pooladdr = (ucx_destructor*) | |
| 122 | ((char*)pool->data[1] + sizeof(ucx_destructor)); | |
| 123 | ||
| 124 | UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") | |
| 125 | ||
| 126 | ucx_mempool_free(pool); | |
| 127 | ||
| 128 | UCX_TEST_ASSERT(*cb == 42, "destructor not called") | |
| 129 | ||
| 130 | free(cb); | |
| 131 | ||
| 132 | UCX_TEST_END | |
| 133 | } | |
| 13 | 134 | |
| 28 | 135 | UCX_TEST_BEGIN(test_ucx_mempool_realloc) { |
| 136 | ||
| 137 | UcxMempool *pool = ucx_mempool_new(2); | |
| 138 | ||
| 139 | ucx_mempool_malloc(pool, sizeof(int)); | |
| 140 | int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); | |
| 141 | ||
| 142 | int *cb = (int*) malloc(sizeof(int)); | |
| 143 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") | |
| 144 | ||
| 145 | test[0] = 5; test[1] = (int) cb; | |
| 146 | *cb = 13; | |
| 147 | ||
| 148 | ucx_mempool_set_destr(test, test_setdestr); | |
| 14 | 149 | |
| 28 | 150 | int *rtest, n = 2; |
| 151 | do { | |
| 152 | n *= 2; | |
| 153 | UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc") | |
| 154 | rtest = ucx_mempool_realloc(pool, test, n*sizeof(int)); | |
| 155 | } while (rtest == test); | |
| 156 | test = rtest; | |
| 157 | ||
| 158 | UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, | |
| 159 | "realloc killed destructor") | |
| 160 | UCX_TEST_ASSERT( | |
| 161 | test[0] == 5 && test[1] == (int) cb, "realloc destroyed data") | |
| 162 | ||
| 163 | ucx_mempool_free(pool); | |
| 164 | ||
| 165 | UCX_TEST_ASSERT(*cb == 42, "destructor not called") | |
| 13 | 166 | |
| 28 | 167 | free(cb); |
| 13 | 168 | |
| 28 | 169 | UCX_TEST_END |
| 13 | 170 | } |