1.1 --- a/test/test_list.c Mon Sep 27 17:49:23 2021 +0200 1.2 +++ b/test/test_list.c Mon Sep 27 18:33:30 2021 +0200 1.3 @@ -35,7 +35,7 @@ 1.4 return left == right ? 0 : (left < right ? -1 : 1); 1.5 } 1.6 1.7 -void test_linked_list_create() { 1.8 +void test_linked_list_create(void) { 1.9 cxTestingAllocatorReset(); 1.10 1.11 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); 1.12 @@ -66,6 +66,40 @@ 1.13 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 1.14 } 1.15 1.16 +void test_linked_list_at(void) { 1.17 + struct node { 1.18 + void *next; 1.19 + void *prev; 1.20 + }; 1.21 + const ptrdiff_t loc_prev = offsetof(struct node, prev); 1.22 + const ptrdiff_t loc_next = offsetof(struct node, next); 1.23 + 1.24 + struct node a, b, c, d; 1.25 + a.prev = NULL; 1.26 + a.next = &b; 1.27 + b.prev = &a; 1.28 + b.next = &c; 1.29 + c.prev = &b; 1.30 + c.next = &d; 1.31 + d.prev = &c; 1.32 + d.next = NULL; 1.33 + 1.34 + CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0)); 1.35 + CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1)); 1.36 + CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2)); 1.37 + CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3)); 1.38 + CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4)); 1.39 + 1.40 + CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0)); 1.41 + CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1)); 1.42 + CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2)); 1.43 + CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3)); 1.44 + CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4)); 1.45 + 1.46 + CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0)); 1.47 + CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1)); 1.48 +} 1.49 + 1.50 int main() { 1.51 CU_pSuite suite = NULL; 1.52 1.53 @@ -80,7 +114,8 @@ 1.54 } 1.55 1.56 if ( 1.57 - !CU_add_test(suite, "create and destroy linked list", test_linked_list_create) 1.58 + !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) || 1.59 + !CU_add_test(suite, "linked list: get node at index", test_linked_list_at) 1.60 ) { 1.61 CU_cleanup_registry(); 1.62 return CU_get_error();