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.

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
universe@449 45 void test_cx_tree_add_node(void) {
olaf@425 46 // prepare test tree
olaf@425 47 TestNode root;
olaf@425 48 memset(&root, 0, sizeof(TestNode));
olaf@425 49
olaf@425 50 TestNode a;
olaf@425 51 memset(&a, 0, sizeof(TestNode));
olaf@425 52 root.children_begin = &a;
olaf@425 53 root.children_end = &a;
olaf@425 54 a.parent = &root;
universe@449 55
olaf@425 56 // new test nodes
olaf@425 57 TestNode b;
olaf@425 58 memset(&b, 0, sizeof(TestNode));
olaf@425 59 TestNode c;
olaf@425 60 memset(&c, 0, sizeof(TestNode));
olaf@425 61
olaf@425 62 // test
olaf@425 63 int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b);
universe@449 64 CU_ASSERT_EQUAL(ret, 0)
universe@449 65 CU_ASSERT_PTR_EQUAL(b.parent, &root)
universe@449 66 CU_ASSERT_PTR_EQUAL(b.prev, &a)
universe@449 67 CU_ASSERT_PTR_NULL(b.next)
universe@449 68 CU_ASSERT_PTR_EQUAL(a.next, &b)
olaf@425 69
olaf@425 70 ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c);
universe@449 71 CU_ASSERT_EQUAL(ret, 0)
universe@449 72 CU_ASSERT_PTR_NULL(c.parent)
universe@449 73 CU_ASSERT_PTR_NULL(c.prev)
universe@449 74 CU_ASSERT_PTR_NULL(c.next)
universe@449 75 CU_ASSERT_PTR_EQUAL(b.next, &c)
olaf@425 76 }
olaf@425 77
universe@449 78 void test_cx_tree_add_child_node(void) {
olaf@430 79 // prepare test tree
olaf@430 80 TestNode root;
olaf@430 81 memset(&root, 0, sizeof(TestNode));
olaf@430 82
olaf@430 83 TestNode a;
olaf@430 84 memset(&a, 0, sizeof(TestNode));
olaf@430 85 TestNode b;
olaf@430 86 memset(&b, 0, sizeof(TestNode));
olaf@430 87 TestNode c;
olaf@430 88 memset(&c, 0, sizeof(TestNode));
olaf@430 89 TestNode a1;
olaf@430 90 memset(&a1, 0, sizeof(TestNode));
olaf@430 91
olaf@430 92 int ret;
olaf@430 93
olaf@430 94 // test
olaf@430 95 ret = cx_tree_add_child_node(
olaf@430 96 &root,
olaf@430 97 offsetof(TestNode, parent),
olaf@430 98 offsetof(TestNode, prev),
olaf@430 99 offsetof(TestNode, next),
olaf@430 100 (void**)&root.children_begin,
olaf@430 101 (void**)&root.children_end,
olaf@430 102 &a);
universe@449 103 CU_ASSERT_EQUAL(ret, 0)
universe@449 104 CU_ASSERT_PTR_EQUAL(root.children_begin, &a)
universe@449 105 CU_ASSERT_PTR_EQUAL(root.children_end, &a)
universe@449 106 CU_ASSERT_PTR_EQUAL(a.parent, &root)
universe@449 107 CU_ASSERT_PTR_NULL(a.prev)
universe@449 108 CU_ASSERT_PTR_NULL(a.next)
universe@449 109
olaf@430 110 ret = cx_tree_add_child_node(
olaf@430 111 &root,
olaf@430 112 offsetof(TestNode, parent),
olaf@430 113 offsetof(TestNode, prev),
olaf@430 114 offsetof(TestNode, next),
olaf@430 115 (void**)&root.children_begin,
olaf@430 116 (void**)&root.children_end,
olaf@430 117 &b);
universe@449 118 CU_ASSERT_EQUAL(ret, 0)
universe@449 119 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
universe@449 120 CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b)
universe@449 121 CU_ASSERT_PTR_EQUAL(root.children_end, &b)
universe@449 122 CU_ASSERT_PTR_EQUAL(b.parent, &root)
universe@449 123 CU_ASSERT_PTR_EQUAL(b.prev, &a)
olaf@430 124
olaf@430 125 ret = cx_tree_add_child_node(
olaf@430 126 &root,
olaf@430 127 -1,
olaf@430 128 -1,
olaf@430 129 offsetof(TestNode, next),
olaf@430 130 (void**)&root.children_begin,
olaf@430 131 NULL,
olaf@430 132 &c);
universe@449 133 CU_ASSERT_EQUAL(ret, 0)
universe@449 134 CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged
universe@449 135 CU_ASSERT_PTR_EQUAL(b.next, &c)
universe@449 136 CU_ASSERT_PTR_NULL(c.prev)
universe@449 137 CU_ASSERT_PTR_NULL(c.next)
universe@449 138 CU_ASSERT_PTR_NULL(c.parent)
olaf@430 139
olaf@430 140 ret = cx_tree_add_child_node(
olaf@430 141 &a,
olaf@430 142 offsetof(TestNode, parent),
olaf@430 143 offsetof(TestNode, prev),
olaf@430 144 offsetof(TestNode, next),
olaf@430 145 (void**)&a.children_begin,
olaf@430 146 (void**)&a.children_end,
olaf@430 147 &a1);
olaf@430 148 CU_ASSERT_EQUAL(ret, 0);
olaf@430 149 CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
olaf@430 150 CU_ASSERT_PTR_EQUAL(a1.parent, &a);
universe@449 151 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
universe@449 152 CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1)
olaf@430 153 }
olaf@430 154
olaf@425 155 int main() {
olaf@425 156 CU_pSuite suite = NULL;
olaf@425 157
olaf@425 158 if (CUE_SUCCESS != CU_initialize_registry()) {
olaf@425 159 return CU_get_error();
olaf@425 160 }
olaf@425 161
olaf@425 162 suite = CU_add_suite("tree suite", NULL, NULL);
olaf@425 163
olaf@443 164 CU_add_test(suite, "ll add tree node", test_cx_tree_add_node);
olaf@443 165 CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node);
olaf@425 166
olaf@425 167
olaf@425 168 CU_basic_set_mode(UCX_CU_BRM);
olaf@425 169
olaf@425 170 int exitcode;
olaf@425 171 if (CU_basic_run_tests()) {
olaf@425 172 exitcode = CU_get_error();
olaf@425 173 } else {
olaf@425 174 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
olaf@425 175 }
olaf@425 176 CU_cleanup_registry();
olaf@425 177 return exitcode;
olaf@425 178 }

mercurial