diff -r 192a440b99df -r 6540096c17b7 tests/test_tree.c --- a/tests/test_tree.c Sun Oct 13 16:47:14 2024 +0200 +++ b/tests/test_tree.c Sat Oct 19 13:08:06 2024 +0200 @@ -429,44 +429,165 @@ CX_TEST_DO { for (unsigned i = 0 ; i <= 10 ; i++) { s = testdata[i]; - r = cx_tree_search_data(&root, &s, test_tree_search_function, + r = cx_tree_search_data(&root, 0, &s, test_tree_search_function, (void **) &n, tree_children(tree_node)); CX_TEST_ASSERT(r == 0); CX_TEST_ASSERT(n == testnodes[i]); } s = -5; - r = cx_tree_search_data(&root, &s, test_tree_search_function, + r = cx_tree_search_data(&root, 0, &s, test_tree_search_function, (void **) &n, tree_children(tree_node)); CX_TEST_ASSERT(r < 0); CX_TEST_ASSERT(n == NULL); s = 26; - r = cx_tree_search_data(&root, &s, test_tree_search_function, + r = cx_tree_search_data(&root, 0, &s, test_tree_search_function, (void **) &n, tree_children(tree_node)); CX_TEST_ASSERT(r > 0); CX_TEST_ASSERT(n == &ba); s = 35; - r = cx_tree_search_data(&root, &s, test_tree_search_function, + r = cx_tree_search_data(&root, 0, &s, test_tree_search_function, (void **) &n, tree_children(tree_node)); CX_TEST_ASSERT(r > 0); CX_TEST_ASSERT(n == &cb); s = 38; - r = cx_tree_search_data(&root, &s, test_tree_search_function, + r = cx_tree_search_data(&root, 0, &s, test_tree_search_function, (void **) &n, tree_children(tree_node)); CX_TEST_ASSERT(r > 0); CX_TEST_ASSERT(n == &cba); s = 42; - r = cx_tree_search_data(&root, &s, test_tree_search_function, + r = cx_tree_search_data(&root, 0, &s, test_tree_search_function, (void **) &n, tree_children(tree_node)); CX_TEST_ASSERT(r > 0); CX_TEST_ASSERT(n == &cc); } } +CX_TEST(test_tree_search_with_max_depth) { + tree_node_file root = {0}; + root.path = "/"; + tree_node_file usr = {0}; + usr.path = "/usr/"; + cx_tree_link(&root, &usr, tree_node_file_layout); + tree_node_file home = {0}; + home.path = "/home/"; + cx_tree_link(&root, &home, tree_node_file_layout); + tree_node_file doe = {0}; + doe.path = "/home/doe/"; + cx_tree_link(&home, &doe, tree_node_file_layout); + tree_node_file lib = {0}; + lib.path = "/usr/lib/"; + cx_tree_link(&usr, &lib, tree_node_file_layout); + tree_node_file modules = {0}; + modules.path = "/usr/lib/modules/"; + cx_tree_link(&lib, &modules, tree_node_file_layout); + + CX_TEST_DO { + int result; + void *found; + + result = cx_tree_search_data( + &root, 1, "/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERTM(result == 0, "root not found"); + CX_TEST_ASSERT(found == &root); + + result = cx_tree_search_data( + &root, 1, "/usr/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERT(result > 0); + CX_TEST_ASSERT(found == &root); + + result = cx_tree_search_data( + &root, 2, "/usr/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERT(result == 0); + CX_TEST_ASSERT(found == &usr); + + result = cx_tree_search_data( + &root, 2, "/usr/lib/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERT(result > 0); + CX_TEST_ASSERT(found == &usr); + + result = cx_tree_search_data( + &root, 3, "/usr/lib/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERTM(result == 0, "lib not found"); + CX_TEST_ASSERT(found == &lib); + + result = cx_tree_search_data( + &root, 1, "/home/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERT(result > 0); + CX_TEST_ASSERT(found == &root); + + result = cx_tree_search_data( + &root, 2, "/home/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERTM(result == 0, "home not found"); + CX_TEST_ASSERT(found == &home); + + result = cx_tree_search_data( + &root, 2, "/home/doe/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERT(result > 0); + CX_TEST_ASSERT(found == &home); + + result = cx_tree_search_data( + &root, 3, "/home/doe/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERTM(result == 0, "doe not found"); + CX_TEST_ASSERT(found == &doe); + + result = cx_tree_search_data( + &root, 3, "/usr/lib/modules/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERT(result > 0); + CX_TEST_ASSERT(found == &lib); + + result = cx_tree_search_data( + &root, 4, "/usr/lib/modules/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERTM(result == 0, "modules not found (start=root)"); + CX_TEST_ASSERT(found == &modules); + + result = cx_tree_search_data( + &usr, 3, "/usr/lib/modules/", + tree_node_file_search_data, &found, + tree_children(tree_node_file) + ); + CX_TEST_ASSERTM(result == 0, "modules not found (start=usr)"); + CX_TEST_ASSERT(found == &modules); + } +} + CX_TEST(test_tree_iterator_create_and_dispose) { tree_node root; CX_TEST_DO { @@ -1825,8 +1946,8 @@ 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 == cxTreeFindInSubtree(tree, "/usr/", foo, 0)); + tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home, 0); CX_TEST_ASSERT(NULL != bar); CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path)); CX_TEST_ASSERT(bar->parent == foo); @@ -1916,8 +2037,8 @@ 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 == cxTreeFindInSubtree(tree, "/usr/", foo, 0)); + tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home, 0); CX_TEST_ASSERT(NULL != bar); CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path)); CX_TEST_ASSERT(bar->parent == foo); @@ -2032,6 +2153,7 @@ cx_test_register(suite, test_tree2_link_move_to_other_parent); cx_test_register(suite, test_tree2_unlink); cx_test_register(suite, test_tree_search); + cx_test_register(suite, test_tree_search_with_max_depth); cx_test_register(suite, test_tree_iterator_create_and_dispose); cx_test_register(suite, test_tree_iterator_create_for_empty_tree); cx_test_register(suite, test_tree_iterator_basic_only_enter);