Sun, 26 Sep 2021 16:12:25 +0200
Automated merge
1.1 --- a/src/tree.c Sun Sep 26 16:12:21 2021 +0200 1.2 +++ b/src/tree.c Sun Sep 26 16:12:25 2021 +0200 1.3 @@ -27,6 +27,7 @@ 1.4 */ 1.5 1.6 #include "cx/tree.h" 1.7 +#include "cx/linked_list.h" 1.8 1.9 #define CX_TR_PTR(cur, off) ((void**)(((char*)cur)+off)) 1.10 1.11 @@ -66,5 +67,12 @@ 1.12 void **children_end, 1.13 void *new_node) 1.14 { 1.15 - return 1; 1.16 + if(cx_linked_list_add(children_begin, children_end, loc_prev, loc_next, new_node)) { 1.17 + return 1; 1.18 + } 1.19 + // optional field 1.20 + if(loc_parent >= 0) { 1.21 + *CX_TR_PTR(new_node, loc_parent) = parent; 1.22 + } 1.23 + return 0; 1.24 }
2.1 --- a/test/test_tree.c Sun Sep 26 16:12:21 2021 +0200 2.2 +++ b/test/test_tree.c Sun Sep 26 16:12:25 2021 +0200 2.3 @@ -80,6 +80,85 @@ 2.4 CU_ASSERT_PTR_EQUAL(b.next, &c); 2.5 } 2.6 2.7 +void test_cx_tree_add_child_node() { 2.8 + // prepare test tree 2.9 + TestNode root; 2.10 + memset(&root, 0, sizeof(TestNode)); 2.11 + 2.12 + TestNode a; 2.13 + memset(&a, 0, sizeof(TestNode)); 2.14 + TestNode b; 2.15 + memset(&b, 0, sizeof(TestNode)); 2.16 + TestNode c; 2.17 + memset(&c, 0, sizeof(TestNode)); 2.18 + TestNode a1; 2.19 + memset(&a1, 0, sizeof(TestNode)); 2.20 + 2.21 + int ret; 2.22 + 2.23 + // test 2.24 + a.content = 1; 2.25 + ret = cx_tree_add_child_node( 2.26 + &root, 2.27 + offsetof(TestNode, parent), 2.28 + offsetof(TestNode, prev), 2.29 + offsetof(TestNode, next), 2.30 + (void**)&root.children_begin, 2.31 + (void**)&root.children_end, 2.32 + &a); 2.33 + CU_ASSERT_EQUAL(ret, 0); 2.34 + CU_ASSERT_PTR_EQUAL(root.children_begin, &a); 2.35 + CU_ASSERT_PTR_EQUAL(root.children_end, &a); 2.36 + CU_ASSERT_PTR_EQUAL(a.parent, &root); 2.37 + CU_ASSERT_PTR_EQUAL(a.prev, NULL); 2.38 + CU_ASSERT_PTR_EQUAL(a.next, NULL); 2.39 + 2.40 + b.content = 2; 2.41 + ret = cx_tree_add_child_node( 2.42 + &root, 2.43 + offsetof(TestNode, parent), 2.44 + offsetof(TestNode, prev), 2.45 + offsetof(TestNode, next), 2.46 + (void**)&root.children_begin, 2.47 + (void**)&root.children_end, 2.48 + &b); 2.49 + CU_ASSERT_EQUAL(ret, 0); 2.50 + CU_ASSERT_TRUE(root.children_begin ? root.children_begin->next == &b : 0); 2.51 + CU_ASSERT_PTR_EQUAL(root.children_end, &b); 2.52 + CU_ASSERT_PTR_EQUAL(b.parent, &root); 2.53 + CU_ASSERT_PTR_EQUAL(b.prev, &a); 2.54 + 2.55 + c.content = 3; 2.56 + ret = cx_tree_add_child_node( 2.57 + &root, 2.58 + -1, 2.59 + -1, 2.60 + offsetof(TestNode, next), 2.61 + (void**)&root.children_begin, 2.62 + NULL, 2.63 + &c); 2.64 + CU_ASSERT_EQUAL(ret, 0); 2.65 + CU_ASSERT_PTR_EQUAL(root.children_end, &b); // children_end unchanged 2.66 + CU_ASSERT_PTR_EQUAL(b.next, &c); 2.67 + CU_ASSERT_PTR_EQUAL(c.prev, NULL); 2.68 + CU_ASSERT_PTR_EQUAL(c.next, NULL); 2.69 + CU_ASSERT_PTR_EQUAL(c.parent, NULL); 2.70 + 2.71 + a1.content = 11; 2.72 + ret = cx_tree_add_child_node( 2.73 + &a, 2.74 + offsetof(TestNode, parent), 2.75 + offsetof(TestNode, prev), 2.76 + offsetof(TestNode, next), 2.77 + (void**)&a.children_begin, 2.78 + (void**)&a.children_end, 2.79 + &a1); 2.80 + CU_ASSERT_EQUAL(ret, 0); 2.81 + CU_ASSERT_PTR_EQUAL(a.children_begin, &a1); 2.82 + CU_ASSERT_PTR_EQUAL(a1.parent, &a); 2.83 + CU_ASSERT_TRUE(root.children_begin ? root.children_begin->children_begin == &a1 : 0); 2.84 +} 2.85 + 2.86 int main() { 2.87 CU_pSuite suite = NULL; 2.88 2.89 @@ -99,6 +178,12 @@ 2.90 CU_cleanup_registry(); 2.91 return CU_get_error(); 2.92 } 2.93 + if ( 2.94 + !CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node) 2.95 + ) { 2.96 + CU_cleanup_registry(); 2.97 + return CU_get_error(); 2.98 + } 2.99 2.100 2.101 CU_basic_set_mode(UCX_CU_BRM);