test/test_list.c

changeset 475
31bf97fdbf71
parent 474
9c1fccda16bc
child 476
60ff4561dc04
equal deleted inserted replaced
474:9c1fccda16bc 475:31bf97fdbf71
86 ptrdiff_t loc_next = offsetof(struct node, next); 86 ptrdiff_t loc_next = offsetof(struct node, next);
87 87
88 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); 88 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
89 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 89 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
90 CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 90 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
91 CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL) 91 CU_ASSERT_PTR_NULL(nodes[0].prev)
92 CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL) 92 CU_ASSERT_PTR_NULL(nodes[0].next)
93 93
94 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); 94 cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
95 CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) 95 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
96 CU_ASSERT_PTR_EQUAL(end, &nodes[1]) 96 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
97 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) 97 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
111 111
112 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]); 112 cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
113 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) 113 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
114 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) 114 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
115 115
116 // test with end only / prev, next
117 memset(nodes, 0, 4 * sizeof(struct node));
118 begin = NULL;
119 end = NULL;
120
121 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]);
122 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
123 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]);
124 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
125 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
126 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0])
127
128 cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]);
129 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
130 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2])
131 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1])
132
116 // test with begin, end / next 133 // test with begin, end / next
117 memset(nodes, 0, 4 * sizeof(struct node)); 134 memset(nodes, 0, 4 * sizeof(struct node));
118 begin = NULL; 135 begin = NULL;
119 end = NULL; 136 end = NULL;
120 137
123 CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 140 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
124 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]); 141 cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
125 CU_ASSERT_PTR_EQUAL(end, &nodes[1]) 142 CU_ASSERT_PTR_EQUAL(end, &nodes[1])
126 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) 143 CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1])
127 CU_ASSERT_PTR_NULL(nodes[1].prev) 144 CU_ASSERT_PTR_NULL(nodes[1].prev)
145 }
146
147 void test_linked_list_prepend(void) {
148 struct node {
149 void *prev;
150 void *next;
151 };
152
153 struct node nodes[4];
154
155 // test with begin, end / prev, next
156 memset(nodes, 0, 4 * sizeof(struct node));
157 void *begin = NULL;
158 void *end = NULL;
159
160 ptrdiff_t loc_prev = offsetof(struct node, prev);
161 ptrdiff_t loc_next = offsetof(struct node, next);
162
163 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
164 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
165 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
166 CU_ASSERT_PTR_NULL(nodes[0].prev)
167 CU_ASSERT_PTR_NULL(nodes[0].next)
168
169 cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
170 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
171 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
172 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
173 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
174
175 // test with begin only / prev, next
176 memset(nodes, 0, 4 * sizeof(struct node));
177 begin = NULL;
178 end = NULL;
179
180 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]);
181 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
182 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]);
183 CU_ASSERT_PTR_EQUAL(begin, &nodes[1])
184 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
185 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
186
187 cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]);
188 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
189 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
190 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
191
192 // test with end only / prev, next
193 memset(nodes, 0, 4 * sizeof(struct node));
194 begin = NULL;
195 end = NULL;
196
197 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]);
198 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
199 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]);
200 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
201 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
202 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1])
203
204 cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]);
205 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
206 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
207 CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2])
208
209 // test with begin, end / next
210 memset(nodes, 0, 4 * sizeof(struct node));
211 begin = NULL;
212 end = NULL;
213
214 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
215 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
216 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
217 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
218 cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
219 CU_ASSERT_PTR_EQUAL(begin, &nodes[2])
220 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
221 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
222 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
223 CU_ASSERT_PTR_NULL(nodes[1].prev)
224 CU_ASSERT_PTR_NULL(nodes[0].prev)
225 }
226
227 void test_linked_list_first(void) {
228 CU_ASSERT_PTR_NULL(cx_linked_list_first(NULL, 0))
229
230 struct node {
231 int data;
232 void *prev;
233 };
234 ptrdiff_t loc = offsetof(struct node, prev);
235
236 struct node first = {1, NULL};
237 struct node second = {2, &first};
238 struct node third = {3, &second};
239
240 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&first, loc), &first)
241 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&second, loc), &first)
242 CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&third, loc), &first)
128 } 243 }
129 244
130 void test_linked_list_last(void) { 245 void test_linked_list_last(void) {
131 CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0)) 246 CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0))
132 247
736 } 851 }
737 852
738 suite = CU_add_suite("low level linked list", NULL, NULL); 853 suite = CU_add_suite("low level linked list", NULL, NULL);
739 854
740 cu_add_test(suite, test_linked_list_at); 855 cu_add_test(suite, test_linked_list_at);
856 cu_add_test(suite, test_linked_list_prepend);
741 cu_add_test(suite, test_linked_list_add); 857 cu_add_test(suite, test_linked_list_add);
858 cu_add_test(suite, test_linked_list_first);
742 cu_add_test(suite, test_linked_list_last); 859 cu_add_test(suite, test_linked_list_last);
743 cu_add_test(suite, test_linked_list_prev); 860 cu_add_test(suite, test_linked_list_prev);
744 cu_add_test(suite, test_linked_list_remove); 861 cu_add_test(suite, test_linked_list_remove);
745 cu_add_test(suite, test_linked_list_size); 862 cu_add_test(suite, test_linked_list_size);
746 cu_add_test(suite, test_linked_list_sort); 863 cu_add_test(suite, test_linked_list_sort);

mercurial