test/test_list.cpp

changeset 647
2e6e9d9f2159
parent 641
d402fead3386
--- a/test/test_list.cpp	Wed Feb 08 18:56:58 2023 +0100
+++ b/test/test_list.cpp	Wed Feb 08 20:26:09 2023 +0100
@@ -697,6 +697,54 @@
         EXPECT_NE(cxListRemove(list, testdata_len), 0);
     }
 
+    static void verifySwap(CxList *list) {
+        ASSERT_EQ(list->size, 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};
+
+        // we have to add the items one by one, because it could be a pointer list
+        cx_for_n(i, 16) {
+            cxListAdd(list, &original[i]);
+        }
+
+        int result;
+
+        // execute the test two times with different item sizes
+        result = cxListSwap(list, 1, 4);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 2, 14);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 9, 6);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 3, 3);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 10, 11);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 8, 0);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 7, 12);
+        EXPECT_EQ(0, result);
+        result = cxListSwap(list, 13, 15);
+        EXPECT_EQ(0, result);
+
+        result = cxListSwap(list, 5, 16);
+        EXPECT_NE(0, result);
+        result = cxListSwap(list, 16, 6);
+        EXPECT_NE(0, result);
+        result = cxListSwap(list, 16, 17);
+        EXPECT_NE(0, result);
+
+        auto iter = cxListBegin(list);
+        cx_foreach(int*, e, iter) {
+            EXPECT_EQ(*e, swapped[iter.index]);
+        }
+        // TODO: replace with backward iterator
+        cx_for_n(i, 16) {
+            EXPECT_EQ(*((int *) cxListAt(list, i)), swapped[i]);
+        }
+    }
+
     void verifyAt(CxList *list) const {
         auto len = testdata_len;
         EXPECT_EQ(list->size, len);
@@ -903,6 +951,40 @@
     verifyRemove(arrayListFromTestData());
 }
 
+TEST_F(LinkedList, cxListSwap) {
+    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
+}
+
+TEST_F(PointerLinkedList, cxListSwap) {
+    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
+    cxListStorePointers(list);
+    verifySwap(list);
+}
+
+TEST_F(ArrayList, cxListSwap) {
+    verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
+}
+
+TEST_F(LinkedList, cxListSwapNoSBO) {
+    CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
+    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
+    CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
+}
+
+TEST_F(PointerLinkedList, cxListSwapNoSBO) {
+    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
+    cxListStorePointers(list);
+    CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
+    verifySwap(list);
+    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());
 }

mercurial