tests/test_list.c

changeset 875
ee84ac776cbc
parent 857
4d12e34bb130
child 876
f4ce7df9cff0
equal deleted inserted replaced
874:cdce47f34d48 875:ee84ac776cbc
926 #define tear_down_combo \ 926 #define tear_down_combo \
927 cxListDestroy(list); \ 927 cxListDestroy(list); \
928 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\ 928 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));\
929 } \ 929 } \
930 cx_testing_allocator_destroy(&talloc); 930 cx_testing_allocator_destroy(&talloc);
931 #define roll_out_test_combos(name, body) \ 931 #define roll_out_test_invokers(name) \
932 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
933 __attribute__((__unused__)) bool isptrlist) body \
934 CX_TEST(test_list_ll_##name) { \ 932 CX_TEST(test_list_ll_##name) { \
935 set_up_combo \ 933 set_up_combo \
936 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ 934 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \
937 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ 935 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
938 tear_down_combo \ 936 tear_down_combo \
953 set_up_combo \ 951 set_up_combo \
954 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ 952 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \
955 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ 953 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
956 tear_down_combo \ 954 tear_down_combo \
957 } 955 }
956 #define roll_out_test_combos(name, body) \
957 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
958 __attribute__((__unused__)) bool isptrlist) body \
959 roll_out_test_invokers(name)
960
961 static void set_default_class_funcs(CxList *list, cx_list_class *defaulted_cl) {
962 cx_list_class const *cl = list->climpl == NULL ? list->cl : list->climpl;
963 memcpy(defaulted_cl, cl, sizeof(cx_list_class));
964 defaulted_cl->insert_array = cx_list_default_insert_array;
965 defaulted_cl->sort = cx_list_default_sort;
966 defaulted_cl->swap = cx_list_default_swap;
967 defaulted_cl->compare = NULL;
968 if (list->climpl == NULL) {
969 list->cl = defaulted_cl;
970 } else {
971 list->climpl = defaulted_cl;
972 }
973 }
974
975 #define do_set_default_class_funcs(list) \
976 cx_list_class defaulted_cl; \
977 set_default_class_funcs(list, &defaulted_cl)
978 #define roll_out_test_combos_with_defaulted_funcs(name, body) \
979 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
980 __attribute__((__unused__)) bool isptrlist) body \
981 roll_out_test_invokers(name) \
982 CX_TEST(test_list_llm_##name) { \
983 set_up_combo \
984 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \
985 do_set_default_class_funcs(list); \
986 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
987 tear_down_combo \
988 } \
989 CX_TEST(test_list_arlm_##name) { \
990 set_up_combo \
991 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \
992 do_set_default_class_funcs(list); \
993 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
994 tear_down_combo \
995 } \
996 CX_TEST(test_list_pllm_##name) { \
997 set_up_combo \
998 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \
999 do_set_default_class_funcs(list); \
1000 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1001 tear_down_combo \
1002 } \
1003 CX_TEST(test_list_parlm_##name) { \
1004 set_up_combo \
1005 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \
1006 do_set_default_class_funcs(list); \
1007 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1008 tear_down_combo \
1009 }
1010
958 #define array_init(...) {__VA_ARGS__} 1011 #define array_init(...) {__VA_ARGS__}
959 1012
960 static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) { 1013 static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) {
961 int *testdata = int_test_data(len); 1014 int *testdata = int_test_data(len);
962 if (isptrlist) { 1015 if (isptrlist) {
1003 CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 13); 1056 CX_TEST_ASSERT(*(int *) cxListAt(list, 1) == 13);
1004 CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 5); 1057 CX_TEST_ASSERT(*(int *) cxListAt(list, 2) == 5);
1005 CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 42); 1058 CX_TEST_ASSERT(*(int *) cxListAt(list, 3) == 42);
1006 }) 1059 })
1007 1060
1008 roll_out_test_combos(insert_array, { 1061 roll_out_test_combos_with_defaulted_funcs(insert_array, {
1009 int a[5] = array_init(5, 47, 11, 13, 42); 1062 int a[5] = array_init(5, 47, 11, 13, 42);
1010 int b[5] = array_init(9, 18, 72, 50, 7); 1063 int b[5] = array_init(9, 18, 72, 50, 7);
1011 int *aptr[5]; 1064 int *aptr[5];
1012 int *bptr[5]; 1065 int *bptr[5];
1013 cx_for_n(i, 5) { 1066 cx_for_n(i, 5) {
1109 } 1162 }
1110 CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL); 1163 CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL);
1111 free(testdata); 1164 free(testdata);
1112 }) 1165 })
1113 1166
1114 roll_out_test_combos(swap, { 1167 roll_out_test_combos_with_defaulted_funcs(swap, {
1115 int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 1168 int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1116 int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13); 1169 int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13);
1117 1170
1118 cx_for_n(i, 16) { 1171 cx_for_n(i, 16) {
1119 cxListAdd(list, &original[i]); 1172 cxListAdd(list, &original[i]);
1176 CX_TEST_ASSERT(cxListFind(list, &notinlist) < 0); 1229 CX_TEST_ASSERT(cxListFind(list, &notinlist) < 0);
1177 1230
1178 free(testdata); 1231 free(testdata);
1179 }) 1232 })
1180 1233
1181 roll_out_test_combos(sort, { 1234 roll_out_test_combos_with_defaulted_funcs(sort, {
1182 const size_t testdata_len = 250; 1235 const size_t testdata_len = 250;
1183 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 1236 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1184 int *expected = malloc(testdata_len*sizeof(int)); 1237 int *expected = malloc(testdata_len*sizeof(int));
1185 memcpy(expected, testdata, testdata_len*sizeof(int)); 1238 memcpy(expected, testdata, testdata_len*sizeof(int));
1186 qsort(expected, testdata_len, sizeof(int), cx_cmp_int); 1239 qsort(expected, testdata_len, sizeof(int), cx_cmp_int);
1260 1313
1261 CxIterator iter = cxListMutIteratorAt(list, 2); 1314 CxIterator iter = cxListMutIteratorAt(list, 2);
1262 CX_TEST_ASSERT(cxIteratorValid(iter)); 1315 CX_TEST_ASSERT(cxIteratorValid(iter));
1263 CX_TEST_ASSERT(iter.index == 2); 1316 CX_TEST_ASSERT(iter.index == 2);
1264 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); 1317 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2);
1318 CX_TEST_ASSERT(iter.elem_count == 5);
1265 cxListInsertAfter(&iter, &newdata[0]); 1319 cxListInsertAfter(&iter, &newdata[0]);
1266 CX_TEST_ASSERT(cxIteratorValid(iter)); 1320 CX_TEST_ASSERT(cxIteratorValid(iter));
1267 CX_TEST_ASSERT(iter.index == 2); 1321 CX_TEST_ASSERT(iter.index == 2);
1268 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); 1322 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2);
1323 CX_TEST_ASSERT(iter.elem_count == 6);
1269 cxListInsertBefore(&iter, &newdata[1]); 1324 cxListInsertBefore(&iter, &newdata[1]);
1270 CX_TEST_ASSERT(cxIteratorValid(iter)); 1325 CX_TEST_ASSERT(cxIteratorValid(iter));
1271 CX_TEST_ASSERT(iter.index == 3); 1326 CX_TEST_ASSERT(iter.index == 3);
1272 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2); 1327 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 2);
1328 CX_TEST_ASSERT(iter.elem_count == 7);
1273 1329
1274 iter = cxListMutIterator(list); 1330 iter = cxListMutIterator(list);
1275 cxListInsertBefore(&iter, &newdata[2]); 1331 cxListInsertBefore(&iter, &newdata[2]);
1276 CX_TEST_ASSERT(cxIteratorValid(iter)); 1332 CX_TEST_ASSERT(cxIteratorValid(iter));
1277 CX_TEST_ASSERT(iter.index == 1); 1333 CX_TEST_ASSERT(iter.index == 1);
1278 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 0); 1334 CX_TEST_ASSERT(*(int *) cxIteratorCurrent(iter) == 0);
1335 CX_TEST_ASSERT(iter.elem_count == 8);
1279 iter = cxListMutIteratorAt(list, cxListSize(list)); 1336 iter = cxListMutIteratorAt(list, cxListSize(list));
1280 cxListInsertBefore(&iter, &newdata[3]); 1337 cxListInsertBefore(&iter, &newdata[3]);
1281 CX_TEST_ASSERT(!cxIteratorValid(iter)); 1338 CX_TEST_ASSERT(!cxIteratorValid(iter));
1282 CX_TEST_ASSERT(iter.index == 9); 1339 CX_TEST_ASSERT(iter.index == 9);
1340 CX_TEST_ASSERT(iter.elem_count == 9);
1283 iter = cxListMutIteratorAt(list, cxListSize(list)); 1341 iter = cxListMutIteratorAt(list, cxListSize(list));
1284 cxListInsertAfter(&iter, &newdata[4]); 1342 cxListInsertAfter(&iter, &newdata[4]);
1285 CX_TEST_ASSERT(!cxIteratorValid(iter)); 1343 CX_TEST_ASSERT(!cxIteratorValid(iter));
1286 CX_TEST_ASSERT(iter.index == 10); 1344 CX_TEST_ASSERT(iter.index == 10);
1345 CX_TEST_ASSERT(iter.elem_count == 10);
1287 1346
1288 int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50); 1347 int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50);
1289 cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]); 1348 cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]);
1290 }) 1349 })
1291 1350
1333 ) 1392 )
1334 1393
1335 roll_out_compare_tests( 1394 roll_out_compare_tests(
1336 parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50) 1395 parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50)
1337 ) 1396 )
1397
1398 roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, {
1399 \
1400 const size_t len = 33; \
1401 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
1402 CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); \
1403 do_set_default_class_funcs(other); \
1404 cx_for_n(i, len)
1405 cxListAdd(other, &testdata[i]); \
1406 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
1407 cxListDestroy(other); \
1408 free(testdata); \
1409
1410 })
1338 1411
1339 static unsigned destr_test_ctr; 1412 static unsigned destr_test_ctr;
1340 static int destr_last_value; 1413 static int destr_last_value;
1341 1414
1342 static void simple_destr_test_fun(void *data) { 1415 static void simple_destr_test_fun(void *data) {
1461 cx_test_register(suite, test_list_parl_advanced_destr); 1534 cx_test_register(suite, test_list_parl_advanced_destr);
1462 1535
1463 return suite; 1536 return suite;
1464 } 1537 }
1465 1538
1539 CxTestSuite *cx_test_suite_array_list_defaulted_funcs(void) {
1540 CxTestSuite *suite = cx_test_suite_new(
1541 "array_list with defaulted functions");
1542
1543 cx_test_register(suite, test_list_arlm_insert_array);
1544 cx_test_register(suite, test_list_parlm_insert_array);
1545 cx_test_register(suite, test_list_arlm_swap);
1546 cx_test_register(suite, test_list_parlm_swap);
1547 cx_test_register(suite, test_list_arlm_sort);
1548 cx_test_register(suite, test_list_parlm_sort);
1549
1550 cx_test_register(suite, test_list_arl_compare_unoptimized);
1551 cx_test_register(suite, test_list_parl_compare_unoptimized);
1552 cx_test_register(suite, test_list_arlm_compare_unoptimized);
1553 cx_test_register(suite, test_list_parlm_compare_unoptimized);
1554
1555 return suite;
1556 }
1557
1466 CxTestSuite *cx_test_suite_linked_list(void) { 1558 CxTestSuite *cx_test_suite_linked_list(void) {
1467 CxTestSuite *suite = cx_test_suite_new("linked_list"); 1559 CxTestSuite *suite = cx_test_suite_new("linked_list");
1468 1560
1469 cx_test_register(suite, test_linked_list_link_unlink); 1561 cx_test_register(suite, test_linked_list_link_unlink);
1470 cx_test_register(suite, test_linked_list_at); 1562 cx_test_register(suite, test_linked_list_at);
1532 cx_test_register(suite, test_list_pll_advanced_destr); 1624 cx_test_register(suite, test_list_pll_advanced_destr);
1533 1625
1534 return suite; 1626 return suite;
1535 } 1627 }
1536 1628
1629 CxTestSuite *cx_test_suite_linked_list_defaulted_funcs(void) {
1630 CxTestSuite *suite = cx_test_suite_new(
1631 "linked_list with defaulted functions");
1632
1633 cx_test_register(suite, test_list_llm_insert_array);
1634 cx_test_register(suite, test_list_pllm_insert_array);
1635 cx_test_register(suite, test_list_llm_swap);
1636 cx_test_register(suite, test_list_pllm_swap);
1637 cx_test_register(suite, test_list_llm_sort);
1638 cx_test_register(suite, test_list_pllm_sort);
1639
1640 cx_test_register(suite, test_list_ll_compare_unoptimized);
1641 cx_test_register(suite, test_list_pll_compare_unoptimized);
1642 cx_test_register(suite, test_list_llm_compare_unoptimized);
1643 cx_test_register(suite, test_list_pllm_compare_unoptimized);
1644
1645 return suite;
1646 }
1647
1537 CxTestSuite *cx_test_suite_empty_list(void) { 1648 CxTestSuite *cx_test_suite_empty_list(void) {
1538 CxTestSuite *suite = cx_test_suite_new("empty list dummy"); 1649 CxTestSuite *suite = cx_test_suite_new("empty list dummy");
1539 1650
1540 cx_test_register(suite, test_empty_list_size); 1651 cx_test_register(suite, test_empty_list_size);
1541 cx_test_register(suite, test_empty_list_iterator); 1652 cx_test_register(suite, test_empty_list_iterator);

mercurial