tests/test_list.c

changeset 1633
fe24b68758bf
parent 1632
f74e4fc496a2
child 1636
5ed91801f79d
equal deleted inserted replaced
1632:f74e4fc496a2 1633:fe24b68758bf
1462 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1462 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1463 } 1463 }
1464 cx_testing_allocator_destroy(&talloc); 1464 cx_testing_allocator_destroy(&talloc);
1465 } 1465 }
1466 1466
1467 CX_TEST(test_list_ll_create_simple) {
1468 CxList *list = cxLinkedListCreate(NULL, sizeof(int));
1469 CX_TEST_DO {
1470 CX_TEST_ASSERT(list != NULL);
1471 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int));
1472 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1473 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1474 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
1475 CX_TEST_ASSERT(cxListSize(list) == 0);
1476 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
1477 CX_TEST_ASSERT(list->collection.simple_cmp == NULL);
1478 CX_TEST_ASSERT(list->collection.advanced_cmp == cx_ccmp_memcmp);
1479 CX_TEST_ASSERT(list->collection.cmp_data == &list->collection.elem_size);
1480 CX_TEST_ASSERT(!list->collection.store_pointer);
1481 }
1482 cxListFree(list);
1483 }
1484
1485 CX_TEST(test_list_ll_create_simple_for_pointers) {
1486 CxList *list = cxLinkedListCreate(NULL, CX_STORE_POINTERS);
1487 CX_TEST_DO {
1488 CX_TEST_ASSERT(list != NULL);
1489 CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*));
1490 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1491 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1492 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
1493 CX_TEST_ASSERT(cxListSize(list) == 0);
1494 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
1495 CX_TEST_ASSERT(list->collection.simple_cmp == cx_cmp_ptr);
1496 CX_TEST_ASSERT(list->collection.advanced_cmp == NULL);
1497 CX_TEST_ASSERT(list->collection.store_pointer);
1498 }
1499 cxListFree(list);
1500 }
1501
1502 CX_TEST(test_list_arl_create) { 1467 CX_TEST(test_list_arl_create) {
1503 CxTestingAllocator talloc; 1468 CxTestingAllocator talloc;
1504 cx_testing_allocator_init(&talloc); 1469 cx_testing_allocator_init(&talloc);
1505 CxAllocator *alloc = &talloc.base; 1470 CxAllocator *alloc = &talloc.base;
1506 CX_TEST_DO { 1471 CX_TEST_DO {
1518 CX_TEST_ASSERT(!list->collection.store_pointer); 1483 CX_TEST_ASSERT(!list->collection.store_pointer);
1519 cxListFree(list); 1484 cxListFree(list);
1520 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1485 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1521 } 1486 }
1522 cx_testing_allocator_destroy(&talloc); 1487 cx_testing_allocator_destroy(&talloc);
1523 }
1524
1525 CX_TEST(test_list_arl_create_simple) {
1526 CxList *list = cxArrayListCreate(NULL, sizeof(int), 8);
1527 CX_TEST_DO {
1528 CX_TEST_ASSERT(list != NULL);
1529 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int));
1530 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1531 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1532 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
1533 CX_TEST_ASSERT(cxListSize(list) == 0);
1534 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
1535 CX_TEST_ASSERT(list->collection.simple_cmp == NULL);
1536 CX_TEST_ASSERT(list->collection.advanced_cmp == cx_ccmp_memcmp);
1537 CX_TEST_ASSERT(list->collection.cmp_data == &list->collection.elem_size);
1538 CX_TEST_ASSERT(!list->collection.store_pointer);
1539 }
1540 cxListFree(list);
1541 }
1542
1543 CX_TEST(test_list_arl_create_simple_for_pointers) {
1544 CxList *list = cxArrayListCreate(NULL, CX_STORE_POINTERS, 8);
1545 CX_TEST_DO {
1546 CX_TEST_ASSERT(list != NULL);
1547 CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*));
1548 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1549 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1550 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
1551 CX_TEST_ASSERT(cxListSize(list) == 0);
1552 CX_TEST_ASSERT(list->collection.allocator == cxDefaultAllocator);
1553 CX_TEST_ASSERT(list->collection.simple_cmp == cx_cmp_ptr);
1554 CX_TEST_ASSERT(list->collection.advanced_cmp == NULL);
1555 CX_TEST_ASSERT(list->collection.store_pointer);
1556 }
1557 cxListFree(list);
1558 } 1488 }
1559 1489
1560 static void test_fake_simple_int_destr(void *elem) { 1490 static void test_fake_simple_int_destr(void *elem) {
1561 *(int *) elem = 42; 1491 *(int *) elem = 42;
1562 } 1492 }
1768 tear_down_combo \ 1698 tear_down_combo \
1769 } 1699 }
1770 1700
1771 #define array_init(...) {__VA_ARGS__} 1701 #define array_init(...) {__VA_ARGS__}
1772 1702
1773 static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) { 1703 static int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) {
1774 int *testdata = int_test_data(len); 1704 int *testdata = int_test_data(len);
1775 if (isptrlist) { 1705 if (isptrlist) {
1776 for (size_t i = 0; i < len; i++) { 1706 for (size_t i = 0; i < len; i++) {
1777 cxListAdd(list, &testdata[i]); 1707 cxListAdd(list, &testdata[i]);
1778 } 1708 }
2309 const size_t testdata_len = 250; 2239 const size_t testdata_len = 250;
2310 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 2240 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
2311 2241
2312 unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) 2242 unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
2313 int val = testdata[exp]; 2243 int val = testdata[exp];
2244 int *x = &testdata[exp];
2314 // randomly picked number could occur earlier in list - find first position 2245 // randomly picked number could occur earlier in list - find first position
2315 for (unsigned i = 0 ; i < exp ; i++) { 2246 for (unsigned i = 0 ; i < exp ; i++) {
2316 if (testdata[i] == val) { 2247 if (testdata[i] == val) {
2317 exp = i; 2248 exp = i;
2249 x = &testdata[i];
2318 break; 2250 break;
2319 } 2251 }
2320 } 2252 }
2253
2254 // if this is not a cx_cmp_ptr test, we use &val to mix up the address
2255 // otherwise, we need the exact address
2256 if (!list->collection.store_pointer || list->collection.simple_cmp != cx_cmp_ptr) {
2257 x = &val;
2258 }
2259
2321 CX_TEST_ASSERT(cxListSize(list) == testdata_len); 2260 CX_TEST_ASSERT(cxListSize(list) == testdata_len);
2322 CX_TEST_ASSERT(cxListFind(list, &val) == exp); 2261 CX_TEST_ASSERT(cxListFind(list, x) == exp);
2323 CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp); 2262 CX_TEST_ASSERT(cxListFindRemove(list, x) == exp);
2324 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); 2263 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
2325 CX_TEST_ASSERT(cxListFind(list, &val) != exp); 2264 CX_TEST_ASSERT(cxListFind(list, x) != exp);
2326 2265
2327 int notinlist = -1; 2266 int notinlist = -1;
2328 CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) == cxListSize(list)); 2267 CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) == cxListSize(list));
2329 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); 2268 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
2330 2269
2331 free(testdata); 2270 free(testdata);
2332 }) 2271 })
2272
2273 CX_TEST(test_list_ll_find_remove_with_default_cmp) {
2274 set_up_combo
2275 CxList *list = cxLinkedListCreate(alloc, sizeof(int));
2276 CX_TEST_CALL_SUBROUTINE(test_list_verify_find_remove, list, false);
2277 tear_down_combo
2278 }
2279 CX_TEST(test_list_arl_find_remove_with_default_cmp) {
2280 set_up_combo
2281 CxList *list = cxArrayListCreate(alloc, sizeof(int), 8);
2282 CX_TEST_CALL_SUBROUTINE(test_list_verify_find_remove, list, false);
2283 tear_down_combo
2284 }
2285 CX_TEST(test_list_kvl_find_remove_with_default_cmp) {
2286 set_up_combo
2287 CxList *list = cxKvListCreate(alloc, sizeof(int));
2288 CX_TEST_CALL_SUBROUTINE(test_list_verify_find_remove, list, false);
2289 tear_down_combo
2290 }
2291 CX_TEST(test_list_pll_find_remove_with_default_cmp) {
2292 set_up_combo
2293 CxList *list = cxLinkedListCreate(alloc, CX_STORE_POINTERS);
2294 CX_TEST_CALL_SUBROUTINE(test_list_verify_find_remove, list, true);
2295 tear_down_combo
2296 }
2297 CX_TEST(test_list_parl_find_remove_with_default_cmp) {
2298 set_up_combo
2299 CxList *list = cxArrayListCreate(alloc, CX_STORE_POINTERS, 8);
2300 CX_TEST_CALL_SUBROUTINE(test_list_verify_find_remove, list, true);
2301 tear_down_combo
2302 }
2303 CX_TEST(test_list_pkvl_find_remove_with_default_cmp) {
2304 set_up_combo
2305 CxList *list = cxKvListCreate(alloc, CX_STORE_POINTERS);
2306 cxSetCompareFunc(list, cx_cmp_int);
2307 CX_TEST_CALL_SUBROUTINE(test_list_verify_find_remove, list, true);
2308 tear_down_combo
2309 }
2333 2310
2334 roll_out_test_combos(find_remove_sorted, { 2311 roll_out_test_combos(find_remove_sorted, {
2335 const size_t testdata_len = 250; 2312 const size_t testdata_len = 250;
2336 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 2313 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
2337 qsort(testdata, testdata_len, sizeof(int), cx_cmp_int); 2314 qsort(testdata, testdata_len, sizeof(int), cx_cmp_int);
3477 3454
3478 CxTestSuite *cx_test_suite_array_list(void) { 3455 CxTestSuite *cx_test_suite_array_list(void) {
3479 CxTestSuite *suite = cx_test_suite_new("array_list"); 3456 CxTestSuite *suite = cx_test_suite_new("array_list");
3480 3457
3481 cx_test_register(suite, test_list_arl_create); 3458 cx_test_register(suite, test_list_arl_create);
3482 cx_test_register(suite, test_list_arl_create_simple);
3483 cx_test_register(suite, test_list_arl_create_simple_for_pointers);
3484 cx_test_register(suite, test_list_parl_destroy_no_destr); 3459 cx_test_register(suite, test_list_parl_destroy_no_destr);
3485 cx_test_register(suite, test_list_parl_destroy_simple_destr); 3460 cx_test_register(suite, test_list_parl_destroy_simple_destr);
3486 cx_test_register(suite, test_list_parl_destroy_adv_destr); 3461 cx_test_register(suite, test_list_parl_destroy_adv_destr);
3487 3462
3488 cx_test_register(suite, test_list_arl_add); 3463 cx_test_register(suite, test_list_arl_add);
3507 cx_test_register(suite, test_list_parl_remove_and_get); 3482 cx_test_register(suite, test_list_parl_remove_and_get);
3508 cx_test_register(suite, test_list_arl_remove_array); 3483 cx_test_register(suite, test_list_arl_remove_array);
3509 cx_test_register(suite, test_list_parl_remove_array); 3484 cx_test_register(suite, test_list_parl_remove_array);
3510 cx_test_register(suite, test_list_arl_find_remove); 3485 cx_test_register(suite, test_list_arl_find_remove);
3511 cx_test_register(suite, test_list_parl_find_remove); 3486 cx_test_register(suite, test_list_parl_find_remove);
3487 cx_test_register(suite, test_list_arl_find_remove_with_default_cmp);
3488 cx_test_register(suite, test_list_parl_find_remove_with_default_cmp);
3512 cx_test_register(suite, test_list_arl_find_remove_sorted); 3489 cx_test_register(suite, test_list_arl_find_remove_sorted);
3513 cx_test_register(suite, test_list_parl_find_remove_sorted); 3490 cx_test_register(suite, test_list_parl_find_remove_sorted);
3514 cx_test_register(suite, test_list_arl_contains); 3491 cx_test_register(suite, test_list_arl_contains);
3515 cx_test_register(suite, test_list_parl_contains); 3492 cx_test_register(suite, test_list_parl_contains);
3516 cx_test_register(suite, test_list_arl_clear); 3493 cx_test_register(suite, test_list_arl_clear);
3623 cx_test_register(suite, test_linked_list_sort_empty); 3600 cx_test_register(suite, test_linked_list_sort_empty);
3624 cx_test_register(suite, test_linked_list_sort); 3601 cx_test_register(suite, test_linked_list_sort);
3625 cx_test_register(suite, test_linked_list_reverse); 3602 cx_test_register(suite, test_linked_list_reverse);
3626 3603
3627 cx_test_register(suite, test_list_ll_create); 3604 cx_test_register(suite, test_list_ll_create);
3628 cx_test_register(suite, test_list_ll_create_simple);
3629 cx_test_register(suite, test_list_ll_create_simple_for_pointers);
3630 cx_test_register(suite, test_list_pll_destroy_no_destr); 3605 cx_test_register(suite, test_list_pll_destroy_no_destr);
3631 cx_test_register(suite, test_list_pll_destroy_simple_destr); 3606 cx_test_register(suite, test_list_pll_destroy_simple_destr);
3632 cx_test_register(suite, test_list_pll_destroy_adv_destr); 3607 cx_test_register(suite, test_list_pll_destroy_adv_destr);
3633 3608
3634 cx_test_register(suite, test_list_ll_add); 3609 cx_test_register(suite, test_list_ll_add);
3653 cx_test_register(suite, test_list_pll_remove_and_get); 3628 cx_test_register(suite, test_list_pll_remove_and_get);
3654 cx_test_register(suite, test_list_ll_remove_array); 3629 cx_test_register(suite, test_list_ll_remove_array);
3655 cx_test_register(suite, test_list_pll_remove_array); 3630 cx_test_register(suite, test_list_pll_remove_array);
3656 cx_test_register(suite, test_list_ll_find_remove); 3631 cx_test_register(suite, test_list_ll_find_remove);
3657 cx_test_register(suite, test_list_pll_find_remove); 3632 cx_test_register(suite, test_list_pll_find_remove);
3633 cx_test_register(suite, test_list_ll_find_remove_with_default_cmp);
3634 cx_test_register(suite, test_list_pll_find_remove_with_default_cmp);
3658 cx_test_register(suite, test_list_ll_find_remove_sorted); 3635 cx_test_register(suite, test_list_ll_find_remove_sorted);
3659 cx_test_register(suite, test_list_pll_find_remove_sorted); 3636 cx_test_register(suite, test_list_pll_find_remove_sorted);
3660 cx_test_register(suite, test_list_ll_contains); 3637 cx_test_register(suite, test_list_ll_contains);
3661 cx_test_register(suite, test_list_pll_contains); 3638 cx_test_register(suite, test_list_pll_contains);
3662 cx_test_register(suite, test_list_ll_clear); 3639 cx_test_register(suite, test_list_ll_clear);
3771 cx_test_register(suite, test_list_pkvl_remove_and_get); 3748 cx_test_register(suite, test_list_pkvl_remove_and_get);
3772 cx_test_register(suite, test_list_kvl_remove_array); 3749 cx_test_register(suite, test_list_kvl_remove_array);
3773 cx_test_register(suite, test_list_pkvl_remove_array); 3750 cx_test_register(suite, test_list_pkvl_remove_array);
3774 cx_test_register(suite, test_list_kvl_find_remove); 3751 cx_test_register(suite, test_list_kvl_find_remove);
3775 cx_test_register(suite, test_list_pkvl_find_remove); 3752 cx_test_register(suite, test_list_pkvl_find_remove);
3753 cx_test_register(suite, test_list_kvl_find_remove_with_default_cmp);
3754 cx_test_register(suite, test_list_pkvl_find_remove_with_default_cmp);
3776 cx_test_register(suite, test_list_kvl_find_remove_sorted); 3755 cx_test_register(suite, test_list_kvl_find_remove_sorted);
3777 cx_test_register(suite, test_list_pkvl_find_remove_sorted); 3756 cx_test_register(suite, test_list_pkvl_find_remove_sorted);
3778 cx_test_register(suite, test_list_kvl_contains); 3757 cx_test_register(suite, test_list_kvl_contains);
3779 cx_test_register(suite, test_list_pkvl_contains); 3758 cx_test_register(suite, test_list_pkvl_contains);
3780 cx_test_register(suite, test_list_kvl_clear); 3759 cx_test_register(suite, test_list_kvl_clear);

mercurial