test/test_tree.c

Sun, 03 Oct 2021 10:43:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 10:43:31 +0200
changeset 449
68ad5750ba6b
parent 443
d6d8712e15bc
child 453
bb144d08cd44
permissions
-rw-r--r--

minor code changes

These changes do not affect program behavior.

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/tree.h"
    30 #include "test_config.h"
    32 #include <stddef.h>
    34 typedef struct TestNode TestNode;
    36 struct TestNode {
    37     TestNode *parent;
    38     TestNode *prev;
    39     TestNode *next;
    41     TestNode *children_begin;
    42     TestNode *children_end;
    43 };
    45 void test_cx_tree_add_node(void) {
    46     // prepare test tree
    47     TestNode root;
    48     memset(&root, 0, sizeof(TestNode));
    50     TestNode a;
    51     memset(&a, 0, sizeof(TestNode));
    52     root.children_begin = &a;
    53     root.children_end = &a;
    54     a.parent = &root;
    56     // new test nodes
    57     TestNode b;
    58     memset(&b, 0, sizeof(TestNode));
    59     TestNode c;
    60     memset(&c, 0, sizeof(TestNode));
    62     // test
    63     int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b);
    64     CU_ASSERT_EQUAL(ret, 0)
    65     CU_ASSERT_PTR_EQUAL(b.parent, &root)
    66     CU_ASSERT_PTR_EQUAL(b.prev, &a)
    67     CU_ASSERT_PTR_NULL(b.next)
    68     CU_ASSERT_PTR_EQUAL(a.next, &b)
    70     ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c);
    71     CU_ASSERT_EQUAL(ret, 0)
    72     CU_ASSERT_PTR_NULL(c.parent)
    73     CU_ASSERT_PTR_NULL(c.prev)
    74     CU_ASSERT_PTR_NULL(c.next)
    75     CU_ASSERT_PTR_EQUAL(b.next, &c)
    76 }
    78 void test_cx_tree_add_child_node(void) {
    79     // prepare test tree
    80     TestNode root;
    81     memset(&root, 0, sizeof(TestNode));
    83     TestNode a;
    84     memset(&a, 0, sizeof(TestNode));
    85     TestNode b;
    86     memset(&b, 0, sizeof(TestNode));
    87     TestNode c;
    88     memset(&c, 0, sizeof(TestNode));
    89     TestNode a1;
    90     memset(&a1, 0, sizeof(TestNode));
    92     int ret;
    94     // test
    95     ret = cx_tree_add_child_node(
    96             &root,
    97             offsetof(TestNode, parent),
    98             offsetof(TestNode, prev),
    99             offsetof(TestNode, next),
   100             (void**)&root.children_begin,
   101             (void**)&root.children_end,
   102             &a);
   103     CU_ASSERT_EQUAL(ret, 0)
   104     CU_ASSERT_PTR_EQUAL(root.children_begin, &a)
   105     CU_ASSERT_PTR_EQUAL(root.children_end, &a)
   106     CU_ASSERT_PTR_EQUAL(a.parent, &root)
   107     CU_ASSERT_PTR_NULL(a.prev)
   108     CU_ASSERT_PTR_NULL(a.next)
   110     ret = cx_tree_add_child_node(
   111             &root,
   112             offsetof(TestNode, parent),
   113             offsetof(TestNode, prev),
   114             offsetof(TestNode, next),
   115             (void**)&root.children_begin,
   116             (void**)&root.children_end,
   117             &b);
   118     CU_ASSERT_EQUAL(ret, 0)
   119     CU_ASSERT_PTR_NOT_NULL(root.children_begin)
   120     CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b)
   121     CU_ASSERT_PTR_EQUAL(root.children_end, &b)
   122     CU_ASSERT_PTR_EQUAL(b.parent, &root)
   123     CU_ASSERT_PTR_EQUAL(b.prev, &a)
   125     ret = cx_tree_add_child_node(
   126             &root,
   127             -1,
   128             -1,
   129             offsetof(TestNode, next),
   130             (void**)&root.children_begin,
   131             NULL,
   132             &c);
   133     CU_ASSERT_EQUAL(ret, 0)
   134     CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged
   135     CU_ASSERT_PTR_EQUAL(b.next, &c)
   136     CU_ASSERT_PTR_NULL(c.prev)
   137     CU_ASSERT_PTR_NULL(c.next)
   138     CU_ASSERT_PTR_NULL(c.parent)
   140     ret = cx_tree_add_child_node(
   141             &a,
   142             offsetof(TestNode, parent),
   143             offsetof(TestNode, prev),
   144             offsetof(TestNode, next),
   145             (void**)&a.children_begin,
   146             (void**)&a.children_end,
   147             &a1);
   148     CU_ASSERT_EQUAL(ret, 0);
   149     CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
   150     CU_ASSERT_PTR_EQUAL(a1.parent, &a);
   151     CU_ASSERT_PTR_NOT_NULL(root.children_begin)
   152     CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1)
   153 }
   155 int main() {
   156     CU_pSuite suite = NULL;
   158     if (CUE_SUCCESS != CU_initialize_registry()) {
   159         return CU_get_error();
   160     }
   162     suite = CU_add_suite("tree suite", NULL, NULL);
   164     CU_add_test(suite, "ll add tree node", test_cx_tree_add_node);
   165     CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node);
   168     CU_basic_set_mode(UCX_CU_BRM);
   170     int exitcode;
   171     if (CU_basic_run_tests()) {
   172         exitcode = CU_get_error();
   173     } else {
   174         exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   175     }
   176     CU_cleanup_registry();
   177     return exitcode;
   178 }

mercurial