fix some memory management bugs in the map and list tests default tip

Mon, 10 Nov 2025 21:56:09 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 10 Nov 2025 21:56:09 +0100
changeset 1483
97a6cf1520ba
parent 1482
6769cb72521b

fix some memory management bugs in the map and list tests

tests/test_hash_map.c file | annotate | diff | comparison | revisions
tests/test_list.c file | annotate | diff | comparison | revisions
--- a/tests/test_hash_map.c	Mon Nov 10 21:36:15 2025 +0100
+++ b/tests/test_hash_map.c	Mon Nov 10 21:56:09 2025 +0100
@@ -1156,21 +1156,22 @@
 }
 
 CX_TEST(test_hash_map_simple_clones) {
+    int v = 47; // the value does not matter in this test
     CxMap *a = cxHashMapCreateSimple(sizeof(int));
-    cxMapPut(a, "k1", "v1");
-    cxMapPut(a, "k2", "v2");
-    cxMapPut(a, "k3", "v3");
-    cxMapPut(a, "k4", "v4");
+    cxMapPut(a, "k1", &v);
+    cxMapPut(a, "k2", &v);
+    cxMapPut(a, "k3", &v);
+    cxMapPut(a, "k4", &v);
 
     CxMap *b = cxHashMapCreateSimple(sizeof(int));
-    cxMapPut(b, "k0", "v0");
-    cxMapPut(b, "k2", "v2");
-    cxMapPut(b, "k5", "v5");
+    cxMapPut(b, "k0", &v);
+    cxMapPut(b, "k2", &v);
+    cxMapPut(b, "k5", &v);
 
     CxMap *c = cxHashMapCreateSimple(sizeof(int));
-    cxMapPut(c, "k3", "v3");
-    cxMapPut(c, "k4", "v4");
-    cxMapPut(c, "k5", "v5");
+    cxMapPut(c, "k3", &v);
+    cxMapPut(c, "k4", &v);
+    cxMapPut(c, "k5", &v);
 
 
     CxHashKey k;
@@ -1192,10 +1193,8 @@
     k = CX_HASH_KEY("k5");
     cxListAdd(kl2, &k);
 
-    CxMap *d1 = cxHashMapCreateSimple(CX_STORE_POINTERS);
-    cxDefineAdvancedDestructor(d1, cxFree, (void*)cxDefaultAllocator);
-    CxMap *d2 = cxHashMapCreateSimple(CX_STORE_POINTERS);
-    cxDefineAdvancedDestructor(d2, cxFree, (void*)cxDefaultAllocator);
+    CxMap *d1 = cxHashMapCreateSimple(sizeof(int));
+    CxMap *d2 = cxHashMapCreateSimple(sizeof(int));
 
     CX_TEST_DO {
         CX_TEST_ASSERT(0 == cxMapCloneSimple(d1, a));
--- a/tests/test_list.c	Mon Nov 10 21:36:15 2025 +0100
+++ b/tests/test_list.c	Mon Nov 10 21:56:09 2025 +0100
@@ -2754,6 +2754,7 @@
     cxListFree(subtrahend);
     cxListFree(expected);
     CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    cx_testing_allocator_destroy(&talloc);
 }
 
 CX_TEST(test_list_difference_unsorted) {
@@ -2846,6 +2847,7 @@
     cxListFree(other);
     cxListFree(expected);
     CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    cx_testing_allocator_destroy(&talloc);
 }
 
 CX_TEST(test_list_intersection_unsorted) {
@@ -2950,6 +2952,7 @@
     cxListFree(other);
     cxListFree(expected);
     CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    cx_testing_allocator_destroy(&talloc);
 }
 
 CX_TEST(test_list_union_unsorted) {
@@ -2996,12 +2999,10 @@
     cxCollectionCompareFunc(ld, cx_cmp_int);
     cxListInsertSortedArray(ld, d, cx_nmemb(d));
 
-    CxList *d1 = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
+    CxList *d1 = cxArrayListCreateSimple(sizeof(int), 8);
     cxCollectionCompareFunc(d1, cx_cmp_int);
-    cxDefineAdvancedDestructor(d1, cxFree, (void*) cxDefaultAllocator);
-    CxList *d2 = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
+    CxList *d2 = cxArrayListCreateSimple(sizeof(int), 8);
     cxCollectionCompareFunc(d2, cx_cmp_int);
-    cxDefineAdvancedDestructor(d2, cxFree, (void*) cxDefaultAllocator);
 
     CX_TEST_DO {
         // clone a into d1
@@ -3015,7 +3016,7 @@
         CxList *expected_union = cxArrayListCreateSimple(sizeof(int), 8);
         {
             int expected[] = {1, 2, 3, 5, 7, 8, 9, 10, 11};
-            cxListInsertSortedArray(expected_union, expected, cx_nmemb(expected));
+            cxListAddArray(expected_union, expected, cx_nmemb(expected));
         }
         CX_TEST_ASSERT(0 == cxListCompare(d2, expected_union));
         cxListFree(expected_union);
@@ -3027,7 +3028,7 @@
         CxList *expected_intersection = cxArrayListCreateSimple(sizeof(int), 8);
         {
             int expected[] = {2, 8, 10};
-            cxListInsertSortedArray(expected_intersection, expected, cx_nmemb(expected));
+            cxListAddArray(expected_intersection, expected, cx_nmemb(expected));
         }
         CX_TEST_ASSERT(0 == cxListCompare(d1, expected_intersection));
         cxListFree(expected_intersection);
@@ -3039,7 +3040,7 @@
         CxList *expected_difference = cxArrayListCreateSimple(sizeof(int), 8);
         {
             int expected[] = {2, 10};
-            cxListInsertSortedArray(expected_difference, expected, cx_nmemb(expected));
+            cxListAddArray(expected_difference, expected, cx_nmemb(expected));
         }
         CX_TEST_ASSERT(0 == cxListCompare(d2, expected_difference));
         cxListFree(expected_difference);

mercurial