tests/test_list.c

changeset 1162
e3bb67b72d33
parent 1113
dce04550fbef
child 1163
68ff0839bc6a
--- a/tests/test_list.c	Sun Jan 26 14:37:07 2025 +0100
+++ b/tests/test_list.c	Mon Jan 27 20:27:39 2025 +0100
@@ -382,22 +382,31 @@
 }
 
 CX_TEST(test_linked_list_find) {
-    void *list = create_nodes_test_data(4);
+    node *list = create_nodes_test_data(4);
     assign_nodes_test_data(list, 2, 4, 6, 8);
     CX_TEST_DO {
+        size_t i = 10;
         int s;
         s = 2;
-        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 0);
+        node *n = list;
+        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
+        CX_TEST_ASSERT(i == 0);
+        n = n->next;
         s = 4;
-        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 1);
+        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
+        CX_TEST_ASSERT(i == 1);
+        n = n->next;
         s = 6;
-        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 2);
+        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
+        CX_TEST_ASSERT(i == 2);
+        n = n->next;
         s = 8;
-        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 3);
+        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
+        CX_TEST_ASSERT(i == 3);
         s = 10;
-        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0);
+        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == NULL);
         s = -2;
-        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0);
+        CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == NULL);
     }
     destroy_nodes_test_data(list);
 }
@@ -967,8 +976,8 @@
 CX_TEST(test_empty_list_find) {
     int x = 42, y = 1337;
     CX_TEST_DO {
-        CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) < 0);
-        CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) < 0);
+        CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) == 0);
+        CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) == 0);
     }
 }
 
@@ -1536,10 +1545,10 @@
     const size_t testdata_len = 250;
     int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
 
-    int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
+    unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
     int val = testdata[exp];
     // randomly picked number could occur earlier in list - find first position
-    for (int i = 0 ; i < exp ; i++) {
+    for (unsigned i = 0 ; i < exp ; i++) {
         if (testdata[i] == val) {
             exp = i;
             break;
@@ -1552,7 +1561,7 @@
     CX_TEST_ASSERT(cxListFind(list, &val) != exp);
 
     int notinlist = -1;
-    CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) < 0);
+    CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) == cxListSize(list));
     CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
 
     free(testdata);
@@ -1571,9 +1580,11 @@
     int *testdata = int_test_data_added_to_list(list, isptrlist, 128);
     CX_TEST_ASSERT(cxListSize(list) == len);
     for (size_t i = 0; i < len; i++) {
+        CX_TEST_ASSERT(cxListIndexValid(list, i));
         CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
     }
-    CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL);
+    CX_TEST_ASSERT(!cxListIndexValid(list, len));
+    CX_TEST_ASSERT(cxListAt(list, len) == NULL);
     free(testdata);
 })
 
@@ -1620,10 +1631,10 @@
     int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
 
     for (size_t attempt = 0; attempt < 25; attempt++) {
-        int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
+        unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
         int val = testdata[exp];
         // randomly picked number could occur earlier in list - find first position
-        for (int i = 0 ; i < exp ; i++) {
+        for (unsigned i = 0 ; i < exp ; i++) {
             if (testdata[i] == val) {
                 exp = i;
                 break;
@@ -1633,7 +1644,7 @@
     }
 
     int notinlist = -1;
-    CX_TEST_ASSERT(cxListFind(list, &notinlist) < 0);
+    CX_TEST_ASSERT(cxListFind(list, &notinlist) == cxListSize(list));
 
     free(testdata);
 })

mercurial