35 UCX_TEST_BEGIN |
35 UCX_TEST_BEGIN |
36 UCX_TEST_ASSERT(pool->size == 16, "wrong size"); |
36 UCX_TEST_ASSERT(pool->size == 16, "wrong size"); |
37 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter"); |
37 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter"); |
38 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed"); |
38 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed"); |
39 UCX_TEST_END |
39 UCX_TEST_END |
40 ucx_mempool_free(pool); |
40 ucx_mempool_destroy(pool); |
41 } |
41 } |
42 |
42 |
43 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { |
43 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { |
44 |
44 |
45 UcxMempool *pool = ucx_mempool_new(1); |
45 UcxMempool *pool = ucx_mempool_new(1); |
88 |
88 |
89 UCX_TEST_ASSERT(test != NULL, "no memory for test data"); |
89 UCX_TEST_ASSERT(test != NULL, "no memory for test data"); |
90 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed"); |
90 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed"); |
91 |
91 |
92 UCX_TEST_END |
92 UCX_TEST_END |
93 ucx_mempool_free(pool); |
93 ucx_mempool_destroy(pool); |
|
94 } |
|
95 |
|
96 UCX_TEST_IMPLEMENT(test_ucx_mempool_free) { |
|
97 UcxMempool *pool = ucx_mempool_new(16); |
|
98 void *mem1; |
|
99 void *mem2; |
|
100 |
|
101 UCX_TEST_BEGIN |
|
102 |
|
103 mem1 = ucx_mempool_malloc(pool, 16); |
|
104 ucx_mempool_free(pool, mem1); |
|
105 |
|
106 UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty"); |
|
107 |
|
108 ucx_mempool_malloc(pool, 16); |
|
109 ucx_mempool_malloc(pool, 16); |
|
110 mem1 = ucx_mempool_malloc(pool, 16); |
|
111 ucx_mempool_malloc(pool, 16); |
|
112 mem2 = ucx_mempool_malloc(pool, 16); |
|
113 |
|
114 ucx_mempool_free(pool, mem1); |
|
115 |
|
116 UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size"); |
|
117 |
|
118 ucx_mempool_free(pool, mem2); |
|
119 |
|
120 UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size"); |
|
121 |
|
122 UCX_TEST_END |
|
123 ucx_mempool_destroy(pool); |
94 } |
124 } |
95 |
125 |
96 void test_setdestr(void* elem) { |
126 void test_setdestr(void* elem) { |
97 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
127 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
98 *cb = 42; |
128 *cb = 42; |
116 UCX_TEST_ASSERT( |
146 UCX_TEST_ASSERT( |
117 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") |
147 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") |
118 UCX_TEST_ASSERT( |
148 UCX_TEST_ASSERT( |
119 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
149 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
120 |
150 |
121 ucx_mempool_free(pool); |
151 ucx_mempool_destroy(pool); |
122 |
152 |
123 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
153 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
124 |
154 |
125 UCX_TEST_END |
155 UCX_TEST_END |
126 if (cb != NULL) free(cb); |
156 if (cb != NULL) free(cb); |
144 ucx_destructor *pooladdr = (ucx_destructor*) |
174 ucx_destructor *pooladdr = (ucx_destructor*) |
145 ((char*)pool->data[0] + sizeof(ucx_destructor)); |
175 ((char*)pool->data[0] + sizeof(ucx_destructor)); |
146 |
176 |
147 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed"); |
177 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed"); |
148 |
178 |
149 ucx_mempool_free(pool); |
179 ucx_mempool_destroy(pool); |
150 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
180 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
151 UCX_TEST_END |
181 UCX_TEST_END |
152 |
182 |
153 if (test != NULL) free(test); |
183 if (test != NULL) free(test); |
154 if (cb != NULL) free(cb); |
184 if (cb != NULL) free(cb); |
181 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, |
211 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, |
182 "realloc killed destructor") |
212 "realloc killed destructor") |
183 UCX_TEST_ASSERT( |
213 UCX_TEST_ASSERT( |
184 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
214 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
185 |
215 |
186 ucx_mempool_free(pool); |
216 ucx_mempool_destroy(pool); |
187 |
217 |
188 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
218 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
189 |
219 |
190 UCX_TEST_END |
220 UCX_TEST_END |
191 if (cb != NULL) free(cb); |
221 if (cb != NULL) free(cb); |