tests/test_list.cpp

changeset 666
b5dd654deb3b
parent 662
d0d95740071b
child 667
2f88a7c13a28
--- a/tests/test_list.cpp	Mon Mar 20 18:05:12 2023 +0100
+++ b/tests/test_list.cpp	Mon Mar 20 19:09:08 2023 +0100
@@ -38,6 +38,19 @@
 #include <unordered_set>
 #include <algorithm>
 
+struct testdatastruct {
+    int x;
+    void *ptr;
+};
+
+static void free_testdatastruct(
+        void *a,
+        void *s
+) {
+    auto al = reinterpret_cast<CxTestingAllocator *>(a);
+    cxFree(al, reinterpret_cast<testdatastruct *>(s)->ptr);
+}
+
 struct node {
     node *next = nullptr;
     node *prev = nullptr;
@@ -697,6 +710,21 @@
         EXPECT_NE(cxListRemove(list, testdata_len), 0);
     }
 
+    void verifyClear(CxList *list) {
+        // use the testing allocator for testing the destructor function
+        list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
+        list->advanced_destructor.func = free_testdatastruct;
+        list->advanced_destructor.data = &testingAllocator;
+
+        testdatastruct s[10];
+        for (auto &t: s) {
+            t.ptr = cxMalloc(&testingAllocator, 16);
+            cxListAdd(list, &t);
+        }
+
+        cxListClear(list);
+    }
+
     static void verifySwap(CxList *list) {
         ASSERT_EQ(list->size, 0);
 
@@ -997,6 +1025,20 @@
     verifyRemove(arrayListFromTestData());
 }
 
+TEST_F(LinkedList, cxListClear) {
+    verifyClear(autofree(cxLinkedListCreateSimple(sizeof(testdatastruct))));
+}
+
+TEST_F(PointerLinkedList, cxListClear) {
+    auto l = cxLinkedListCreateSimple(sizeof(testdatastruct));
+    cxListStorePointers(l);
+    verifyClear(autofree(l));
+}
+
+TEST_F(ArrayList, cxListClear) {
+    verifyClear(autofree(cxArrayListCreateSimple(sizeof(testdatastruct), 8)));
+}
+
 TEST_F(LinkedList, cxListSwap) {
     verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
 }

mercurial