tests/test_list.cpp

changeset 677
b09aae58bba4
parent 672
55d8fdd38ca4
child 679
022fbd4bc057
--- a/tests/test_list.cpp	Fri Apr 07 11:30:28 2023 +0200
+++ b/tests/test_list.cpp	Sun Apr 09 19:03:58 2023 +0200
@@ -592,8 +592,7 @@
     ) {
         auto len = testdata_len;
         cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
-        EXPECT_EQ(list->size, len);
-        EXPECT_GE(list->capacity, list->size);
+        EXPECT_EQ(cxListSize(list), len);
         cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
         cx_for_n (i, len) ++testdata.data[i];
         if (as_pointer) {
@@ -607,17 +606,16 @@
         int a = 5, b = 47, c = 13, d = 42;
 
         EXPECT_NE(cxListInsert(list, 1, &a), 0);
-        EXPECT_EQ(list->size, 0);
+        EXPECT_EQ(cxListSize(list), 0);
         EXPECT_EQ(cxListInsert(list, 0, &a), 0);
-        EXPECT_EQ(list->size, 1);
+        EXPECT_EQ(cxListSize(list), 1);
         EXPECT_EQ(cxListInsert(list, 0, &b), 0);
-        EXPECT_EQ(list->size, 2);
+        EXPECT_EQ(cxListSize(list), 2);
         EXPECT_EQ(cxListInsert(list, 1, &c), 0);
-        EXPECT_EQ(list->size, 3);
+        EXPECT_EQ(cxListSize(list), 3);
         EXPECT_EQ(cxListInsert(list, 3, &d), 0);
 
-        ASSERT_EQ(list->size, 4);
-        EXPECT_GE(list->capacity, list->size);
+        ASSERT_EQ(cxListSize(list), 4);
 
         EXPECT_EQ(*(int *) cxListAt(list, 0), 47);
         EXPECT_EQ(*(int *) cxListAt(list, 1), 13);
@@ -672,8 +670,7 @@
     void verifyRemove(CxList *list) const {
         EXPECT_EQ(cxListRemove(list, 2), 0);
         EXPECT_EQ(cxListRemove(list, 4), 0);
-        EXPECT_EQ(list->size, testdata_len - 2);
-        EXPECT_GE(list->capacity, list->size);
+        EXPECT_EQ(cxListSize(list), testdata_len - 2);
         EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[0]);
         EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[1]);
         EXPECT_EQ(*(int *) cxListAt(list, 2), testdata.data[3]);
@@ -681,8 +678,7 @@
         EXPECT_EQ(*(int *) cxListAt(list, 4), testdata.data[6]);
 
         EXPECT_EQ(cxListRemove(list, 0), 0);
-        EXPECT_EQ(list->size, testdata_len - 3);
-        EXPECT_GE(list->capacity, list->size);
+        EXPECT_EQ(cxListSize(list), testdata_len - 3);
         EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]);
         EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]);
 
@@ -691,7 +687,7 @@
 
     static void verifyClear(CxList *list) {
         cxListClear(list);
-        EXPECT_EQ(0, list->size);
+        EXPECT_EQ(0, cxListSize(list));
     }
 
     static unsigned destr_test_ctr;
@@ -717,33 +713,33 @@
         cxListRemove(list, 15);
         EXPECT_EQ(1, destr_test_ctr);
         EXPECT_EQ(testdata.data[15], destr_last_value + off);
-        EXPECT_EQ(testdata_len - destr_test_ctr, list->size);
+        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
         cxListRemove(list, 47);
         EXPECT_EQ(2, destr_test_ctr);
         EXPECT_EQ(testdata.data[48], destr_last_value + off);
-        EXPECT_EQ(testdata_len - destr_test_ctr, list->size);
+        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
 
         auto iter = cxListMutIteratorAt(list, 7);
         cxIteratorNext(iter);
         EXPECT_EQ(2, destr_test_ctr);
         EXPECT_EQ(testdata.data[48], destr_last_value + off);
-        EXPECT_EQ(testdata_len - destr_test_ctr, list->size);
+        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
         cxIteratorFlagRemoval(iter);
         cxIteratorNext(iter);
         EXPECT_EQ(3, destr_test_ctr);
         EXPECT_EQ(testdata.data[8], destr_last_value + off);
-        EXPECT_EQ(testdata_len - destr_test_ctr, list->size);
+        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
 
         iter = cxListMutBackwardsIteratorAt(list, 5);
         cxIteratorNext(iter);
         EXPECT_EQ(3, destr_test_ctr);
         EXPECT_EQ(testdata.data[8], destr_last_value + off);
-        EXPECT_EQ(testdata_len - destr_test_ctr, list->size);
+        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
         cxIteratorFlagRemoval(iter);
         cxIteratorNext(iter);
         EXPECT_EQ(4, destr_test_ctr);
         EXPECT_EQ(testdata.data[4], destr_last_value + off);
-        EXPECT_EQ(testdata_len - destr_test_ctr, list->size);
+        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
 
         cxListClear(list);
         EXPECT_EQ(testdata_len, destr_test_ctr);
@@ -754,20 +750,18 @@
 
     void verifySimpleDestructor(CxList *list) {
         destr_test_ctr = 0;
-        list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
         list->simple_destructor = simple_destr_test_fun;
         verifyAnyDestructor(list);
     }
 
     void verifyAdvancedDestructor(CxList *list) {
         destr_test_ctr = 0;
-        list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
-        list->advanced_destructor.func = advanced_destr_test_fun;
+        list->advanced_destructor = advanced_destr_test_fun;
         verifyAnyDestructor(list);
     }
 
     static void verifySwap(CxList *list) {
-        ASSERT_EQ(list->size, 0);
+        ASSERT_EQ(cxListSize(list), 0);
 
         int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
         int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13};
@@ -816,11 +810,11 @@
 
     void verifyAt(CxList *list) const {
         auto len = testdata_len;
-        EXPECT_EQ(list->size, len);
+        EXPECT_EQ(cxListSize(list), len);
         cx_for_n (i, len) {
             EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
         }
-        EXPECT_EQ(cxListAt(list, list->size), nullptr);
+        EXPECT_EQ(cxListAt(list, cxListSize(list)), nullptr);
     }
 
     void verifyFind(CxList *list) const {
@@ -838,7 +832,7 @@
         }
 
         int notinlist = -1;
-        EXPECT_EQ(list->size, cxListFind(list, &notinlist));
+        EXPECT_EQ(cxListSize(list), cxListFind(list, &notinlist));
     }
 
     void verifySort(CxList *list) const {
@@ -856,7 +850,7 @@
             EXPECT_EQ(*x, testdata.data[iter.index]);
             i++;
         }
-        ASSERT_EQ(i, list->size);
+        ASSERT_EQ(i, cxListSize(list));
         iter = cxListBackwardsIterator(list);
         cx_foreach(int*, x, iter) {
             ASSERT_EQ(i - 1, iter.index);
@@ -887,7 +881,7 @@
             j++;
         }
         ASSERT_EQ(i, 0);
-        ASSERT_EQ(list->size, len / 2);
+        ASSERT_EQ(cxListSize(list), len / 2);
         cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]);
     }
 
@@ -912,11 +906,11 @@
         EXPECT_TRUE(cxIteratorValid(iter));
         EXPECT_EQ(iter.index, 1);
         EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0);
-        iter = cxListMutIteratorAt(list, list->size);
+        iter = cxListMutIteratorAt(list, cxListSize(list));
         cxListInsertBefore(&iter, &newdata[3]);
         EXPECT_FALSE(cxIteratorValid(iter));
         EXPECT_EQ(iter.index, 9);
-        iter = cxListMutIteratorAt(list, list->size);
+        iter = cxListMutIteratorAt(list, cxListSize(list));
         cxListInsertAfter(&iter, &newdata[4]);
         EXPECT_FALSE(cxIteratorValid(iter));
         EXPECT_EQ(iter.index, 10);
@@ -939,16 +933,16 @@
         EXPECT_EQ(cxListCompare(left, right), 0);
         int x = 42;
         cxListAdd(left, &x);
-        ASSERT_GT(left->size, right->size);
+        ASSERT_GT(cxListSize(left), cxListSize(right));
         EXPECT_GT(cxListCompare(left, right), 0);
         EXPECT_LT(cxListCompare(right, left), 0);
         cxListAdd(right, &x);
-        ASSERT_EQ(left->size, right->size);
+        ASSERT_EQ(cxListSize(left), cxListSize(right));
         EXPECT_EQ(cxListCompare(left, right), 0);
         int a = 5, b = 10;
         cxListInsert(left, 15, &a);
         cxListInsert(right, 15, &b);
-        ASSERT_EQ(left->size, right->size);
+        ASSERT_EQ(cxListSize(left), cxListSize(right));
         EXPECT_LT(cxListCompare(left, right), 0);
         EXPECT_GT(cxListCompare(right, left), 0);
         *(int *) cxListAt(left, 15) = 10;
@@ -972,7 +966,7 @@
     auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
     EXPECT_FALSE(cxListIsStoringPointers(list));
     cxListStorePointers(list);
-    EXPECT_EQ(list->itemsize, sizeof(void *));
+    EXPECT_EQ(list->item_size, sizeof(void *));
     EXPECT_NE(list->cl, nullptr);
     EXPECT_NE(list->climpl, nullptr);
     EXPECT_TRUE(cxListIsStoringPointers(list));
@@ -985,10 +979,11 @@
 TEST_F(LinkedList, cxLinkedListCreate) {
     CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
     ASSERT_NE(list, nullptr);
-    EXPECT_EQ(list->itemsize, sizeof(int));
-    EXPECT_EQ(list->capacity, (size_t) -1);
-    EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
-    EXPECT_EQ(list->size, 0);
+    EXPECT_EQ(list->item_size, sizeof(int));
+    EXPECT_EQ(list->simple_destructor, nullptr);
+    EXPECT_EQ(list->advanced_destructor, nullptr);
+    EXPECT_EQ(list->destructor_data, nullptr);
+    EXPECT_EQ(cxListSize(list), 0);
     EXPECT_EQ(list->allocator, &testingAllocator);
     EXPECT_EQ(list->cmpfunc, cx_cmp_int);
     EXPECT_FALSE(cxListIsStoringPointers(list));
@@ -997,30 +992,37 @@
 TEST_F(LinkedList, cxLinkedListCreateSimple) {
     CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int)));
     ASSERT_NE(list, nullptr);
-    EXPECT_EQ(list->itemsize, sizeof(int));
-    EXPECT_EQ(list->capacity, (size_t) -1);
+    EXPECT_EQ(list->item_size, sizeof(int));
     EXPECT_EQ(list->cmpfunc, nullptr);
     EXPECT_EQ(list->allocator, cxDefaultAllocator);
+    EXPECT_EQ(list->simple_destructor, nullptr);
+    EXPECT_EQ(list->advanced_destructor, nullptr);
+    EXPECT_EQ(list->destructor_data, nullptr);
+    EXPECT_EQ(cxListSize(list), 0);
     EXPECT_FALSE(cxListIsStoringPointers(list));
 }
 
 TEST_F(LinkedList, cxLinkedListCreateSimpleForPointers) {
     CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
     ASSERT_NE(list, nullptr);
-    EXPECT_EQ(list->itemsize, sizeof(void *));
-    EXPECT_EQ(list->capacity, (size_t) -1);
+    EXPECT_EQ(list->item_size, sizeof(void *));
     EXPECT_EQ(list->cmpfunc, nullptr);
     EXPECT_EQ(list->allocator, cxDefaultAllocator);
+    EXPECT_EQ(list->simple_destructor, nullptr);
+    EXPECT_EQ(list->advanced_destructor, nullptr);
+    EXPECT_EQ(list->destructor_data, nullptr);
+    EXPECT_EQ(cxListSize(list), 0);
     EXPECT_TRUE(cxListIsStoringPointers(list));
 }
 
 TEST_F(ArrayList, cxArrayListCreate) {
     CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
     ASSERT_NE(list, nullptr);
-    EXPECT_EQ(list->itemsize, sizeof(int));
-    EXPECT_EQ(list->capacity, 8);
-    EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
-    EXPECT_EQ(list->size, 0);
+    EXPECT_EQ(list->item_size, sizeof(int));
+    EXPECT_EQ(list->simple_destructor, nullptr);
+    EXPECT_EQ(list->advanced_destructor, nullptr);
+    EXPECT_EQ(list->destructor_data, nullptr);
+    EXPECT_EQ(cxListSize(list), 0);
     EXPECT_EQ(list->allocator, &testingAllocator);
     EXPECT_EQ(list->cmpfunc, cx_cmp_int);
     EXPECT_FALSE(cxListIsStoringPointers(list));
@@ -1031,8 +1033,11 @@
     ASSERT_NE(list, nullptr);
     EXPECT_EQ(list->cmpfunc, nullptr);
     EXPECT_EQ(list->allocator, cxDefaultAllocator);
-    EXPECT_EQ(list->itemsize, sizeof(int));
-    EXPECT_EQ(list->capacity, 8);
+    EXPECT_EQ(list->item_size, sizeof(int));
+    EXPECT_EQ(list->simple_destructor, nullptr);
+    EXPECT_EQ(list->advanced_destructor, nullptr);
+    EXPECT_EQ(list->destructor_data, nullptr);
+    EXPECT_EQ(cxListSize(list), 0);
     EXPECT_FALSE(cxListIsStoringPointers(list));
 }
 
@@ -1041,8 +1046,7 @@
     ASSERT_NE(list, nullptr);
     EXPECT_EQ(list->cmpfunc, nullptr);
     EXPECT_EQ(list->allocator, cxDefaultAllocator);
-    EXPECT_EQ(list->itemsize, sizeof(void *));
-    EXPECT_EQ(list->capacity, 8);
+    EXPECT_EQ(list->item_size, sizeof(void *));
     EXPECT_TRUE(cxListIsStoringPointers(list));
 }
 
@@ -1313,7 +1317,6 @@
 TEST_F(PointerLinkedList, DestroySimpleDestructor) {
     int item = 0;
     auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
-    list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
     list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
     cxListAdd(list, &item);
     cxListDestroy(list);
@@ -1323,9 +1326,8 @@
 TEST_F(PointerLinkedList, DestroyAdvancedDestructor) {
     void *item = cxMalloc(&testingAllocator, sizeof(int));
     auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
-    list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
-    list->advanced_destructor.data = &testingAllocator;
-    list->advanced_destructor.func = (cx_destructor_func2) cxFree;
+    list->destructor_data = &testingAllocator;
+    list->advanced_destructor = (cx_destructor_func2) cxFree;
     cxListAdd(list, item);
     ASSERT_FALSE(testingAllocator.verify());
     cxListDestroy(list);

mercurial