test/test_list.cpp

changeset 528
4fbfac557df8
parent 521
e5dc54131d55
child 552
4373c2a90066
--- a/test/test_list.cpp	Mon Apr 18 16:56:29 2022 +0200
+++ b/test/test_list.cpp	Mon Apr 18 17:26:21 2022 +0200
@@ -92,7 +92,10 @@
 }
 
 template<typename InputIter>
-static node_test_data create_nodes_test_data(InputIter begin, InputIter end) {
+static node_test_data create_nodes_test_data(
+        InputIter begin,
+        InputIter end
+) {
     if (begin == end) return node_test_data{nullptr};
     node *first = new node;
     first->data = *begin;
@@ -592,13 +595,17 @@
     }
 
     void verifyCreate(CxList *list) const {
+        EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
         EXPECT_EQ(list->size, 0);
         EXPECT_EQ(list->capacity, (size_t) -1);
         EXPECT_EQ(list->allocator, &testingAllocator);
         EXPECT_EQ(list->cmpfunc, cmp_int);
     }
 
-    void verifyAdd(CxList *list, bool write_through) {
+    void verifyAdd(
+            CxList *list,
+            bool write_through
+    ) {
         auto len = testdata_len;
         cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
         EXPECT_EQ(list->size, len);
@@ -741,7 +748,10 @@
         }
     }
 
-    static void verifyCompare(CxList *left, CxList *right) {
+    static void verifyCompare(
+            CxList *left,
+            CxList *right
+    ) {
         EXPECT_EQ(cxListCompare(left, right), 0);
         int x = 42;
         cxListAdd(left, &x);
@@ -757,7 +767,7 @@
         ASSERT_EQ(left->size, right->size);
         EXPECT_LT(cxListCompare(left, right), 0);
         EXPECT_GT(cxListCompare(right, left), 0);
-        *(int*)cxListAt(left, 15) = 10;
+        *(int *) cxListAt(left, 15) = 10;
         EXPECT_EQ(cxListCompare(left, right), 0);
     }
 };
@@ -879,3 +889,35 @@
     verifyCompare(left, right);
 }
 
+TEST_F(PointerLinkedList, NoDestructor) {
+    void *item = cxMalloc(&testingAllocator, sizeof(int));
+    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int);
+    cxListAdd(list, item);
+    ASSERT_FALSE(testingAllocator.verify());
+    cxListDestroy(list);
+    EXPECT_FALSE(testingAllocator.verify());
+    cxFree(&testingAllocator, item);
+    EXPECT_TRUE(testingAllocator.verify());
+}
+
+TEST_F(PointerLinkedList, SimpleDestructor) {
+    int item = 0;
+    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int);
+    list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
+    list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
+    cxListAdd(list, &item);
+    cxListDestroy(list);
+    EXPECT_EQ(item, 42);
+}
+
+TEST_F(PointerLinkedList, AdvancedDestructor) {
+    void *item = cxMalloc(&testingAllocator, sizeof(int));
+    auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cmp_int);
+    list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
+    list->advanced_destructor.data = &testingAllocator;
+    list->advanced_destructor.func = (cx_destructor_func2) cxFree;
+    cxListAdd(list, item);
+    ASSERT_FALSE(testingAllocator.verify());
+    cxListDestroy(list);
+    EXPECT_TRUE(testingAllocator.verify());
+}

mercurial