tests/test_list.c

changeset 1422
8bfccb342895
parent 1419
e46406fd1b3c
child 1423
9a72258446cd
--- a/tests/test_list.c	Fri Oct 10 19:40:24 2025 +0200
+++ b/tests/test_list.c	Sat Oct 11 11:55:46 2025 +0200
@@ -1143,18 +1143,24 @@
     CxIterator it2 = cxListBackwardsIterator(list);
     CxIterator it3 = cxListMutIterator(list);
     CxIterator it4 = cxListMutBackwardsIterator(list);
+    CxIterator it5 = cxListMutIteratorAt(list, 0);
+    CxIterator it6 = cxListMutBackwardsIteratorAt(list, 0);
 
     CX_TEST_DO {
         CX_TEST_ASSERT(!cxIteratorValid(it1));
         CX_TEST_ASSERT(!cxIteratorValid(it2));
         CX_TEST_ASSERT(!cxIteratorValid(it3));
         CX_TEST_ASSERT(!cxIteratorValid(it4));
+        CX_TEST_ASSERT(!cxIteratorValid(it5));
+        CX_TEST_ASSERT(!cxIteratorValid(it6));
 
         int c = 0;
         cx_foreach(void*, data, it1) c++;
         cx_foreach(void*, data, it2) c++;
         cx_foreach(void*, data, it3) c++;
         cx_foreach(void*, data, it4) c++;
+        cx_foreach(void*, data, it5) c++;
+        cx_foreach(void*, data, it6) c++;
         CX_TEST_ASSERT(c == 0);
     }
 }
@@ -1220,6 +1226,13 @@
     cxListFree(al);
 }
 
+CX_TEST(test_null_list_free) {
+    CX_TEST_DO {
+        // cannot really verify, but asan or valgrind would complain
+        cxListFree(NULL);
+    }
+}
+
 CX_TEST(test_list_ll_create) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
@@ -2415,6 +2428,34 @@
     free(testdata);
 })
 
+CX_TEST(test_list_pointer_list_supports_null) {
+    CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
+    int x = 47;
+    int y = 11;
+    int z = 1337;
+    int *nptr = NULL;
+    cxListAdd(list, &x);
+    cxListAdd(list, &y);
+    cxListAdd(list, nptr);
+    cxListAdd(list, &z);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(cxListSize(list) == 4);
+        CX_TEST_ASSERT(*(int *) cxListAt(list, 0) == 47);
+        CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 11);
+        CX_TEST_ASSERT((int *) cxListAt(list, 2) == NULL);
+        CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 1337);
+        CX_TEST_ASSERT(cxListFind(list, nptr) == 2);
+
+        // when we sort the list, NULL is supposed to be smaller than any value
+        cxListSort(list);
+        CX_TEST_ASSERT((int *) cxListAt(list, 0) == NULL);
+        CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 11);
+        CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 47);
+        CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 1337);
+    }
+    cxListFree(list);
+}
+
 CxTestSuite *cx_test_suite_array_list(void) {
     CxTestSuite *suite = cx_test_suite_new("array_list");
 
@@ -2697,6 +2738,15 @@
     cx_test_register(suite, test_empty_list_at);
     cx_test_register(suite, test_empty_list_find);
     cx_test_register(suite, test_empty_list_compare);
+    cx_test_register(suite, test_null_list_free);
 
     return suite;
 }
+
+CxTestSuite *cx_test_suite_list_corner_cases(void) {
+    CxTestSuite *suite = cx_test_suite_new("list corner cases");
+
+    cx_test_register(suite, test_list_pointer_list_supports_null);
+
+    return suite;
+}

mercurial