diff -r 8bfccb342895 -r 9a72258446cd tests/test_list.c --- a/tests/test_list.c Sat Oct 11 11:55:46 2025 +0200 +++ b/tests/test_list.c Sat Oct 11 15:42:48 2025 +0200 @@ -36,6 +36,7 @@ #include #include +#include CX_TEST(test_array_add) { CX_ARRAY_DECLARE(int, arr); @@ -2456,6 +2457,73 @@ cxListFree(list); } +CX_TEST(test_list_use_insert_unique_to_remove_duplicates) { + CxList *linked_list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); + CxList *array_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); + CxList *defaulted_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); + do_set_default_class_funcs(defaulted_list); + int test_array[23] = { + 120, -13, 100, -90, 13, -56, 74, 20, 28, 80, 18, -56, 130, 12, 15, 0, 39, 100, 0, 29, 28, 85, 20 + }; + int test_array_unique[18] = { + -90, -56, -13, 0, 12, 13, 15, 18, 20, 28, 29, 39, 74, 80, 85, 100, 120, 130 + }; + CX_TEST_DO { + qsort(test_array, 23, sizeof(int), cx_cmp_int); + cxListInsertUniqueArray(linked_list, test_array, 23); + cxListInsertUniqueArray(array_list, test_array, 23); + cxListInsertUniqueArray(defaulted_list, test_array, 23); + CX_TEST_ASSERT(cxListSize(linked_list) == 18); + CX_TEST_ASSERT(cxListSize(array_list) == 18); + CX_TEST_ASSERT(cxListSize(defaulted_list) == 18); + for (unsigned i = 0; i < 18; i++) { + CX_TEST_ASSERT(*(int *) cxListAt(linked_list, i) == test_array_unique[i]); + CX_TEST_ASSERT(*(int *) cxListAt(array_list, i) == test_array_unique[i]); + CX_TEST_ASSERT(*(int *) cxListAt(defaulted_list, i) == test_array_unique[i]); + } + } + cxListFree(defaulted_list); + cxListFree(linked_list); + cxListFree(array_list); +} + +CX_TEST(test_list_use_insert_unique_with_duplicates_in_source) { + CxList *linked_list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); + CxList *array_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); + CxList *defaulted_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); + do_set_default_class_funcs(defaulted_list); + int pre_filled[10] = {-13, 5, 18, 30, 40, 45, 50, 80, 85, 110}; + cxListInsertSortedArray(linked_list, pre_filled, 10); + cxListInsertSortedArray(array_list, pre_filled, 10); + cxListInsertSortedArray(defaulted_list, pre_filled, 10); + int test_array[23] = { + 120, -13, 100, -90, 13, -56, 74, 20, 28, 80, 18, -56, 130, 12, 15, 0, 39, 100, 0, 29, 28, 85, 20 + }; + int test_array_unique[24] = { + -90, -56, -13, 0, 5, 12, 13, 15, 18, 20, 28, 29, 30, 39, 40, 45, 50, 74, 80, 85, 100, 110, 120, 130 + }; + CX_TEST_DO { + qsort(test_array, 23, sizeof(int), cx_cmp_int); + cxListInsertUniqueArray(linked_list, test_array, 23); + cxListInsertUniqueArray(array_list, test_array, 23); + cxListInsertUniqueArray(defaulted_list, test_array, 23); + CX_TEST_ASSERT(cxListSize(linked_list) == 24); + CX_TEST_ASSERT(cxListSize(array_list) == 24); + CX_TEST_ASSERT(cxListSize(defaulted_list) == 24); + for (unsigned i = 0; i < 24; i++) { + CX_TEST_ASSERT(*(int *) cxListAt(linked_list, i) == test_array_unique[i]); + CX_TEST_ASSERT(*(int *) cxListAt(array_list, i) == test_array_unique[i]); + CX_TEST_ASSERT(*(int *) cxListAt(defaulted_list, i) == test_array_unique[i]); + } + CX_TEST_ASSERT(*(int*)cxListLast(linked_list) == 130); + CX_TEST_ASSERT(*(int*)cxListLast(array_list) == 130); + CX_TEST_ASSERT(*(int*)cxListLast(defaulted_list) == 130); + } + cxListFree(defaulted_list); + cxListFree(linked_list); + cxListFree(array_list); +} + CxTestSuite *cx_test_suite_array_list(void) { CxTestSuite *suite = cx_test_suite_new("array_list"); @@ -2747,6 +2815,8 @@ CxTestSuite *suite = cx_test_suite_new("list corner cases"); cx_test_register(suite, test_list_pointer_list_supports_null); + cx_test_register(suite, test_list_use_insert_unique_with_duplicates_in_source); + cx_test_register(suite, test_list_use_insert_unique_to_remove_duplicates); return suite; }