add cx_tree_add_node test

Sun, 26 Sep 2021 14:21:20 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 26 Sep 2021 14:21:20 +0200
changeset 425
a75b808d653b
parent 424
2d6f6cb24132
child 426
9aa38cd4c992

add cx_tree_add_node test

src/tree.c file | annotate | diff | comparison | revisions
test/CMakeLists.txt file | annotate | diff | comparison | revisions
test/test_tree.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/tree.c	Sun Sep 26 13:34:30 2021 +0200
     1.2 +++ b/src/tree.c	Sun Sep 26 14:21:20 2021 +0200
     1.3 @@ -27,3 +27,20 @@
     1.4   */
     1.5  
     1.6  #include "cx/tree.h"
     1.7 +
     1.8 +
     1.9 +int cx_tree_add_node(void *node, ptrdiff_t loc_parent, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
    1.10 +    return 1;
    1.11 +}
    1.12 +
    1.13 +int cx_tree_add_child_node(
    1.14 +        void *parent,
    1.15 +        ptrdiff_t loc_parent,
    1.16 +        ptrdiff_t loc_prev,
    1.17 +        ptrdiff_t loc_next,
    1.18 +        void **children_begin,
    1.19 +        void **children_end,
    1.20 +        void *new_node)
    1.21 +{
    1.22 +    return 1;
    1.23 +}
     2.1 --- a/test/CMakeLists.txt	Sun Sep 26 13:34:30 2021 +0200
     2.2 +++ b/test/CMakeLists.txt	Sun Sep 26 14:21:20 2021 +0200
     2.3 @@ -10,6 +10,7 @@
     2.4      set(TESTS
     2.5              test_allocator
     2.6              test_list
     2.7 +            test_tree
     2.8      )
     2.9  
    2.10      foreach(test ${TESTS})
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/test_tree.c	Sun Sep 26 14:21:20 2021 +0200
     3.3 @@ -0,0 +1,114 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +
    3.32 +#include "cx/tree.h"
    3.33 +#include "test_config.h"
    3.34 +
    3.35 +#include <stddef.h>
    3.36 +
    3.37 +typedef struct TestNode TestNode;
    3.38 +
    3.39 +struct TestNode {
    3.40 +    TestNode *parent;
    3.41 +    TestNode *prev;
    3.42 +    TestNode *next;
    3.43 +    
    3.44 +    TestNode *children_begin;
    3.45 +    TestNode *children_end;
    3.46 +    
    3.47 +    int content;
    3.48 +};
    3.49 +
    3.50 +void test_cx_tree_add_node() {
    3.51 +    // prepare test tree
    3.52 +    TestNode root;
    3.53 +    memset(&root, 0, sizeof(TestNode));
    3.54 +    
    3.55 +    TestNode a;
    3.56 +    memset(&a, 0, sizeof(TestNode));
    3.57 +    root.children_begin = &a;
    3.58 +    root.children_end = &a;
    3.59 +    a.parent = &root;
    3.60 +    a.content = 1;
    3.61 +    
    3.62 +    // new test nodes
    3.63 +    TestNode b;
    3.64 +    memset(&b, 0, sizeof(TestNode));
    3.65 +    TestNode c;
    3.66 +    memset(&c, 0, sizeof(TestNode));
    3.67 +    
    3.68 +    // test
    3.69 +    b.content = 2;
    3.70 +    int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b);
    3.71 +    CU_ASSERT_EQUAL(ret, 0);
    3.72 +    CU_ASSERT_PTR_EQUAL(b.parent, &root);
    3.73 +    CU_ASSERT_PTR_EQUAL(b.prev, &a);
    3.74 +    CU_ASSERT_PTR_EQUAL(b.next, NULL);
    3.75 +    CU_ASSERT_PTR_EQUAL(a.next, &b);
    3.76 +    
    3.77 +    c.content = 3;
    3.78 +    ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c);
    3.79 +    CU_ASSERT_EQUAL(ret, 0);
    3.80 +    CU_ASSERT_PTR_EQUAL(c.parent, NULL);
    3.81 +    CU_ASSERT_PTR_EQUAL(c.prev, NULL);
    3.82 +    CU_ASSERT_PTR_EQUAL(c.next, NULL);
    3.83 +    CU_ASSERT_PTR_EQUAL(b.next, &c);
    3.84 +}
    3.85 +
    3.86 +int main() {
    3.87 +    CU_pSuite suite = NULL;
    3.88 +
    3.89 +    if (CUE_SUCCESS != CU_initialize_registry()) {
    3.90 +        return CU_get_error();
    3.91 +    }
    3.92 +
    3.93 +    suite = CU_add_suite("tree suite", NULL, NULL);
    3.94 +    if (NULL == suite) {
    3.95 +        CU_cleanup_registry();
    3.96 +        return CU_get_error();
    3.97 +    }
    3.98 +
    3.99 +    if (
   3.100 +            !CU_add_test(suite, "ll add tree node", test_cx_tree_add_node)
   3.101 +            ) {
   3.102 +        CU_cleanup_registry();
   3.103 +        return CU_get_error();
   3.104 +    }
   3.105 +    
   3.106 +    
   3.107 +    CU_basic_set_mode(UCX_CU_BRM);
   3.108 +
   3.109 +    int exitcode;
   3.110 +    if (CU_basic_run_tests()) {
   3.111 +        exitcode = CU_get_error();
   3.112 +    } else {
   3.113 +        exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   3.114 +    }
   3.115 +    CU_cleanup_registry();
   3.116 +    return exitcode;
   3.117 +}

mercurial