test/test_list.c

changeset 438
cd3069757010
parent 435
0fe204d50f54
child 442
310019ddfe4e
equal deleted inserted replaced
437:9d4971ea0625 438:cd3069757010
33 int cmp_int(int const *l, int const *r) { 33 int cmp_int(int const *l, int const *r) {
34 int left = *l, right = *r; 34 int left = *l, right = *r;
35 return left == right ? 0 : (left < right ? -1 : 1); 35 return left == right ? 0 : (left < right ? -1 : 1);
36 } 36 }
37 37
38 void test_linked_list_create() { 38 void test_linked_list_create(void) {
39 cxTestingAllocatorReset(); 39 cxTestingAllocatorReset();
40 40
41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); 41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
42 42
43 CU_ASSERT_EQUAL(list->size, 0) 43 CU_ASSERT_EQUAL(list->size, 0)
64 cxLinkedListDestroy(list); 64 cxLinkedListDestroy(list);
65 65
66 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 66 CU_ASSERT_TRUE(cxTestingAllocatorVerify())
67 } 67 }
68 68
69 void test_linked_list_at(void) {
70 struct node {
71 void *next;
72 void *prev;
73 };
74 const ptrdiff_t loc_prev = offsetof(struct node, prev);
75 const ptrdiff_t loc_next = offsetof(struct node, next);
76
77 struct node a, b, c, d;
78 a.prev = NULL;
79 a.next = &b;
80 b.prev = &a;
81 b.next = &c;
82 c.prev = &b;
83 c.next = &d;
84 d.prev = &c;
85 d.next = NULL;
86
87 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&a, 0, loc_next, 0));
88 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&a, 0, loc_next, 1));
89 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&a, 0, loc_next, 2));
90 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&a, 0, loc_next, 3));
91 CU_ASSERT_PTR_NULL(cx_linked_list_at(&a, 0, loc_next, 4));
92
93 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&b, 1, loc_prev, 0));
94 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&b, 1, loc_next, 1));
95 CU_ASSERT_PTR_EQUAL(&c, cx_linked_list_at(&b, 1, loc_next, 2));
96 CU_ASSERT_PTR_EQUAL(&d, cx_linked_list_at(&b, 1, loc_next, 3));
97 CU_ASSERT_PTR_NULL(cx_linked_list_at(&b, 1, loc_next, 4));
98
99 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0));
100 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1));
101 }
102
69 int main() { 103 int main() {
70 CU_pSuite suite = NULL; 104 CU_pSuite suite = NULL;
71 105
72 if (CUE_SUCCESS != CU_initialize_registry()) { 106 if (CUE_SUCCESS != CU_initialize_registry()) {
73 return CU_get_error(); 107 return CU_get_error();
78 CU_cleanup_registry(); 112 CU_cleanup_registry();
79 return CU_get_error(); 113 return CU_get_error();
80 } 114 }
81 115
82 if ( 116 if (
83 !CU_add_test(suite, "create and destroy linked list", test_linked_list_create) 117 !CU_add_test(suite, "linked list: create and destroy", test_linked_list_create) ||
118 !CU_add_test(suite, "linked list: get node at index", test_linked_list_at)
84 ) { 119 ) {
85 CU_cleanup_registry(); 120 CU_cleanup_registry();
86 return CU_get_error(); 121 return CU_get_error();
87 } 122 }
88 123

mercurial