162 cxMempoolFree(pool); |
162 cxMempoolFree(pool); |
163 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
163 CX_TEST_ASSERT(test_mempool_destructor_called == 2); |
164 } |
164 } |
165 } |
165 } |
166 |
166 |
|
167 CX_TEST(test_mempool_transfer) { |
|
168 CxMempool *src = cxMempoolCreateSimple(4); |
|
169 CxMempool *dest = cxMempoolCreateSimple(4); |
|
170 CX_TEST_DO { |
|
171 // check that the destructor functions are also transferred |
|
172 src->auto_destr = test_mempool_destructor; |
|
173 |
|
174 // allocate first object |
|
175 int *a = cxMalloc(src->allocator, sizeof(int)); |
|
176 // allocate second object |
|
177 int *b = cxMalloc(src->allocator, sizeof(int)); |
|
178 // register foreign object |
|
179 int *c = malloc(sizeof(int)); |
|
180 cxMempoolRegister(src, c, test_mempool_destructor); |
|
181 |
|
182 // check source pool |
|
183 CX_TEST_ASSERT(src->size == 3); |
|
184 const CxAllocator *old_allocator = src->allocator; |
|
185 CX_TEST_ASSERT(old_allocator->data == src); |
|
186 |
|
187 // perform transfer |
|
188 int result = cxMempoolTransfer(src, dest); |
|
189 CX_TEST_ASSERT(result == 0); |
|
190 |
|
191 // check transfer |
|
192 CX_TEST_ASSERT(src->size == 0); |
|
193 CX_TEST_ASSERT(dest->size == 4); // 3 objects + the old allocator |
|
194 CX_TEST_ASSERT(src->allocator != old_allocator); |
|
195 CX_TEST_ASSERT(old_allocator->data == dest); |
|
196 |
|
197 // verify that destroying old pool does nothing |
|
198 test_mempool_destructor_called = 0; |
|
199 cxMempoolFree(src); |
|
200 CX_TEST_ASSERT(test_mempool_destructor_called == 0); |
|
201 |
|
202 // verify that destroying new pool calls the destructors |
|
203 // but only three times (the old allocator has a different destructor) |
|
204 cxMempoolFree(dest); |
|
205 CX_TEST_ASSERT(test_mempool_destructor_called == 3); |
|
206 |
|
207 // free the foreign object |
|
208 free(c); |
|
209 } |
|
210 } |
167 |
211 |
168 CxTestSuite *cx_test_suite_mempool(void) { |
212 CxTestSuite *cx_test_suite_mempool(void) { |
169 CxTestSuite *suite = cx_test_suite_new("mempool"); |
213 CxTestSuite *suite = cx_test_suite_new("mempool"); |
170 |
214 |
171 cx_test_register(suite, test_mempool_create); |
215 cx_test_register(suite, test_mempool_create); |
173 cx_test_register(suite, test_mempool_calloc); |
217 cx_test_register(suite, test_mempool_calloc); |
174 cx_test_register(suite, test_mempool_realloc); |
218 cx_test_register(suite, test_mempool_realloc); |
175 cx_test_register(suite, test_mempool_free); |
219 cx_test_register(suite, test_mempool_free); |
176 cx_test_register(suite, test_mempool_destroy); |
220 cx_test_register(suite, test_mempool_destroy); |
177 cx_test_register(suite, test_mempool_register); |
221 cx_test_register(suite, test_mempool_register); |
|
222 cx_test_register(suite, test_mempool_transfer); |
178 |
223 |
179 return suite; |
224 return suite; |
180 } |
225 } |