tests/test_list.c

changeset 962
cd418898af5c
parent 961
bc8b7c5f55fb
equal deleted inserted replaced
961:bc8b7c5f55fb 962:cd418898af5c
27 */ 27 */
28 28
29 #include "cx/test.h" 29 #include "cx/test.h"
30 #include "util_allocator.h" 30 #include "util_allocator.h"
31 #include "cx/compare.h" 31 #include "cx/compare.h"
32 #include "cx/utils.h"
33 32
34 #include "cx/array_list.h" 33 #include "cx/array_list.h"
35 #include "cx/linked_list.h" 34 #include "cx/linked_list.h"
36 35
37 #include <stdarg.h> 36 #include <stdarg.h>
152 40, 50, 51, 52, 54, 56, 57, 58, 60, 151 40, 50, 51, 52, 54, 56, 57, 58, 60,
153 62, 64, 65, 70, 75, 77, 78, 80, 90 152 62, 64, 65, 70, 75, 77, 78, 80, 90
154 }; 153 };
155 154
156 CX_TEST_DO { 155 CX_TEST_DO {
157 cx_for_n(i, 18) { 156 for (size_t i = 0; i < 18; i++) {
158 CX_TEST_ASSERT(i == cx_array_binary_search(array, 18, sizeof(int), &array[i], cx_cmp_int)); 157 CX_TEST_ASSERT(i == cx_array_binary_search(array, 18, sizeof(int), &array[i], cx_cmp_int));
159 } 158 }
160 159
161 int s = 58; 160 int s = 58;
162 CX_TEST_ASSERT(7 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); 161 CX_TEST_ASSERT(7 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int));
1223 #define array_init(...) {__VA_ARGS__} 1222 #define array_init(...) {__VA_ARGS__}
1224 1223
1225 static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) { 1224 static inline int *int_test_data_added_to_list(CxList *list, bool isptrlist, size_t len) {
1226 int *testdata = int_test_data(len); 1225 int *testdata = int_test_data(len);
1227 if (isptrlist) { 1226 if (isptrlist) {
1228 cx_for_n(i, len) { 1227 for (size_t i = 0; i < len; i++) {
1229 cxListAdd(list, &testdata[i]); 1228 cxListAdd(list, &testdata[i]);
1230 } 1229 }
1231 } else { 1230 } else {
1232 cxListAddArray(list, testdata, len); 1231 cxListAddArray(list, testdata, len);
1233 } 1232 }
1235 } 1234 }
1236 1235
1237 roll_out_test_combos(add, { 1236 roll_out_test_combos(add, {
1238 const size_t len = 250; 1237 const size_t len = 250;
1239 int *testdata = int_test_data(len); 1238 int *testdata = int_test_data(len);
1240 cx_for_n (i, len) CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0); 1239 for (size_t i = 0; i < len; i++) {
1240 CX_TEST_ASSERT(cxListAdd(list, &testdata[i]) == 0);
1241 }
1241 CX_TEST_ASSERT(cxListSize(list) == len); 1242 CX_TEST_ASSERT(cxListSize(list) == len);
1242 cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); 1243 for (size_t i = 0; i < len; i++) {
1243 cx_for_n (i, len) ++testdata[i]; 1244 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
1245 }
1246 for (size_t i = 0; i < len; i++) {
1247 ++testdata[i];
1248 }
1244 if (isptrlist) { 1249 if (isptrlist) {
1245 cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); 1250 for (size_t i = 0; i < len; i++) {
1251 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
1252 }
1246 } else { 1253 } else {
1247 cx_for_n (i, len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1); 1254 for (size_t i = 0; i < len; i++) {
1255 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i] - 1);
1256 }
1248 } 1257 }
1249 free(testdata); 1258 free(testdata);
1250 }) 1259 })
1251 1260
1252 roll_out_test_combos(insert, { 1261 roll_out_test_combos(insert, {
1273 roll_out_test_combos_with_defaulted_funcs(insert_array, { 1282 roll_out_test_combos_with_defaulted_funcs(insert_array, {
1274 int a[5] = array_init(5, 47, 11, 13, 42); 1283 int a[5] = array_init(5, 47, 11, 13, 42);
1275 int b[5] = array_init(9, 18, 72, 50, 7); 1284 int b[5] = array_init(9, 18, 72, 50, 7);
1276 int *aptr[5]; 1285 int *aptr[5];
1277 int *bptr[5]; 1286 int *bptr[5];
1278 cx_for_n(i, 5) { 1287 for (size_t i = 0; i < 5; i++) {
1279 aptr[i] = &a[i]; 1288 aptr[i] = &a[i];
1280 bptr[i] = &b[i]; 1289 bptr[i] = &b[i];
1281 } 1290 }
1282 1291
1283 size_t inserted; 1292 size_t inserted;
1320 int d6a[6] = array_init(52, 54, 56, 62, 64, 75); 1329 int d6a[6] = array_init(52, 54, 56, 62, 64, 75);
1321 int d7a[6] = array_init(51, 57, 58, 65, 77, 78); 1330 int d7a[6] = array_init(51, 57, 58, 65, 77, 78);
1322 int d8 = 90; 1331 int d8 = 90;
1323 int *d6ptr[6]; 1332 int *d6ptr[6];
1324 int *d7ptr[6]; 1333 int *d7ptr[6];
1325 cx_for_n(i, 6) 1334 for (size_t i = 0; i < 6; i++) {
1326 {
1327 d6ptr[i] = &d6a[i]; 1335 d6ptr[i] = &d6a[i];
1328 d7ptr[i] = &d7a[i]; 1336 d7ptr[i] = &d7a[i];
1329 } 1337 }
1330 size_t inserted; 1338 size_t inserted;
1331 int expected[18] = array_init( 1339 int expected[18] = array_init(
1351 } 1359 }
1352 CX_TEST_ASSERT(inserted == 6); 1360 CX_TEST_ASSERT(inserted == 6);
1353 CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d8)); 1361 CX_TEST_ASSERT(0 == cxListInsertSorted(list, &d8));
1354 1362
1355 CX_TEST_ASSERT(cxListSize(list) == 18); 1363 CX_TEST_ASSERT(cxListSize(list) == 18);
1356 cx_for_n(i, 18) 1364 for (size_t i = 0; i < 18; i++) {
1357 {
1358 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]); 1365 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
1359 } 1366 }
1360 }) 1367 })
1361 1368
1362 roll_out_test_combos(remove, { 1369 roll_out_test_combos(remove, {
1491 1498
1492 roll_out_test_combos(at, { 1499 roll_out_test_combos(at, {
1493 size_t len = 128; 1500 size_t len = 128;
1494 int *testdata = int_test_data_added_to_list(list, isptrlist, 128); 1501 int *testdata = int_test_data_added_to_list(list, isptrlist, 128);
1495 CX_TEST_ASSERT(cxListSize(list) == len); 1502 CX_TEST_ASSERT(cxListSize(list) == len);
1496 cx_for_n (i, len) { 1503 for (size_t i = 0; i < len; i++) {
1497 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]); 1504 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[i]);
1498 } 1505 }
1499 CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL); 1506 CX_TEST_ASSERT(cxListAt(list, cxListSize(list)) == NULL);
1500 free(testdata); 1507 free(testdata);
1501 }) 1508 })
1502 1509
1503 roll_out_test_combos_with_defaulted_funcs(swap, { 1510 roll_out_test_combos_with_defaulted_funcs(swap, {
1504 int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); 1511 int original[16] = array_init(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1505 int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13); 1512 int swapped[16] = array_init(8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13);
1506 1513
1507 cx_for_n(i, 16) { 1514 for (size_t i = 0; i < 16; i++) {
1508 cxListAdd(list, &original[i]); 1515 cxListAdd(list, &original[i]);
1509 } 1516 }
1510 1517
1511 CX_TEST_ASSERT(0 == cxListSwap(list, 1, 4)); 1518 CX_TEST_ASSERT(0 == cxListSwap(list, 1, 4));
1512 CX_TEST_ASSERT(0 == cxListSwap(list, 2, 14)); 1519 CX_TEST_ASSERT(0 == cxListSwap(list, 2, 14));
1546 1553
1547 roll_out_test_combos(find, { 1554 roll_out_test_combos(find, {
1548 const size_t testdata_len = 500; 1555 const size_t testdata_len = 500;
1549 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 1556 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1550 1557
1551 cx_for_n (attempt, 25) { 1558 for (size_t attempt = 0; attempt < 25; attempt++) {
1552 int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp) 1559 int exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
1553 int val = testdata[exp]; 1560 int val = testdata[exp];
1554 // randomly picked number could occur earlier in list - find first position 1561 // randomly picked number could occur earlier in list - find first position
1555 for (int i = 0 ; i < exp ; i++) { 1562 for (int i = 0 ; i < exp ; i++) {
1556 if (testdata[i] == val) { 1563 if (testdata[i] == val) {
1573 int *expected = malloc(testdata_len*sizeof(int)); 1580 int *expected = malloc(testdata_len*sizeof(int));
1574 memcpy(expected, testdata, testdata_len*sizeof(int)); 1581 memcpy(expected, testdata, testdata_len*sizeof(int));
1575 qsort(expected, testdata_len, sizeof(int), cx_cmp_int); 1582 qsort(expected, testdata_len, sizeof(int), cx_cmp_int);
1576 1583
1577 cxListSort(list); 1584 cxListSort(list);
1578 cx_for_n (i, testdata_len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]); 1585 for (size_t i = 0; i < testdata_len; i++) {
1586 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
1587 }
1579 1588
1580 free(expected); 1589 free(expected);
1581 free(testdata); 1590 free(testdata);
1582 }) 1591 })
1583 1592
1584 roll_out_test_combos(reverse, { 1593 roll_out_test_combos(reverse, {
1585 const size_t testdata_len = 50; 1594 const size_t testdata_len = 50;
1586 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 1595 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
1587 cxListReverse(list); 1596 cxListReverse(list);
1588 cx_for_n(i, testdata_len) { 1597 for (size_t i = 0; i < testdata_len; i++) {
1589 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[testdata_len - 1 - i]); 1598 CX_TEST_ASSERT(*(int *) cxListAt(list, i) == testdata[testdata_len - 1 - i]);
1590 } 1599 }
1591 free(testdata); 1600 free(testdata);
1592 }) 1601 })
1593 1602
1635 i--; 1644 i--;
1636 j++; 1645 j++;
1637 } 1646 }
1638 CX_TEST_ASSERT(i == 0); 1647 CX_TEST_ASSERT(i == 0);
1639 CX_TEST_ASSERT(cxListSize(list) == len / 2); 1648 CX_TEST_ASSERT(cxListSize(list) == len / 2);
1640 cx_for_n(k, len / 2) CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]); 1649 for (size_t k = 0; k < len / 2; k++) {
1650 CX_TEST_ASSERT(*(int *) cxListAt(list, k) == testdata[k * 2]);
1651 }
1641 1652
1642 free(testdata); 1653 free(testdata);
1643 }) 1654 })
1644 1655
1645 roll_out_test_combos(insert_with_iterator, { 1656 roll_out_test_combos(insert_with_iterator, {
1646 int fivenums[] = array_init(0, 1, 2, 3, 4); 1657 int fivenums[] = array_init(0, 1, 2, 3, 4);
1647 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]); 1658 for (size_t i = 0; i < 5; i++) {
1659 cxListAdd(list, &fivenums[i]);
1660 }
1648 int newdata[] = array_init(10, 20, 30, 40, 50); 1661 int newdata[] = array_init(10, 20, 30, 40, 50);
1649 1662
1650 CxIterator iter = cxListMutIteratorAt(list, 2); 1663 CxIterator iter = cxListMutIteratorAt(list, 2);
1651 CX_TEST_ASSERT(cxIteratorValid(iter)); 1664 CX_TEST_ASSERT(cxIteratorValid(iter));
1652 CX_TEST_ASSERT(iter.index == 2); 1665 CX_TEST_ASSERT(iter.index == 2);
1679 CX_TEST_ASSERT(!cxIteratorValid(iter)); 1692 CX_TEST_ASSERT(!cxIteratorValid(iter));
1680 CX_TEST_ASSERT(iter.index == 10); 1693 CX_TEST_ASSERT(iter.index == 10);
1681 CX_TEST_ASSERT(iter.elem_count == 10); 1694 CX_TEST_ASSERT(iter.elem_count == 10);
1682 1695
1683 int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50); 1696 int expdata[] = array_init(30, 0, 1, 20, 2, 10, 3, 4, 40, 50);
1684 cx_for_n (j, 10) CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]); 1697 for (size_t j = 0; j < 10; j++) {
1698 CX_TEST_ASSERT(*(int *) cxListAt(list, j) == expdata[j]);
1699 }
1685 }) 1700 })
1686 1701
1687 static CX_TEST_SUBROUTINE(test_list_verify_compare, CxList *left, CxList *right) { 1702 static CX_TEST_SUBROUTINE(test_list_verify_compare, CxList *left, CxList *right) {
1688 CX_TEST_ASSERTM(cxListCompare(left, right) == 0, "lists don't start identical"); 1703 CX_TEST_ASSERTM(cxListCompare(left, right) == 0, "lists don't start identical");
1689 int x = 42; 1704 int x = 42;
1707 #define roll_out_compare_tests(suffix, otherctr) \ 1722 #define roll_out_compare_tests(suffix, otherctr) \
1708 roll_out_test_combos(compare_##suffix, { \ 1723 roll_out_test_combos(compare_##suffix, { \
1709 const size_t len = 47; \ 1724 const size_t len = 47; \
1710 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ 1725 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
1711 CxList *other = otherctr; \ 1726 CxList *other = otherctr; \
1712 cx_for_n(i, len) cxListAdd(other, &testdata[i]); \ 1727 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \
1713 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ 1728 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
1714 cxListDestroy(other); \ 1729 cxListDestroy(other); \
1715 free(testdata); \ 1730 free(testdata); \
1716 }) 1731 })
1717 1732
1730 roll_out_compare_tests( 1745 roll_out_compare_tests(
1731 parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50) 1746 parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50)
1732 ) 1747 )
1733 1748
1734 roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, { 1749 roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, {
1735 \ 1750 const size_t len = 33;
1736 const size_t len = 33; \ 1751 int *testdata = int_test_data_added_to_list(list, isptrlist, len);
1737 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ 1752 CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50);
1738 CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); \ 1753 do_set_default_class_funcs(other);
1739 do_set_default_class_funcs(other); \ 1754 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]);
1740 cx_for_n(i, len) 1755 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other);
1741 cxListAdd(other, &testdata[i]); \ 1756 cxListDestroy(other);
1742 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ 1757 free(testdata);
1743 cxListDestroy(other); \
1744 free(testdata); \
1745
1746 }) 1758 })
1747 1759
1748 static unsigned destr_test_ctr; 1760 static unsigned destr_test_ctr;
1749 static int destr_last_value; 1761 static int destr_last_value;
1750 1762

mercurial