diff -r 9533fc293aea -r 72db8e42b95e tests/test_tree.c --- a/tests/test_tree.c Sun Oct 06 12:40:44 2024 +0200 +++ b/tests/test_tree.c Sun Oct 06 13:37:05 2024 +0200 @@ -1792,7 +1792,7 @@ CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/")); CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/")); - CX_TEST_ASSERT(0 == cxTreeRemove(tree, usr, test_tree_remove_node_relink_mock)); + CX_TEST_ASSERT(0 == cxTreeRemoveNode(tree, usr, test_tree_remove_node_relink_mock)); CX_TEST_ASSERT(tree->size == 5); CX_TEST_ASSERT(usr->parent == NULL); CX_TEST_ASSERT(usr->children == NULL); @@ -1825,6 +1825,83 @@ cx_testing_allocator_destroy(&talloc); } +CX_TEST(test_tree_high_add_find_destroy_nodes) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *alloc = &talloc.base; + + CX_TEST_DO { + CxTree *tree = cxTreeCreate( + alloc, tree_node_file_create_hl, + tree_node_file_search, tree_node_file_search_data, + tree_node_file_layout + ); + + const char *paths[] = { + "/", + "/usr/", + "/home/", + "/usr/lib/", + "/home/foo/", + "/home/foo/bar/" + }; + cxTreeInsertArray(tree, paths, sizeof(const char*), 6); + + tree_node_file *foo = cxTreeFind(tree, "/home/foo/"); + CX_TEST_ASSERT(NULL != foo); + CX_TEST_ASSERT(0 == strcmp("/home/foo/", foo->path)); + CX_TEST_ASSERT(NULL != foo->parent); + CX_TEST_ASSERT(0 == strcmp("/home/", foo->parent->path)); + CX_TEST_ASSERT(tree->size == 6); + + CX_TEST_ASSERT(0 == cxTreeAddChild(tree, foo->parent, "/home/baz/")); + tree_node_file *baz = cxTreeFind(tree, "/home/baz/"); + CX_TEST_ASSERT(NULL != baz); + CX_TEST_ASSERT(0 == strcmp("/home/baz/", baz->path)); + CX_TEST_ASSERT(NULL != baz->parent); + CX_TEST_ASSERT(0 == strcmp("/home/", baz->parent->path)); + CX_TEST_ASSERT(tree->size == 7); + + tree_node_file *home = cxTreeFind(tree, "/home/"); + CX_TEST_ASSERT(NULL == cxTreeFindInSubtree(tree, "/usr/", foo)); + tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home); + CX_TEST_ASSERT(NULL != bar); + CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path)); + CX_TEST_ASSERT(bar->parent == foo); + + tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file)); + share->path = "/usr/share/"; + tree_node_file *usr = cxTreeFind(tree, "/usr/"); + cxTreeAddChildNode(tree, usr, share); + CX_TEST_ASSERT(tree->size == 8); + + cxTreeDestroySubtree(tree, foo); + CX_TEST_ASSERT(tree->size == 6); + CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/")); + CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/foo/bar/")); + CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/home/bar/")); + + CX_TEST_ASSERT(0 == cxTreeDestroyNode(tree, usr, test_tree_remove_node_relink_mock)); + CX_TEST_ASSERT(tree->size == 5); + CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/")); + CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/lib/")); + CX_TEST_ASSERT(NULL == cxTreeFind(tree, "/usr/share/")); + tree_node_file *relinked_share = cxTreeFind(tree, "/share/"); + tree_node_file *relinked_lib = cxTreeFind(tree, "/lib/"); + CX_TEST_ASSERT(relinked_share != NULL); + CX_TEST_ASSERT(relinked_share->parent == tree->root); + CX_TEST_ASSERT(relinked_lib != NULL); + CX_TEST_ASSERT(relinked_lib->parent == tree->root); + CX_TEST_ASSERT(NULL != cxTreeFind(tree, "/home/")); + + + cxTreeDestroy(tree); + // all memory should be free when using destroy instead of remove + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + CX_TEST(test_tree_high_remove_root) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); @@ -1847,7 +1924,7 @@ }; cxTreeInsertArray(tree, paths, sizeof(const char*), 6); void *root = tree->root; - CX_TEST_ASSERT(0 != cxTreeRemove(tree, root, NULL)); + CX_TEST_ASSERT(0 != cxTreeRemoveNode(tree, root, NULL)); CX_TEST_ASSERT(tree->root == root); CX_TEST_ASSERT(tree->size == 6); cxTreeRemoveSubtree(tree, root); @@ -1931,6 +2008,7 @@ cx_test_register(suite, test_tree_high_insert_one); cx_test_register(suite, test_tree_high_insert_many); cx_test_register(suite, test_tree_high_add_find_remove_nodes); + cx_test_register(suite, test_tree_high_add_find_destroy_nodes); cx_test_register(suite, test_tree_high_remove_root); cx_test_register(suite, test_tree_high_simple_destructor);