diff -r 8aea65ae1eaf -r 0d3c6075f82c test/test_list.c --- a/test/test_list.c Sat Apr 09 16:37:43 2022 +0200 +++ b/test/test_list.c Sat Apr 09 18:02:53 2022 +0200 @@ -27,6 +27,7 @@ */ #include "cx/linked_list.h" +#include "cx/utils.h" #include "test_config.h" #include "util_allocator.h" @@ -52,11 +53,11 @@ const ptrdiff_t loc_next = offsetof(struct node, next); const ptrdiff_t loc_data = offsetof(struct node, data); -struct node *create_test_data( +static struct node *create_nodes_test_data( size_t n, int const data[] ) { - if (n == 0) return NULL; + CU_ASSERT_NOT_EQUAL_FATAL(n, 0) struct node *begin = calloc(1, sizeof(struct node)); struct node *prev = begin; if (data) begin->data = data[0]; @@ -69,7 +70,7 @@ return begin; } -void destroy_test_data(struct node *begin) { +static void destroy_nodes_test_data(struct node *begin) { struct node *node = begin; while (node) { struct node *next = node->next; @@ -78,6 +79,12 @@ } } +static int *create_ints_test_data(size_t len) { + int *data = malloc(sizeof(int) * len); + cx_for_n (i, len) data[i] = rand(); // NOLINT(cert-msc50-cpp) + return data; +} + void test_linked_list_link_unlink(void) { struct node nd(a), nd(b), nd(c); @@ -129,7 +136,7 @@ void test_linked_list_find(void) { int data[] = {2, 4, 6, 8}; - void *list = create_test_data(4, data); + void *list = create_nodes_test_data(4, data); int s; s = 2; @@ -157,9 +164,9 @@ int b[] = {2, 4, 6}; int c[] = {2, 4, 6, 9}; - void *la = create_test_data(4, a); - void *lb = create_test_data(3, b); - void *lc = create_test_data(4, c); + void *la = create_nodes_test_data(4, a); + void *lb = create_nodes_test_data(3, b); + void *lc = create_nodes_test_data(4, c); CU_ASSERT_TRUE(0 < cx_linked_list_compare(la, lb, loc_next, loc_data, false, cmp_int) @@ -177,9 +184,9 @@ false, cmp_int) ) - destroy_test_data(la); - destroy_test_data(lb); - destroy_test_data(lc); + destroy_nodes_test_data(la); + destroy_nodes_test_data(lb); + destroy_nodes_test_data(lc); } void test_linked_list_add(void) { @@ -421,35 +428,35 @@ } void test_linked_list_first(void) { - struct node *begin = create_test_data(3, NULL); + struct node *begin = create_nodes_test_data(3, NULL); CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin, loc_prev), begin) CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next, loc_prev), begin) CU_ASSERT_PTR_EQUAL(cx_linked_list_first(begin->next->next, loc_prev), begin) - destroy_test_data(begin); + destroy_nodes_test_data(begin); } void test_linked_list_last(void) { - struct node *begin = create_test_data(3, NULL); + struct node *begin = create_nodes_test_data(3, NULL); struct node *end = begin->next->next; CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin, loc_next), end) CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next, loc_next), end) CU_ASSERT_PTR_EQUAL(cx_linked_list_last(begin->next->next, loc_next), end) - destroy_test_data(begin); + destroy_nodes_test_data(begin); } void test_linked_list_prev(void) { - struct node *begin = create_test_data(3, NULL); + struct node *begin = create_nodes_test_data(3, NULL); CU_ASSERT_PTR_NULL(cx_linked_list_prev(begin, loc_next, begin)) CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next), begin) CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next) - destroy_test_data(begin); + destroy_nodes_test_data(begin); } void test_linked_list_remove(void) { void *begin, *end; int data[] = {2, 4, 6}; - begin = create_test_data(3, data); + begin = create_nodes_test_data(3, data); struct node *first = begin; struct node *second = first->next; struct node *third = second->next; @@ -483,13 +490,13 @@ CU_ASSERT_PTR_EQUAL(cx_linked_list_size(NULL, loc_next), 0) - list = create_test_data(5, NULL); + list = create_nodes_test_data(5, NULL); CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 5) - destroy_test_data(list); + destroy_nodes_test_data(list); - list = create_test_data(13, NULL); + list = create_nodes_test_data(13, NULL); CU_ASSERT_EQUAL(cx_linked_list_size(list, loc_next), 13) - destroy_test_data(list); + destroy_nodes_test_data(list); } void test_linked_list_sort(void) { @@ -510,7 +517,7 @@ 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681 }; - void *begin = create_test_data(100, scrambled); + void *begin = create_nodes_test_data(100, scrambled); void *end = cx_linked_list_last(begin, loc_next); cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, @@ -520,7 +527,7 @@ struct node *check_last = NULL; CU_ASSERT_PTR_NULL(check->prev) CU_ASSERT_EQUAL(check->data, expected[0]) - for (int i = 0; i < 100; i++) { + cx_for_n (i, 100) { CU_ASSERT_EQUAL(check->data, expected[i]) CU_ASSERT_PTR_EQUAL(check->prev, check_last) if (i < 99) { @@ -532,7 +539,7 @@ CU_ASSERT_PTR_NULL(check) CU_ASSERT_PTR_EQUAL(end, check_last) - destroy_test_data(begin); + destroy_nodes_test_data(begin); } void test_linked_list_reverse(void) { @@ -541,8 +548,8 @@ int data[] = {2, 4, 6, 8}; int reversed[] = {8, 6, 4, 2}; - void *list = create_test_data(4, data); - void *expected = create_test_data(4, reversed); + void *list = create_nodes_test_data(4, data); + void *expected = create_nodes_test_data(4, reversed); begin = list; end = cx_linked_list_last(list, loc_next); @@ -553,8 +560,8 @@ CU_ASSERT_TRUE(0 == cx_linked_list_compare(begin, expected, loc_next, loc_data, 0, cmp_int)) - destroy_test_data(begin); - destroy_test_data(expected); + destroy_nodes_test_data(begin); + destroy_nodes_test_data(expected); } static void verify_linked_list_create(CxList *list) { @@ -582,7 +589,7 @@ int data[] = {2, 4, 5, 7, 10, 15}; CxList *expected = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); - for (int i = 0; i < 5; i++) cxListAdd(expected, &data[i]); + cx_for_n (i, 5) cxListAdd(expected, &data[i]); CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, data); @@ -598,25 +605,15 @@ size_t len, bool write_through ) { - for (size_t i = 0; i < len; i++) { - CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0) - } + cx_for_n (i, len) CU_ASSERT_EQUAL(cxListAdd(list, &data[i]), 0) CU_ASSERT_EQUAL(list->size, len) CU_ASSERT_TRUE(list->capacity >= list->size) - for (size_t i = 0; i < len; i++) { - CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) - } - for (size_t i = 0; i < len; i++) { - ++data[i]; - } + cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) + cx_for_n (i, len) ++data[i]; if (write_through) { - for (size_t i = 0; i < len; i++) { - CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) - } + cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) } else { - for (size_t i = 0; i < len; i++) { - CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1) - } + cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i] - 1) } cxListDestroy(list); } @@ -629,42 +626,11 @@ void test_hl_ptr_linked_list_add(void) { CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - int data[] = {5, 47, 13, 9, 18, 1, 42}; + int data[] = {5, 47, 84, 13, 9, 18, 90, 1, 42}; verify_hl_linked_list_add(list, data, sizeof(data) / sizeof(int), true); } -void test_hl_linked_list_insert(void) { - int data; - CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); - - data = 5; - CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &data), 0) - CU_ASSERT_EQUAL(list->size, 0) - CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) - CU_ASSERT_EQUAL(list->size, 1) - data = 47; - CU_ASSERT_EQUAL(cxListInsert(list, 0, &data), 0) - CU_ASSERT_EQUAL(list->size, 2) - data = 13; - CU_ASSERT_EQUAL(cxListInsert(list, 1, &data), 0) - CU_ASSERT_EQUAL(list->size, 3) - data = 42; - CU_ASSERT_EQUAL(cxListInsert(list, 3, &data), 0) - - CU_ASSERT_EQUAL(list->size, 4) - CU_ASSERT_TRUE(list->capacity >= list->size) - - int exp[] = {47, 13, 5, 42}; - CxList *expected = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 4, exp); - CU_ASSERT_TRUE(0 == cxListCompare(list, expected)) - - cxListDestroy(list); - cxListDestroy(expected); -} - -void test_hl_ptr_linked_list_insert(void) { - CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - +static void verify_hl_linked_list_insert(CxList *list) { int a = 5, b = 47, c = 13, d = 42; CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) @@ -688,11 +654,15 @@ cxListDestroy(list); } -void test_hl_linked_list_remove(void) { - int data[] = {5, 47, 42, 13}; - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, - sizeof(int), 4, data); +void test_hl_linked_list_insert(void) { + verify_hl_linked_list_insert(cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int))); +} +void test_hl_ptr_linked_list_insert(void) { + verify_hl_linked_list_insert(cxPointerLinkedListCreate(cxTestingAllocator, cmp_int)); +} + +static void verify_hl_linked_list_remove(CxList *list) { CU_ASSERT_EQUAL(list->size, 4) CU_ASSERT_TRUE(list->capacity >= list->size) @@ -725,187 +695,131 @@ cxListDestroy(list); } +void test_hl_linked_list_remove(void) { + int data[] = {5, 47, 42, 13}; + CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, + sizeof(int), 4, data); + verify_hl_linked_list_remove(list); +} + void test_hl_ptr_linked_list_remove(void) { int a = 5, b = 47, c = 42, d = 13; CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - cxListAdd(list, &a); cxListAdd(list, &b); cxListAdd(list, &c); cxListAdd(list, &d); + verify_hl_linked_list_remove(list); +} - CU_ASSERT_EQUAL(list->size, 4) - CU_ASSERT_TRUE(list->capacity >= list->size) - - CU_ASSERT_NOT_EQUAL(cxListRemove(list, 4), 0) - - CU_ASSERT_EQUAL(cxListRemove(list, 2), 0) - CU_ASSERT_EQUAL(list->size, 3) - CU_ASSERT_TRUE(list->capacity >= list->size) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) - - CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) - CU_ASSERT_EQUAL(list->size, 2) - CU_ASSERT_TRUE(list->capacity >= list->size) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 13) - - CU_ASSERT_EQUAL(cxListRemove(list, 1), 0) - CU_ASSERT_EQUAL(list->size, 1) - CU_ASSERT_TRUE(list->capacity >= list->size) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 47) - - CU_ASSERT_EQUAL(cxListRemove(list, 0), 0) - CU_ASSERT_EQUAL(list->size, 0) - CU_ASSERT_TRUE(list->capacity >= list->size) - - CU_ASSERT_NOT_EQUAL(cxListRemove(list, 0), 0) - +static void verify_hl_linked_list_at( + CxList *list, + size_t len, + int *data +) { + CU_ASSERT_EQUAL(list->size, len) + cx_for_n (i, len) CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), data[i]) + CU_ASSERT_PTR_NULL(cxListAt(list, len)) + free(data); cxListDestroy(list); } void test_hl_linked_list_at(void) { - int data[] = {5, 47, 13}; + size_t len = 100; + int *data = create_ints_test_data(len); CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, - sizeof(int), 3, data); - - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) - CU_ASSERT_PTR_NULL(cxListAt(list, 3)) - - cxListDestroy(list); + sizeof(int), len, data); + verify_hl_linked_list_at(list, len, data); } void test_hl_ptr_linked_list_at(void) { + size_t len = 250; + int *data = create_ints_test_data(len); CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + cx_for_n (i, len) cxListAdd(list, &data[i]); + verify_hl_linked_list_at(list, len, data); +} - int a = 5, b = 47, c = 13; - cxListAdd(list, &a); - cxListAdd(list, &b); - cxListAdd(list, &c); - - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 5) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 47) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 13) - CU_ASSERT_PTR_NULL(cxListAt(list, 3)) - +static void verify_hl_linked_list_find( + CxList *list, + size_t len, + int *data +) { + cx_for_n (attempt, 100) { + size_t exp = rand() % len; // NOLINT(cert-msc50-cpp) + int val = data[exp]; + cx_for_n (i, exp) { + if (data[i] == val) { + exp = i; + break; + } + } + CU_ASSERT_EQUAL(cxListFind(list, &val), exp) + } + free(data); cxListDestroy(list); } void test_hl_linked_list_find(void) { - int data[] = {5, 47, 13}; - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, - sizeof(int), 3, data); - CU_ASSERT_EQUAL(list->size, 3) - CU_ASSERT_TRUE(list->capacity >= list->size) - - int criteria; - - criteria = 5; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) - criteria = 47; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) - criteria = 13; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) - criteria = 9000; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) - criteria = -5; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) - - cxListDestroy(list); + size_t len = 100; + int *data = create_ints_test_data(len); + CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), len, data); + verify_hl_linked_list_find(list, len, data); } void test_hl_ptr_linked_list_find(void) { - int a = 5, b = 47, c = 13, criteria; + size_t len = 250; + int *data = create_ints_test_data(len); CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + cx_for_n (i, len) cxListAdd(list, &data[i]); + verify_hl_linked_list_find(list, len, data); +} - cxListAdd(list, &a); - cxListAdd(list, &b); - cxListAdd(list, &c); +struct sort_test_data { + size_t len; + int *data; + int *sorted; +}; - CU_ASSERT_EQUAL(list->size, 3) - CU_ASSERT_TRUE(list->capacity >= list->size) +static struct sort_test_data create_sort_test_data(void) { + size_t len = 1000; + int *data = create_ints_test_data(len); + int *sorted = malloc(sizeof(int) * len); + memcpy(sorted, data, sizeof(int) * len); + qsort(sorted, len, sizeof(int), cmp_int); + struct sort_test_data s = {len, data, sorted}; + return s; +} - criteria = 5; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 0) - criteria = 47; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) - criteria = 13; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 2) - criteria = 9000; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) - criteria = -5; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 3) - b = -5; - CU_ASSERT_EQUAL(cxListFind(list, &criteria), 1) +static void free_sort_test_data(struct sort_test_data s) { + free(s.data); + free(s.sorted); +} +static void verify_hl_linked_list_sort( + CxList *list, + struct sort_test_data td +) { + cxListSort(list); + cx_for_n (i, td.len) CU_ASSERT_EQUAL_FATAL(*(int *) cxListAt(list, i), td.sorted[i]) + free_sort_test_data(td); cxListDestroy(list); } void test_hl_linked_list_sort(void) { - 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 - }; - CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, scrambled); - CxList *exp = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 100, expected); - - cxListSort(list); - CU_ASSERT_TRUE(0 == cxListCompare(list, exp)) - - cxListDestroy(list); - cxListDestroy(exp); + struct sort_test_data td = create_sort_test_data(); + CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), td.len, td.data); + verify_hl_linked_list_sort(list, td); } void test_hl_ptr_linked_list_sort(void) { - 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 sort_test_data td = create_sort_test_data(); CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - - for (int i = 0; i < 100; i++) { - cxListAdd(list, &scrambled[i]); - } - - cxListSort(list); - - for (int i = 0; i < 100; i++) { - CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) - } - - cxListDestroy(list); + cx_for_n (i, td.len) cxListAdd(list, &td.data[i]); + verify_hl_linked_list_sort(list, td); } -void test_hl_linked_list_iterator_impl(CxList *list) { +void verify_hl_linked_list_iterator(CxList *list) { int i = 0; CxIterator iter = cxListBegin(list); cx_foreach(int*, x, iter) { @@ -916,111 +830,75 @@ } CU_ASSERT_EQUAL(i, 10) CU_ASSERT_EQUAL_FATAL(list->size, 5) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 0) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 2) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 4) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 6) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 4), 8) + cx_for_n(j, 5) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), (int) j * 2) cxListDestroy(list); } void test_hl_linked_list_iterator(void) { CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); - for (int i = 0; i < 10; i++) { - cxListAdd(list, &i); - } - test_hl_linked_list_iterator_impl(list); + cx_for_n (i, 10) cxListAdd(list, &i); + verify_hl_linked_list_iterator(list); } void test_hl_ptr_linked_list_iterator(void) { CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - for (int i = 0; i < 10; i++) { - cxListAdd(list, &data[i]); - } - test_hl_linked_list_iterator_impl(list); + cx_for_n (i, 10) cxListAdd(list, &data[i]); + verify_hl_linked_list_iterator(list); } -void test_hl_linked_list_insert_via_iterator(void) { - CxList *list = cxLinkedListCreate(cxTestingAllocator, cmp_int, sizeof(int)); - for (int i = 0; i < 5; i++) { - cxListAdd(list, &i); - } +static void verify_hl_linked_list_insert_via_iterator( + CxList *list, + int *testdata +) { CxIterator iter = cxListIterator(list, 2); CU_ASSERT_EQUAL(iter.index, 2) CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) + size_t i = 4; - int data = 10; - cxListInsertAfter(&iter, &data); + ++i; + cxListInsertAfter(&iter, &testdata[i]); CU_ASSERT_EQUAL(iter.index, 2) CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) - data = 20; - cxListInsertBefore(&iter, &data); + ++i; + cxListInsertBefore(&iter, &testdata[i]); CU_ASSERT_EQUAL(iter.index, 3) CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) - data = 30; iter = cxListBegin(list); - cxListInsertBefore(&iter, &data); + ++i; + cxListInsertBefore(&iter, &testdata[i]); CU_ASSERT_EQUAL(iter.index, 1) CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) - data = 40; iter = cxListIterator(list, list->size); - cxListInsertBefore(&iter, &data); + ++i; + cxListInsertBefore(&iter, &testdata[i]); CU_ASSERT_EQUAL(iter.index, 9) CU_ASSERT_FALSE(cxIteratorValid(&iter)) - data = 50; iter = cxListIterator(list, list->size); - cxListInsertAfter(&iter, &data); + ++i; + cxListInsertAfter(&iter, &testdata[i]); CU_ASSERT_EQUAL(iter.index, 10) CU_ASSERT_FALSE(cxIteratorValid(&iter)) int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; - CxList *expected = cxLinkedListFromArray(cxTestingAllocator, - cmp_int, sizeof(int), 10, expdata); + cx_for_n (j, 10) CU_ASSERT_EQUAL(*(int *) cxListAt(list, j), expdata[j]) + cxListDestroy(list); +} - CU_ASSERT_EQUAL(0, cxListCompare(list, expected)) - cxListDestroy(list); - cxListDestroy(expected); +void test_hl_linked_list_insert_via_iterator(void) { + int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; + // only add the first five elements, the remaining five will be inserted + CxList *list = cxLinkedListFromArray(cxTestingAllocator, cmp_int, sizeof(int), 5, testdata); + verify_hl_linked_list_insert_via_iterator(list, testdata); } void test_hl_ptr_linked_list_insert_via_iterator(void) { int testdata[] = {0, 1, 2, 3, 4, 10, 20, 30, 40, 50}; CxList *list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - int i; - for (i = 0; i < 5; i++) { - cxListAdd(list, &testdata[i]); - } - CxIterator iter = cxListIterator(list, 2); - CU_ASSERT_EQUAL(iter.index, 2) - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) - - cxListInsertAfter(&iter, &testdata[i++]); - CU_ASSERT_EQUAL(iter.index, 2) - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) - cxListInsertBefore(&iter, &testdata[i++]); - CU_ASSERT_EQUAL(iter.index, 3) - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 2) - - iter = cxListBegin(list); - cxListInsertBefore(&iter, &testdata[i++]); - CU_ASSERT_EQUAL(iter.index, 1) - CU_ASSERT_EQUAL(*(int *) cxIteratorCurrent(&iter), 0) - iter = cxListIterator(list, list->size); - cxListInsertBefore(&iter, &testdata[i++]); - CU_ASSERT_EQUAL(iter.index, 9) - CU_ASSERT_FALSE(cxIteratorValid(&iter)) - iter = cxListIterator(list, list->size); - cxListInsertAfter(&iter, &testdata[i++]); - CU_ASSERT_EQUAL(iter.index, 10) - CU_ASSERT_FALSE(cxIteratorValid(&iter)) - - int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50}; - for (i = 0; i < 10; i++) { - CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expdata[i]) - } - - cxListDestroy(list); + // only add the first five elements, the remaining five will be inserted + cx_for_n (i, 5) cxListAdd(list, &testdata[i]); + verify_hl_linked_list_insert_via_iterator(list, testdata); } static void test_setup_allocator(void) {