tests/test_list.c

changeset 1469
9b2b40a3c9f0
parent 1466
a58c65d31342
equal deleted inserted replaced
1468:0f4d90a1ae23 1469:9b2b40a3c9f0
2775 } 2775 }
2776 2776
2777 CX_TEST(test_list_difference_sorted_alloc_fail) { 2777 CX_TEST(test_list_difference_sorted_alloc_fail) {
2778 CX_TEST_DO { 2778 CX_TEST_DO {
2779 CX_TEST_CALL_SUBROUTINE(verify_difference, true, true); 2779 CX_TEST_CALL_SUBROUTINE(verify_difference, true, true);
2780 }
2781 }
2782
2783 static CX_TEST_SUBROUTINE(verify_intersection, bool sorted, bool alloc_fail) {
2784 CxTestingAllocator talloc;
2785 cx_testing_allocator_init(&talloc);
2786
2787 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2788 cxDefineAdvancedDestructor(dst, cxFree, &talloc);
2789 CxList *src = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int));
2790 CxList *other = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int));
2791
2792 int dst_data[] = {47, 178, 176, 83};
2793 int src_data[] = {
2794 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,
2795 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
2796 };
2797 int other_data[] = {
2798 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,
2799 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
2800 };
2801
2802 for (unsigned i = 0 ; i < cx_nmemb(dst_data) ; i++) {
2803 int *x = cxMalloc(&talloc.base, sizeof(int));
2804 *x = dst_data[i];
2805 cxListAdd(dst, x);
2806 }
2807 cxListAddArray(src, src_data, 50);
2808 cxListAddArray(other, other_data, 50);
2809 if (sorted) {
2810 cxListSort(dst);
2811 cxListSort(src);
2812 cxListSort(other);
2813 }
2814
2815 // expected 14 elements in the intersection
2816 size_t expected_len = 18;
2817 int expected_unsorted[] = {
2818 47, 178, 176, 83,
2819 130, 27, 161, 71, 53, 199, 151, 176, 49, 85, 198, 188, 63, 41
2820 };
2821 int expected_sorted[] = {
2822 47, 83, 176, 178,
2823 27, 41, 49, 53, 63, 71, 85, 130, 151, 161, 176, 188, 198, 199
2824 };
2825
2826 if (alloc_fail) {
2827 test_clone_func_max_enabled = true;
2828 test_clone_func_max_clones = 10;
2829 expected_len = 14;
2830 }
2831 CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len);
2832 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len);
2833
2834 int result = cxListIntersection(dst, src, other, test_clone_func, &talloc.base, NULL);
2835 if (alloc_fail) {
2836 CX_TEST_ASSERT(result != 0);
2837 } else {
2838 CX_TEST_ASSERT(result == 0);
2839 }
2840 test_clone_func_max_enabled = false;
2841 CX_TEST_ASSERT(expected_len == cxListSize(dst));
2842 CX_TEST_ASSERT(0 == cxListCompare(dst, expected));
2843
2844 cxListFree(dst);
2845 cxListFree(src);
2846 cxListFree(other);
2847 cxListFree(expected);
2848 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
2849 }
2850
2851 CX_TEST(test_list_intersection_unsorted) {
2852 CX_TEST_DO {
2853 CX_TEST_CALL_SUBROUTINE(verify_intersection, false, false);
2854 }
2855 }
2856
2857 CX_TEST(test_list_intersection_sorted) {
2858 CX_TEST_DO {
2859 CX_TEST_CALL_SUBROUTINE(verify_intersection, true, false);
2860 }
2861 }
2862
2863 CX_TEST(test_list_intersection_unsorted_alloc_fail) {
2864 CX_TEST_DO {
2865 CX_TEST_CALL_SUBROUTINE(verify_intersection, false, true);
2866 }
2867 }
2868
2869 CX_TEST(test_list_intersection_sorted_alloc_fail) {
2870 CX_TEST_DO {
2871 CX_TEST_CALL_SUBROUTINE(verify_intersection, true, true);
2780 } 2872 }
2781 } 2873 }
2782 2874
2783 CX_TEST(test_list_pointer_list_supports_null) { 2875 CX_TEST(test_list_pointer_list_supports_null) {
2784 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2876 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
3237 // we do not perform the following tests with every combination of list types 3329 // we do not perform the following tests with every combination of list types
3238 cx_test_register(suite, test_list_difference_unsorted); 3330 cx_test_register(suite, test_list_difference_unsorted);
3239 cx_test_register(suite, test_list_difference_sorted); 3331 cx_test_register(suite, test_list_difference_sorted);
3240 cx_test_register(suite, test_list_difference_unsorted_alloc_fail); 3332 cx_test_register(suite, test_list_difference_unsorted_alloc_fail);
3241 cx_test_register(suite, test_list_difference_sorted_alloc_fail); 3333 cx_test_register(suite, test_list_difference_sorted_alloc_fail);
3334 cx_test_register(suite, test_list_intersection_unsorted);
3335 cx_test_register(suite, test_list_intersection_sorted);
3336 cx_test_register(suite, test_list_intersection_unsorted_alloc_fail);
3337 cx_test_register(suite, test_list_intersection_sorted_alloc_fail);
3242 3338
3243 return suite; 3339 return suite;
3244 } 3340 }
3245 3341
3246 CxTestSuite *cx_test_suite_list_corner_cases(void) { 3342 CxTestSuite *cx_test_suite_list_corner_cases(void) {

mercurial