tests/test_tree.c

changeset 909
f4e00bb3f3b2
parent 908
f49f8a7060aa
child 911
db829e54539e
equal deleted inserted replaced
908:f49f8a7060aa 909:f4e00bb3f3b2
1717 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1717 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1718 } 1718 }
1719 cx_testing_allocator_destroy(&talloc); 1719 cx_testing_allocator_destroy(&talloc);
1720 } 1720 }
1721 1721
1722 static void test_tree_remove_node_relink_mock(
1723 void *node,
1724 __attribute__((__unused__)) const void *oldp,
1725 __attribute__((__unused__)) const void *newp
1726 ) {
1727 tree_node_file * n = node;
1728 // this function fakes the relink logic in below test
1729 if (strcmp(n->path, "/usr/share/") == 0) {
1730 n->path = "/share/";
1731 } else if (strcmp(n->path, "/usr/lib/") == 0) {
1732 n->path = "/lib/";
1733 }
1734 }
1735
1722 CX_TEST(test_tree_high_add_find_remove_nodes) { 1736 CX_TEST(test_tree_high_add_find_remove_nodes) {
1723 CxTestingAllocator talloc; 1737 CxTestingAllocator talloc;
1724 cx_testing_allocator_init(&talloc); 1738 cx_testing_allocator_init(&talloc);
1725 CxAllocator *alloc = &talloc.base; 1739 CxAllocator *alloc = &talloc.base;
1726 1740
1763 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path)); 1777 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path));
1764 CX_TEST_ASSERT(bar->parent == foo); 1778 CX_TEST_ASSERT(bar->parent == foo);
1765 1779
1766 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file)); 1780 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file));
1767 share->path = "/usr/share/"; 1781 share->path = "/usr/share/";
1768 cxTreeAddChildNode(tree, cxTreeFind(tree, "/usr/"), share); 1782 tree_node_file *usr = cxTreeFind(tree, "/usr/");
1783 cxTreeAddChildNode(tree, usr, share);
1769 CX_TEST_ASSERT(tree->size == 8); 1784 CX_TEST_ASSERT(tree->size == 8);
1770 1785
1771 cxTreeRemoveSubtree(tree, foo); 1786 cxTreeRemoveSubtree(tree, foo);
1787 CX_TEST_ASSERT(tree->size == 6);
1788 CX_TEST_ASSERT(foo->children == bar);
1789 CX_TEST_ASSERT(foo->last_child == bar);
1772 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/")); 1790 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/"));
1773 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/")); 1791 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/"));
1774 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/")); 1792 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/"));
1775 CX_TEST_ASSERT(tree->size == 6); 1793
1794 CX_TEST_ASSERT(0 == cxTreeRemove(tree, usr, test_tree_remove_node_relink_mock));
1795 CX_TEST_ASSERT(tree->size == 5);
1796 CX_TEST_ASSERT(usr->parent == NULL);
1797 CX_TEST_ASSERT(usr->children == NULL);
1798 CX_TEST_ASSERT(usr->last_child == NULL);
1799 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/"));
1800 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/lib/"));
1801 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/share/"));
1802 tree_node_file *relinked_share = cxTreeFind(tree, "/share/");
1803 tree_node_file *relinked_lib = cxTreeFind(tree, "/lib/");
1804 CX_TEST_ASSERT(relinked_share != NULL);
1805 CX_TEST_ASSERT(relinked_share->parent == tree->root);
1806 CX_TEST_ASSERT(relinked_lib != NULL);
1807 CX_TEST_ASSERT(relinked_lib->parent == tree->root);
1808 CX_TEST_ASSERT(NULL != cxTreeFind(tree, "/home/"));
1809
1776 1810
1777 cxTreeDestroy(tree); 1811 cxTreeDestroy(tree);
1778 // we are not done yet, because we need to free the removed subtree 1812 // we are not done yet, because we need to free the removed stuff
1779 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1813 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1780 1814
1781 // for this, we use a little trick and wrap the removed subtree 1815 cxFree(alloc, usr);
1816 // for the subtree, we use a little trick and wrap it in a new tree
1782 CxTree *foo_tree = cxTreeCreateWrapped(alloc, foo, tree_node_file_layout); 1817 CxTree *foo_tree = cxTreeCreateWrapped(alloc, foo, tree_node_file_layout);
1783 foo_tree->allocator = alloc; 1818 foo_tree->allocator = alloc;
1784 foo_tree->advanced_destructor = (cx_destructor_func2 ) cxFree; 1819 foo_tree->advanced_destructor = (cx_destructor_func2 ) cxFree;
1785 foo_tree->destructor_data = alloc; 1820 foo_tree->destructor_data = alloc;
1786 cxTreeDestroy(foo_tree); 1821 cxTreeDestroy(foo_tree);
1809 "/home/foo/", 1844 "/home/foo/",
1810 "/home/foo/bar/" 1845 "/home/foo/bar/"
1811 }; 1846 };
1812 cxTreeInsertArray(tree, paths, sizeof(const char*), 6); 1847 cxTreeInsertArray(tree, paths, sizeof(const char*), 6);
1813 void *root = tree->root; 1848 void *root = tree->root;
1849 CX_TEST_ASSERT(0 != cxTreeRemove(tree, root, NULL));
1850 CX_TEST_ASSERT(tree->root == root);
1851 CX_TEST_ASSERT(tree->size == 6);
1814 cxTreeRemoveSubtree(tree, root); 1852 cxTreeRemoveSubtree(tree, root);
1815 CX_TEST_ASSERT(tree->size == 0); 1853 CX_TEST_ASSERT(tree->size == 0);
1816 CX_TEST_ASSERT(tree->root == NULL); 1854 CX_TEST_ASSERT(tree->root == NULL);
1817 CX_TEST_ASSERT(cxTreeDepth(tree) == 0); 1855 CX_TEST_ASSERT(cxTreeDepth(tree) == 0);
1818 cxTreeDestroy(tree); 1856 cxTreeDestroy(tree);

mercurial