tests/test_tree.c

changeset 904
cdc49211d87f
parent 903
a018f5916d3b
child 905
39aa4f106a71
--- a/tests/test_tree.c	Thu Oct 03 15:42:35 2024 +0200
+++ b/tests/test_tree.c	Thu Oct 03 16:31:09 2024 +0200
@@ -72,6 +72,12 @@
     return node;
 }
 
+static void *tree_node_file_create_hl(
+        const void *dptr,
+        void *tree) {
+    return tree_node_file_create(dptr, (void*)((CxTree*)tree)->allocator);
+}
+
 static int tree_node_file_search(const void *l, const void *r) {
     const tree_node_file *left = l;
     const tree_node_file *right = r;
@@ -1532,13 +1538,13 @@
     CX_TEST_DO {
         CxTree *tree = cxTreeCreate(
                 &talloc.base,
-                tree_node_file_create,
+                tree_node_file_create_hl,
                 tree_node_file_search,
                 tree_node_file_layout
         );
         CX_TEST_ASSERT(tree->cl != NULL);
         CX_TEST_ASSERT(tree->allocator == &talloc.base);
-        CX_TEST_ASSERT(tree->node_create == tree_node_file_create);
+        CX_TEST_ASSERT(tree->node_create == tree_node_file_create_hl);
         CX_TEST_ASSERT(tree->search == tree_node_file_search);
         CX_TEST_ASSERT(tree->size == 0);
         CX_TEST_ASSERT(tree->simple_destructor == NULL);
@@ -1563,12 +1569,13 @@
 CX_TEST(test_tree_high_create_simple) {
     CX_TEST_DO {
         CxTree *tree = cxTreeCreateSimple(
-                tree_node_file_create,
+                cxDefaultAllocator,
+                tree_node_file_create_hl,
                 tree_node_file_search
         );
         CX_TEST_ASSERT(tree->cl != NULL);
         CX_TEST_ASSERT(tree->allocator == cxDefaultAllocator);
-        CX_TEST_ASSERT(tree->node_create == tree_node_file_create);
+        CX_TEST_ASSERT(tree->node_create == tree_node_file_create_hl);
         CX_TEST_ASSERT(tree->search == tree_node_file_search);
         CX_TEST_ASSERT(tree->size == 0);
         CX_TEST_ASSERT(tree->simple_destructor == NULL);
@@ -1624,6 +1631,70 @@
     cxTreeDestroy(tree);
 }
 
+CX_TEST(test_tree_high_insert_one) {
+    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_layout
+        );
+
+        int result = 0;
+        result |= cxTreeInsert(tree, "/");
+        result |= cxTreeInsert(tree, "/usr/");
+        result |= cxTreeInsert(tree, "/home/");
+        result |= cxTreeInsert(tree, "/usr/lib/");
+        result |= cxTreeInsert(tree, "/home/foo/");
+        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(1 == cxTreeInsertArray(tree, "/home/foo/bar/", sizeof(const char*), 1));
+        CX_TEST_ASSERT(tree->size == 6);
+        CX_TEST_ASSERT(0 != cxTreeInsert(tree, "newroot"));
+        CX_TEST_ASSERT(tree->size == 6);
+
+        CX_TEST_ASSERT(talloc.alloc_total == 8);
+        CX_TEST_ASSERT(talloc.free_total == 1); // the one that could not be added
+
+        cxTreeDestroy(tree);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
+CX_TEST(test_tree_high_insert_many) {
+    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_layout
+        );
+
+        const char *paths[] = {
+                "/",
+                "/usr/",
+                "/home/",
+                "/usr/lib/",
+                "/home/foo/",
+                "/home/foo/bar/",
+                "newroot"
+        };
+        CX_TEST_ASSERT(6 == cxTreeInsertArray(tree, paths, sizeof(const char*), 7));
+        CX_TEST_ASSERT(tree->size == 6);
+
+        CX_TEST_ASSERT(talloc.alloc_total == 8);
+        CX_TEST_ASSERT(talloc.free_total == 1); // the one that could not be added
+
+        cxTreeDestroy(tree);
+        CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
+    }
+    cx_testing_allocator_destroy(&talloc);
+}
+
 CxTestSuite *cx_test_suite_tree_low_level(void) {
     CxTestSuite *suite = cx_test_suite_new("tree (low level)");
 
@@ -1667,6 +1738,8 @@
     cx_test_register(suite, test_tree_high_create_simple);
     cx_test_register(suite, test_tree_high_create_wrapped);
     cx_test_register(suite, test_tree_high_subtree_depth);
+    cx_test_register(suite, test_tree_high_insert_one);
+    cx_test_register(suite, test_tree_high_insert_many);
 
     return suite;
 }

mercurial