tests/test_list.c

changeset 1477
9170a7dff573
parent 1469
9b2b40a3c9f0
equal deleted inserted replaced
1476:79d4c281a63b 1477:9170a7dff573
2867 } 2867 }
2868 2868
2869 CX_TEST(test_list_intersection_sorted_alloc_fail) { 2869 CX_TEST(test_list_intersection_sorted_alloc_fail) {
2870 CX_TEST_DO { 2870 CX_TEST_DO {
2871 CX_TEST_CALL_SUBROUTINE(verify_intersection, true, true); 2871 CX_TEST_CALL_SUBROUTINE(verify_intersection, true, true);
2872 }
2873 }
2874
2875 static CX_TEST_SUBROUTINE(verify_union, bool sorted, bool alloc_fail) {
2876 CxTestingAllocator talloc;
2877 cx_testing_allocator_init(&talloc);
2878
2879 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2880 cxDefineAdvancedDestructor(dst, cxFree, &talloc);
2881 CxList *src = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int));
2882 CxList *other = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int));
2883
2884 int dst_data[] = {47, 178, 176, 83};
2885 int src_data[] = {
2886 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,
2887 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
2888 };
2889 int other_data[] = {
2890 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,
2891 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
2892 };
2893
2894 for (unsigned i = 0 ; i < cx_nmemb(dst_data) ; i++) {
2895 int *x = cxMalloc(&talloc.base, sizeof(int));
2896 *x = dst_data[i];
2897 cxListAdd(dst, x);
2898 }
2899 cxListAddArray(src, src_data, 50);
2900 cxListAddArray(other, other_data, 50);
2901 if (sorted) {
2902 cxListSort(dst);
2903 cxListSort(src);
2904 cxListSort(other);
2905 }
2906
2907 // the 14 elements from the intersection should not appear twice in the destination:
2908 // 27, 41, 49, 53, 63, 71, 85, 130, 151, 161, 176, 188, 198, 199
2909 // however, the elements that are already in dst may appear twice
2910 size_t expected_len = 90;
2911 int expected_unsorted[] = {
2912 47, 178, 176, 83,
2913 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161,
2914 88, 116, 71, 53, 199, 124, 32, 9, 76, 151, 33, 51, 37, 65, 176, 49, 12,
2915 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97,
2916 83, 75, 137, 111, 197, 141, 46, 103, 69, 146, 79, 154, 45, 38, 139, 193,
2917 90, 64, 142, 115, 120, 78, 100, 101, 42, 21, 1, 10, 114, 181, 178, 136,
2918 59, 73, 99, 144, 118
2919 };
2920 int expected_sorted[] = {
2921 47, 83, 176, 178,
2922 1, 4, 9, 10, 12, 20, 21, 27, 28, 29, 32, 33, 37, 38, 41, 42, 44, 45,
2923 46, 49, 51, 53, 54, 59, 63, 64, 65, 69, 70, 71, 73, 74, 75, 76, 77, 78,
2924 79, 83, 85, 88, 90, 92, 94, 97, 99, 100, 101, 103, 106, 109, 111, 114,
2925 115, 116, 118, 120, 124, 129, 130, 136, 137, 139, 141, 142, 144, 146,
2926 150, 151, 153, 154, 161, 162, 171, 173, 175, 176, 177, 178, 181, 188,
2927 193, 194, 197, 198, 199, 200
2928 };
2929
2930 if (alloc_fail) {
2931 test_clone_func_max_enabled = true;
2932 test_clone_func_max_clones = 66;
2933 expected_len = 70;
2934 }
2935 CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len);
2936 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len);
2937
2938 int result = cxListUnion(dst, src, other, test_clone_func, &talloc.base, NULL);
2939 if (alloc_fail) {
2940 CX_TEST_ASSERT(result != 0);
2941 } else {
2942 CX_TEST_ASSERT(result == 0);
2943 }
2944 test_clone_func_max_enabled = false;
2945 CX_TEST_ASSERT(expected_len == cxListSize(dst));
2946 CX_TEST_ASSERT(0 == cxListCompare(dst, expected));
2947
2948 cxListFree(dst);
2949 cxListFree(src);
2950 cxListFree(other);
2951 cxListFree(expected);
2952 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
2953 }
2954
2955 CX_TEST(test_list_union_unsorted) {
2956 CX_TEST_DO {
2957 CX_TEST_CALL_SUBROUTINE(verify_union, false, false);
2958 }
2959 }
2960
2961 CX_TEST(test_list_union_sorted) {
2962 CX_TEST_DO {
2963 CX_TEST_CALL_SUBROUTINE(verify_union, true, false);
2964 }
2965 }
2966
2967 CX_TEST(test_list_union_unsorted_alloc_fail) {
2968 CX_TEST_DO {
2969 CX_TEST_CALL_SUBROUTINE(verify_union, false, true);
2970 }
2971 }
2972
2973 CX_TEST(test_list_union_sorted_alloc_fail) {
2974 CX_TEST_DO {
2975 CX_TEST_CALL_SUBROUTINE(verify_union, true, true);
2872 } 2976 }
2873 } 2977 }
2874 2978
2875 CX_TEST(test_list_pointer_list_supports_null) { 2979 CX_TEST(test_list_pointer_list_supports_null) {
2876 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2980 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
3325 3429
3326 CxTestSuite *cx_test_suite_list_set_ops(void) { 3430 CxTestSuite *cx_test_suite_list_set_ops(void) {
3327 CxTestSuite *suite = cx_test_suite_new("list collection operations"); 3431 CxTestSuite *suite = cx_test_suite_new("list collection operations");
3328 3432
3329 // we do not perform the following tests with every combination of list types 3433 // we do not perform the following tests with every combination of list types
3434 cx_test_register(suite, test_list_union_unsorted);
3435 cx_test_register(suite, test_list_union_sorted);
3436 cx_test_register(suite, test_list_union_unsorted_alloc_fail);
3437 cx_test_register(suite, test_list_union_sorted_alloc_fail);
3330 cx_test_register(suite, test_list_difference_unsorted); 3438 cx_test_register(suite, test_list_difference_unsorted);
3331 cx_test_register(suite, test_list_difference_sorted); 3439 cx_test_register(suite, test_list_difference_sorted);
3332 cx_test_register(suite, test_list_difference_unsorted_alloc_fail); 3440 cx_test_register(suite, test_list_difference_unsorted_alloc_fail);
3333 cx_test_register(suite, test_list_difference_sorted_alloc_fail); 3441 cx_test_register(suite, test_list_difference_sorted_alloc_fail);
3334 cx_test_register(suite, test_list_intersection_unsorted); 3442 cx_test_register(suite, test_list_intersection_unsorted);

mercurial