tests/test_tree.c

changeset 904
cdc49211d87f
parent 903
a018f5916d3b
child 905
39aa4f106a71
equal deleted inserted replaced
903:a018f5916d3b 904:cdc49211d87f
70 node->last_child = (void *) 0xf00ba5; 70 node->last_child = (void *) 0xf00ba5;
71 71
72 return node; 72 return node;
73 } 73 }
74 74
75 static void *tree_node_file_create_hl(
76 const void *dptr,
77 void *tree) {
78 return tree_node_file_create(dptr, (void*)((CxTree*)tree)->allocator);
79 }
80
75 static int tree_node_file_search(const void *l, const void *r) { 81 static int tree_node_file_search(const void *l, const void *r) {
76 const tree_node_file *left = l; 82 const tree_node_file *left = l;
77 const tree_node_file *right = r; 83 const tree_node_file *right = r;
78 size_t len1 = strlen(left->path); 84 size_t len1 = strlen(left->path);
79 size_t len2 = strlen(right->path); 85 size_t len2 = strlen(right->path);
1530 CxTestingAllocator talloc; 1536 CxTestingAllocator talloc;
1531 cx_testing_allocator_init(&talloc); 1537 cx_testing_allocator_init(&talloc);
1532 CX_TEST_DO { 1538 CX_TEST_DO {
1533 CxTree *tree = cxTreeCreate( 1539 CxTree *tree = cxTreeCreate(
1534 &talloc.base, 1540 &talloc.base,
1535 tree_node_file_create, 1541 tree_node_file_create_hl,
1536 tree_node_file_search, 1542 tree_node_file_search,
1537 tree_node_file_layout 1543 tree_node_file_layout
1538 ); 1544 );
1539 CX_TEST_ASSERT(tree->cl != NULL); 1545 CX_TEST_ASSERT(tree->cl != NULL);
1540 CX_TEST_ASSERT(tree->allocator == &talloc.base); 1546 CX_TEST_ASSERT(tree->allocator == &talloc.base);
1541 CX_TEST_ASSERT(tree->node_create == tree_node_file_create); 1547 CX_TEST_ASSERT(tree->node_create == tree_node_file_create_hl);
1542 CX_TEST_ASSERT(tree->search == tree_node_file_search); 1548 CX_TEST_ASSERT(tree->search == tree_node_file_search);
1543 CX_TEST_ASSERT(tree->size == 0); 1549 CX_TEST_ASSERT(tree->size == 0);
1544 CX_TEST_ASSERT(tree->simple_destructor == NULL); 1550 CX_TEST_ASSERT(tree->simple_destructor == NULL);
1545 CX_TEST_ASSERT(tree->advanced_destructor == (cx_destructor_func2) cxFree); 1551 CX_TEST_ASSERT(tree->advanced_destructor == (cx_destructor_func2) cxFree);
1546 CX_TEST_ASSERT(tree->destructor_data == &talloc.base); 1552 CX_TEST_ASSERT(tree->destructor_data == &talloc.base);
1561 } 1567 }
1562 1568
1563 CX_TEST(test_tree_high_create_simple) { 1569 CX_TEST(test_tree_high_create_simple) {
1564 CX_TEST_DO { 1570 CX_TEST_DO {
1565 CxTree *tree = cxTreeCreateSimple( 1571 CxTree *tree = cxTreeCreateSimple(
1566 tree_node_file_create, 1572 cxDefaultAllocator,
1573 tree_node_file_create_hl,
1567 tree_node_file_search 1574 tree_node_file_search
1568 ); 1575 );
1569 CX_TEST_ASSERT(tree->cl != NULL); 1576 CX_TEST_ASSERT(tree->cl != NULL);
1570 CX_TEST_ASSERT(tree->allocator == cxDefaultAllocator); 1577 CX_TEST_ASSERT(tree->allocator == cxDefaultAllocator);
1571 CX_TEST_ASSERT(tree->node_create == tree_node_file_create); 1578 CX_TEST_ASSERT(tree->node_create == tree_node_file_create_hl);
1572 CX_TEST_ASSERT(tree->search == tree_node_file_search); 1579 CX_TEST_ASSERT(tree->search == tree_node_file_search);
1573 CX_TEST_ASSERT(tree->size == 0); 1580 CX_TEST_ASSERT(tree->size == 0);
1574 CX_TEST_ASSERT(tree->simple_destructor == NULL); 1581 CX_TEST_ASSERT(tree->simple_destructor == NULL);
1575 CX_TEST_ASSERT(tree->advanced_destructor == (cx_destructor_func2) cxFree); 1582 CX_TEST_ASSERT(tree->advanced_destructor == (cx_destructor_func2) cxFree);
1576 CX_TEST_ASSERT(tree->destructor_data == cxDefaultAllocator); 1583 CX_TEST_ASSERT(tree->destructor_data == cxDefaultAllocator);
1620 CX_TEST_ASSERT(cxTreeSubtreeDepth(tree, &child1) == 2); 1627 CX_TEST_ASSERT(cxTreeSubtreeDepth(tree, &child1) == 2);
1621 CX_TEST_ASSERT(cxTreeSubtreeDepth(tree, &child2) == 1); 1628 CX_TEST_ASSERT(cxTreeSubtreeDepth(tree, &child2) == 1);
1622 CX_TEST_ASSERT(cxTreeSubtreeDepth(tree, &child3) == 1); 1629 CX_TEST_ASSERT(cxTreeSubtreeDepth(tree, &child3) == 1);
1623 } 1630 }
1624 cxTreeDestroy(tree); 1631 cxTreeDestroy(tree);
1632 }
1633
1634 CX_TEST(test_tree_high_insert_one) {
1635 CxTestingAllocator talloc;
1636 cx_testing_allocator_init(&talloc);
1637 CxAllocator *alloc = &talloc.base;
1638
1639 CX_TEST_DO {
1640 CxTree *tree = cxTreeCreate(
1641 alloc, tree_node_file_create_hl, tree_node_file_search,
1642 tree_node_file_layout
1643 );
1644
1645 int result = 0;
1646 result |= cxTreeInsert(tree, "/");
1647 result |= cxTreeInsert(tree, "/usr/");
1648 result |= cxTreeInsert(tree, "/home/");
1649 result |= cxTreeInsert(tree, "/usr/lib/");
1650 result |= cxTreeInsert(tree, "/home/foo/");
1651 CX_TEST_ASSERT(result == 0);
1652 CX_TEST_ASSERT(1 == cxTreeInsertArray(tree, "/home/foo/bar/", sizeof(const char*), 1));
1653 CX_TEST_ASSERT(tree->size == 6);
1654 CX_TEST_ASSERT(0 != cxTreeInsert(tree, "newroot"));
1655 CX_TEST_ASSERT(tree->size == 6);
1656
1657 CX_TEST_ASSERT(talloc.alloc_total == 8);
1658 CX_TEST_ASSERT(talloc.free_total == 1); // the one that could not be added
1659
1660 cxTreeDestroy(tree);
1661 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1662 }
1663 cx_testing_allocator_destroy(&talloc);
1664 }
1665
1666 CX_TEST(test_tree_high_insert_many) {
1667 CxTestingAllocator talloc;
1668 cx_testing_allocator_init(&talloc);
1669 CxAllocator *alloc = &talloc.base;
1670
1671 CX_TEST_DO {
1672 CxTree *tree = cxTreeCreate(
1673 alloc, tree_node_file_create_hl, tree_node_file_search,
1674 tree_node_file_layout
1675 );
1676
1677 const char *paths[] = {
1678 "/",
1679 "/usr/",
1680 "/home/",
1681 "/usr/lib/",
1682 "/home/foo/",
1683 "/home/foo/bar/",
1684 "newroot"
1685 };
1686 CX_TEST_ASSERT(6 == cxTreeInsertArray(tree, paths, sizeof(const char*), 7));
1687 CX_TEST_ASSERT(tree->size == 6);
1688
1689 CX_TEST_ASSERT(talloc.alloc_total == 8);
1690 CX_TEST_ASSERT(talloc.free_total == 1); // the one that could not be added
1691
1692 cxTreeDestroy(tree);
1693 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1694 }
1695 cx_testing_allocator_destroy(&talloc);
1625 } 1696 }
1626 1697
1627 CxTestSuite *cx_test_suite_tree_low_level(void) { 1698 CxTestSuite *cx_test_suite_tree_low_level(void) {
1628 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); 1699 CxTestSuite *suite = cx_test_suite_new("tree (low level)");
1629 1700
1665 1736
1666 cx_test_register(suite, test_tree_high_create); 1737 cx_test_register(suite, test_tree_high_create);
1667 cx_test_register(suite, test_tree_high_create_simple); 1738 cx_test_register(suite, test_tree_high_create_simple);
1668 cx_test_register(suite, test_tree_high_create_wrapped); 1739 cx_test_register(suite, test_tree_high_create_wrapped);
1669 cx_test_register(suite, test_tree_high_subtree_depth); 1740 cx_test_register(suite, test_tree_high_subtree_depth);
1741 cx_test_register(suite, test_tree_high_insert_one);
1742 cx_test_register(suite, test_tree_high_insert_many);
1670 1743
1671 return suite; 1744 return suite;
1672 } 1745 }

mercurial