| 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 unsigned test_clone_func_max_clones; |
| 2529 static void *test_clone_func(void *dest, const void *src, const CxAllocator *al, void *data) { |
2530 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 test_clone_func_max_clones--; |
| |
2533 |
| 2530 if (dest == NULL) { |
2534 if (dest == NULL) { |
| 2531 dest = cxMalloc(al, sizeof(int)); |
2535 dest = cxMalloc(al, sizeof(int)); |
| 2532 } |
2536 } |
| 2533 |
2537 |
| 2534 *((int*) dest) = *(int*) src + *(int*) data; |
2538 *((int*) dest) = *(int*) src + *(int*) data; |
| 2535 (*(int*) data)++; |
2539 (*(int*) data)++; |
| 2536 |
2540 |
| 2537 // TODO: create a separate test with another clone func which simulates a failure after a few allocations |
|
| 2538 return dest; |
2541 return dest; |
| 2539 } |
2542 } |
| 2540 |
2543 |
| 2541 static CX_TEST_SUBROUTINE(verify_clone, CxList *target, CxList *source, bool target_isptrlist) { |
2544 static CX_TEST_SUBROUTINE(verify_clone, CxList *target, CxList *source, bool target_isptrlist) { |
| 2542 // testing allocator for the target elements if target_isptrlist is true |
2545 // testing allocator for the target elements if target_isptrlist is true |
| 2568 } |
2571 } |
| 2569 |
2572 |
| 2570 // perform the test |
2573 // perform the test |
| 2571 int expected_data[12] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9, 11, 13, 15); |
2574 int expected_data[12] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9, 11, 13, 15); |
| 2572 int c = 0; |
2575 int c = 0; |
| |
2576 test_clone_func_max_clones = 100; // no limit |
| 2573 size_t cloned = cxListClone(target, source, test_clone_func, testing_alloc, &c); |
2577 size_t cloned = cxListClone(target, source, test_clone_func, testing_alloc, &c); |
| 2574 CX_TEST_ASSERT(cloned == 8); |
2578 CX_TEST_ASSERT(cloned == 8); |
| 2575 CX_TEST_ASSERT(c == 8); |
2579 CX_TEST_ASSERT(c == 8); |
| 2576 CX_TEST_ASSERT(cxListSize(target) == 12); |
2580 CX_TEST_ASSERT(cxListSize(target) == 12); |
| 2577 CX_TEST_ASSERT(cxListSize(source) == 8); |
2581 CX_TEST_ASSERT(cxListSize(source) == 8); |
| 2584 cxListFree(target); |
2588 cxListFree(target); |
| 2585 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
2589 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 2586 cx_testing_allocator_destroy(&talloc); |
2590 cx_testing_allocator_destroy(&talloc); |
| 2587 } |
2591 } |
| 2588 |
2592 |
| |
2593 static CX_TEST_SUBROUTINE(verify_clone_alloc_fail, CxList *target, CxList *source, bool target_isptrlist) { |
| |
2594 // testing allocator for the target elements if target_isptrlist is true |
| |
2595 CxTestingAllocator talloc; |
| |
2596 cx_testing_allocator_init(&talloc); |
| |
2597 CxAllocator *testing_alloc = &talloc.base; |
| |
2598 |
| |
2599 // register a destructor for the target list if it is storing pointers |
| |
2600 if (target_isptrlist) { |
| |
2601 cxDefineAdvancedDestructor(target, cxFree, testing_alloc); |
| |
2602 } |
| |
2603 |
| |
2604 // fill the source list |
| |
2605 int source_data[8] = array_init(1, 2, 3, 4, 5, 6, 7, 8); |
| |
2606 for (unsigned i = 0 ; i < 8 ; i++) { |
| |
2607 cxListAdd(source, &source_data[i]); |
| |
2608 } |
| |
2609 |
| |
2610 // add some initial data to the target |
| |
2611 int initial_data[4] = array_init(1, 2, 3, 4); |
| |
2612 for (unsigned i = 0; i < 4; i++) { |
| |
2613 if (target_isptrlist) { |
| |
2614 int *x = cxMalloc(testing_alloc, sizeof(int)); |
| |
2615 *x = initial_data[i]; |
| |
2616 cxListAdd(target, x); |
| |
2617 } else { |
| |
2618 cxListAdd(target, &initial_data[i]); |
| |
2619 } |
| |
2620 } |
| |
2621 |
| |
2622 // perform the test |
| |
2623 int expected_data[9] = array_init(1, 2, 3, 4, 1, 3, 5, 7, 9); |
| |
2624 int c = 0; |
| |
2625 test_clone_func_max_clones = 5; |
| |
2626 size_t cloned = cxListClone(target, source, test_clone_func, testing_alloc, &c); |
| |
2627 CX_TEST_ASSERT(cloned == 5); |
| |
2628 CX_TEST_ASSERT(c == 5); |
| |
2629 CX_TEST_ASSERT(cxListSize(target) == 9); |
| |
2630 CX_TEST_ASSERT(cxListSize(source) == 8); |
| |
2631 for (unsigned i = 0 ; i < 9 ; i++) { |
| |
2632 CX_TEST_ASSERT(*(int*)cxListAt(target, i) == expected_data[i]); |
| |
2633 } |
| |
2634 for (unsigned i = 0 ; i < 8 ; i++) { |
| |
2635 CX_TEST_ASSERT(*(int*)cxListAt(source, i) == source_data[i]); |
| |
2636 } |
| |
2637 cxListFree(target); |
| |
2638 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| |
2639 cx_testing_allocator_destroy(&talloc); |
| |
2640 } |
| |
2641 |
| 2589 roll_out_test_combos(clone_into_arl, { |
2642 roll_out_test_combos(clone_into_arl, { |
| 2590 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); |
2643 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); |
| 2591 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, false); |
2644 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, false); |
| 2592 }) |
2645 }) |
| 2593 |
2646 |
| 2602 }) |
2655 }) |
| 2603 |
2656 |
| 2604 roll_out_test_combos(clone_into_pll, { |
2657 roll_out_test_combos(clone_into_pll, { |
| 2605 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); |
2658 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); |
| 2606 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, true); |
2659 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, true); |
| |
2660 }) |
| |
2661 |
| |
2662 roll_out_test_combos(clone_alloc_fail_into_arl, { |
| |
2663 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); |
| |
2664 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, false); |
| |
2665 }) |
| |
2666 |
| |
2667 roll_out_test_combos(clone_alloc_fail_into_ll, { |
| |
2668 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); |
| |
2669 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, false); |
| |
2670 }) |
| |
2671 |
| |
2672 roll_out_test_combos(clone_alloc_fail_into_parl, { |
| |
2673 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 8); |
| |
2674 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true); |
| |
2675 }) |
| |
2676 |
| |
2677 roll_out_test_combos(clone_alloc_fail_into_pll, { |
| |
2678 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); |
| |
2679 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true); |
| 2607 }) |
2680 }) |
| 2608 |
2681 |
| 2609 CX_TEST(test_list_pointer_list_supports_null) { |
2682 CX_TEST(test_list_pointer_list_supports_null) { |
| 2610 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); |
2683 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); |
| 2611 int x = 47; |
2684 int x = 47; |
| 2784 cx_test_register(suite, test_list_parl_clone_into_ll); |
2857 cx_test_register(suite, test_list_parl_clone_into_ll); |
| 2785 cx_test_register(suite, test_list_arl_clone_into_parl); |
2858 cx_test_register(suite, test_list_arl_clone_into_parl); |
| 2786 cx_test_register(suite, test_list_parl_clone_into_parl); |
2859 cx_test_register(suite, test_list_parl_clone_into_parl); |
| 2787 cx_test_register(suite, test_list_arl_clone_into_pll); |
2860 cx_test_register(suite, test_list_arl_clone_into_pll); |
| 2788 cx_test_register(suite, test_list_parl_clone_into_pll); |
2861 cx_test_register(suite, test_list_parl_clone_into_pll); |
| |
2862 cx_test_register(suite, test_list_arl_clone_alloc_fail_into_arl); |
| |
2863 cx_test_register(suite, test_list_parl_clone_alloc_fail_into_arl); |
| |
2864 cx_test_register(suite, test_list_arl_clone_alloc_fail_into_ll); |
| |
2865 cx_test_register(suite, test_list_parl_clone_alloc_fail_into_ll); |
| |
2866 cx_test_register(suite, test_list_arl_clone_alloc_fail_into_parl); |
| |
2867 cx_test_register(suite, test_list_parl_clone_alloc_fail_into_parl); |
| |
2868 cx_test_register(suite, test_list_arl_clone_alloc_fail_into_pll); |
| |
2869 cx_test_register(suite, test_list_parl_clone_alloc_fail_into_pll); |
| 2789 |
2870 |
| 2790 return suite; |
2871 return suite; |
| 2791 } |
2872 } |
| 2792 |
2873 |
| 2793 CxTestSuite *cx_test_suite_array_list_defaulted_funcs(void) { |
2874 CxTestSuite *cx_test_suite_array_list_defaulted_funcs(void) { |
| 2911 cx_test_register(suite, test_list_pll_clone_into_ll); |
2992 cx_test_register(suite, test_list_pll_clone_into_ll); |
| 2912 cx_test_register(suite, test_list_ll_clone_into_parl); |
2993 cx_test_register(suite, test_list_ll_clone_into_parl); |
| 2913 cx_test_register(suite, test_list_pll_clone_into_parl); |
2994 cx_test_register(suite, test_list_pll_clone_into_parl); |
| 2914 cx_test_register(suite, test_list_ll_clone_into_pll); |
2995 cx_test_register(suite, test_list_ll_clone_into_pll); |
| 2915 cx_test_register(suite, test_list_pll_clone_into_pll); |
2996 cx_test_register(suite, test_list_pll_clone_into_pll); |
| |
2997 cx_test_register(suite, test_list_ll_clone_alloc_fail_into_arl); |
| |
2998 cx_test_register(suite, test_list_pll_clone_alloc_fail_into_arl); |
| |
2999 cx_test_register(suite, test_list_ll_clone_alloc_fail_into_ll); |
| |
3000 cx_test_register(suite, test_list_pll_clone_alloc_fail_into_ll); |
| |
3001 cx_test_register(suite, test_list_ll_clone_alloc_fail_into_parl); |
| |
3002 cx_test_register(suite, test_list_pll_clone_alloc_fail_into_parl); |
| |
3003 cx_test_register(suite, test_list_ll_clone_alloc_fail_into_pll); |
| |
3004 cx_test_register(suite, test_list_pll_clone_alloc_fail_into_pll); |
| 2916 |
3005 |
| 2917 return suite; |
3006 return suite; |
| 2918 } |
3007 } |
| 2919 |
3008 |
| 2920 CxTestSuite *cx_test_suite_linked_list_defaulted_funcs(void) { |
3009 CxTestSuite *cx_test_suite_linked_list_defaulted_funcs(void) { |
| 3012 cx_test_register(suite, test_list_pkvl_clone_into_ll); |
3101 cx_test_register(suite, test_list_pkvl_clone_into_ll); |
| 3013 cx_test_register(suite, test_list_kvl_clone_into_parl); |
3102 cx_test_register(suite, test_list_kvl_clone_into_parl); |
| 3014 cx_test_register(suite, test_list_pkvl_clone_into_parl); |
3103 cx_test_register(suite, test_list_pkvl_clone_into_parl); |
| 3015 cx_test_register(suite, test_list_kvl_clone_into_pll); |
3104 cx_test_register(suite, test_list_kvl_clone_into_pll); |
| 3016 cx_test_register(suite, test_list_pkvl_clone_into_pll); |
3105 cx_test_register(suite, test_list_pkvl_clone_into_pll); |
| |
3106 cx_test_register(suite, test_list_kvl_clone_alloc_fail_into_arl); |
| |
3107 cx_test_register(suite, test_list_pkvl_clone_alloc_fail_into_arl); |
| |
3108 cx_test_register(suite, test_list_kvl_clone_alloc_fail_into_ll); |
| |
3109 cx_test_register(suite, test_list_pkvl_clone_alloc_fail_into_ll); |
| |
3110 cx_test_register(suite, test_list_kvl_clone_alloc_fail_into_parl); |
| |
3111 cx_test_register(suite, test_list_pkvl_clone_alloc_fail_into_parl); |
| |
3112 cx_test_register(suite, test_list_kvl_clone_alloc_fail_into_pll); |
| |
3113 cx_test_register(suite, test_list_pkvl_clone_alloc_fail_into_pll); |
| 3017 |
3114 |
| 3018 return suite; |
3115 return suite; |
| 3019 } |
3116 } |
| 3020 |
3117 |
| 3021 CxTestSuite *cx_test_suite_empty_list(void) { |
3118 CxTestSuite *cx_test_suite_empty_list(void) { |