test/test_list.c

changeset 482
0d998f19d130
parent 479
a29bdd703e02
child 486
d7ca126eab7f
equal deleted inserted replaced
481:eef025d82a34 482:0d998f19d130
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_link_unlink(void) {
39 struct node {
40 void *next;
41 void *prev;
42 };
43 const ptrdiff_t loc_prev = offsetof(struct node, prev);
44 const ptrdiff_t loc_next = offsetof(struct node, next);
45
46 struct node a = {NULL, NULL}, b = {NULL, NULL};
47
48 cx_linked_list_link(&a, &b, loc_prev, loc_next);
49 CU_ASSERT_PTR_NULL(a.prev)
50 CU_ASSERT_PTR_EQUAL(a.next, &b)
51 CU_ASSERT_PTR_EQUAL(b.prev, &a)
52 CU_ASSERT_PTR_NULL(b.next)
53
54 cx_linked_list_unlink(&a, &b, loc_prev, loc_next);
55 CU_ASSERT_PTR_NULL(a.prev)
56 CU_ASSERT_PTR_NULL(a.next)
57 CU_ASSERT_PTR_NULL(b.prev)
58 CU_ASSERT_PTR_NULL(b.next)
59
60 struct node c = {NULL, NULL};
61 cx_linked_list_link(&b, &c, loc_prev, loc_next);
62 cx_linked_list_link(&a, &b, loc_prev, loc_next);
63 cx_linked_list_unlink(&b, &c, loc_prev, loc_next);
64 CU_ASSERT_PTR_NULL(a.prev)
65 CU_ASSERT_PTR_EQUAL(a.next, &b)
66 CU_ASSERT_PTR_EQUAL(b.prev, &a)
67 CU_ASSERT_PTR_NULL(b.next)
68 CU_ASSERT_PTR_NULL(c.prev)
69 CU_ASSERT_PTR_NULL(c.next)
70 }
71
38 void test_linked_list_at(void) { 72 void test_linked_list_at(void) {
39 struct node { 73 struct node {
40 void *next; 74 void *next;
41 void *prev; 75 void *prev;
42 }; 76 };
220 CU_ASSERT_PTR_EQUAL(end, &nodes[0]) 254 CU_ASSERT_PTR_EQUAL(end, &nodes[0])
221 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) 255 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0])
222 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) 256 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1])
223 CU_ASSERT_PTR_NULL(nodes[1].prev) 257 CU_ASSERT_PTR_NULL(nodes[1].prev)
224 CU_ASSERT_PTR_NULL(nodes[0].prev) 258 CU_ASSERT_PTR_NULL(nodes[0].prev)
259 }
260
261 void test_linked_list_insert(void) {
262 struct node {
263 void *prev;
264 void *next;
265 };
266 ptrdiff_t loc_prev = offsetof(struct node, prev);
267 ptrdiff_t loc_next = offsetof(struct node, next);
268
269 struct node nodes[4];
270 void *begin, *end;
271
272 // insert mid list
273 memset(nodes, 0, 4 * sizeof(struct node));
274 begin = &nodes[0];
275 end = &nodes[2];
276
277 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
278 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
279
280 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]);
281 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
282 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
283 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
284 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[3])
285 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
286 CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[2])
287
288 // insert end
289 memset(nodes, 0, 4 * sizeof(struct node));
290 begin = &nodes[0];
291 end = &nodes[2];
292
293 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
294 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
295
296 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]);
297 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
298 CU_ASSERT_PTR_EQUAL(end, &nodes[3])
299 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
300 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
301 CU_ASSERT_PTR_NULL(nodes[3].next)
302
303 // insert begin
304 memset(nodes, 0, 4 * sizeof(struct node));
305 begin = &nodes[0];
306 end = &nodes[2];
307
308 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
309 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
310
311 cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]);
312 CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
313 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
314 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[3])
315 CU_ASSERT_PTR_NULL(nodes[3].prev)
316 CU_ASSERT_PTR_EQUAL(nodes[3].next, &nodes[0])
317 }
318
319 void test_linked_list_insert_chain(void) {
320 struct node {
321 void *prev;
322 void *next;
323 };
324 ptrdiff_t loc_prev = offsetof(struct node, prev);
325 ptrdiff_t loc_next = offsetof(struct node, next);
326
327 struct node nodes[5];
328 void *begin, *end;
329
330 // insert mid list
331 memset(nodes, 0, 5 * sizeof(struct node));
332 begin = &nodes[0];
333 end = &nodes[2];
334
335 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
336 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
337 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
338
339 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL);
340 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
341 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
342 CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[3])
343 CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[4])
344 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[1])
345 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[2])
346
347 // insert end
348 memset(nodes, 0, 5 * sizeof(struct node));
349 begin = &nodes[0];
350 end = &nodes[2];
351
352 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
353 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
354 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
355
356 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL);
357 CU_ASSERT_PTR_EQUAL(begin, &nodes[0])
358 CU_ASSERT_PTR_EQUAL(end, &nodes[4])
359 CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[3])
360 CU_ASSERT_PTR_EQUAL(nodes[3].prev, &nodes[2])
361 CU_ASSERT_PTR_NULL(nodes[4].next)
362
363 // insert begin
364 memset(nodes, 0, 5 * sizeof(struct node));
365 begin = &nodes[0];
366 end = &nodes[2];
367
368 cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
369 cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
370 cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);
371
372 cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL);
373 CU_ASSERT_PTR_EQUAL(begin, &nodes[3])
374 CU_ASSERT_PTR_EQUAL(end, &nodes[2])
375 CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[4])
376 CU_ASSERT_PTR_NULL(nodes[3].prev)
377 CU_ASSERT_PTR_EQUAL(nodes[4].next, &nodes[0])
225 } 378 }
226 379
227 void test_linked_list_first(void) { 380 void test_linked_list_first(void) {
228 struct node { 381 struct node {
229 int data; 382 int data;
871 return CU_get_error(); 1024 return CU_get_error();
872 } 1025 }
873 1026
874 suite = CU_add_suite("low level linked list", NULL, NULL); 1027 suite = CU_add_suite("low level linked list", NULL, NULL);
875 1028
1029 cu_add_test(suite, test_linked_list_link_unlink);
876 cu_add_test(suite, test_linked_list_at); 1030 cu_add_test(suite, test_linked_list_at);
877 cu_add_test(suite, test_linked_list_prepend); 1031 cu_add_test(suite, test_linked_list_prepend);
878 cu_add_test(suite, test_linked_list_add); 1032 cu_add_test(suite, test_linked_list_add);
1033 cu_add_test(suite, test_linked_list_insert);
1034 cu_add_test(suite, test_linked_list_insert_chain);
879 cu_add_test(suite, test_linked_list_first); 1035 cu_add_test(suite, test_linked_list_first);
880 cu_add_test(suite, test_linked_list_last); 1036 cu_add_test(suite, test_linked_list_last);
881 cu_add_test(suite, test_linked_list_prev); 1037 cu_add_test(suite, test_linked_list_prev);
882 cu_add_test(suite, test_linked_list_remove); 1038 cu_add_test(suite, test_linked_list_remove);
883 cu_add_test(suite, test_linked_list_size); 1039 cu_add_test(suite, test_linked_list_size);

mercurial