tests/test_mempool.c

changeset 1336
5acc23b518aa
parent 1328
2cf66dee40b8
--- a/tests/test_mempool.c	Sun Jun 15 18:40:31 2025 +0200
+++ b/tests/test_mempool.c	Sat Jul 19 21:09:07 2025 +0200
@@ -399,6 +399,53 @@
     CX_TEST_DO {
         // allocate the first object
         int *c = cxMalloc(src->allocator, sizeof(int));
+        // check that the destructor functions are also transferred
+        cxMempoolSetDestructor(c, test_mempool_destructor);
+        // allocate the second object
+        c = cxMalloc(src->allocator, sizeof(int));
+        cxMempoolSetDestructor(c, test_mempool_destructor);
+
+
+        // check source pool
+        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->registered_size == 0);
+        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 == 2);
+        CX_TEST_ASSERT(src->registered_size == 0);
+        CX_TEST_ASSERT(dest->registered_size == 1); // 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);
+
+        // cover illegal arguments
+        result = cxMempoolTransfer(dest, dest);
+        CX_TEST_ASSERT(result != 0);
+
+        // verify that destroying new pool calls the destructors
+        // but only two times (the old allocator has a different destructor)
+        cxMempoolFree(dest);
+        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
+    }
+}
+
+CX_TEST(test_mempool_transfer_with_foreign_memory) {
+    CxMempool *src = cxMempoolCreateSimple(4);
+    CxMempool *dest = cxMempoolCreateSimple(4);
+    CX_TEST_DO {
+        // allocate the first object
+        int *c = cxMalloc(src->allocator, sizeof(int));
         // allocate the second object
         c = cxMalloc(src->allocator, sizeof(int));
         // check that the destructor functions are also transferred
@@ -430,10 +477,6 @@
         cxMempoolFree(src);
         CX_TEST_ASSERT(test_mempool_destructor_called == 0);
 
-        // cover illegal arguments
-        result = cxMempoolTransfer(dest, dest);
-        CX_TEST_ASSERT(result != 0);
-
         // verify that destroying new pool calls the destructors
         // but only two times (the old allocator has a different destructor)
         cxMempoolFree(dest);
@@ -444,6 +487,48 @@
     }
 }
 
+CX_TEST(test_mempool_transfer_foreign_memory_only) {
+    CxMempool *src = cxMempoolCreateSimple(4);
+    CxMempool *dest = cxMempoolCreateSimple(4);
+    int *a = malloc(sizeof(int));
+    int *b = malloc(sizeof(int));
+    CX_TEST_DO {
+        // register foreign objects
+        cxMempoolRegister(src, a, test_mempool_destructor);
+        cxMempoolRegister(src, b, test_mempool_destructor);
+
+        // check source pool
+        CX_TEST_ASSERT(src->size == 0);
+        CX_TEST_ASSERT(src->registered_size == 2);
+        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 == 0);
+        CX_TEST_ASSERT(src->registered_size == 0);
+        CX_TEST_ASSERT(dest->registered_size == 3); // 2 objects + 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 two times (the old allocator has a different destructor)
+        cxMempoolFree(dest);
+        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
+    }
+    free(a);
+    free(b);
+}
+
 CX_TEST(test_mempool_transfer_object) {
     CxMempool *src = cxMempoolCreateSimple(4);
     CxMempool *dest = cxMempoolCreateSimple(4);
@@ -518,6 +603,8 @@
     cx_test_register(suite, test_mempool_register);
     cx_test_register(suite, test_mempool_register2);
     cx_test_register(suite, test_mempool_transfer);
+    cx_test_register(suite, test_mempool_transfer_with_foreign_memory);
+    cx_test_register(suite, test_mempool_transfer_foreign_memory_only);
     cx_test_register(suite, test_mempool_transfer_object);
 
     return suite;

mercurial