test/test_tree.c

Sun, 03 Oct 2021 16:02:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 16:02:53 +0200
changeset 455
8168e16cd1e9
parent 453
bb144d08cd44
permissions
-rw-r--r--

change test names

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@455 45 void test_tree_add_sibling(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
universe@453 63 cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b);
universe@449 64 CU_ASSERT_PTR_EQUAL(b.parent, &root)
universe@449 65 CU_ASSERT_PTR_EQUAL(b.prev, &a)
universe@449 66 CU_ASSERT_PTR_NULL(b.next)
universe@449 67 CU_ASSERT_PTR_EQUAL(a.next, &b)
universe@453 68
universe@453 69 cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c);
universe@449 70 CU_ASSERT_PTR_NULL(c.parent)
universe@449 71 CU_ASSERT_PTR_NULL(c.prev)
universe@449 72 CU_ASSERT_PTR_NULL(c.next)
universe@449 73 CU_ASSERT_PTR_EQUAL(b.next, &c)
olaf@425 74 }
olaf@425 75
universe@455 76 void test_tree_add_child(void) {
olaf@430 77 // prepare test tree
olaf@430 78 TestNode root;
olaf@430 79 memset(&root, 0, sizeof(TestNode));
olaf@430 80
olaf@430 81 TestNode a;
olaf@430 82 memset(&a, 0, sizeof(TestNode));
olaf@430 83 TestNode b;
olaf@430 84 memset(&b, 0, sizeof(TestNode));
olaf@430 85 TestNode c;
olaf@430 86 memset(&c, 0, sizeof(TestNode));
olaf@430 87 TestNode a1;
olaf@430 88 memset(&a1, 0, sizeof(TestNode));
olaf@430 89
olaf@430 90 // test
universe@453 91 cx_tree_add_child(
universe@453 92 (void **) &root.children_begin,
universe@453 93 (void **) &root.children_end,
olaf@430 94 offsetof(TestNode, prev),
olaf@430 95 offsetof(TestNode, next),
universe@453 96 &a,
universe@453 97 offsetof(TestNode, parent),
universe@453 98 &root);
universe@449 99 CU_ASSERT_PTR_EQUAL(root.children_begin, &a)
universe@449 100 CU_ASSERT_PTR_EQUAL(root.children_end, &a)
universe@449 101 CU_ASSERT_PTR_EQUAL(a.parent, &root)
universe@449 102 CU_ASSERT_PTR_NULL(a.prev)
universe@449 103 CU_ASSERT_PTR_NULL(a.next)
universe@449 104
universe@453 105 cx_tree_add_child(
universe@453 106 (void **) &root.children_begin,
universe@453 107 (void **) &root.children_end,
olaf@430 108 offsetof(TestNode, prev),
olaf@430 109 offsetof(TestNode, next),
universe@453 110 &b,
universe@453 111 offsetof(TestNode, parent),
universe@453 112 &root);
universe@449 113 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
universe@449 114 CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b)
universe@449 115 CU_ASSERT_PTR_EQUAL(root.children_end, &b)
universe@449 116 CU_ASSERT_PTR_EQUAL(b.parent, &root)
universe@449 117 CU_ASSERT_PTR_EQUAL(b.prev, &a)
universe@453 118
universe@453 119 cx_tree_add_child(
universe@453 120 (void **) &root.children_begin,
universe@453 121 NULL,
olaf@430 122 -1,
olaf@430 123 offsetof(TestNode, next),
universe@453 124 &c,
universe@453 125 -1,
universe@453 126 &root);
universe@449 127 CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged
universe@449 128 CU_ASSERT_PTR_EQUAL(b.next, &c)
universe@449 129 CU_ASSERT_PTR_NULL(c.prev)
universe@449 130 CU_ASSERT_PTR_NULL(c.next)
universe@449 131 CU_ASSERT_PTR_NULL(c.parent)
universe@453 132
universe@453 133 cx_tree_add_child(
universe@453 134 (void **) &a.children_begin,
universe@453 135 (void **) &a.children_end,
olaf@430 136 offsetof(TestNode, prev),
olaf@430 137 offsetof(TestNode, next),
universe@453 138 &a1,
universe@453 139 offsetof(TestNode, parent),
universe@453 140 &a);
olaf@430 141 CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
olaf@430 142 CU_ASSERT_PTR_EQUAL(a1.parent, &a);
universe@449 143 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
universe@449 144 CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1)
olaf@430 145 }
olaf@430 146
olaf@425 147 int main() {
olaf@425 148 CU_pSuite suite = NULL;
olaf@425 149
olaf@425 150 if (CUE_SUCCESS != CU_initialize_registry()) {
olaf@425 151 return CU_get_error();
olaf@425 152 }
olaf@425 153
olaf@425 154 suite = CU_add_suite("tree suite", NULL, NULL);
olaf@425 155
universe@455 156 cu_add_test(suite, test_tree_add_sibling);
universe@455 157 cu_add_test(suite, test_tree_add_child);
universe@455 158
olaf@425 159 CU_basic_set_mode(UCX_CU_BRM);
olaf@425 160
olaf@425 161 int exitcode;
olaf@425 162 if (CU_basic_run_tests()) {
olaf@425 163 exitcode = CU_get_error();
olaf@425 164 } else {
olaf@425 165 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
olaf@425 166 }
olaf@425 167 CU_cleanup_registry();
olaf@425 168 return exitcode;
olaf@425 169 }

mercurial