test/test_tree.c

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
child 430
d4d643def2ac
permissions
-rw-r--r--

add cx_tree_add_node test

     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 int main() {
    84     CU_pSuite suite = NULL;
    86     if (CUE_SUCCESS != CU_initialize_registry()) {
    87         return CU_get_error();
    88     }
    90     suite = CU_add_suite("tree suite", NULL, NULL);
    91     if (NULL == suite) {
    92         CU_cleanup_registry();
    93         return CU_get_error();
    94     }
    96     if (
    97             !CU_add_test(suite, "ll add tree node", test_cx_tree_add_node)
    98             ) {
    99         CU_cleanup_registry();
   100         return CU_get_error();
   101     }
   104     CU_basic_set_mode(UCX_CU_BRM);
   106     int exitcode;
   107     if (CU_basic_run_tests()) {
   108         exitcode = CU_get_error();
   109     } else {
   110         exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
   111     }
   112     CU_cleanup_registry();
   113     return exitcode;
   114 }

mercurial