diff -r cdce47f34d48 -r ee84ac776cbc tests/test_list.c --- a/tests/test_list.c Thu Aug 29 20:48:15 2024 +0200 +++ b/tests/test_list.c Thu Aug 29 21:30:52 2024 +0200 @@ -928,9 +928,7 @@ CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\ } \ cx_testing_allocator_destroy(&talloc); -#define roll_out_test_combos(name, body) \ -static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ - __attribute__((__unused__)) bool isptrlist) body \ +#define roll_out_test_invokers(name) \ CX_TEST(test_list_ll_##name) { \ set_up_combo \ CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ @@ -955,6 +953,61 @@ CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ tear_down_combo \ } +#define roll_out_test_combos(name, body) \ +static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ + __attribute__((__unused__)) bool isptrlist) body \ +roll_out_test_invokers(name) + +static void set_default_class_funcs(CxList *list, cx_list_class *defaulted_cl) { + cx_list_class const *cl = list->climpl == NULL ? list->cl : list->climpl; + memcpy(defaulted_cl, cl, sizeof(cx_list_class)); + defaulted_cl->insert_array = cx_list_default_insert_array; + defaulted_cl->sort = cx_list_default_sort; + defaulted_cl->swap = cx_list_default_swap; + defaulted_cl->compare = NULL; + if (list->climpl == NULL) { + list->cl = defaulted_cl; + } else { + list->climpl = defaulted_cl; + } +} + +#define do_set_default_class_funcs(list) \ + cx_list_class defaulted_cl; \ + set_default_class_funcs(list, &defaulted_cl) +#define roll_out_test_combos_with_defaulted_funcs(name, body) \ +static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ + __attribute__((__unused__)) bool isptrlist) body \ +roll_out_test_invokers(name) \ +CX_TEST(test_list_llm_##name) { \ + set_up_combo \ + CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ + do_set_default_class_funcs(list); \ + CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ + tear_down_combo \ +} \ +CX_TEST(test_list_arlm_##name) { \ + set_up_combo \ + CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \ + do_set_default_class_funcs(list); \ + CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ + tear_down_combo \ +} \ +CX_TEST(test_list_pllm_##name) { \ + set_up_combo \ + CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ + do_set_default_class_funcs(list); \ + CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ + tear_down_combo \ +} \ +CX_TEST(test_list_parlm_##name) { \ + set_up_combo \ + CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ + do_set_default_class_funcs(list); \ + CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ + tear_down_combo \ +} + #define array_init(...) {__VA_ARGS__} static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) { @@ -1005,7 +1058,7 @@ CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 42); }) -roll_out_test_combos(insert_array, { +roll_out_test_combos_with_defaulted_funcs(insert_array, { int a[5] = array_init(5, 47, 11, 13, 42); int b[5] = array_init(9, 18, 72, 50, 7); int *aptr[5]; @@ -1111,7 +1164,7 @@ free(testdata); }) -roll_out_test_combos(swap, { +roll_out_test_combos_with_defaulted_funcs(swap, { int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13); @@ -1178,7 +1231,7 @@ free(testdata); }) -roll_out_test_combos(sort, { +roll_out_test_combos_with_defaulted_funcs(sort, { const size_t testdata_len = 250; int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); int *expected = malloc(testdata_len*sizeof(int)); @@ -1262,28 +1315,34 @@ CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 2); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); + CX_TEST_ASSERT(iter.elem_count == 5); cxListInsertAfter(&iter, &newdata[0]); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 2); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); + CX_TEST_ASSERT(iter.elem_count == 6); cxListInsertBefore(&iter, &newdata[1]); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 3); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); + CX_TEST_ASSERT(iter.elem_count == 7); iter = cxListMutIterator(list); cxListInsertBefore(&iter, &newdata[2]); CX_TEST_ASSERT(cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 1); CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 0); + CX_TEST_ASSERT(iter.elem_count == 8); iter = cxListMutIteratorAt(list, cxListSize(list)); cxListInsertBefore(&iter, &newdata[3]); CX_TEST_ASSERT(!cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 9); + CX_TEST_ASSERT(iter.elem_count == 9); iter = cxListMutIteratorAt(list, cxListSize(list)); cxListInsertAfter(&iter, &newdata[4]); CX_TEST_ASSERT(!cxIteratorValid(iter)); CX_TEST_ASSERT(iter.index == 10); + CX_TEST_ASSERT(iter.elem_count == 10); int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50); cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]); @@ -1336,6 +1395,20 @@ parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50) ) +roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, { + \ + const size_t len = 33; \ + int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ + CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); \ + do_set_default_class_funcs(other); \ + cx_for_n(i, len) + cxListAdd(other, &testdata[i]); \ + CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ + cxListDestroy(other); \ + free(testdata); \ + +}) + static unsigned destr_test_ctr; static int destr_last_value; @@ -1463,6 +1536,25 @@ return suite; } +CxTestSuite *cx_test_suite_array_list_defaulted_funcs(void) { + CxTestSuite *suite = cx_test_suite_new( + "array_list with defaulted functions"); + + cx_test_register(suite, test_list_arlm_insert_array); + cx_test_register(suite, test_list_parlm_insert_array); + cx_test_register(suite, test_list_arlm_swap); + cx_test_register(suite, test_list_parlm_swap); + cx_test_register(suite, test_list_arlm_sort); + cx_test_register(suite, test_list_parlm_sort); + + cx_test_register(suite, test_list_arl_compare_unoptimized); + cx_test_register(suite, test_list_parl_compare_unoptimized); + cx_test_register(suite, test_list_arlm_compare_unoptimized); + cx_test_register(suite, test_list_parlm_compare_unoptimized); + + return suite; +} + CxTestSuite *cx_test_suite_linked_list(void) { CxTestSuite *suite = cx_test_suite_new("linked_list"); @@ -1534,6 +1626,25 @@ return suite; } +CxTestSuite *cx_test_suite_linked_list_defaulted_funcs(void) { + CxTestSuite *suite = cx_test_suite_new( + "linked_list with defaulted functions"); + + cx_test_register(suite, test_list_llm_insert_array); + cx_test_register(suite, test_list_pllm_insert_array); + cx_test_register(suite, test_list_llm_swap); + cx_test_register(suite, test_list_pllm_swap); + cx_test_register(suite, test_list_llm_sort); + cx_test_register(suite, test_list_pllm_sort); + + cx_test_register(suite, test_list_ll_compare_unoptimized); + cx_test_register(suite, test_list_pll_compare_unoptimized); + cx_test_register(suite, test_list_llm_compare_unoptimized); + cx_test_register(suite, test_list_pllm_compare_unoptimized); + + return suite; +} + CxTestSuite *cx_test_suite_empty_list(void) { CxTestSuite *suite = cx_test_suite_new("empty list dummy");