tests/test_list.c

changeset 1480
83146195a1db
parent 1477
9170a7dff573
child 1481
1dda1eed1899
--- a/tests/test_list.c	Sat Nov 08 23:45:19 2025 +0100
+++ b/tests/test_list.c	Sun Nov 09 16:12:07 2025 +0100
@@ -2976,6 +2976,83 @@
     }
 }
 
+CX_TEST(test_list_simple_clones) {
+
+    int a[] = {1, 2, 5, 8, 10};
+    int b[] = {1, 3, 5, 7, 9, 11};
+    int c[] = {2, 4, 6, 8, 10, 12};
+    int d[] = {4, 8, 12};
+
+    CxList *la = cxArrayListCreateSimple(sizeof(int), 8);
+    cxCollectionCompareFunc(la, cx_cmp_int);
+    cxListInsertSortedArray(la, a, cx_nmemb(a));
+    CxList *lb = cxArrayListCreateSimple(sizeof(int), 8);
+    cxCollectionCompareFunc(lb, cx_cmp_int);
+    cxListInsertSortedArray(lb, b, cx_nmemb(b));
+    CxList *lc = cxArrayListCreateSimple(sizeof(int), 8);
+    cxCollectionCompareFunc(lc, cx_cmp_int);
+    cxListInsertSortedArray(lc, c, cx_nmemb(c));
+    CxList *ld = cxArrayListCreateSimple(sizeof(int), 8);
+    cxCollectionCompareFunc(ld, cx_cmp_int);
+    cxListInsertSortedArray(ld, d, cx_nmemb(d));
+
+    CxList *d1 = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
+    cxCollectionCompareFunc(d1, cx_cmp_int);
+    cxDefineAdvancedDestructor(d1, cxFree, (void*) cxDefaultAllocator);
+    CxList *d2 = cxArrayListCreateSimple(CX_STORE_POINTERS, 8);
+    cxCollectionCompareFunc(d2, cx_cmp_int);
+    cxDefineAdvancedDestructor(d2, cxFree, (void*) cxDefaultAllocator);
+
+    CX_TEST_DO {
+        // clone a into d1
+        CX_TEST_ASSERT(0 == cxListCloneSimple(d1, la));
+        CX_TEST_ASSERT(0 == cxListCompare(d1, la));
+        CX_TEST_ASSERT(cxCollectionSorted(d1));
+
+        // union of a (in d1) and b
+        CX_TEST_ASSERT(0 == cxListUnionSimple(d2, d1, lb));
+        CX_TEST_ASSERT(cxCollectionSorted(d2));
+        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));
+        }
+        CX_TEST_ASSERT(0 == cxListCompare(d2, expected_union));
+        cxListFree(expected_union);
+
+        // intersection of (a union b) and c
+        cxListClear(d1);
+        CX_TEST_ASSERT(0 == cxListIntersectionSimple(d1, d2, lc));
+        CX_TEST_ASSERT(cxCollectionSorted(d1));
+        CxList *expected_intersection = cxArrayListCreateSimple(sizeof(int), 8);
+        {
+            int expected[] = {2, 8, 10};
+            cxListInsertSortedArray(expected_intersection, expected, cx_nmemb(expected));
+        }
+        CX_TEST_ASSERT(0 == cxListCompare(d1, expected_intersection));
+        cxListFree(expected_intersection);
+
+        // difference of ((a union b) intersect c) minus d
+        cxListClear(d2);
+        CX_TEST_ASSERT(0 == cxListDifferenceSimple(d2, d1, ld));
+        CX_TEST_ASSERT(cxCollectionSorted(d2));
+        CxList *expected_difference = cxArrayListCreateSimple(sizeof(int), 8);
+        {
+            int expected[] = {2, 10};
+            cxListInsertSortedArray(expected_difference, expected, cx_nmemb(expected));
+        }
+        CX_TEST_ASSERT(0 == cxListCompare(d2, expected_difference));
+        cxListFree(expected_difference);
+    }
+
+    cxListFree(d1);
+    cxListFree(d2);
+    cxListFree(la);
+    cxListFree(lb);
+    cxListFree(lc);
+    cxListFree(ld);
+}
+
 CX_TEST(test_list_pointer_list_supports_null) {
     CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
     int x = 47;
@@ -3443,6 +3520,7 @@
     cx_test_register(suite, test_list_intersection_sorted);
     cx_test_register(suite, test_list_intersection_unsorted_alloc_fail);
     cx_test_register(suite, test_list_intersection_sorted_alloc_fail);
+    cx_test_register(suite, test_list_simple_clones);
 
     return suite;
 }

mercurial