Automated merge

Sun, 26 Sep 2021 16:12:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 16:12:25 +0200
changeset 432
32679add2a2b
parent 429
3d8235c96a27 (current diff)
parent 431
dcf01bb852f4 (diff)
child 433
f1e4c6dabfb4

Automated merge

--- a/src/tree.c	Sun Sep 26 16:12:21 2021 +0200
+++ b/src/tree.c	Sun Sep 26 16:12:25 2021 +0200
@@ -27,6 +27,7 @@
  */
 
 #include "cx/tree.h"
+#include "cx/linked_list.h"
 
 #define CX_TR_PTR(cur, off) ((void**)(((char*)cur)+off))
 
@@ -66,5 +67,12 @@
         void **children_end,
         void *new_node)
 {
-    return 1;
+    if(cx_linked_list_add(children_begin, children_end, loc_prev, loc_next, new_node)) {
+        return 1;
+    }
+    // optional field
+    if(loc_parent >= 0) {
+        *CX_TR_PTR(new_node, loc_parent) = parent;
+    }
+    return 0;
 }
--- a/test/test_tree.c	Sun Sep 26 16:12:21 2021 +0200
+++ b/test/test_tree.c	Sun Sep 26 16:12:25 2021 +0200
@@ -80,6 +80,85 @@
     CU_ASSERT_PTR_EQUAL(b.next, &c);
 }
 
+void test_cx_tree_add_child_node() {
+    // prepare test tree
+    TestNode root;
+    memset(&root, 0, sizeof(TestNode));
+    
+    TestNode a;
+    memset(&a, 0, sizeof(TestNode));
+    TestNode b;
+    memset(&b, 0, sizeof(TestNode));
+    TestNode c;
+    memset(&c, 0, sizeof(TestNode));
+    TestNode a1;
+    memset(&a1, 0, sizeof(TestNode));
+    
+    int ret;
+    
+    // test
+    a.content = 1;
+    ret = cx_tree_add_child_node(
+            &root,
+            offsetof(TestNode, parent),
+            offsetof(TestNode, prev),
+            offsetof(TestNode, next),
+            (void**)&root.children_begin,
+            (void**)&root.children_end,
+            &a);
+    CU_ASSERT_EQUAL(ret, 0);
+    CU_ASSERT_PTR_EQUAL(root.children_begin, &a);
+    CU_ASSERT_PTR_EQUAL(root.children_end, &a);
+    CU_ASSERT_PTR_EQUAL(a.parent, &root);
+    CU_ASSERT_PTR_EQUAL(a.prev, NULL);
+    CU_ASSERT_PTR_EQUAL(a.next, NULL);
+    
+    b.content = 2;
+    ret = cx_tree_add_child_node(
+            &root,
+            offsetof(TestNode, parent),
+            offsetof(TestNode, prev),
+            offsetof(TestNode, next),
+            (void**)&root.children_begin,
+            (void**)&root.children_end,
+            &b);
+    CU_ASSERT_EQUAL(ret, 0);
+    CU_ASSERT_TRUE(root.children_begin ? root.children_begin->next == &b : 0);
+    CU_ASSERT_PTR_EQUAL(root.children_end, &b);
+    CU_ASSERT_PTR_EQUAL(b.parent, &root);
+    CU_ASSERT_PTR_EQUAL(b.prev, &a);
+    
+    c.content = 3;
+    ret = cx_tree_add_child_node(
+            &root,
+            -1,
+            -1,
+            offsetof(TestNode, next),
+            (void**)&root.children_begin,
+            NULL,
+            &c);
+    CU_ASSERT_EQUAL(ret, 0);
+    CU_ASSERT_PTR_EQUAL(root.children_end, &b); // children_end unchanged
+    CU_ASSERT_PTR_EQUAL(b.next, &c);
+    CU_ASSERT_PTR_EQUAL(c.prev, NULL);
+    CU_ASSERT_PTR_EQUAL(c.next, NULL);
+    CU_ASSERT_PTR_EQUAL(c.parent, NULL);
+    
+    a1.content = 11;
+    ret = cx_tree_add_child_node(
+            &a,
+            offsetof(TestNode, parent),
+            offsetof(TestNode, prev),
+            offsetof(TestNode, next),
+            (void**)&a.children_begin,
+            (void**)&a.children_end,
+            &a1);
+    CU_ASSERT_EQUAL(ret, 0);
+    CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
+    CU_ASSERT_PTR_EQUAL(a1.parent, &a);
+    CU_ASSERT_TRUE(root.children_begin ? root.children_begin->children_begin == &a1 : 0);
+}
+
 int main() {
     CU_pSuite suite = NULL;
 
@@ -99,6 +178,12 @@
         CU_cleanup_registry();
         return CU_get_error();
     }
+    if (
+            !CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node)
+            ) {
+        CU_cleanup_registry();
+        return CU_get_error();
+    }
     
     
     CU_basic_set_mode(UCX_CU_BRM);

mercurial