--- a/test/test_list.c Sat Jan 22 19:10:04 2022 +0100 +++ b/test/test_list.c Sat Jan 29 12:46:07 2022 +0100 @@ -572,6 +572,21 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_ptr_linked_list_create(void) { + cxTestingAllocatorReset(); + + CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + + CU_ASSERT_EQUAL(list->size, 0) + CU_ASSERT_EQUAL(list->capacity, (size_t) -1) + CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) + CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) + CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + void test_hl_linked_list_from_array(void) { cxTestingAllocatorReset(); @@ -614,6 +629,36 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_ptr_linked_list_add(void) { + cxTestingAllocatorReset(); + + CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + + int a = 5, b = 47, c = 13; + + CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) + CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) + CU_ASSERT_EQUAL(cxListAdd(list, &c), 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) + + a = 9; + b = 10; + c = 11; + + CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) + CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) + CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + void test_hl_linked_list_insert(void) { cxTestingAllocatorReset(); @@ -646,6 +691,35 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_ptr_linked_list_insert(void) { + cxTestingAllocatorReset(); + + CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + + int a = 5, b = 47, c = 13, d = 42; + + CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) + CU_ASSERT_EQUAL(list->size, 0) + CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) + CU_ASSERT_EQUAL(list->size, 1) + CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) + CU_ASSERT_EQUAL(list->size, 2) + CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) + CU_ASSERT_EQUAL(list->size, 3) + CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) + + CU_ASSERT_EQUAL(list->size, 4) + 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(*(int *) cxListAt(list, 2), 5) + CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + void test_hl_linked_list_remove(void) { cxTestingAllocatorReset(); @@ -686,6 +760,50 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_ptr_linked_list_remove(void) { + cxTestingAllocatorReset(); + + 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); + + 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) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + void test_hl_linked_list_at(void) { cxTestingAllocatorReset(); @@ -702,6 +820,25 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_ptr_linked_list_at(void) { + cxTestingAllocatorReset(); + + CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + + 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)) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + void test_hl_linked_list_find(void) { cxTestingAllocatorReset(); @@ -728,6 +865,36 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +void test_hl_ptr_linked_list_find(void) { + cxTestingAllocatorReset(); + + int a = 5, b = 47, c = 13, criteria; + CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); + + cxListAdd(list, &a); + cxListAdd(list, &b); + cxListAdd(list, &c); + + CU_ASSERT_EQUAL(list->size, 3) + CU_ASSERT_TRUE(list->capacity >= list->size) + + 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) + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + 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, @@ -759,6 +926,42 @@ CU_ASSERT_TRUE(cxTestingAllocatorVerify()) } +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 + }; + + cxTestingAllocatorReset(); + + 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]) + } + + cxLinkedListDestroy(list); + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) +} + void test_hl_linked_list_iterator_impl(CxList list) { int i = 0; CxIterator iter = cxListBegin(list); @@ -798,209 +1001,6 @@ test_hl_linked_list_iterator_impl(list); } -void test_hl_ptr_linked_list_create(void) { - cxTestingAllocatorReset(); - - CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - - CU_ASSERT_EQUAL(list->size, 0) - CU_ASSERT_EQUAL(list->capacity, (size_t) -1) - CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator) - CU_ASSERT_EQUAL(list->itemsize, sizeof(void *)) - CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int) - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - -void test_hl_ptr_linked_list_add(void) { - cxTestingAllocatorReset(); - - CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - - int a = 5, b = 47, c = 13; - - CU_ASSERT_EQUAL(cxListAdd(list, &a), 0) - CU_ASSERT_EQUAL(cxListAdd(list, &b), 0) - CU_ASSERT_EQUAL(cxListAdd(list, &c), 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) - - a = 9; - b = 10; - c = 11; - - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 0), 9) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 1), 10) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 2), 11) - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - -void test_hl_ptr_linked_list_insert(void) { - cxTestingAllocatorReset(); - - CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - - int a = 5, b = 47, c = 13, d = 42; - - CU_ASSERT_NOT_EQUAL(cxListInsert(list, 1, &a), 0) - CU_ASSERT_EQUAL(list->size, 0) - CU_ASSERT_EQUAL(cxListInsert(list, 0, &a), 0) - CU_ASSERT_EQUAL(list->size, 1) - CU_ASSERT_EQUAL(cxListInsert(list, 0, &b), 0) - CU_ASSERT_EQUAL(list->size, 2) - CU_ASSERT_EQUAL(cxListInsert(list, 1, &c), 0) - CU_ASSERT_EQUAL(list->size, 3) - CU_ASSERT_EQUAL(cxListInsert(list, 3, &d), 0) - - CU_ASSERT_EQUAL(list->size, 4) - 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(*(int *) cxListAt(list, 2), 5) - CU_ASSERT_EQUAL(*(int *) cxListAt(list, 3), 42) - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - -void test_hl_ptr_linked_list_remove(void) { - cxTestingAllocatorReset(); - - 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); - - 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) - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - -void test_hl_ptr_linked_list_at(void) { - cxTestingAllocatorReset(); - - CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - - 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)) - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - -void test_hl_ptr_linked_list_find(void) { - cxTestingAllocatorReset(); - - int a = 5, b = 47, c = 13, criteria; - CxList list = cxPointerLinkedListCreate(cxTestingAllocator, cmp_int); - - cxListAdd(list, &a); - cxListAdd(list, &b); - cxListAdd(list, &c); - - CU_ASSERT_EQUAL(list->size, 3) - CU_ASSERT_TRUE(list->capacity >= list->size) - - 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) - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - -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 - }; - - cxTestingAllocatorReset(); - - 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]) - } - - cxLinkedListDestroy(list); - CU_ASSERT_TRUE(cxTestingAllocatorVerify()) -} - int main() { CU_pSuite suite = NULL;