tests/test_mempool.c

changeset 1323
deccdb82f24e
parent 1299
5dfce68057ce
child 1325
20caf6efaf07
--- a/tests/test_mempool.c	Thu May 22 16:23:55 2025 +0200
+++ b/tests/test_mempool.c	Thu May 22 16:25:32 2025 +0200
@@ -34,7 +34,9 @@
 CX_TEST(test_mempool_create) {
     CxMempool *pool = cxMempoolCreateSimple(16);
     CX_TEST_DO {
-        CX_TEST_ASSERT(pool->auto_destr == NULL);
+        CX_TEST_ASSERT(pool->destr == NULL);
+        CX_TEST_ASSERT(pool->destr2 == NULL);
+        CX_TEST_ASSERT(pool->destr2_data == NULL);
         CX_TEST_ASSERT(pool->allocator != NULL);
         CX_TEST_ASSERT(pool->allocator->cl != NULL);
         CX_TEST_ASSERT(pool->allocator->data == pool);
@@ -86,9 +88,10 @@
 }
 
 CX_TEST(test_mempool_realloc) {
-    CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor);
+    CxMempool *pool = cxMempoolCreateSimple(4);
+    cxMempoolGlobalDestructor(pool, test_mempool_destructor);
     CX_TEST_DO {
-        CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor);
+        CX_TEST_ASSERT(pool->destr == test_mempool_destructor);
         int *data = cxMalloc(pool->allocator, sizeof(int));
         *data = 13;
 
@@ -168,19 +171,19 @@
     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
-        src->auto_destr = test_mempool_destructor;
-
-        // allocate first object
-        int *c = cxMalloc(src->allocator, sizeof(int));
-        // allocate second object
-        c = cxMalloc(src->allocator, sizeof(int));
+        cxMempoolSetDestructor(c, test_mempool_destructor);
         // register foreign object
         c = malloc(sizeof(int));
         cxMempoolRegister(src, c, test_mempool_destructor);
 
         // check source pool
-        CX_TEST_ASSERT(src->size == 3);
+        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->registered_size == 1);
         const CxAllocator *old_allocator = src->allocator;
         CX_TEST_ASSERT(old_allocator->data == src);
 
@@ -190,7 +193,9 @@
 
         // check transfer
         CX_TEST_ASSERT(src->size == 0);
-        CX_TEST_ASSERT(dest->size == 4); // 3 objects + the old allocator
+        CX_TEST_ASSERT(dest->size == 2);
+        CX_TEST_ASSERT(src->registered_size == 0);
+        CX_TEST_ASSERT(dest->registered_size == 2); // 1 object + old allocator
         CX_TEST_ASSERT(src->allocator != old_allocator);
         CX_TEST_ASSERT(old_allocator->data == dest);
 
@@ -204,9 +209,9 @@
         CX_TEST_ASSERT(result != 0);
 
         // verify that destroying new pool calls the destructors
-        // but only three times (the old allocator has a different destructor)
+        // but only two times (the old allocator has a different destructor)
         cxMempoolFree(dest);
-        CX_TEST_ASSERT(test_mempool_destructor_called == 3);
+        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
 
         // free the foreign object
         free(c);
@@ -221,30 +226,32 @@
         b = cxMalloc(src->allocator, sizeof(int));
         int *c = malloc(sizeof(int));
         cxMempoolRegister(src, c, free);
+        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->registered_size == 1);
 
-        CX_TEST_ASSERT(src->size == 3);
         int result = cxMempoolTransferObject(src, dest, b);
         CX_TEST_ASSERT(result == 0);
-        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->size == 1);
         CX_TEST_ASSERT(dest->size == 1);
         result = cxMempoolTransferObject(src, dest, b);
         CX_TEST_ASSERT(result != 0);
-        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->size == 1);
         CX_TEST_ASSERT(dest->size == 1);
+
         // cannot transfer foreign memory this way
         result = cxMempoolTransferObject(src, dest, c);
         CX_TEST_ASSERT(result != 0);
-        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->size == 1);
         CX_TEST_ASSERT(dest->size == 1);
+
         result = cxMempoolTransferObject(dest, dest, b);
         CX_TEST_ASSERT(result != 0);
-        CX_TEST_ASSERT(src->size == 2);
+        CX_TEST_ASSERT(src->size == 1);
         CX_TEST_ASSERT(dest->size == 1);
-
-        cxMempoolFree(src);
-        cxMempoolFree(dest);
-        // let valgrind check that everything worked
     }
+    cxMempoolFree(src);
+    cxMempoolFree(dest);
+    // let valgrind check that everything worked
 }
 
 CxTestSuite *cx_test_suite_mempool(void) {

mercurial