--- a/test/test_list.c Tue Oct 05 13:04:20 2021 +0200 +++ b/test/test_list.c Tue Oct 05 16:33:11 2021 +0200 @@ -146,6 +146,82 @@ CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) } +void test_linked_list_size(void) { + struct node { + void *next; + }; + ptrdiff_t loc = offsetof(struct node, next); + + struct node first = {NULL}; + struct node second = {NULL}; + struct node third = {NULL}; + + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0) + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1) + first.next = &second; + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2) + second.next = &third; + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3) + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2) +} + +void test_linked_list_sort(void) { + struct node { + void *prev; + void *next; + int data; + }; + + int expected[] = { + 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, + 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, + 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, + 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, + 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, + 4785, 4791, 4801, 4859, 4903, 4973 + }; + int scrambled[] = { + 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, + 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, + 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, + 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, + 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, + 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 + }; + + struct node *nodes = calloc(100, sizeof(struct node)); + for (int i = 0; i < 100; i++) { + nodes[i].prev = i == 0 ? NULL : &nodes[i - 1]; + nodes[i].next = i == 99 ? NULL : &nodes[i + 1]; + nodes[i].data = scrambled[i]; + } + + struct node *begin = &nodes[0]; + struct node *end = &nodes[99]; + + cx_linked_list_sort((void **) &begin, (void **) &end, + offsetof(struct node, prev), + offsetof(struct node, next), + offsetof(struct node, data), + 0, (CxListComparator) cmp_int); + + CU_ASSERT_PTR_NULL(begin->prev) + CU_ASSERT_EQUAL(begin->data, expected[0]) + struct node *check = begin; + struct node *check_last = NULL; + for (int i = 0 ; i < 100 ; i++) { + CU_ASSERT_EQUAL(check->data, expected[i]) + CU_ASSERT_PTR_EQUAL(check->prev, check_last) + if (i < 99) { + CU_ASSERT_PTR_NOT_NULL(check->next) + } + check_last = check; + check = check->next; + } + CU_ASSERT_PTR_NULL(check) + CU_ASSERT_EQUAL(end->data, expected[99]) +} + void test_hl_linked_list_create(void) { cxTestingAllocatorReset(); @@ -494,6 +570,8 @@ cu_add_test(suite, test_linked_list_at); cu_add_test(suite, test_linked_list_add); cu_add_test(suite, test_linked_list_last); + cu_add_test(suite, test_linked_list_size); + cu_add_test(suite, test_linked_list_sort); suite = CU_add_suite("high level linked list", NULL, NULL);