add cx_tree_add_child_node tests

Sun, 26 Sep 2021 15:43:41 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 26 Sep 2021 15:43:41 +0200
changeset 430
d4d643def2ac
parent 428
da66264af8ad
child 431
dcf01bb852f4

add cx_tree_add_child_node tests

test/test_tree.c file | annotate | diff | comparison | revisions
     1.1 --- a/test/test_tree.c	Sun Sep 26 15:26:43 2021 +0200
     1.2 +++ b/test/test_tree.c	Sun Sep 26 15:43:41 2021 +0200
     1.3 @@ -80,6 +80,85 @@
     1.4      CU_ASSERT_PTR_EQUAL(b.next, &c);
     1.5  }
     1.6  
     1.7 +void test_cx_tree_add_child_node() {
     1.8 +    // prepare test tree
     1.9 +    TestNode root;
    1.10 +    memset(&root, 0, sizeof(TestNode));
    1.11 +    
    1.12 +    TestNode a;
    1.13 +    memset(&a, 0, sizeof(TestNode));
    1.14 +    TestNode b;
    1.15 +    memset(&b, 0, sizeof(TestNode));
    1.16 +    TestNode c;
    1.17 +    memset(&c, 0, sizeof(TestNode));
    1.18 +    TestNode a1;
    1.19 +    memset(&a1, 0, sizeof(TestNode));
    1.20 +    
    1.21 +    int ret;
    1.22 +    
    1.23 +    // test
    1.24 +    a.content = 1;
    1.25 +    ret = cx_tree_add_child_node(
    1.26 +            &root,
    1.27 +            offsetof(TestNode, parent),
    1.28 +            offsetof(TestNode, prev),
    1.29 +            offsetof(TestNode, next),
    1.30 +            (void**)&root.children_begin,
    1.31 +            (void**)&root.children_end,
    1.32 +            &a);
    1.33 +    CU_ASSERT_EQUAL(ret, 0);
    1.34 +    CU_ASSERT_PTR_EQUAL(root.children_begin, &a);
    1.35 +    CU_ASSERT_PTR_EQUAL(root.children_end, &a);
    1.36 +    CU_ASSERT_PTR_EQUAL(a.parent, &root);
    1.37 +    CU_ASSERT_PTR_EQUAL(a.prev, NULL);
    1.38 +    CU_ASSERT_PTR_EQUAL(a.next, NULL);
    1.39 +    
    1.40 +    b.content = 2;
    1.41 +    ret = cx_tree_add_child_node(
    1.42 +            &root,
    1.43 +            offsetof(TestNode, parent),
    1.44 +            offsetof(TestNode, prev),
    1.45 +            offsetof(TestNode, next),
    1.46 +            (void**)&root.children_begin,
    1.47 +            (void**)&root.children_end,
    1.48 +            &b);
    1.49 +    CU_ASSERT_EQUAL(ret, 0);
    1.50 +    CU_ASSERT_TRUE(root.children_begin ? root.children_begin->next == &b : 0);
    1.51 +    CU_ASSERT_PTR_EQUAL(root.children_end, &b);
    1.52 +    CU_ASSERT_PTR_EQUAL(b.parent, &root);
    1.53 +    CU_ASSERT_PTR_EQUAL(b.prev, &a);
    1.54 +    
    1.55 +    c.content = 3;
    1.56 +    ret = cx_tree_add_child_node(
    1.57 +            &root,
    1.58 +            -1,
    1.59 +            -1,
    1.60 +            offsetof(TestNode, next),
    1.61 +            (void**)&root.children_begin,
    1.62 +            NULL,
    1.63 +            &c);
    1.64 +    CU_ASSERT_EQUAL(ret, 0);
    1.65 +    CU_ASSERT_PTR_EQUAL(root.children_end, &b); // children_end unchanged
    1.66 +    CU_ASSERT_PTR_EQUAL(b.next, &c);
    1.67 +    CU_ASSERT_PTR_EQUAL(c.prev, NULL);
    1.68 +    CU_ASSERT_PTR_EQUAL(c.next, NULL);
    1.69 +    CU_ASSERT_PTR_EQUAL(c.parent, NULL);
    1.70 +    
    1.71 +    a1.content = 11;
    1.72 +    ret = cx_tree_add_child_node(
    1.73 +            &a,
    1.74 +            offsetof(TestNode, parent),
    1.75 +            offsetof(TestNode, prev),
    1.76 +            offsetof(TestNode, next),
    1.77 +            (void**)&a.children_begin,
    1.78 +            (void**)&a.children_end,
    1.79 +            &a1);
    1.80 +    CU_ASSERT_EQUAL(ret, 0);
    1.81 +    CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
    1.82 +    CU_ASSERT_PTR_EQUAL(a1.parent, &a);
    1.83 +    CU_ASSERT_TRUE(root.children_begin ? root.children_begin->children_begin == &a1 : 0);
    1.84 +}
    1.85 +
    1.86  int main() {
    1.87      CU_pSuite suite = NULL;
    1.88  
    1.89 @@ -99,6 +178,12 @@
    1.90          CU_cleanup_registry();
    1.91          return CU_get_error();
    1.92      }
    1.93 +    if (
    1.94 +            !CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node)
    1.95 +            ) {
    1.96 +        CU_cleanup_registry();
    1.97 +        return CU_get_error();
    1.98 +    }
    1.99      
   1.100      
   1.101      CU_basic_set_mode(UCX_CU_BRM);

mercurial