tests/test_list.cpp

changeset 679
022fbd4bc057
parent 677
b09aae58bba4
child 680
19379743e5a0
--- a/tests/test_list.cpp	Sun Apr 09 19:06:16 2023 +0200
+++ b/tests/test_list.cpp	Sun Apr 09 19:37:00 2023 +0200
@@ -586,6 +586,13 @@
         return list;
     }
 
+    auto pointerArrayListFromTestData() const -> CxList * {
+        auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 256));
+        // note: cannot use cxListAddArray() because we don't have a list of pointers
+        cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
+        return list;
+    }
+
     void verifyAdd(
             CxList *list,
             bool as_pointer
@@ -962,6 +969,9 @@
 class ArrayList : public HighLevelTest {
 };
 
+class PointerArrayList : public HighLevelTest {
+};
+
 TEST_F(PointerLinkedList, cxListStorePointers) {
     auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
     EXPECT_FALSE(cxListIsStoringPointers(list));
@@ -1002,7 +1012,7 @@
     EXPECT_FALSE(cxListIsStoringPointers(list));
 }
 
-TEST_F(LinkedList, cxLinkedListCreateSimpleForPointers) {
+TEST_F(PointerLinkedList, cxLinkedListCreateSimpleForPointers) {
     CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
     ASSERT_NE(list, nullptr);
     EXPECT_EQ(list->item_size, sizeof(void *));
@@ -1041,7 +1051,7 @@
     EXPECT_FALSE(cxListIsStoringPointers(list));
 }
 
-TEST_F(ArrayList, cxArrayListCreateSimpleForPointers) {
+TEST_F(PointerArrayList, cxArrayListCreateSimpleForPointers) {
     CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8));
     ASSERT_NE(list, nullptr);
     EXPECT_EQ(list->cmpfunc, nullptr);
@@ -1065,6 +1075,11 @@
     verifyAdd(list, false);
 }
 
+TEST_F(PointerArrayList, cxListAdd) {
+    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 8));
+    verifyAdd(list, true);
+}
+
 TEST_F(LinkedList, cxListInsert) {
     verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
 }
@@ -1077,6 +1092,10 @@
     verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
 }
 
+TEST_F(PointerArrayList, cxListInsert) {
+    verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 2)));
+}
+
 TEST_F(LinkedList, cxListInsertArray) {
     verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
 }
@@ -1089,6 +1108,10 @@
     verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
 }
 
+TEST_F(PointerArrayList, cxListInsertArray) {
+    verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 4)), true);
+}
+
 TEST_F(LinkedList, cxListRemove) {
     verifyRemove(linkedListFromTestData());
 }
@@ -1101,6 +1124,10 @@
     verifyRemove(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, cxListRemove) {
+    verifyRemove(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, cxListClear) {
     verifyClear(linkedListFromTestData());
 }
@@ -1113,6 +1140,10 @@
     verifyClear(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, cxListClear) {
+    verifyClear(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, cxListSwap) {
     verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
 }
@@ -1125,6 +1156,10 @@
     verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
 }
 
+TEST_F(PointerArrayList, cxListSwap) {
+    verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 16)));
+}
+
 TEST_F(LinkedList, cxListSwapNoSBO) {
     CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
     verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
@@ -1137,12 +1172,6 @@
     CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
 }
 
-TEST_F(ArrayList, cxListSwapNoSBO) {
-    CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
-    verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
-    CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
-}
-
 TEST_F(LinkedList, cxListAt) {
     verifyAt(linkedListFromTestData());
 }
@@ -1155,6 +1184,10 @@
     verifyAt(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, cxListAt) {
+    verifyAt(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, cxListFind) {
     verifyFind(linkedListFromTestData());
 }
@@ -1167,6 +1200,10 @@
     verifyFind(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, cxListFind) {
+    verifyFind(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, cxListSort) {
     verifySort(linkedListFromTestData());
 }
@@ -1179,6 +1216,10 @@
     verifySort(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, cxListSort) {
+    verifySort(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, Iterator) {
     verifyIterator(linkedListFromTestData());
 }
@@ -1191,6 +1232,10 @@
     verifyIterator(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, Iterator) {
+    verifyIterator(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, InsertViaIterator) {
     int fivenums[] = {0, 1, 2, 3, 4, 5};
     CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
@@ -1213,6 +1258,14 @@
     verifyInsertViaIterator(list);
 }
 
+TEST_F(PointerArrayList, InsertViaIterator) {
+    int fivenums[] = {0, 1, 2, 3, 4, 5};
+    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 4));
+    // note: cannot use cxListAddArray() because we don't have a list of pointers
+    cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
+    verifyInsertViaIterator(list);
+}
+
 TEST_F(LinkedList, cxListReverse) {
     verifyReverse(linkedListFromTestData());
 }
@@ -1225,6 +1278,10 @@
     verifyReverse(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, cxListReverse) {
+    verifyReverse(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, cxListCompare) {
     auto left = linkedListFromTestData();
     auto right = linkedListFromTestData();
@@ -1243,6 +1300,12 @@
     verifyCompare(left, right);
 }
 
+TEST_F(LinkedList, cxListCompareWithPtrArrayList) {
+    auto left = linkedListFromTestData();
+    auto right = pointerArrayListFromTestData();
+    verifyCompare(left, right);
+}
+
 TEST_F(PointerLinkedList, cxListCompare) {
     auto left = pointerLinkedListFromTestData();
     auto right = pointerLinkedListFromTestData();
@@ -1261,6 +1324,12 @@
     verifyCompare(left, right);
 }
 
+TEST_F(PointerLinkedList, cxListCompareWithPtrArrayList) {
+    auto left = pointerLinkedListFromTestData();
+    auto right = pointerArrayListFromTestData();
+    verifyCompare(left, right);
+}
+
 TEST_F(ArrayList, cxListCompare) {
     auto left = arrayListFromTestData();
     auto right = arrayListFromTestData();
@@ -1279,6 +1348,36 @@
     verifyCompare(left, right);
 }
 
+TEST_F(ArrayList, cxListCompareWithPtrArrayList) {
+    auto left = arrayListFromTestData();
+    auto right = pointerArrayListFromTestData();
+    verifyCompare(left, right);
+}
+
+TEST_F(PointerArrayList, cxListCompare) {
+    auto left = pointerArrayListFromTestData();
+    auto right = pointerArrayListFromTestData();
+    verifyCompare(left, right);
+}
+
+TEST_F(PointerArrayList, cxListCompareWithPtrList) {
+    auto left = pointerArrayListFromTestData();
+    auto right = pointerLinkedListFromTestData();
+    verifyCompare(left, right);
+}
+
+TEST_F(PointerArrayList, cxListCompareWithNormalList) {
+    auto left = pointerArrayListFromTestData();
+    auto right = linkedListFromTestData();
+    verifyCompare(left, right);
+}
+
+TEST_F(PointerArrayList, cxListCompareWithNormalArrayList) {
+    auto left = pointerArrayListFromTestData();
+    auto right = arrayListFromTestData();
+    verifyCompare(left, right);
+}
+
 TEST_F(LinkedList, SimpleDestructor) {
     verifySimpleDestructor(linkedListFromTestData());
 }
@@ -1291,6 +1390,10 @@
     verifySimpleDestructor(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, SimpleDestructor) {
+    verifySimpleDestructor(pointerArrayListFromTestData());
+}
+
 TEST_F(LinkedList, AdvancedDestructor) {
     verifyAdvancedDestructor(linkedListFromTestData());
 }
@@ -1303,6 +1406,10 @@
     verifyAdvancedDestructor(arrayListFromTestData());
 }
 
+TEST_F(PointerArrayList, AdvancedDestructor) {
+    verifyAdvancedDestructor(pointerArrayListFromTestData());
+}
+
 TEST_F(PointerLinkedList, DestroyNoDestructor) {
     void *item = cxMalloc(&testingAllocator, sizeof(int));
     auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
@@ -1333,3 +1440,35 @@
     cxListDestroy(list);
     EXPECT_TRUE(testingAllocator.verify());
 }
+
+TEST_F(PointerArrayList, DestroyNoDestructor) {
+    void *item = cxMalloc(&testingAllocator, sizeof(int));
+    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
+    cxListAdd(list, item);
+    ASSERT_FALSE(testingAllocator.verify());
+    cxListDestroy(list);
+    EXPECT_FALSE(testingAllocator.verify());
+    cxFree(&testingAllocator, item);
+    EXPECT_TRUE(testingAllocator.verify());
+}
+
+TEST_F(PointerArrayList, DestroySimpleDestructor) {
+    int item = 0;
+    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
+    list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
+    cxListAdd(list, &item);
+    cxListDestroy(list);
+    EXPECT_EQ(item, 42);
+}
+
+TEST_F(PointerArrayList, DestroyAdvancedDestructor) {
+    void *item = cxMalloc(&testingAllocator, sizeof(int));
+    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
+    list->destructor_data = &testingAllocator;
+    list->advanced_destructor = (cx_destructor_func2) cxFree;
+    cxListAdd(list, item);
+    ASSERT_FALSE(testingAllocator.verify());
+    cxListDestroy(list);
+    EXPECT_TRUE(testingAllocator.verify());
+}
+

mercurial