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

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

mercurial