tests/test_list.c

changeset 1162
e3bb67b72d33
parent 1113
dce04550fbef
child 1163
68ff0839bc6a
equal deleted inserted replaced
1161:747c4baed44f 1162:e3bb67b72d33
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));
1556 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1); 1565 CX_TEST_ASSERT(cxListSize(list) == testdata_len - 1);
1557 1566
1558 free(testdata); 1567 free(testdata);
1559 }) 1568 })
1560 1569
1569 roll_out_test_combos(at, { 1578 roll_out_test_combos(at, {
1570 size_t len = 128; 1579 size_t len = 128;
1571 int *testdata = int_test_data_added_to_list(list, isptrlist, 128); 1580 int *testdata = int_test_data_added_to_list(list, isptrlist, 128);
1572 CX_TEST_ASSERT(cxListSize(list) == len); 1581 CX_TEST_ASSERT(cxListSize(list) == len);
1573 for (size_t i = 0; i < len; i++) { 1582 for (size_t i = 0; i < len; i++) {
1583 CX_TEST_ASSERT(cxListIndexValid(list, i));
1574 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); 1584 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
1575 } 1585 }
1576 CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL); 1586 CX_TEST_ASSERT(!cxListIndexValid(list, len));
1587 CX_TEST_ASSERT(cxListAt(list, len) == NULL);
1577 free(testdata); 1588 free(testdata);
1578 }) 1589 })
1579 1590
1580 roll_out_test_combos_with_defaulted_funcs(swap, { 1591 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); 1592 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, { 1629 roll_out_test_combos(find, {
1619 const size_t testdata_len = 500; 1630 const size_t testdata_len = 500;
1620 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 1631 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1621 1632
1622 for (size_t attempt = 0; attempt < 25; attempt++) { 1633 for (size_t attempt = 0; attempt < 25; attempt++) {
1623 int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) 1634 unsigned exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
1624 int val = testdata[exp]; 1635 int val = testdata[exp];
1625 // randomly picked number could occur earlier in list - find first position 1636 // randomly picked number could occur earlier in list - find first position
1626 for (int i = 0 ; i < exp ; i++) { 1637 for (unsigned i = 0 ; i < exp ; i++) {
1627 if (testdata[i] == val) { 1638 if (testdata[i] == val) {
1628 exp = i; 1639 exp = i;
1629 break; 1640 break;
1630 } 1641 }
1631 } 1642 }
1632 CX_TEST_ASSERT(cxListFind(list, &val) == exp); 1643 CX_TEST_ASSERT(cxListFind(list, &val) == exp);
1633 } 1644 }
1634 1645
1635 int notinlist = -1; 1646 int notinlist = -1;
1636 CX_TEST_ASSERT(cxListFind(list, &notinlist) < 0); 1647 CX_TEST_ASSERT(cxListFind(list, &notinlist) == cxListSize(list));
1637 1648
1638 free(testdata); 1649 free(testdata);
1639 }) 1650 })
1640 1651
1641 roll_out_test_combos_with_defaulted_funcs(sort, { 1652 roll_out_test_combos_with_defaulted_funcs(sort, {

mercurial