--- a/tests/test_mempool.c Fri Apr 11 09:15:21 2025 +0200 +++ b/tests/test_mempool.c Fri Apr 11 13:20:07 2025 +0200 @@ -164,6 +164,50 @@ } } +CX_TEST(test_mempool_transfer) { + CxMempool *src = cxMempoolCreateSimple(4); + CxMempool *dest = cxMempoolCreateSimple(4); + CX_TEST_DO { + // check that the destructor functions are also transferred + src->auto_destr = test_mempool_destructor; + + // allocate first object + int *a = cxMalloc(src->allocator, sizeof(int)); + // allocate second object + int *b = cxMalloc(src->allocator, sizeof(int)); + // register foreign object + int *c = malloc(sizeof(int)); + cxMempoolRegister(src, c, test_mempool_destructor); + + // check source pool + CX_TEST_ASSERT(src->size == 3); + const CxAllocator *old_allocator = src->allocator; + CX_TEST_ASSERT(old_allocator->data == src); + + // perform transfer + int result = cxMempoolTransfer(src, dest); + CX_TEST_ASSERT(result == 0); + + // check transfer + CX_TEST_ASSERT(src->size == 0); + CX_TEST_ASSERT(dest->size == 4); // 3 objects + the old allocator + CX_TEST_ASSERT(src->allocator != old_allocator); + CX_TEST_ASSERT(old_allocator->data == dest); + + // verify that destroying old pool does nothing + test_mempool_destructor_called = 0; + cxMempoolFree(src); + CX_TEST_ASSERT(test_mempool_destructor_called == 0); + + // verify that destroying new pool calls the destructors + // but only three times (the old allocator has a different destructor) + cxMempoolFree(dest); + CX_TEST_ASSERT(test_mempool_destructor_called == 3); + + // free the foreign object + free(c); + } +} CxTestSuite *cx_test_suite_mempool(void) { CxTestSuite *suite = cx_test_suite_new("mempool"); @@ -175,6 +219,7 @@ cx_test_register(suite, test_mempool_free); cx_test_register(suite, test_mempool_destroy); cx_test_register(suite, test_mempool_register); + cx_test_register(suite, test_mempool_transfer); return suite; }