tests/test_list.c

branch
docs/3.1
changeset 1164
148b7c7ccaf9
parent 1163
68ff0839bc6a
equal deleted inserted replaced
1148:8ff82697f2c3 1164:148b7c7ccaf9
380 CX_TEST_ASSERT(cx_linked_list_at(&d, 3, loc_prev, 1) == &b); 380 CX_TEST_ASSERT(cx_linked_list_at(&d, 3, loc_prev, 1) == &b);
381 } 381 }
382 } 382 }
383 383
384 CX_TEST(test_linked_list_find) { 384 CX_TEST(test_linked_list_find) {
385 void *list = create_nodes_test_data(4); 385 node *list = create_nodes_test_data(4);
386 assign_nodes_test_data(list, 2, 4, 6, 8); 386 assign_nodes_test_data(list, 2, 4, 6, 8);
387 CX_TEST_DO { 387 CX_TEST_DO {
388 size_t i = 10;
388 int s; 389 int s;
389 s = 2; 390 s = 2;
390 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 0); 391 node *n = list;
392 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
393 CX_TEST_ASSERT(i == 0);
394 n = n->next;
391 s = 4; 395 s = 4;
392 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 1); 396 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
397 CX_TEST_ASSERT(i == 1);
398 n = n->next;
393 s = 6; 399 s = 6;
394 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 2); 400 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
401 CX_TEST_ASSERT(i == 2);
402 n = n->next;
395 s = 8; 403 s = 8;
396 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) == 3); 404 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == n);
405 CX_TEST_ASSERT(i == 3);
397 s = 10; 406 s = 10;
398 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0); 407 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == NULL);
399 s = -2; 408 s = -2;
400 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s) < 0); 409 CX_TEST_ASSERT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s, &i) == NULL);
401 } 410 }
402 destroy_nodes_test_data(list); 411 destroy_nodes_test_data(list);
403 } 412 }
404 413
405 CX_TEST(test_linked_list_compare) { 414 CX_TEST(test_linked_list_compare) {
965 } 974 }
966 975
967 CX_TEST(test_empty_list_find) { 976 CX_TEST(test_empty_list_find) {
968 int x = 42, y = 1337; 977 int x = 42, y = 1337;
969 CX_TEST_DO { 978 CX_TEST_DO {
970 CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) < 0); 979 CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) == 0);
971 CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) < 0); 980 CX_TEST_ASSERT(cxListFind(cxEmptyList, &y) == 0);
972 } 981 }
973 } 982 }
974 983
975 CX_TEST(test_empty_list_compare) { 984 CX_TEST(test_empty_list_compare) {
976 CxList *empty = cxEmptyList; 985 CxList *empty = cxEmptyList;
1534 1543
1535 roll_out_test_combos(find_remove, { 1544 roll_out_test_combos(find_remove, {
1536 const size_t testdata_len = 250; 1545 const size_t testdata_len = 250;
1537 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 1546 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1538 1547
1539 int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) 1548 unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
1540 int val = testdata[exp]; 1549 int val = testdata[exp];
1541 // randomly picked number could occur earlier in list - find first position 1550 // randomly picked number could occur earlier in list - find first position
1542 for (int i = 0 ; i < exp ; i++) { 1551 for (unsigned i = 0 ; i < exp ; i++) {
1543 if (testdata[i] == val) { 1552 if (testdata[i] == val) {
1544 exp = i; 1553 exp = i;
1545 break; 1554 break;
1546 } 1555 }
1547 } 1556 }
1550 CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp); 1559 CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp);
1551 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); 1560 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
1552 CX_TEST_ASSERT(cxListFind(list, &val) != exp); 1561 CX_TEST_ASSERT(cxListFind(list, &val) != exp);
1553 1562
1554 int notinlist = -1; 1563 int notinlist = -1;
1555 CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) < 0); 1564 CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) == cxListSize(list));
1565 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
1566
1567 free(testdata);
1568 })
1569
1570 roll_out_test_combos(find_remove_sorted, {
1571 const size_t testdata_len = 250;
1572 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1573 qsort(testdata, testdata_len, sizeof(int), cx_cmp_int);
1574 cxListSort(list);
1575
1576 unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
1577 int val = testdata[exp];
1578 // randomly picked number could occur earlier in list - find first position
1579 for (unsigned i = 0 ; i < exp ; i++) {
1580 if (testdata[i] == val) {
1581 exp = i;
1582 break;
1583 }
1584 }
1585 CX_TEST_ASSERT(cxListSize(list) == testdata_len);
1586 CX_TEST_ASSERT(cxListFind(list, &val) == exp);
1587 CX_TEST_ASSERT(cxListFindRemove(list, &val) == exp);
1588 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
1589 CX_TEST_ASSERT(cxListFind(list, &val) != exp);
1590
1591 int notinlist = -1;
1592 CX_TEST_ASSERT(cxListFindRemove(list, &notinlist) == cxListSize(list));
1556 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); 1593 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
1557 1594
1558 free(testdata); 1595 free(testdata);
1559 }) 1596 })
1560 1597
1569 roll_out_test_combos(at, { 1606 roll_out_test_combos(at, {
1570 size_t len = 128; 1607 size_t len = 128;
1571 int *testdata = int_test_data_added_to_list(list, isptrlist, 128); 1608 int *testdata = int_test_data_added_to_list(list, isptrlist, 128);
1572 CX_TEST_ASSERT(cxListSize(list) == len); 1609 CX_TEST_ASSERT(cxListSize(list) == len);
1573 for (size_t i = 0; i < len; i++) { 1610 for (size_t i = 0; i < len; i++) {
1611 CX_TEST_ASSERT(cxListIndexValid(list, i));
1574 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); 1612 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
1575 } 1613 }
1576 CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL); 1614 CX_TEST_ASSERT(!cxListIndexValid(list, len));
1615 CX_TEST_ASSERT(cxListAt(list, len) == NULL);
1577 free(testdata); 1616 free(testdata);
1578 }) 1617 })
1579 1618
1580 roll_out_test_combos_with_defaulted_funcs(swap, { 1619 roll_out_test_combos_with_defaulted_funcs(swap, {
1581 int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 1620 int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1618 roll_out_test_combos(find, { 1657 roll_out_test_combos(find, {
1619 const size_t testdata_len = 500; 1658 const size_t testdata_len = 500;
1620 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 1659 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1621 1660
1622 for (size_t attempt = 0; attempt < 25; attempt++) { 1661 for (size_t attempt = 0; attempt < 25; attempt++) {
1623 int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) 1662 unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
1624 int val = testdata[exp]; 1663 int val = testdata[exp];
1625 // randomly picked number could occur earlier in list - find first position 1664 // randomly picked number could occur earlier in list - find first position
1626 for (int i = 0 ; i < exp ; i++) { 1665 for (unsigned i = 0 ; i < exp ; i++) {
1627 if (testdata[i] == val) { 1666 if (testdata[i] == val) {
1628 exp = i; 1667 exp = i;
1629 break; 1668 break;
1630 } 1669 }
1631 } 1670 }
1632 CX_TEST_ASSERT(cxListFind(list, &val) == exp); 1671 CX_TEST_ASSERT(cxListFind(list, &val) == exp);
1633 } 1672 }
1634 1673
1635 int notinlist = -1; 1674 int notinlist = -1;
1636 CX_TEST_ASSERT(cxListFind(list, &notinlist) < 0); 1675 CX_TEST_ASSERT(cxListFind(list, &notinlist) == cxListSize(list));
1637 1676
1638 free(testdata); 1677 free(testdata);
1639 }) 1678 })
1640 1679
1641 roll_out_test_combos_with_defaulted_funcs(sort, { 1680 roll_out_test_combos_with_defaulted_funcs(sort, {
1922 cx_test_register(suite, test_list_parl_remove); 1961 cx_test_register(suite, test_list_parl_remove);
1923 cx_test_register(suite, test_list_arl_remove_array); 1962 cx_test_register(suite, test_list_arl_remove_array);
1924 cx_test_register(suite, test_list_parl_remove_array); 1963 cx_test_register(suite, test_list_parl_remove_array);
1925 cx_test_register(suite, test_list_arl_find_remove); 1964 cx_test_register(suite, test_list_arl_find_remove);
1926 cx_test_register(suite, test_list_parl_find_remove); 1965 cx_test_register(suite, test_list_parl_find_remove);
1966 cx_test_register(suite, test_list_arl_find_remove_sorted);
1967 cx_test_register(suite, test_list_parl_find_remove_sorted);
1927 cx_test_register(suite, test_list_arl_clear); 1968 cx_test_register(suite, test_list_arl_clear);
1928 cx_test_register(suite, test_list_parl_clear); 1969 cx_test_register(suite, test_list_parl_clear);
1929 cx_test_register(suite, test_list_arl_at); 1970 cx_test_register(suite, test_list_arl_at);
1930 cx_test_register(suite, test_list_parl_at); 1971 cx_test_register(suite, test_list_parl_at);
1931 cx_test_register(suite, test_list_arl_swap); 1972 cx_test_register(suite, test_list_arl_swap);
2019 cx_test_register(suite, test_list_pll_remove); 2060 cx_test_register(suite, test_list_pll_remove);
2020 cx_test_register(suite, test_list_ll_remove_array); 2061 cx_test_register(suite, test_list_ll_remove_array);
2021 cx_test_register(suite, test_list_pll_remove_array); 2062 cx_test_register(suite, test_list_pll_remove_array);
2022 cx_test_register(suite, test_list_ll_find_remove); 2063 cx_test_register(suite, test_list_ll_find_remove);
2023 cx_test_register(suite, test_list_pll_find_remove); 2064 cx_test_register(suite, test_list_pll_find_remove);
2065 cx_test_register(suite, test_list_ll_find_remove_sorted);
2066 cx_test_register(suite, test_list_pll_find_remove_sorted);
2024 cx_test_register(suite, test_list_ll_clear); 2067 cx_test_register(suite, test_list_ll_clear);
2025 cx_test_register(suite, test_list_pll_clear); 2068 cx_test_register(suite, test_list_pll_clear);
2026 cx_test_register(suite, test_list_ll_at); 2069 cx_test_register(suite, test_list_ll_at);
2027 cx_test_register(suite, test_list_pll_at); 2070 cx_test_register(suite, test_list_pll_at);
2028 cx_test_register(suite, test_list_ll_swap); 2071 cx_test_register(suite, test_list_ll_swap);

mercurial