tests/test_list.c

changeset 1605
55b13f583356
parent 1604
68b75c091028
child 1606
f5883f6e42e7
equal deleted inserted replaced
1604:68b75c091028 1605:55b13f583356
1392 CX_TEST_DO { 1392 CX_TEST_DO {
1393 // the placeholder empty list 1393 // the placeholder empty list
1394 CX_TEST_ASSERT(cxListAt(cxEmptyList, 0) == NULL); 1394 CX_TEST_ASSERT(cxListAt(cxEmptyList, 0) == NULL);
1395 CX_TEST_ASSERT(cxListAt(cxEmptyList, 1) == NULL); 1395 CX_TEST_ASSERT(cxListAt(cxEmptyList, 1) == NULL);
1396 // a "true" empty list 1396 // a "true" empty list
1397 CxList *list = cxLinkedListCreateSimple(sizeof(int)); 1397 CxList *list = cxLinkedListCreate(NULL, sizeof(int));
1398 CX_TEST_ASSERT(cxListAt(list, 0) == NULL); 1398 CX_TEST_ASSERT(cxListAt(list, 0) == NULL);
1399 CX_TEST_ASSERT(cxListAt(list, 1) == NULL); 1399 CX_TEST_ASSERT(cxListAt(list, 1) == NULL);
1400 cxListFree(list); 1400 cxListFree(list);
1401 } 1401 }
1402 } 1402 }
1406 CX_TEST_DO { 1406 CX_TEST_DO {
1407 // the placeholder empty list 1407 // the placeholder empty list
1408 CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) == 0); 1408 CX_TEST_ASSERT(cxListFind(cxEmptyList, &x) == 0);
1409 CX_TEST_ASSERT(cxListFindRemove(cxEmptyList, &y) == 0); 1409 CX_TEST_ASSERT(cxListFindRemove(cxEmptyList, &y) == 0);
1410 // a "true" empty list 1410 // a "true" empty list
1411 CxList *list = cxLinkedListCreateSimple(sizeof(int)); 1411 CxList *list = cxLinkedListCreate(NULL, sizeof(int));
1412 CX_TEST_ASSERT(cxListFind(list, &x) == 0); 1412 CX_TEST_ASSERT(cxListFind(list, &x) == 0);
1413 CX_TEST_ASSERT(cxListFindRemove(list, &y) == 0); 1413 CX_TEST_ASSERT(cxListFindRemove(list, &y) == 0);
1414 cxListFree(list); 1414 cxListFree(list);
1415 } 1415 }
1416 } 1416 }
1417 1417
1418 CX_TEST(test_empty_list_compare) { 1418 CX_TEST(test_empty_list_compare) {
1419 CxList *empty = cxEmptyList; 1419 CxList *empty = cxEmptyList;
1420 CxList *ll = cxLinkedListCreateSimple(sizeof(int)); 1420 CxList *ll = cxLinkedListCreate(NULL, sizeof(int));
1421 CxList *al = cxArrayListCreateSimple(sizeof(int), 8); 1421 CxList *al = cxArrayListCreate(NULL, sizeof(int), 8);
1422 int x = 5; 1422 int x = 5;
1423 CX_TEST_DO { 1423 CX_TEST_DO {
1424 CX_TEST_ASSERT(0 == cxListCompare(empty, cxEmptyList)); 1424 CX_TEST_ASSERT(0 == cxListCompare(empty, cxEmptyList));
1425 CX_TEST_ASSERT(0 == cxListCompare(ll, cxEmptyList)); 1425 CX_TEST_ASSERT(0 == cxListCompare(ll, cxEmptyList));
1426 CX_TEST_ASSERT(0 == cxListCompare(al, cxEmptyList)); 1426 CX_TEST_ASSERT(0 == cxListCompare(al, cxEmptyList));
1449 CX_TEST(test_list_ll_create) { 1449 CX_TEST(test_list_ll_create) {
1450 CxTestingAllocator talloc; 1450 CxTestingAllocator talloc;
1451 cx_testing_allocator_init(&talloc); 1451 cx_testing_allocator_init(&talloc);
1452 CxAllocator *alloc = &talloc.base; 1452 CxAllocator *alloc = &talloc.base;
1453 CX_TEST_DO { 1453 CX_TEST_DO {
1454 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); 1454 CxList *list = cxLinkedListCreate(alloc, sizeof(int));
1455 CX_TEST_ASSERT(list != NULL); 1455 CX_TEST_ASSERT(list != NULL);
1456 cxSetCompareFunc(list, cx_cmp_int);
1456 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); 1457 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int));
1457 CX_TEST_ASSERT(list->collection.simple_destructor == NULL); 1458 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1458 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); 1459 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1459 CX_TEST_ASSERT(list->collection.destructor_data == NULL); 1460 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
1460 CX_TEST_ASSERT(cxListSize(list) == 0); 1461 CX_TEST_ASSERT(cxListSize(list) == 0);
1466 } 1467 }
1467 cx_testing_allocator_destroy(&talloc); 1468 cx_testing_allocator_destroy(&talloc);
1468 } 1469 }
1469 1470
1470 CX_TEST(test_list_ll_create_simple) { 1471 CX_TEST(test_list_ll_create_simple) {
1471 CxList *list = cxLinkedListCreateSimple(sizeof(int)); 1472 CxList *list = cxLinkedListCreate(NULL, sizeof(int));
1472 CX_TEST_DO { 1473 CX_TEST_DO {
1473 CX_TEST_ASSERT(list != NULL); 1474 CX_TEST_ASSERT(list != NULL);
1474 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); 1475 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int));
1475 CX_TEST_ASSERT(list->collection.simple_destructor == NULL); 1476 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1476 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); 1477 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1482 } 1483 }
1483 cxListFree(list); 1484 cxListFree(list);
1484 } 1485 }
1485 1486
1486 CX_TEST(test_list_ll_create_simple_for_pointers) { 1487 CX_TEST(test_list_ll_create_simple_for_pointers) {
1487 CxList *list = cxLinkedListCreateSimple(CX_STORE_POINTERS); 1488 CxList *list = cxLinkedListCreate(NULL, CX_STORE_POINTERS);
1488 CX_TEST_DO { 1489 CX_TEST_DO {
1489 CX_TEST_ASSERT(list != NULL); 1490 CX_TEST_ASSERT(list != NULL);
1490 CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*)); 1491 CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*));
1491 CX_TEST_ASSERT(list->collection.simple_destructor == NULL); 1492 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1492 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); 1493 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1502 CX_TEST(test_list_arl_create) { 1503 CX_TEST(test_list_arl_create) {
1503 CxTestingAllocator talloc; 1504 CxTestingAllocator talloc;
1504 cx_testing_allocator_init(&talloc); 1505 cx_testing_allocator_init(&talloc);
1505 CxAllocator *alloc = &talloc.base; 1506 CxAllocator *alloc = &talloc.base;
1506 CX_TEST_DO { 1507 CX_TEST_DO {
1507 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); 1508 CxList *list = cxArrayListCreate(alloc, sizeof(int), 8);
1508 CX_TEST_ASSERT(list != NULL); 1509 CX_TEST_ASSERT(list != NULL);
1510 cxSetCompareFunc(list, cx_cmp_int);
1509 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); 1511 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int));
1510 CX_TEST_ASSERT(list->collection.simple_destructor == NULL); 1512 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1511 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); 1513 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1512 CX_TEST_ASSERT(list->collection.destructor_data == NULL); 1514 CX_TEST_ASSERT(list->collection.destructor_data == NULL);
1513 CX_TEST_ASSERT(cxListSize(list) == 0); 1515 CX_TEST_ASSERT(cxListSize(list) == 0);
1519 } 1521 }
1520 cx_testing_allocator_destroy(&talloc); 1522 cx_testing_allocator_destroy(&talloc);
1521 } 1523 }
1522 1524
1523 CX_TEST(test_list_arl_create_simple) { 1525 CX_TEST(test_list_arl_create_simple) {
1524 CxList *list = cxArrayListCreateSimple(sizeof(int), 8); 1526 CxList *list = cxArrayListCreate(NULL, sizeof(int), 8);
1525 CX_TEST_DO { 1527 CX_TEST_DO {
1526 CX_TEST_ASSERT(list != NULL); 1528 CX_TEST_ASSERT(list != NULL);
1527 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int)); 1529 CX_TEST_ASSERT(list->collection.elem_size == sizeof(int));
1528 CX_TEST_ASSERT(list->collection.simple_destructor == NULL); 1530 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1529 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); 1531 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1535 } 1537 }
1536 cxListFree(list); 1538 cxListFree(list);
1537 } 1539 }
1538 1540
1539 CX_TEST(test_list_arl_create_simple_for_pointers) { 1541 CX_TEST(test_list_arl_create_simple_for_pointers) {
1540 CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 8); 1542 CxList *list = cxArrayListCreate(NULL, CX_STORE_POINTERS, 8);
1541 CX_TEST_DO { 1543 CX_TEST_DO {
1542 CX_TEST_ASSERT(list != NULL); 1544 CX_TEST_ASSERT(list != NULL);
1543 CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*)); 1545 CX_TEST_ASSERT(list->collection.elem_size == sizeof(void*));
1544 CX_TEST_ASSERT(list->collection.simple_destructor == NULL); 1546 CX_TEST_ASSERT(list->collection.simple_destructor == NULL);
1545 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL); 1547 CX_TEST_ASSERT(list->collection.advanced_destructor == NULL);
1560 CxTestingAllocator talloc; 1562 CxTestingAllocator talloc;
1561 cx_testing_allocator_init(&talloc); 1563 cx_testing_allocator_init(&talloc);
1562 CxAllocator *alloc = &talloc.base; 1564 CxAllocator *alloc = &talloc.base;
1563 CX_TEST_DO { 1565 CX_TEST_DO {
1564 void *item = cxMalloc(alloc, sizeof(int)); 1566 void *item = cxMalloc(alloc, sizeof(int));
1565 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1567 CxList *list = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
1568 cxSetCompareFunc(list, cx_cmp_int);
1566 cxListAdd(list, item); 1569 cxListAdd(list, item);
1567 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1570 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1568 cxListFree(list); 1571 cxListFree(list);
1569 // item is not yet freed 1572 // item is not yet freed
1570 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1573 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1575 } 1578 }
1576 1579
1577 CX_TEST(test_list_pll_destroy_simple_destr) { 1580 CX_TEST(test_list_pll_destroy_simple_destr) {
1578 CX_TEST_DO { 1581 CX_TEST_DO {
1579 int item = 0; 1582 int item = 0;
1580 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1583 CxList *list = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
1584 cxSetCompareFunc(list, cx_cmp_int);
1581 list->collection.simple_destructor = test_fake_simple_int_destr; 1585 list->collection.simple_destructor = test_fake_simple_int_destr;
1582 cxListAdd(list, &item); 1586 cxListAdd(list, &item);
1583 cxListFree(list); 1587 cxListFree(list);
1584 CX_TEST_ASSERT(item == 42); 1588 CX_TEST_ASSERT(item == 42);
1585 } 1589 }
1589 CxTestingAllocator talloc; 1593 CxTestingAllocator talloc;
1590 cx_testing_allocator_init(&talloc); 1594 cx_testing_allocator_init(&talloc);
1591 CxAllocator *alloc = &talloc.base; 1595 CxAllocator *alloc = &talloc.base;
1592 CX_TEST_DO { 1596 CX_TEST_DO {
1593 void *item = cxMalloc(alloc, sizeof(int)); 1597 void *item = cxMalloc(alloc, sizeof(int));
1594 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 1598 CxList *list = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
1599 cxSetCompareFunc(list, cx_cmp_int);
1595 list->collection.destructor_data = alloc; 1600 list->collection.destructor_data = alloc;
1596 list->collection.advanced_destructor = (cx_destructor_func2) cxFree; 1601 list->collection.advanced_destructor = (cx_destructor_func2) cxFree;
1597 cxListAdd(list, item); 1602 cxListAdd(list, item);
1598 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1603 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1599 cxListFree(list); 1604 cxListFree(list);
1606 CxTestingAllocator talloc; 1611 CxTestingAllocator talloc;
1607 cx_testing_allocator_init(&talloc); 1612 cx_testing_allocator_init(&talloc);
1608 CxAllocator *alloc = &talloc.base; 1613 CxAllocator *alloc = &talloc.base;
1609 CX_TEST_DO { 1614 CX_TEST_DO {
1610 void *item = cxMalloc(alloc, sizeof(int)); 1615 void *item = cxMalloc(alloc, sizeof(int));
1611 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); 1616 CxList *list = cxArrayListCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4);
1617 cxSetCompareFunc(list, cx_cmp_int);
1612 cxListAdd(list, item); 1618 cxListAdd(list, item);
1613 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1619 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1614 cxListFree(list); 1620 cxListFree(list);
1615 // item is not yet freed 1621 // item is not yet freed
1616 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1622 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1621 } 1627 }
1622 1628
1623 CX_TEST(test_list_parl_destroy_simple_destr) { 1629 CX_TEST(test_list_parl_destroy_simple_destr) {
1624 CX_TEST_DO { 1630 CX_TEST_DO {
1625 int item = 0; 1631 int item = 0;
1626 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); 1632 CxList *list = cxArrayListCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4);
1633 cxSetCompareFunc(list, cx_cmp_int);
1627 list->collection.simple_destructor = test_fake_simple_int_destr; 1634 list->collection.simple_destructor = test_fake_simple_int_destr;
1628 cxListAdd(list, &item); 1635 cxListAdd(list, &item);
1629 cxListFree(list); 1636 cxListFree(list);
1630 CX_TEST_ASSERT(item == 42); 1637 CX_TEST_ASSERT(item == 42);
1631 } 1638 }
1635 CxTestingAllocator talloc; 1642 CxTestingAllocator talloc;
1636 cx_testing_allocator_init(&talloc); 1643 cx_testing_allocator_init(&talloc);
1637 CxAllocator *alloc = &talloc.base; 1644 CxAllocator *alloc = &talloc.base;
1638 CX_TEST_DO { 1645 CX_TEST_DO {
1639 void *item = cxMalloc(alloc, sizeof(int)); 1646 void *item = cxMalloc(alloc, sizeof(int));
1640 CxList *list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4); 1647 CxList *list = cxArrayListCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4);
1648 cxSetCompareFunc(list, cx_cmp_int);
1641 list->collection.destructor_data = alloc; 1649 list->collection.destructor_data = alloc;
1642 list->collection.advanced_destructor = (cx_destructor_func2) cxFree; 1650 list->collection.advanced_destructor = (cx_destructor_func2) cxFree;
1643 cxListAdd(list, item); 1651 cxListAdd(list, item);
1644 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1652 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1645 cxListFree(list); 1653 cxListFree(list);
1659 } \ 1667 } \
1660 cx_testing_allocator_destroy(&talloc); 1668 cx_testing_allocator_destroy(&talloc);
1661 #define roll_out_test_invokers(name) \ 1669 #define roll_out_test_invokers(name) \
1662 CX_TEST(test_list_ll_##name) { \ 1670 CX_TEST(test_list_ll_##name) { \
1663 set_up_combo \ 1671 set_up_combo \
1664 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ 1672 CxList *list = cxLinkedListCreate(alloc, sizeof(int)); \
1673 cxSetCompareFunc(list, cx_cmp_int); \
1665 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ 1674 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
1666 tear_down_combo \ 1675 tear_down_combo \
1667 } \ 1676 } \
1668 CX_TEST(test_list_arl_##name) { \ 1677 CX_TEST(test_list_arl_##name) { \
1669 set_up_combo \ 1678 set_up_combo \
1670 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \ 1679 CxList *list = cxArrayListCreate(alloc, sizeof(int), 8); \
1680 cxSetCompareFunc(list, cx_cmp_int); \
1671 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ 1681 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
1672 tear_down_combo \ 1682 tear_down_combo \
1673 } \ 1683 } \
1674 CX_TEST(test_list_kvl_##name) { \ 1684 CX_TEST(test_list_kvl_##name) { \
1675 set_up_combo \ 1685 set_up_combo \
1676 CxList *list = cxKvListCreate(alloc, cx_cmp_int, sizeof(int)); \ 1686 CxList *list = cxKvListCreate(alloc, sizeof(int)); \
1687 cxSetCompareFunc(list, cx_cmp_int); \
1677 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ 1688 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
1678 tear_down_combo \ 1689 tear_down_combo \
1679 } \ 1690 } \
1680 CX_TEST(test_list_pll_##name) { \ 1691 CX_TEST(test_list_pll_##name) { \
1681 set_up_combo \ 1692 set_up_combo \
1682 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ 1693 CxList *list = cxLinkedListCreate(alloc, CX_STORE_POINTERS); \
1694 cxSetCompareFunc(list, cx_cmp_int); \
1683 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ 1695 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1684 tear_down_combo \ 1696 tear_down_combo \
1685 } \ 1697 } \
1686 CX_TEST(test_list_parl_##name) { \ 1698 CX_TEST(test_list_parl_##name) { \
1687 set_up_combo \ 1699 set_up_combo \
1688 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ 1700 CxList *list = cxArrayListCreate(alloc, CX_STORE_POINTERS, 8); \
1701 cxSetCompareFunc(list, cx_cmp_int); \
1689 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ 1702 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1690 tear_down_combo \ 1703 tear_down_combo \
1691 } \ 1704 } \
1692 CX_TEST(test_list_pkvl_##name) { \ 1705 CX_TEST(test_list_pkvl_##name) { \
1693 set_up_combo \ 1706 set_up_combo \
1694 CxList *list = cxKvListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ 1707 CxList *list = cxKvListCreate(alloc, CX_STORE_POINTERS); \
1708 cxSetCompareFunc(list, cx_cmp_int); \
1695 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ 1709 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1696 tear_down_combo \ 1710 tear_down_combo \
1697 } 1711 }
1698 #define roll_out_test_combos(name, body) \ 1712 #define roll_out_test_combos(name, body) \
1699 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ 1713 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
1723 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \ 1737 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
1724 cx_attr_unused bool isptrlist) body \ 1738 cx_attr_unused bool isptrlist) body \
1725 roll_out_test_invokers(name) \ 1739 roll_out_test_invokers(name) \
1726 CX_TEST(test_list_llm_##name) { \ 1740 CX_TEST(test_list_llm_##name) { \
1727 set_up_combo \ 1741 set_up_combo \
1728 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \ 1742 CxList *list = cxLinkedListCreate(alloc, sizeof(int)); \
1743 cxSetCompareFunc(list, cx_cmp_int); \
1729 do_set_default_class_funcs(list); \ 1744 do_set_default_class_funcs(list); \
1730 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ 1745 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
1731 tear_down_combo \ 1746 tear_down_combo \
1732 } \ 1747 } \
1733 CX_TEST(test_list_arlm_##name) { \ 1748 CX_TEST(test_list_arlm_##name) { \
1734 set_up_combo \ 1749 set_up_combo \
1735 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \ 1750 CxList *list = cxArrayListCreate(alloc, sizeof(int), 8); \
1751 cxSetCompareFunc(list, cx_cmp_int); \
1736 do_set_default_class_funcs(list); \ 1752 do_set_default_class_funcs(list); \
1737 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \ 1753 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
1738 tear_down_combo \ 1754 tear_down_combo \
1739 } \ 1755 } \
1740 CX_TEST(test_list_pllm_##name) { \ 1756 CX_TEST(test_list_pllm_##name) { \
1741 set_up_combo \ 1757 set_up_combo \
1742 CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \ 1758 CxList *list = cxLinkedListCreate(alloc, CX_STORE_POINTERS); \
1759 cxSetCompareFunc(list, cx_cmp_int); \
1743 do_set_default_class_funcs(list); \ 1760 do_set_default_class_funcs(list); \
1744 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ 1761 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1745 tear_down_combo \ 1762 tear_down_combo \
1746 } \ 1763 } \
1747 CX_TEST(test_list_parlm_##name) { \ 1764 CX_TEST(test_list_parlm_##name) { \
1748 set_up_combo \ 1765 set_up_combo \
1749 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \ 1766 CxList *list = cxArrayListCreate(alloc, CX_STORE_POINTERS, 8); \
1767 cxSetCompareFunc(list, cx_cmp_int); \
1750 do_set_default_class_funcs(list); \ 1768 do_set_default_class_funcs(list); \
1751 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \ 1769 CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
1752 tear_down_combo \ 1770 tear_down_combo \
1753 } 1771 }
1754 1772
2219 } 2237 }
2220 2238
2221 roll_out_test_combos(remove_array, { 2239 roll_out_test_combos(remove_array, {
2222 const size_t testdata_len = 32; 2240 const size_t testdata_len = 32;
2223 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len); 2241 int *testdata = int_test_data_added_to_list(list, isptrlist, testdata_len);
2224 cxDefineDestructor(list, test_remove_array_destr); 2242 cxSetDestructor(list, test_remove_array_destr);
2225 test_remove_array_destr_ctr = 0; 2243 test_remove_array_destr_ctr = 0;
2226 2244
2227 // first, remove and get - no destructor must be called 2245 // first, remove and get - no destructor must be called
2228 int targete[8]; 2246 int targete[8];
2229 int *targetp[8]; 2247 int *targetp[8];
2441 CxTestingAllocator talloc; 2459 CxTestingAllocator talloc;
2442 cx_testing_allocator_init(&talloc); 2460 cx_testing_allocator_init(&talloc);
2443 CxAllocator *alloc = &talloc.base; 2461 CxAllocator *alloc = &talloc.base;
2444 CX_TEST_DO { 2462 CX_TEST_DO {
2445 size_t item_size = 2*cx_array_swap_sbo_size; 2463 size_t item_size = 2*cx_array_swap_sbo_size;
2446 CxList *list = cxArrayListCreate(alloc, cx_cmp_int, item_size, 8); 2464 CxList *list = cxArrayListCreate(alloc, item_size, 8);
2465 cxSetCompareFunc(list, cx_cmp_int);
2447 2466
2448 char *obj = malloc(item_size); 2467 char *obj = malloc(item_size);
2449 for (char c = 'a' ; c <= 'z' ; c++) { 2468 for (char c = 'a' ; c <= 'z' ; c++) {
2450 obj[0] = c; 2469 obj[0] = c;
2451 obj[item_size-1] = c; 2470 obj[item_size-1] = c;
2641 #define roll_out_compare_tests(suffix, otherctr) \ 2660 #define roll_out_compare_tests(suffix, otherctr) \
2642 roll_out_test_combos(compare_##suffix, { \ 2661 roll_out_test_combos(compare_##suffix, { \
2643 const size_t len = 47; \ 2662 const size_t len = 47; \
2644 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \ 2663 int *testdata = int_test_data_added_to_list(list, isptrlist, len); \
2645 CxList *other = otherctr; \ 2664 CxList *other = otherctr; \
2665 cxSetCompareFunc(other, cx_cmp_int); \
2646 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \ 2666 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); \
2647 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \ 2667 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); \
2648 cxListFree(other); \ 2668 cxListFree(other); \
2649 free(testdata); \ 2669 free(testdata); \
2650 }) 2670 })
2651 2671
2652 roll_out_compare_tests( 2672 roll_out_compare_tests(
2653 ll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)) 2673 ll, cxLinkedListCreate(cxDefaultAllocator, sizeof(int))
2654 ) 2674 )
2655 2675
2656 roll_out_compare_tests( 2676 roll_out_compare_tests(
2657 pll, cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS) 2677 pll, cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS)
2658 ) 2678 )
2659 2679
2660 roll_out_compare_tests( 2680 roll_out_compare_tests(
2661 arl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50) 2681 arl, cxArrayListCreate(cxDefaultAllocator, sizeof(int), 50)
2662 ) 2682 )
2663 2683
2664 roll_out_compare_tests( 2684 roll_out_compare_tests(
2665 parl, cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 50) 2685 parl, cxArrayListCreate(cxDefaultAllocator, CX_STORE_POINTERS, 50)
2666 ) 2686 )
2667 2687
2668 roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, { 2688 roll_out_test_combos_with_defaulted_funcs(compare_unoptimized, {
2669 const size_t len = 33; 2689 const size_t len = 33;
2670 int *testdata = int_test_data_added_to_list(list, isptrlist, len); 2690 int *testdata = int_test_data_added_to_list(list, isptrlist, len);
2671 CxList *other = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 50); 2691 CxList *other = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 50);
2692 cxSetCompareFunc(other, cx_cmp_int);
2672 do_set_default_class_funcs(other); 2693 do_set_default_class_funcs(other);
2673 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]); 2694 for (size_t i = 0; i < len; i++) cxListAdd(other, &testdata[i]);
2674 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other); 2695 CX_TEST_CALL_SUBROUTINE(test_list_verify_compare, list, other);
2675 cxListFree(other); 2696 cxListFree(other);
2676 free(testdata); 2697 free(testdata);
2736 } 2757 }
2737 2758
2738 roll_out_test_combos(simple_destr, { 2759 roll_out_test_combos(simple_destr, {
2739 const size_t len = 60; 2760 const size_t len = 60;
2740 int *testdata = int_test_data_added_to_list(list, isptrlist, len); 2761 int *testdata = int_test_data_added_to_list(list, isptrlist, len);
2741 cxDefineDestructor(list, simple_destr_test_fun); 2762 cxSetDestructor(list, simple_destr_test_fun);
2742 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); 2763 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len);
2743 free(testdata); 2764 free(testdata);
2744 }) 2765 })
2745 2766
2746 roll_out_test_combos(advanced_destr, { 2767 roll_out_test_combos(advanced_destr, {
2747 const size_t len = 75; 2768 const size_t len = 75;
2748 int *testdata = int_test_data_added_to_list(list, isptrlist, len); 2769 int *testdata = int_test_data_added_to_list(list, isptrlist, len);
2749 cxDefineAdvancedDestructor(list, advanced_destr_test_fun, NULL); 2770 cxSetAdvancedDestructor(list, advanced_destr_test_fun, NULL);
2750 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len); 2771 CX_TEST_CALL_SUBROUTINE(test_list_verify_destructor, list, testdata, len);
2751 free(testdata); 2772 free(testdata);
2752 }) 2773 })
2753 2774
2754 roll_out_test_combos(reserve_and_shrink, { 2775 roll_out_test_combos(reserve_and_shrink, {
2804 cx_testing_allocator_init(&talloc); 2825 cx_testing_allocator_init(&talloc);
2805 CxAllocator *testing_alloc = &talloc.base; 2826 CxAllocator *testing_alloc = &talloc.base;
2806 2827
2807 // register a destructor for the target list if it is storing pointers 2828 // register a destructor for the target list if it is storing pointers
2808 if (target_isptrlist) { 2829 if (target_isptrlist) {
2809 cxDefineAdvancedDestructor(target, cxFree, testing_alloc); 2830 cxSetAdvancedDestructor(target, cxFree, testing_alloc);
2810 } 2831 }
2811 2832
2812 // fill the source list 2833 // fill the source list
2813 int source_data[8] = array_init(1, 2, 3, 4, 5, 6, 7, 8); 2834 int source_data[8] = array_init(1, 2, 3, 4, 5, 6, 7, 8);
2814 for (unsigned i = 0 ; i < 8 ; i++) { 2835 for (unsigned i = 0 ; i < 8 ; i++) {
2851 cx_testing_allocator_init(&talloc); 2872 cx_testing_allocator_init(&talloc);
2852 CxAllocator *testing_alloc = &talloc.base; 2873 CxAllocator *testing_alloc = &talloc.base;
2853 2874
2854 // register a destructor for the target list if it is storing pointers 2875 // register a destructor for the target list if it is storing pointers
2855 if (target_isptrlist) { 2876 if (target_isptrlist) {
2856 cxDefineAdvancedDestructor(target, cxFree, testing_alloc); 2877 cxSetAdvancedDestructor(target, cxFree, testing_alloc);
2857 } 2878 }
2858 2879
2859 // fill the source list 2880 // fill the source list
2860 int source_data[8] = array_init(1, 2, 3, 4, 5, 6, 7, 8); 2881 int source_data[8] = array_init(1, 2, 3, 4, 5, 6, 7, 8);
2861 for (unsigned i = 0 ; i < 8 ; i++) { 2882 for (unsigned i = 0 ; i < 8 ; i++) {
2894 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 2915 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
2895 cx_testing_allocator_destroy(&talloc); 2916 cx_testing_allocator_destroy(&talloc);
2896 } 2917 }
2897 2918
2898 roll_out_test_combos(clone_into_arl, { 2919 roll_out_test_combos(clone_into_arl, {
2899 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); 2920 CxList *target = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 8);
2921 cxSetCompareFunc(target, cx_cmp_int);
2900 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, false); 2922 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, false);
2901 }) 2923 })
2902 2924
2903 roll_out_test_combos(clone_into_ll, { 2925 roll_out_test_combos(clone_into_ll, {
2904 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 2926 CxList *target = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
2927 cxSetCompareFunc(target, cx_cmp_int);
2905 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, false); 2928 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, false);
2906 }) 2929 })
2907 2930
2908 roll_out_test_combos(clone_into_parl, { 2931 roll_out_test_combos(clone_into_parl, {
2909 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 8); 2932 CxList *target = cxArrayListCreate(cxDefaultAllocator, CX_STORE_POINTERS, 8);
2933 cxSetCompareFunc(target, cx_cmp_int);
2910 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, true); 2934 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, true);
2911 }) 2935 })
2912 2936
2913 roll_out_test_combos(clone_into_pll, { 2937 roll_out_test_combos(clone_into_pll, {
2914 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2938 CxList *target = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
2939 cxSetCompareFunc(target, cx_cmp_int);
2915 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, true); 2940 CX_TEST_CALL_SUBROUTINE(verify_clone, target, list, true);
2916 }) 2941 })
2917 2942
2918 roll_out_test_combos(clone_alloc_fail_into_arl, { 2943 roll_out_test_combos(clone_alloc_fail_into_arl, {
2919 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); 2944 CxList *target = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 8);
2945 cxSetCompareFunc(target, cx_cmp_int);
2920 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, false); 2946 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, false);
2921 }) 2947 })
2922 2948
2923 roll_out_test_combos(clone_alloc_fail_into_ll, { 2949 roll_out_test_combos(clone_alloc_fail_into_ll, {
2924 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 2950 CxList *target = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
2951 cxSetCompareFunc(target, cx_cmp_int);
2925 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, false); 2952 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, false);
2926 }) 2953 })
2927 2954
2928 roll_out_test_combos(clone_alloc_fail_into_parl, { 2955 roll_out_test_combos(clone_alloc_fail_into_parl, {
2929 CxList *target = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 8); 2956 CxList *target = cxArrayListCreate(cxDefaultAllocator, CX_STORE_POINTERS, 8);
2957 cxSetCompareFunc(target, cx_cmp_int);
2930 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true); 2958 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true);
2931 }) 2959 })
2932 2960
2933 roll_out_test_combos(clone_alloc_fail_into_pll, { 2961 roll_out_test_combos(clone_alloc_fail_into_pll, {
2934 CxList *target = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2962 CxList *target = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
2963 cxSetCompareFunc(target, cx_cmp_int);
2935 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true); 2964 CX_TEST_CALL_SUBROUTINE(verify_clone_alloc_fail, target, list, true);
2936 }) 2965 })
2937 2966
2938 static CX_TEST_SUBROUTINE(verify_difference, bool sorted, bool alloc_fail) { 2967 static CX_TEST_SUBROUTINE(verify_difference, bool sorted, bool alloc_fail) {
2939 CxTestingAllocator talloc; 2968 CxTestingAllocator talloc;
2940 cx_testing_allocator_init(&talloc); 2969 cx_testing_allocator_init(&talloc);
2941 2970
2942 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 2971 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
2943 cxDefineAdvancedDestructor(dst, cxFree, &talloc); 2972 cxSetAdvancedDestructor(dst, cxFree, &talloc);
2944 CxList *minuend = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 2973 CxList *minuend = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
2945 CxList *subtrahend = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 2974 CxList *subtrahend = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
2975 cxSetCompareFunc(dst, cx_cmp_int);
2976 cxSetCompareFunc(minuend, cx_cmp_int);
2977 cxSetCompareFunc(subtrahend, cx_cmp_int);
2946 2978
2947 int dst_data[] = {47, 178, 176, 83}; 2979 int dst_data[] = {47, 178, 176, 83};
2948 int minuend_data[] = { 2980 int minuend_data[] = {
2949 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76, 2981 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76,
2950 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83 2982 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83
2983 if (alloc_fail) { 3015 if (alloc_fail) {
2984 test_clone_func_max_enabled = true; 3016 test_clone_func_max_enabled = true;
2985 test_clone_func_max_clones = 30; 3017 test_clone_func_max_clones = 30;
2986 expected_len = 34; 3018 expected_len = 34;
2987 } 3019 }
2988 CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len); 3020 CxList *expected = cxArrayListCreate(cxDefaultAllocator, sizeof(int), expected_len);
3021 cxSetCompareFunc(expected, cx_cmp_int);
2989 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len); 3022 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len);
2990 3023
2991 int result = cxListDifference(dst, minuend, subtrahend, test_clone_func, &talloc.base, NULL); 3024 int result = cxListDifference(dst, minuend, subtrahend, test_clone_func, &talloc.base, NULL);
2992 if (alloc_fail) { 3025 if (alloc_fail) {
2993 CX_TEST_ASSERT(result != 0); 3026 CX_TEST_ASSERT(result != 0);
3032 3065
3033 static CX_TEST_SUBROUTINE(verify_intersection, bool sorted, bool alloc_fail) { 3066 static CX_TEST_SUBROUTINE(verify_intersection, bool sorted, bool alloc_fail) {
3034 CxTestingAllocator talloc; 3067 CxTestingAllocator talloc;
3035 cx_testing_allocator_init(&talloc); 3068 cx_testing_allocator_init(&talloc);
3036 3069
3037 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 3070 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
3038 cxDefineAdvancedDestructor(dst, cxFree, &talloc); 3071 cxSetAdvancedDestructor(dst, cxFree, &talloc);
3039 CxList *src = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 3072 CxList *src = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
3040 CxList *other = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 3073 CxList *other = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
3074 cxSetCompareFunc(dst, cx_cmp_int);
3075 cxSetCompareFunc(src, cx_cmp_int);
3076 cxSetCompareFunc(other, cx_cmp_int);
3041 3077
3042 int dst_data[] = {47, 178, 176, 83}; 3078 int dst_data[] = {47, 178, 176, 83};
3043 int src_data[] = { 3079 int src_data[] = {
3044 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76, 3080 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76,
3045 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83 3081 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83
3076 if (alloc_fail) { 3112 if (alloc_fail) {
3077 test_clone_func_max_enabled = true; 3113 test_clone_func_max_enabled = true;
3078 test_clone_func_max_clones = 10; 3114 test_clone_func_max_clones = 10;
3079 expected_len = 14; 3115 expected_len = 14;
3080 } 3116 }
3081 CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len); 3117 CxList *expected = cxArrayListCreate(cxDefaultAllocator, sizeof(int), expected_len);
3118 cxSetCompareFunc(expected, cx_cmp_int);
3082 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len); 3119 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len);
3083 3120
3084 int result = cxListIntersection(dst, src, other, test_clone_func, &talloc.base, NULL); 3121 int result = cxListIntersection(dst, src, other, test_clone_func, &talloc.base, NULL);
3085 if (alloc_fail) { 3122 if (alloc_fail) {
3086 CX_TEST_ASSERT(result != 0); 3123 CX_TEST_ASSERT(result != 0);
3125 3162
3126 static CX_TEST_SUBROUTINE(verify_union, bool sorted, bool alloc_fail) { 3163 static CX_TEST_SUBROUTINE(verify_union, bool sorted, bool alloc_fail) {
3127 CxTestingAllocator talloc; 3164 CxTestingAllocator talloc;
3128 cx_testing_allocator_init(&talloc); 3165 cx_testing_allocator_init(&talloc);
3129 3166
3130 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 3167 CxList *dst = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
3131 cxDefineAdvancedDestructor(dst, cxFree, &talloc); 3168 cxSetCompareFunc(dst, cx_cmp_int);
3132 CxList *src = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 3169 cxSetAdvancedDestructor(dst, cxFree, &talloc);
3133 CxList *other = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 3170 CxList *src = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
3171 CxList *other = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
3172 cxSetCompareFunc(src, cx_cmp_int);
3173 cxSetCompareFunc(other, cx_cmp_int);
3134 3174
3135 int dst_data[] = {47, 178, 176, 83}; 3175 int dst_data[] = {47, 178, 176, 83};
3136 int src_data[] = { 3176 int src_data[] = {
3137 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76, 3177 153, 106, 171, 130, 74, 173, 150, 94, 27, 92, 70, 175, 200, 20, 29, 161, 88, 116, 71, 53, 199, 124, 32, 9, 76,
3138 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83 3178 151, 33, 51, 37, 65, 176, 49, 12, 162, 28, 85, 4, 177, 198, 54, 109, 188, 44, 77, 194, 63, 41, 129, 97, 83
3181 if (alloc_fail) { 3221 if (alloc_fail) {
3182 test_clone_func_max_enabled = true; 3222 test_clone_func_max_enabled = true;
3183 test_clone_func_max_clones = 66; 3223 test_clone_func_max_clones = 66;
3184 expected_len = 70; 3224 expected_len = 70;
3185 } 3225 }
3186 CxList *expected = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), expected_len); 3226 CxList *expected = cxArrayListCreate(cxDefaultAllocator, sizeof(int), expected_len);
3227 cxSetCompareFunc(expected, cx_cmp_int);
3187 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len); 3228 cxListAddArray(expected, sorted ? expected_sorted : expected_unsorted, expected_len);
3188 3229
3189 int result = cxListUnion(dst, src, other, test_clone_func, &talloc.base, NULL); 3230 int result = cxListUnion(dst, src, other, test_clone_func, &talloc.base, NULL);
3190 if (alloc_fail) { 3231 if (alloc_fail) {
3191 CX_TEST_ASSERT(result != 0); 3232 CX_TEST_ASSERT(result != 0);
3233 int a[] = {1, 2, 5, 8, 10}; 3274 int a[] = {1, 2, 5, 8, 10};
3234 int b[] = {1, 3, 5, 7, 9, 11}; 3275 int b[] = {1, 3, 5, 7, 9, 11};
3235 int c[] = {2, 4, 6, 8, 10, 12}; 3276 int c[] = {2, 4, 6, 8, 10, 12};
3236 int d[] = {4, 8, 12}; 3277 int d[] = {4, 8, 12};
3237 3278
3238 CxList *la = cxArrayListCreateSimple(sizeof(int), 8); 3279 CxList *la = cxArrayListCreate(NULL, sizeof(int), 8);
3239 cxCollectionCompareFunc(la, cx_cmp_int); 3280 cxSetCompareFunc(la, cx_cmp_int);
3240 cxListInsertSortedArray(la, a, cx_nmemb(a)); 3281 cxListInsertSortedArray(la, a, cx_nmemb(a));
3241 CxList *lb = cxArrayListCreateSimple(sizeof(int), 8); 3282 CxList *lb = cxArrayListCreate(NULL, sizeof(int), 8);
3242 cxCollectionCompareFunc(lb, cx_cmp_int); 3283 cxSetCompareFunc(lb, cx_cmp_int);
3243 cxListInsertSortedArray(lb, b, cx_nmemb(b)); 3284 cxListInsertSortedArray(lb, b, cx_nmemb(b));
3244 CxList *lc = cxArrayListCreateSimple(sizeof(int), 8); 3285 CxList *lc = cxArrayListCreate(NULL, sizeof(int), 8);
3245 cxCollectionCompareFunc(lc, cx_cmp_int); 3286 cxSetCompareFunc(lc, cx_cmp_int);
3246 cxListInsertSortedArray(lc, c, cx_nmemb(c)); 3287 cxListInsertSortedArray(lc, c, cx_nmemb(c));
3247 CxList *ld = cxArrayListCreateSimple(sizeof(int), 8); 3288 CxList *ld = cxArrayListCreate(NULL, sizeof(int), 8);
3248 cxCollectionCompareFunc(ld, cx_cmp_int); 3289 cxSetCompareFunc(ld, cx_cmp_int);
3249 cxListInsertSortedArray(ld, d, cx_nmemb(d)); 3290 cxListInsertSortedArray(ld, d, cx_nmemb(d));
3250 3291
3251 CxList *d1 = cxArrayListCreateSimple(sizeof(int), 8); 3292 CxList *d1 = cxArrayListCreate(NULL, sizeof(int), 8);
3252 cxCollectionCompareFunc(d1, cx_cmp_int); 3293 cxSetCompareFunc(d1, cx_cmp_int);
3253 CxList *d2 = cxArrayListCreateSimple(sizeof(int), 8); 3294 CxList *d2 = cxArrayListCreate(NULL, sizeof(int), 8);
3254 cxCollectionCompareFunc(d2, cx_cmp_int); 3295 cxSetCompareFunc(d2, cx_cmp_int);
3255 3296
3256 CX_TEST_DO { 3297 CX_TEST_DO {
3257 // clone a into d1 3298 // clone a into d1
3258 CX_TEST_ASSERT(0 == cxListCloneShallow(d1, la)); 3299 CX_TEST_ASSERT(0 == cxListCloneShallow(d1, la));
3259 CX_TEST_ASSERT(0 == cxListCompare(d1, la)); 3300 CX_TEST_ASSERT(0 == cxListCompare(d1, la));
3260 CX_TEST_ASSERT(cxCollectionSorted(d1)); 3301 CX_TEST_ASSERT(cxCollectionSorted(d1));
3261 3302
3262 // union of a (in d1) and b 3303 // union of a (in d1) and b
3263 CX_TEST_ASSERT(0 == cxListUnionShallow(d2, d1, lb)); 3304 CX_TEST_ASSERT(0 == cxListUnionShallow(d2, d1, lb));
3264 CX_TEST_ASSERT(cxCollectionSorted(d2)); 3305 CX_TEST_ASSERT(cxCollectionSorted(d2));
3265 CxList *expected_union = cxArrayListCreateSimple(sizeof(int), 8); 3306 CxList *expected_union = cxArrayListCreate(NULL, sizeof(int), 8);
3266 { 3307 {
3267 int expected[] = {1, 2, 3, 5, 7, 8, 9, 10, 11}; 3308 int expected[] = {1, 2, 3, 5, 7, 8, 9, 10, 11};
3268 cxListAddArray(expected_union, expected, cx_nmemb(expected)); 3309 cxListAddArray(expected_union, expected, cx_nmemb(expected));
3269 } 3310 }
3270 CX_TEST_ASSERT(0 == cxListCompare(d2, expected_union)); 3311 CX_TEST_ASSERT(0 == cxListCompare(d2, expected_union));
3272 3313
3273 // intersection of (a union b) and c 3314 // intersection of (a union b) and c
3274 cxListClear(d1); 3315 cxListClear(d1);
3275 CX_TEST_ASSERT(0 == cxListIntersectionShallow(d1, d2, lc)); 3316 CX_TEST_ASSERT(0 == cxListIntersectionShallow(d1, d2, lc));
3276 CX_TEST_ASSERT(cxCollectionSorted(d1)); 3317 CX_TEST_ASSERT(cxCollectionSorted(d1));
3277 CxList *expected_intersection = cxArrayListCreateSimple(sizeof(int), 8); 3318 CxList *expected_intersection = cxArrayListCreate(NULL, sizeof(int), 8);
3278 { 3319 {
3279 int expected[] = {2, 8, 10}; 3320 int expected[] = {2, 8, 10};
3280 cxListAddArray(expected_intersection, expected, cx_nmemb(expected)); 3321 cxListAddArray(expected_intersection, expected, cx_nmemb(expected));
3281 } 3322 }
3282 CX_TEST_ASSERT(0 == cxListCompare(d1, expected_intersection)); 3323 CX_TEST_ASSERT(0 == cxListCompare(d1, expected_intersection));
3284 3325
3285 // difference of ((a union b) intersect c) minus d 3326 // difference of ((a union b) intersect c) minus d
3286 cxListClear(d2); 3327 cxListClear(d2);
3287 CX_TEST_ASSERT(0 == cxListDifferenceShallow(d2, d1, ld)); 3328 CX_TEST_ASSERT(0 == cxListDifferenceShallow(d2, d1, ld));
3288 CX_TEST_ASSERT(cxCollectionSorted(d2)); 3329 CX_TEST_ASSERT(cxCollectionSorted(d2));
3289 CxList *expected_difference = cxArrayListCreateSimple(sizeof(int), 8); 3330 CxList *expected_difference = cxArrayListCreate(NULL, sizeof(int), 8);
3290 { 3331 {
3291 int expected[] = {2, 10}; 3332 int expected[] = {2, 10};
3292 cxListAddArray(expected_difference, expected, cx_nmemb(expected)); 3333 cxListAddArray(expected_difference, expected, cx_nmemb(expected));
3293 } 3334 }
3294 CX_TEST_ASSERT(0 == cxListCompare(d2, expected_difference)); 3335 CX_TEST_ASSERT(0 == cxListCompare(d2, expected_difference));
3302 cxListFree(lc); 3343 cxListFree(lc);
3303 cxListFree(ld); 3344 cxListFree(ld);
3304 } 3345 }
3305 3346
3306 CX_TEST(test_list_pointer_list_supports_null) { 3347 CX_TEST(test_list_pointer_list_supports_null) {
3307 CxList *list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 3348 CxList *list = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
3349 cxSetCompareFunc(list, cx_cmp_int);
3308 int x = 47; 3350 int x = 47;
3309 int y = 11; 3351 int y = 11;
3310 int z = 1337; 3352 int z = 1337;
3311 int *nptr = NULL; 3353 int *nptr = NULL;
3312 cxListAdd(list, &x); 3354 cxListAdd(list, &x);
3330 } 3372 }
3331 cxListFree(list); 3373 cxListFree(list);
3332 } 3374 }
3333 3375
3334 CX_TEST(test_list_use_insert_unique_to_remove_duplicates) { 3376 CX_TEST(test_list_use_insert_unique_to_remove_duplicates) {
3335 CxList *linked_list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 3377 CxList *linked_list = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
3336 CxList *array_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); 3378 CxList *array_list = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 8);
3337 CxList *defaulted_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); 3379 CxList *defaulted_list = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 8);
3380 cxSetCompareFunc(linked_list, cx_cmp_int);
3381 cxSetCompareFunc(array_list, cx_cmp_int);
3382 cxSetCompareFunc(defaulted_list, cx_cmp_int);
3338 do_set_default_class_funcs(defaulted_list); 3383 do_set_default_class_funcs(defaulted_list);
3339 int test_array[23] = { 3384 int test_array[23] = {
3340 120, -13, 100, -90, 13, -56, 74, 20, 28, 80, 18, -56, 130, 12, 15, 0, 39, 100, 0, 29, 28, 85, 20 3385 120, -13, 100, -90, 13, -56, 74, 20, 28, 80, 18, -56, 130, 12, 15, 0, 39, 100, 0, 29, 28, 85, 20
3341 }; 3386 };
3342 int test_array_unique[18] = { 3387 int test_array_unique[18] = {
3360 cxListFree(linked_list); 3405 cxListFree(linked_list);
3361 cxListFree(array_list); 3406 cxListFree(array_list);
3362 } 3407 }
3363 3408
3364 CX_TEST(test_list_use_insert_unique_with_duplicates_in_source) { 3409 CX_TEST(test_list_use_insert_unique_with_duplicates_in_source) {
3365 CxList *linked_list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int)); 3410 CxList *linked_list = cxLinkedListCreate(cxDefaultAllocator, sizeof(int));
3366 CxList *array_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); 3411 CxList *array_list = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 8);
3367 CxList *defaulted_list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int), 8); 3412 CxList *defaulted_list = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 8);
3413 cxSetCompareFunc(linked_list, cx_cmp_int);
3414 cxSetCompareFunc(array_list, cx_cmp_int);
3415 cxSetCompareFunc(defaulted_list, cx_cmp_int);
3368 do_set_default_class_funcs(defaulted_list); 3416 do_set_default_class_funcs(defaulted_list);
3369 int pre_filled[10] = {-13, 5, 18, 30, 40, 45, 50, 80, 85, 110}; 3417 int pre_filled[10] = {-13, 5, 18, 30, 40, 45, 50, 80, 85, 110};
3370 cxListInsertSortedArray(linked_list, pre_filled, 10); 3418 cxListInsertSortedArray(linked_list, pre_filled, 10);
3371 cxListInsertSortedArray(array_list, pre_filled, 10); 3419 cxListInsertSortedArray(array_list, pre_filled, 10);
3372 cxListInsertSortedArray(defaulted_list, pre_filled, 10); 3420 cxListInsertSortedArray(defaulted_list, pre_filled, 10);

mercurial