test/test_tree.c

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 425
a75b808d653b
child 443
d6d8712e15bc
permissions
-rw-r--r--

add cx_tree_add_child_node tests

     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;
    44     int content;
    45 };
    47 void test_cx_tree_add_node() {
    48     // prepare test tree
    49     TestNode root;
    50     memset(&root, 0, sizeof(TestNode));
    52     TestNode a;
    53     memset(&a, 0, sizeof(TestNode));
    54     root.children_begin = &a;
    55     root.children_end = &a;
    56     a.parent = &root;
    57     a.content = 1;
    59     // new test nodes
    60     TestNode b;
    61     memset(&b, 0, sizeof(TestNode));
    62     TestNode c;
    63     memset(&c, 0, sizeof(TestNode));
    65     // test
    66     b.content = 2;
    67     int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b);
    68     CU_ASSERT_EQUAL(ret, 0);
    69     CU_ASSERT_PTR_EQUAL(b.parent, &root);
    70     CU_ASSERT_PTR_EQUAL(b.prev, &a);
    71     CU_ASSERT_PTR_EQUAL(b.next, NULL);
    72     CU_ASSERT_PTR_EQUAL(a.next, &b);
    74     c.content = 3;
    75     ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c);
    76     CU_ASSERT_EQUAL(ret, 0);
    77     CU_ASSERT_PTR_EQUAL(c.parent, NULL);
    78     CU_ASSERT_PTR_EQUAL(c.prev, NULL);
    79     CU_ASSERT_PTR_EQUAL(c.next, NULL);
    80     CU_ASSERT_PTR_EQUAL(b.next, &c);
    81 }
    83 void test_cx_tree_add_child_node() {
    84     // prepare test tree
    85     TestNode root;
    86     memset(&root, 0, sizeof(TestNode));
    88     TestNode a;
    89     memset(&a, 0, sizeof(TestNode));
    90     TestNode b;
    91     memset(&b, 0, sizeof(TestNode));
    92     TestNode c;
    93     memset(&c, 0, sizeof(TestNode));
    94     TestNode a1;
    95     memset(&a1, 0, sizeof(TestNode));
    97     int ret;
    99     // test
   100     a.content = 1;
   101     ret = cx_tree_add_child_node(
   102             &root,
   103             offsetof(TestNode, parent),
   104             offsetof(TestNode, prev),
   105             offsetof(TestNode, next),
   106             (void**)&root.children_begin,
   107             (void**)&root.children_end,
   108             &a);
   109     CU_ASSERT_EQUAL(ret, 0);
   110     CU_ASSERT_PTR_EQUAL(root.children_begin, &a);
   111     CU_ASSERT_PTR_EQUAL(root.children_end, &a);
   112     CU_ASSERT_PTR_EQUAL(a.parent, &root);
   113     CU_ASSERT_PTR_EQUAL(a.prev, NULL);
   114     CU_ASSERT_PTR_EQUAL(a.next, NULL);
   116     b.content = 2;
   117     ret = cx_tree_add_child_node(
   118             &root,
   119             offsetof(TestNode, parent),
   120             offsetof(TestNode, prev),
   121             offsetof(TestNode, next),
   122             (void**)&root.children_begin,
   123             (void**)&root.children_end,
   124             &b);
   125     CU_ASSERT_EQUAL(ret, 0);
   126     CU_ASSERT_TRUE(root.children_begin ? root.children_begin->next == &b : 0);
   127     CU_ASSERT_PTR_EQUAL(root.children_end, &b);
   128     CU_ASSERT_PTR_EQUAL(b.parent, &root);
   129     CU_ASSERT_PTR_EQUAL(b.prev, &a);
   131     c.content = 3;
   132     ret = cx_tree_add_child_node(
   133             &root,
   134             -1,
   135             -1,
   136             offsetof(TestNode, next),
   137             (void**)&root.children_begin,
   138             NULL,
   139             &c);
   140     CU_ASSERT_EQUAL(ret, 0);
   141     CU_ASSERT_PTR_EQUAL(root.children_end, &b); // children_end unchanged
   142     CU_ASSERT_PTR_EQUAL(b.next, &c);
   143     CU_ASSERT_PTR_EQUAL(c.prev, NULL);
   144     CU_ASSERT_PTR_EQUAL(c.next, NULL);
   145     CU_ASSERT_PTR_EQUAL(c.parent, NULL);
   147     a1.content = 11;
   148     ret = cx_tree_add_child_node(
   149             &a,
   150             offsetof(TestNode, parent),
   151             offsetof(TestNode, prev),
   152             offsetof(TestNode, next),
   153             (void**)&a.children_begin,
   154             (void**)&a.children_end,
   155             &a1);
   156     CU_ASSERT_EQUAL(ret, 0);
   157     CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
   158     CU_ASSERT_PTR_EQUAL(a1.parent, &a);
   159     CU_ASSERT_TRUE(root.children_begin ? root.children_begin->children_begin == &a1 : 0);
   160 }
   162 int main() {
   163     CU_pSuite suite = NULL;
   165     if (CUE_SUCCESS != CU_initialize_registry()) {
   166         return CU_get_error();
   167     }
   169     suite = CU_add_suite("tree suite", NULL, NULL);
   170     if (NULL == suite) {
   171         CU_cleanup_registry();
   172         return CU_get_error();
   173     }
   175     if (
   176             !CU_add_test(suite, "ll add tree node", test_cx_tree_add_node)
   177             ) {
   178         CU_cleanup_registry();
   179         return CU_get_error();
   180     }
   181     if (
   182             !CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node)
   183             ) {
   184         CU_cleanup_registry();
   185         return CU_get_error();
   186     }
   189     CU_basic_set_mode(UCX_CU_BRM);
   191     int exitcode;
   192     if (CU_basic_run_tests()) {
   193         exitcode = CU_get_error();
   194     } else {
   195         exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   196     }
   197     CU_cleanup_registry();
   198     return exitcode;
   199 }

mercurial