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