tests/test_list.c

changeset 1462
9263d3f15379
parent 1444
dd9dcbb39c2f
equal deleted inserted replaced
1461:dfd79d615049 1462:9263d3f15379
2524 cxDefineAdvancedDestructor(list, advanced_destr_test_fun, NULL); 2524 cxDefineAdvancedDestructor(list, advanced_destr_test_fun, NULL);
2525 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); 2525 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len);
2526 free(testdata); 2526 free(testdata);
2527 }) 2527 })
2528 2528
2529 static bool test_clone_func_max_enabled = false;
2529 static unsigned test_clone_func_max_clones; 2530 static unsigned test_clone_func_max_clones;
2530 static void *test_clone_func(void *dest, const void *src, const CxAllocator *al, void *data) { 2531 static void *test_clone_func(void *dest, const void *src, const CxAllocator *al, void *data) {
2531 if (test_clone_func_max_clones == 0) return NULL; 2532 if (test_clone_func_max_enabled) {
2532 test_clone_func_max_clones--; 2533 if (test_clone_func_max_clones == 0) return NULL;
2534 test_clone_func_max_clones--;
2535 }
2533 2536
2534 if (dest == NULL) { 2537 if (dest == NULL) {
2535 dest = cxMalloc(al, sizeof(int)); 2538 dest = cxMalloc(al, sizeof(int));
2539 }
2540
2541 int z = 0;
2542 if (data == NULL) {
2543 data = &z;
2536 } 2544 }
2537 2545
2538 *((int*) dest) = *(int*) src + *(int*) data; 2546 *((int*) dest) = *(int*) src + *(int*) data;
2539 (*(int*) data)++; 2547 (*(int*) data)++;
2540 2548
2571 } 2579 }
2572 2580
2573 // perform the test 2581 // perform the test
2574 int expected_data[12] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9, 11, 13, 15); 2582 int expected_data[12] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9, 11, 13, 15);
2575 int c = 0; 2583 int c = 0;
2576 test_clone_func_max_clones = 100; // no limit
2577 CX_TEST_ASSERT(0 == cxListClone(target, source, test_clone_func, testing_alloc, &c)); 2584 CX_TEST_ASSERT(0 == cxListClone(target, source, test_clone_func, testing_alloc, &c));
2578 CX_TEST_ASSERT(c == 8); 2585 CX_TEST_ASSERT(c == 8);
2579 CX_TEST_ASSERT(cxListSize(target) == 12); 2586 CX_TEST_ASSERT(cxListSize(target) == 12);
2580 CX_TEST_ASSERT(cxListSize(source) == 8); 2587 CX_TEST_ASSERT(cxListSize(source) == 8);
2581 for (unsigned i = 0 ; i < 12 ; i++) { 2588 for (unsigned i = 0 ; i < 12 ; i++) {
2619 } 2626 }
2620 2627
2621 // perform the test 2628 // perform the test
2622 int expected_data[9] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9); 2629 int expected_data[9] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9);
2623 int c = 0; 2630 int c = 0;
2631 test_clone_func_max_enabled = true;
2624 test_clone_func_max_clones = 5; 2632 test_clone_func_max_clones = 5;
2625 CX_TEST_ASSERT(0 != cxListClone(target, source, test_clone_func, testing_alloc, &c)); 2633 CX_TEST_ASSERT(0 != cxListClone(target, source, test_clone_func, testing_alloc, &c));
2634 test_clone_func_max_enabled = false;
2626 CX_TEST_ASSERT(c == 5); 2635 CX_TEST_ASSERT(c == 5);
2627 CX_TEST_ASSERT(cxListSize(target) == 9); 2636 CX_TEST_ASSERT(cxListSize(target) == 9);
2628 CX_TEST_ASSERT(cxListSize(source) == 8); 2637 CX_TEST_ASSERT(cxListSize(source) == 8);
2629 for (unsigned i = 0 ; i < 9 ; i++) { 2638 for (unsigned i = 0 ; i < 9 ; i++) {
2630 CX_TEST_ASSERT(*(int*)cxListAt(target, i) == expected_data[i]); 2639 CX_TEST_ASSERT(*(int*)cxListAt(target, i) == expected_data[i]);
2674 2683
2675 roll_out_test_combos(clone_alloc_fail_into_pll, { 2684 roll_out_test_combos(clone_alloc_fail_into_pll, {
2676 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2685 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2677 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true); 2686 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true);
2678 }) 2687 })
2688
2689 static CX_TEST_SUBROUTINE(verify_difference, bool sorted, bool alloc_fail) {
2690 CxTestingAllocator talloc;
2691 cx_testing_allocator_init(&talloc);
2692
2693 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2694 cxDefineAdvancedDestructor(dst, cxFree, &talloc);
2695 CxList *minuend = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int));
2696 CxList *subtrahend = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int));
2697
2698 int dst_data[] = {
2699 47, 178, 176, 83, 109, 149, 192, 38, 101, 116, 121, 73, 94, 197, 91, 79, 158, 86, 190, 138, 100, 39, 30, 144,
2700 35, 182, 155, 84, 139, 186, 67, 123, 62, 37, 3, 64, 172, 56, 160, 1, 156, 132, 131, 49, 10, 126, 171, 145, 77,
2701 107
2702 };
2703 int minuend_data[] = {
2704 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,
2705 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
2706 };
2707 int subtrahend_data[] = {
2708 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,
2709 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
2710 };
2711
2712 {
2713 CxList *tmp = cxArrayListCreateSimple(sizeof(int), 50);
2714 cxListAddArray(tmp, dst_data, 50);
2715 cxListClone(dst, tmp, test_clone_func, &talloc.base, NULL);
2716 cxListFree(tmp);
2717 }
2718 cxListAddArray(minuend, minuend_data, 50);
2719 cxListAddArray(subtrahend, subtrahend_data, 50);
2720 if (sorted) {
2721 cxListSort(dst);
2722 cxListSort(minuend);
2723 cxListSort(subtrahend);
2724 }
2725
2726 // expected 36 elements stay in dst, and 36 will be added from the difference
2727 size_t expected_len = 72;
2728 int expected_unsorted[] = {
2729 47, 83, 109, 149, 192, 116, 121, 94, 91, 158, 86, 190, 138, 39, 30, 35, 182, 155, 84, 186, 67, 123, 62, 37,
2730 3, 172, 56, 160, 156, 132, 131, 126, 171, 145, 77, 107,
2731 153, 106, 171, 74, 173, 150, 94, 92, 70, 175, 200, 20, 29, 88, 116, 124, 32, 9, 76, 33, 51, 37, 65, 12, 162,
2732 28, 4, 177, 54, 109, 44, 77, 194, 129, 97, 83
2733 };
2734 int expected_sorted[] = {
2735 3, 30, 35, 37, 39, 47, 56, 62, 67, 77, 83, 84, 86, 91, 94, 107, 109, 116, 121, 123, 126, 131, 132, 138, 145,
2736 149, 155, 156, 158, 160, 171, 172, 182, 186, 190, 192,
2737 4, 9, 12, 20, 28, 29, 32, 33, 37, 44, 51, 54, 65, 70, 74, 76, 77, 83, 88, 92, 94, 97, 106, 109, 116, 124, 129,
2738 150, 153, 162, 171, 173, 175, 177, 194, 200
2739 };
2740
2741 if (alloc_fail) {
2742 test_clone_func_max_enabled = true;
2743 test_clone_func_max_clones = 30;
2744 expected_len = 66;
2745 }
2746 CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len);
2747 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len);
2748
2749 int result = cxListDifference(dst, minuend, subtrahend, test_clone_func, &talloc.base, NULL);
2750 if (alloc_fail) {
2751 CX_TEST_ASSERT(result != 0);
2752 } else {
2753 CX_TEST_ASSERT(result == 0);
2754 }
2755 test_clone_func_max_enabled = false;
2756 CX_TEST_ASSERT(expected_len == cxListSize(dst));
2757 CX_TEST_ASSERT(0 == cxListCompare(dst, expected));
2758
2759 cxListFree(dst);
2760 cxListFree(minuend);
2761 cxListFree(subtrahend);
2762 cxListFree(expected);
2763 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
2764 }
2765
2766 CX_TEST(test_list_difference_unsorted) {
2767 CX_TEST_DO {
2768 CX_TEST_CALL_SUBROUTINE(verify_difference, false, false);
2769 }
2770 }
2771
2772 CX_TEST(test_list_difference_sorted) {
2773 CX_TEST_DO {
2774 CX_TEST_CALL_SUBROUTINE(verify_difference, true, false);
2775 }
2776 }
2777
2778 CX_TEST(test_list_difference_unsorted_alloc_fail) {
2779 CX_TEST_DO {
2780 CX_TEST_CALL_SUBROUTINE(verify_difference, false, true);
2781 }
2782 }
2783
2784 CX_TEST(test_list_difference_sorted_alloc_fail) {
2785 CX_TEST_DO {
2786 CX_TEST_CALL_SUBROUTINE(verify_difference, true, true);
2787 }
2788 }
2679 2789
2680 CX_TEST(test_list_pointer_list_supports_null) { 2790 CX_TEST(test_list_pointer_list_supports_null) {
2681 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2791 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
2682 int x = 47; 2792 int x = 47;
2683 int y = 11; 2793 int y = 11;
3126 cx_test_register(suite, test_null_list_free); 3236 cx_test_register(suite, test_null_list_free);
3127 3237
3128 return suite; 3238 return suite;
3129 } 3239 }
3130 3240
3241 CxTestSuite *cx_test_suite_list_set_ops(void) {
3242 CxTestSuite *suite = cx_test_suite_new("list collection operations");
3243
3244 // we do not perform the following tests with every combination of list types
3245 cx_test_register(suite, test_list_difference_unsorted);
3246 cx_test_register(suite, test_list_difference_sorted);
3247 cx_test_register(suite, test_list_difference_unsorted_alloc_fail);
3248 cx_test_register(suite, test_list_difference_sorted_alloc_fail);
3249
3250 return suite;
3251 }
3252
3131 CxTestSuite *cx_test_suite_list_corner_cases(void) { 3253 CxTestSuite *cx_test_suite_list_corner_cases(void) {
3132 CxTestSuite *suite = cx_test_suite_new("list corner cases"); 3254 CxTestSuite *suite = cx_test_suite_new("list corner cases");
3133 3255
3134 cx_test_register(suite, test_list_pointer_list_supports_null); 3256 cx_test_register(suite, test_list_pointer_list_supports_null);
3135 cx_test_register(suite, test_list_use_insert_unique_with_duplicates_in_source); 3257 cx_test_register(suite, test_list_use_insert_unique_with_duplicates_in_source);

mercurial