tests/test_tree.c

changeset 913
72db8e42b95e
parent 911
db829e54539e
child 915
23db9f0c1acd
equal deleted inserted replaced
912:9533fc293aea 913:72db8e42b95e
1790 CX_TEST_ASSERT(foo->last_child == bar); 1790 CX_TEST_ASSERT(foo->last_child == bar);
1791 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/")); 1791 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/"));
1792 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/")); 1792 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/"));
1793 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/")); 1793 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/"));
1794 1794
1795 CX_TEST_ASSERT(0 == cxTreeRemove(tree, usr, test_tree_remove_node_relink_mock)); 1795 CX_TEST_ASSERT(0 == cxTreeRemoveNode(tree, usr, test_tree_remove_node_relink_mock));
1796 CX_TEST_ASSERT(tree->size == 5); 1796 CX_TEST_ASSERT(tree->size == 5);
1797 CX_TEST_ASSERT(usr->parent == NULL); 1797 CX_TEST_ASSERT(usr->parent == NULL);
1798 CX_TEST_ASSERT(usr->children == NULL); 1798 CX_TEST_ASSERT(usr->children == NULL);
1799 CX_TEST_ASSERT(usr->last_child == NULL); 1799 CX_TEST_ASSERT(usr->last_child == NULL);
1800 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/")); 1800 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/"));
1823 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1823 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1824 } 1824 }
1825 cx_testing_allocator_destroy(&talloc); 1825 cx_testing_allocator_destroy(&talloc);
1826 } 1826 }
1827 1827
1828 CX_TEST(test_tree_high_add_find_destroy_nodes) {
1829 CxTestingAllocator talloc;
1830 cx_testing_allocator_init(&talloc);
1831 CxAllocator *alloc = &talloc.base;
1832
1833 CX_TEST_DO {
1834 CxTree *tree = cxTreeCreate(
1835 alloc, tree_node_file_create_hl,
1836 tree_node_file_search, tree_node_file_search_data,
1837 tree_node_file_layout
1838 );
1839
1840 const char *paths[] = {
1841 "/",
1842 "/usr/",
1843 "/home/",
1844 "/usr/lib/",
1845 "/home/foo/",
1846 "/home/foo/bar/"
1847 };
1848 cxTreeInsertArray(tree, paths, sizeof(const char*), 6);
1849
1850 tree_node_file *foo = cxTreeFind(tree, "/home/foo/");
1851 CX_TEST_ASSERT(NULL != foo);
1852 CX_TEST_ASSERT(0 == strcmp("/home/foo/", foo->path));
1853 CX_TEST_ASSERT(NULL != foo->parent);
1854 CX_TEST_ASSERT(0 == strcmp("/home/", foo->parent->path));
1855 CX_TEST_ASSERT(tree->size == 6);
1856
1857 CX_TEST_ASSERT(0 == cxTreeAddChild(tree, foo->parent, "/home/baz/"));
1858 tree_node_file *baz = cxTreeFind(tree, "/home/baz/");
1859 CX_TEST_ASSERT(NULL != baz);
1860 CX_TEST_ASSERT(0 == strcmp("/home/baz/", baz->path));
1861 CX_TEST_ASSERT(NULL != baz->parent);
1862 CX_TEST_ASSERT(0 == strcmp("/home/", baz->parent->path));
1863 CX_TEST_ASSERT(tree->size == 7);
1864
1865 tree_node_file *home = cxTreeFind(tree, "/home/");
1866 CX_TEST_ASSERT(NULL == cxTreeFindInSubtree(tree, "/usr/", foo));
1867 tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home);
1868 CX_TEST_ASSERT(NULL != bar);
1869 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path));
1870 CX_TEST_ASSERT(bar->parent == foo);
1871
1872 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file));
1873 share->path = "/usr/share/";
1874 tree_node_file *usr = cxTreeFind(tree, "/usr/");
1875 cxTreeAddChildNode(tree, usr, share);
1876 CX_TEST_ASSERT(tree->size == 8);
1877
1878 cxTreeDestroySubtree(tree, foo);
1879 CX_TEST_ASSERT(tree->size == 6);
1880 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/"));
1881 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/"));
1882 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/"));
1883
1884 CX_TEST_ASSERT(0 == cxTreeDestroyNode(tree, usr, test_tree_remove_node_relink_mock));
1885 CX_TEST_ASSERT(tree->size == 5);
1886 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/"));
1887 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/lib/"));
1888 CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/share/"));
1889 tree_node_file *relinked_share = cxTreeFind(tree, "/share/");
1890 tree_node_file *relinked_lib = cxTreeFind(tree, "/lib/");
1891 CX_TEST_ASSERT(relinked_share != NULL);
1892 CX_TEST_ASSERT(relinked_share->parent == tree->root);
1893 CX_TEST_ASSERT(relinked_lib != NULL);
1894 CX_TEST_ASSERT(relinked_lib->parent == tree->root);
1895 CX_TEST_ASSERT(NULL != cxTreeFind(tree, "/home/"));
1896
1897
1898 cxTreeDestroy(tree);
1899 // all memory should be free when using destroy instead of remove
1900 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1901 }
1902 cx_testing_allocator_destroy(&talloc);
1903 }
1904
1828 CX_TEST(test_tree_high_remove_root) { 1905 CX_TEST(test_tree_high_remove_root) {
1829 CxTestingAllocator talloc; 1906 CxTestingAllocator talloc;
1830 cx_testing_allocator_init(&talloc); 1907 cx_testing_allocator_init(&talloc);
1831 CxAllocator *alloc = &talloc.base; 1908 CxAllocator *alloc = &talloc.base;
1832 1909
1845 "/home/foo/", 1922 "/home/foo/",
1846 "/home/foo/bar/" 1923 "/home/foo/bar/"
1847 }; 1924 };
1848 cxTreeInsertArray(tree, paths, sizeof(const char*), 6); 1925 cxTreeInsertArray(tree, paths, sizeof(const char*), 6);
1849 void *root = tree->root; 1926 void *root = tree->root;
1850 CX_TEST_ASSERT(0 != cxTreeRemove(tree, root, NULL)); 1927 CX_TEST_ASSERT(0 != cxTreeRemoveNode(tree, root, NULL));
1851 CX_TEST_ASSERT(tree->root == root); 1928 CX_TEST_ASSERT(tree->root == root);
1852 CX_TEST_ASSERT(tree->size == 6); 1929 CX_TEST_ASSERT(tree->size == 6);
1853 cxTreeRemoveSubtree(tree, root); 1930 cxTreeRemoveSubtree(tree, root);
1854 CX_TEST_ASSERT(tree->size == 0); 1931 CX_TEST_ASSERT(tree->size == 0);
1855 CX_TEST_ASSERT(tree->root == NULL); 1932 CX_TEST_ASSERT(tree->root == NULL);
1929 cx_test_register(suite, test_tree_high_create_wrapped); 2006 cx_test_register(suite, test_tree_high_create_wrapped);
1930 cx_test_register(suite, test_tree_high_tree_depth); 2007 cx_test_register(suite, test_tree_high_tree_depth);
1931 cx_test_register(suite, test_tree_high_insert_one); 2008 cx_test_register(suite, test_tree_high_insert_one);
1932 cx_test_register(suite, test_tree_high_insert_many); 2009 cx_test_register(suite, test_tree_high_insert_many);
1933 cx_test_register(suite, test_tree_high_add_find_remove_nodes); 2010 cx_test_register(suite, test_tree_high_add_find_remove_nodes);
2011 cx_test_register(suite, test_tree_high_add_find_destroy_nodes);
1934 cx_test_register(suite, test_tree_high_remove_root); 2012 cx_test_register(suite, test_tree_high_remove_root);
1935 cx_test_register(suite, test_tree_high_simple_destructor); 2013 cx_test_register(suite, test_tree_high_simple_destructor);
1936 2014
1937 return suite; 2015 return suite;
1938 } 2016 }

mercurial