31 #include "util_allocator.h" |
31 #include "util_allocator.h" |
32 |
32 |
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 } |
|
37 |
|
38 void test_linked_list_create(void) { |
|
39 cxTestingAllocatorReset(); |
|
40 |
|
41 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
|
42 |
|
43 CU_ASSERT_EQUAL(list->size, 0) |
|
44 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
|
45 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
|
46 CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) |
|
47 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
|
48 |
|
49 // assume this structure for a linked list |
|
50 struct ll_check { |
|
51 cx_list_s base; |
|
52 void *begin; |
|
53 void *end; |
|
54 }; |
|
55 |
|
56 struct ll_check *actual = (struct ll_check *) list; |
|
57 CU_ASSERT_PTR_NULL(actual->begin) |
|
58 CU_ASSERT_PTR_NULL(actual->end) |
|
59 |
|
60 cxLinkedListDestroy(list); |
|
61 |
|
62 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
|
63 } |
36 } |
64 |
37 |
65 void test_linked_list_at(void) { |
38 void test_linked_list_at(void) { |
66 struct node { |
39 struct node { |
67 void *next; |
40 void *next; |
152 CU_ASSERT_PTR_EQUAL(end, &nodes[1]) |
125 CU_ASSERT_PTR_EQUAL(end, &nodes[1]) |
153 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
126 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) |
154 CU_ASSERT_PTR_NULL(nodes[1].prev) |
127 CU_ASSERT_PTR_NULL(nodes[1].prev) |
155 } |
128 } |
156 |
129 |
|
130 |
|
131 void test_linked_list_create(void) { |
|
132 cxTestingAllocatorReset(); |
|
133 |
|
134 CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); |
|
135 |
|
136 CU_ASSERT_EQUAL(list->size, 0) |
|
137 CU_ASSERT_EQUAL(list->capacity, (size_t) -1) |
|
138 CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) |
|
139 CU_ASSERT_EQUAL(list->itemsize, sizeof(int)) |
|
140 CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) |
|
141 |
|
142 // assume this structure for a linked list |
|
143 struct ll_check { |
|
144 cx_list_s base; |
|
145 void *begin; |
|
146 void *end; |
|
147 }; |
|
148 |
|
149 struct ll_check *actual = (struct ll_check *) list; |
|
150 CU_ASSERT_PTR_NULL(actual->begin) |
|
151 CU_ASSERT_PTR_NULL(actual->end) |
|
152 |
|
153 cxLinkedListDestroy(list); |
|
154 |
|
155 CU_ASSERT_TRUE(cxTestingAllocatorVerify()) |
|
156 } |
|
157 |
157 int main() { |
158 int main() { |
158 CU_pSuite suite = NULL; |
159 CU_pSuite suite = NULL; |
159 |
160 |
160 if (CUE_SUCCESS != CU_initialize_registry()) { |
161 if (CUE_SUCCESS != CU_initialize_registry()) { |
161 return CU_get_error(); |
162 return CU_get_error(); |
162 } |
163 } |
163 |
164 |
164 suite = CU_add_suite("linked list suite", NULL, NULL); |
165 suite = CU_add_suite("low level linked list suite", NULL, NULL); |
165 |
166 |
166 CU_add_test(suite, "linked list: create and destroy", test_linked_list_create); |
167 cu_add_test(suite, test_linked_list_at); |
167 CU_add_test(suite, "linked list: get node at index", test_linked_list_at); |
168 cu_add_test(suite, test_linked_list_add); |
168 CU_add_test(suite, "linked list: add", test_linked_list_add); |
169 |
|
170 suite = CU_add_suite("high level linked list suite", NULL, NULL); |
|
171 |
|
172 cu_add_test(suite, test_linked_list_create); |
169 |
173 |
170 CU_basic_set_mode(UCX_CU_BRM); |
174 CU_basic_set_mode(UCX_CU_BRM); |
171 |
175 |
172 int exitcode; |
176 int exitcode; |
173 if (CU_basic_run_tests()) { |
177 if (CU_basic_run_tests()) { |