98 |
98 |
99 CU_ASSERT_PTR_EQUAL(&a, cx_linked_list_at(&d, 3, loc_prev, 0)); |
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)); |
100 CU_ASSERT_PTR_EQUAL(&b, cx_linked_list_at(&d, 3, loc_prev, 1)); |
101 } |
101 } |
102 |
102 |
|
103 void test_linked_list_add(void) { |
|
104 struct node { |
|
105 void *prev; |
|
106 void *next; |
|
107 int value; |
|
108 }; |
|
109 |
|
110 struct node nodes[4]; |
|
111 |
|
112 // test with begin, end / prev, next |
|
113 memset(nodes, 0, 4*sizeof(struct node)); |
|
114 void *begin = NULL; |
|
115 void *end = NULL; |
|
116 |
|
117 ptrdiff_t loc_prev = offsetof(struct node, prev); |
|
118 ptrdiff_t loc_next = offsetof(struct node, next); |
|
119 |
|
120 int ret; |
|
121 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); |
|
122 CU_ASSERT_EQUAL(ret, 0); |
|
123 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); |
|
124 CU_ASSERT_PTR_EQUAL(end, &nodes[0]); |
|
125 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL); |
|
126 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL); |
|
127 |
|
128 ret = cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); |
|
129 CU_ASSERT_EQUAL(ret, 0); |
|
130 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); |
|
131 CU_ASSERT_PTR_EQUAL(end, &nodes[1]); |
|
132 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); |
|
133 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); |
|
134 |
|
135 // test with begin only / prev, next |
|
136 memset(nodes, 0, 4*sizeof(struct node)); |
|
137 begin = NULL; |
|
138 end = NULL; |
|
139 |
|
140 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]); |
|
141 CU_ASSERT_EQUAL(ret, 0); |
|
142 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); |
|
143 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]); |
|
144 CU_ASSERT_EQUAL(ret, 0); |
|
145 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); |
|
146 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); |
|
147 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]); |
|
148 |
|
149 ret = cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); |
|
150 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]); |
|
151 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]); |
|
152 |
|
153 // test with begin, end / next |
|
154 memset(nodes, 0, 4*sizeof(struct node)); |
|
155 begin = NULL; |
|
156 end = NULL; |
|
157 |
|
158 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]); |
|
159 CU_ASSERT_EQUAL(ret, 0); |
|
160 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]); |
|
161 CU_ASSERT_PTR_EQUAL(end, &nodes[0]); |
|
162 ret = cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); |
|
163 CU_ASSERT_PTR_EQUAL(end, &nodes[1]); |
|
164 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]); |
|
165 CU_ASSERT_PTR_EQUAL(nodes[1].prev, NULL); |
|
166 } |
|
167 |
103 int main() { |
168 int main() { |
104 CU_pSuite suite = NULL; |
169 CU_pSuite suite = NULL; |
105 |
170 |
106 if (CUE_SUCCESS != CU_initialize_registry()) { |
171 if (CUE_SUCCESS != CU_initialize_registry()) { |
107 return CU_get_error(); |
172 return CU_get_error(); |
108 } |
173 } |
109 |
174 |
110 suite = CU_add_suite("linked list suite", NULL, NULL); |
175 suite = CU_add_suite("linked list suite", NULL, NULL); |
111 if (NULL == suite) { |
176 |
112 CU_cleanup_registry(); |
177 CU_add_test(suite, "linked list: create and destroy", test_linked_list_create); |
113 return CU_get_error(); |
178 CU_add_test(suite, "linked list: get node at index", test_linked_list_at); |
114 } |
179 CU_add_test(suite, "linked list: add", test_linked_list_add); |
115 |
|
116 if ( |
|
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) |
|
119 ) { |
|
120 CU_cleanup_registry(); |
|
121 return CU_get_error(); |
|
122 } |
|
123 |
|
124 suite = CU_add_suite("array suite", NULL, NULL); |
|
125 if (NULL == suite) { |
|
126 CU_cleanup_registry(); |
|
127 return CU_get_error(); |
|
128 } |
|
129 |
|
130 /* |
|
131 if ( |
|
132 !CU_add_test(suite, "array...", test_array...) |
|
133 ) { |
|
134 CU_cleanup_registry(); |
|
135 return CU_get_error(); |
|
136 } |
|
137 */ |
|
138 |
180 |
139 CU_basic_set_mode(UCX_CU_BRM); |
181 CU_basic_set_mode(UCX_CU_BRM); |
140 |
182 |
141 int exitcode; |
183 int exitcode; |
142 if (CU_basic_run_tests()) { |
184 if (CU_basic_run_tests()) { |