diff -r 79d4c281a63b -r 9170a7dff573 tests/test_list.c --- a/tests/test_list.c Fri Nov 07 18:42:06 2025 +0100 +++ b/tests/test_list.c Fri Nov 07 19:13:28 2025 +0100 @@ -2872,6 +2872,110 @@ } } +static CX_TEST_SUBROUTINE(verify_union, bool sorted, bool alloc_fail) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + + CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); + cxDefineAdvancedDestructor(dst, cxFree, &talloc); + CxList *src = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); + CxList *other = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); + + int dst_data[] = {47, 178, 176, 83}; + int src_data[] = { + 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76, + 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83 + }; + int other_data[] = { + 75, 137, 176, 111, 85, 27, 197, 141, 46, 103, 69, 146, 49, 79, 63, 130, 154, 45, 38, 139, 193, 90, 64, 142, 115, + 120, 78, 100, 101, 42, 21, 1, 161, 10, 114, 198, 181, 178, 136, 188, 59, 41, 73, 99, 151, 144, 118, 53, 199, 71 + }; + + for (unsigned i = 0 ; i < cx_nmemb(dst_data) ; i++) { + int *x = cxMalloc(&talloc.base, sizeof(int)); + *x = dst_data[i]; + cxListAdd(dst, x); + } + cxListAddArray(src, src_data, 50); + cxListAddArray(other, other_data, 50); + if (sorted) { + cxListSort(dst); + cxListSort(src); + cxListSort(other); + } + + // the 14 elements from the intersection should not appear twice in the destination: + // 27, 41, 49, 53, 63, 71, 85, 130, 151, 161, 176, 188, 198, 199 + // however, the elements that are already in dst may appear twice + size_t expected_len = 90; + int expected_unsorted[] = { + 47, 178, 176, 83, + 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, + 88, 116, 71, 53, 199, 124, 32, 9, 76, 151, 33, 51, 37, 65, 176, 49, 12, + 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, + 83, 75, 137, 111, 197, 141, 46, 103, 69, 146, 79, 154, 45, 38, 139, 193, + 90, 64, 142, 115, 120, 78, 100, 101, 42, 21, 1, 10, 114, 181, 178, 136, + 59, 73, 99, 144, 118 + }; + int expected_sorted[] = { + 47, 83, 176, 178, + 1, 4, 9, 10, 12, 20, 21, 27, 28, 29, 32, 33, 37, 38, 41, 42, 44, 45, + 46, 49, 51, 53, 54, 59, 63, 64, 65, 69, 70, 71, 73, 74, 75, 76, 77, 78, + 79, 83, 85, 88, 90, 92, 94, 97, 99, 100, 101, 103, 106, 109, 111, 114, + 115, 116, 118, 120, 124, 129, 130, 136, 137, 139, 141, 142, 144, 146, + 150, 151, 153, 154, 161, 162, 171, 173, 175, 176, 177, 178, 181, 188, + 193, 194, 197, 198, 199, 200 + }; + + if (alloc_fail) { + test_clone_func_max_enabled = true; + test_clone_func_max_clones = 66; + expected_len = 70; + } + CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len); + cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len); + + int result = cxListUnion(dst, src, other, test_clone_func, &talloc.base, NULL); + if (alloc_fail) { + CX_TEST_ASSERT(result != 0); + } else { + CX_TEST_ASSERT(result == 0); + } + test_clone_func_max_enabled = false; + CX_TEST_ASSERT(expected_len == cxListSize(dst)); + CX_TEST_ASSERT(0 == cxListCompare(dst, expected)); + + cxListFree(dst); + cxListFree(src); + cxListFree(other); + cxListFree(expected); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); +} + +CX_TEST(test_list_union_unsorted) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_union, false, false); + } +} + +CX_TEST(test_list_union_sorted) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_union, true, false); + } +} + +CX_TEST(test_list_union_unsorted_alloc_fail) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_union, false, true); + } +} + +CX_TEST(test_list_union_sorted_alloc_fail) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_union, true, true); + } +} + CX_TEST(test_list_pointer_list_supports_null) { CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); int x = 47; @@ -3327,6 +3431,10 @@ CxTestSuite *suite = cx_test_suite_new("list collection operations"); // we do not perform the following tests with every combination of list types + cx_test_register(suite, test_list_union_unsorted); + cx_test_register(suite, test_list_union_sorted); + cx_test_register(suite, test_list_union_unsorted_alloc_fail); + cx_test_register(suite, test_list_union_sorted_alloc_fail); cx_test_register(suite, test_list_difference_unsorted); cx_test_register(suite, test_list_difference_sorted); cx_test_register(suite, test_list_difference_unsorted_alloc_fail);