diff -r 0f4d90a1ae23 -r 9b2b40a3c9f0 tests/test_list.c --- a/tests/test_list.c Tue Nov 04 14:31:31 2025 +0100 +++ b/tests/test_list.c Tue Nov 04 14:38:42 2025 +0100 @@ -2780,6 +2780,98 @@ } } +static CX_TEST_SUBROUTINE(verify_intersection, 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); + } + + // expected 14 elements in the intersection + size_t expected_len = 18; + int expected_unsorted[] = { + 47, 178, 176, 83, + 130, 27, 161, 71, 53, 199, 151, 176, 49, 85, 198, 188, 63, 41 + }; + int expected_sorted[] = { + 47, 83, 176, 178, + 27, 41, 49, 53, 63, 71, 85, 130, 151, 161, 176, 188, 198, 199 + }; + + if (alloc_fail) { + test_clone_func_max_enabled = true; + test_clone_func_max_clones = 10; + expected_len = 14; + } + CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len); + cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len); + + int result = cxListIntersection(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_intersection_unsorted) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_intersection, false, false); + } +} + +CX_TEST(test_list_intersection_sorted) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_intersection, true, false); + } +} + +CX_TEST(test_list_intersection_unsorted_alloc_fail) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_intersection, false, true); + } +} + +CX_TEST(test_list_intersection_sorted_alloc_fail) { + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(verify_intersection, true, true); + } +} + CX_TEST(test_list_pointer_list_supports_null) { CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); int x = 47; @@ -3239,6 +3331,10 @@ cx_test_register(suite, test_list_difference_sorted); cx_test_register(suite, test_list_difference_unsorted_alloc_fail); cx_test_register(suite, test_list_difference_sorted_alloc_fail); + cx_test_register(suite, test_list_intersection_unsorted); + cx_test_register(suite, test_list_intersection_sorted); + cx_test_register(suite, test_list_intersection_unsorted_alloc_fail); + cx_test_register(suite, test_list_intersection_sorted_alloc_fail); return suite; }