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

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

mercurial