test/test_list.c

changeset 438
cd3069757010
parent 435
0fe204d50f54
child 442
310019ddfe4e
--- a/test/test_list.c	Mon Sep 27 17:49:23 2021 +0200
+++ b/test/test_list.c	Mon Sep 27 18:33:30 2021 +0200
@@ -35,7 +35,7 @@
     return left == right ? 0 : (left < right ? -1 : 1);
 }
 
-void test_linked_list_create() {
+void test_linked_list_create(void) {
     cxTestingAllocatorReset();
 
     CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
@@ -66,6 +66,40 @@
     CU_ASSERT_TRUE(cxTestingAllocatorVerify())
 }
 
+void test_linked_list_at(void) {
+    struct node {
+        void *next;
+        void *prev;
+    };
+    const ptrdiff_t loc_prev = offsetof(struct node, prev);
+    const ptrdiff_t loc_next = offsetof(struct node, next);
+
+    struct node a, b, c, d;
+    a.prev = NULL;
+    a.next = &b;
+    b.prev = &a;
+    b.next = &c;
+    c.prev = &b;
+    c.next = &d;
+    d.prev = &c;
+    d.next = NULL;
+
+    CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0));
+    CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1));
+    CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2));
+    CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3));
+    CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4));
+
+    CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0));
+    CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1));
+    CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2));
+    CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3));
+    CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4));
+
+    CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0));
+    CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1));
+}
+
 int main() {
     CU_pSuite suite = NULL;
 
@@ -80,7 +114,8 @@
     }
 
     if (
-            !CU_add_test(suite, "create and destroy linked list", test_linked_list_create)
+            !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) ||
+            !CU_add_test(suite, "linked list: get node at index", test_linked_list_at)
             ) {
         CU_cleanup_registry();
         return CU_get_error();

mercurial