1.1 --- a/test/test_list.c Tue Oct 05 13:04:20 2021 +0200 1.2 +++ b/test/test_list.c Tue Oct 05 16:33:11 2021 +0200 1.3 @@ -146,6 +146,82 @@ 1.4 CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) 1.5 } 1.6 1.7 +void test_linked_list_size(void) { 1.8 + struct node { 1.9 + void *next; 1.10 + }; 1.11 + ptrdiff_t loc = offsetof(struct node, next); 1.12 + 1.13 + struct node first = {NULL}; 1.14 + struct node second = {NULL}; 1.15 + struct node third = {NULL}; 1.16 + 1.17 + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc), 0) 1.18 + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 1) 1.19 + first.next = &second; 1.20 + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 2) 1.21 + second.next = &third; 1.22 + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&first, loc), 3) 1.23 + CU_ASSERT_PTR_EQUAL(cx_linked_list_size(&second, loc), 2) 1.24 +} 1.25 + 1.26 +void test_linked_list_sort(void) { 1.27 + struct node { 1.28 + void *prev; 1.29 + void *next; 1.30 + int data; 1.31 + }; 1.32 + 1.33 + int expected[] = { 1.34 + 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880, 1.35 + 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894, 1.36 + 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797, 1.37 + 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677, 1.38 + 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681, 1.39 + 4785, 4791, 4801, 4859, 4903, 4973 1.40 + }; 1.41 + int scrambled[] = { 1.42 + 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079, 1.43 + 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888, 1.44 + 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300, 1.45 + 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424, 1.46 + 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973, 1.47 + 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 1.48 + }; 1.49 + 1.50 + struct node *nodes = calloc(100, sizeof(struct node)); 1.51 + for (int i = 0; i < 100; i++) { 1.52 + nodes[i].prev = i == 0 ? NULL : &nodes[i - 1]; 1.53 + nodes[i].next = i == 99 ? NULL : &nodes[i + 1]; 1.54 + nodes[i].data = scrambled[i]; 1.55 + } 1.56 + 1.57 + struct node *begin = &nodes[0]; 1.58 + struct node *end = &nodes[99]; 1.59 + 1.60 + cx_linked_list_sort((void **) &begin, (void **) &end, 1.61 + offsetof(struct node, prev), 1.62 + offsetof(struct node, next), 1.63 + offsetof(struct node, data), 1.64 + 0, (CxListComparator) cmp_int); 1.65 + 1.66 + CU_ASSERT_PTR_NULL(begin->prev) 1.67 + CU_ASSERT_EQUAL(begin->data, expected[0]) 1.68 + struct node *check = begin; 1.69 + struct node *check_last = NULL; 1.70 + for (int i = 0 ; i < 100 ; i++) { 1.71 + CU_ASSERT_EQUAL(check->data, expected[i]) 1.72 + CU_ASSERT_PTR_EQUAL(check->prev, check_last) 1.73 + if (i < 99) { 1.74 + CU_ASSERT_PTR_NOT_NULL(check->next) 1.75 + } 1.76 + check_last = check; 1.77 + check = check->next; 1.78 + } 1.79 + CU_ASSERT_PTR_NULL(check) 1.80 + CU_ASSERT_EQUAL(end->data, expected[99]) 1.81 +} 1.82 + 1.83 1.84 void test_hl_linked_list_create(void) { 1.85 cxTestingAllocatorReset(); 1.86 @@ -494,6 +570,8 @@ 1.87 cu_add_test(suite, test_linked_list_at); 1.88 cu_add_test(suite, test_linked_list_add); 1.89 cu_add_test(suite, test_linked_list_last); 1.90 + cu_add_test(suite, test_linked_list_size); 1.91 + cu_add_test(suite, test_linked_list_sort); 1.92 1.93 suite = CU_add_suite("high level linked list", NULL, NULL); 1.94