olaf@425: /* olaf@425: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@425: * olaf@425: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. olaf@425: * olaf@425: * Redistribution and use in source and binary forms, with or without olaf@425: * modification, are permitted provided that the following conditions are met: olaf@425: * olaf@425: * 1. Redistributions of source code must retain the above copyright olaf@425: * notice, this list of conditions and the following disclaimer. olaf@425: * olaf@425: * 2. Redistributions in binary form must reproduce the above copyright olaf@425: * notice, this list of conditions and the following disclaimer in the olaf@425: * documentation and/or other materials provided with the distribution. olaf@425: * olaf@425: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" olaf@425: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE olaf@425: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE olaf@425: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE olaf@425: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR olaf@425: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF olaf@425: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS olaf@425: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN olaf@425: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) olaf@425: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE olaf@425: * POSSIBILITY OF SUCH DAMAGE. olaf@425: */ olaf@425: olaf@425: #include "cx/tree.h" olaf@425: #include "test_config.h" olaf@425: olaf@425: #include olaf@425: olaf@425: typedef struct TestNode TestNode; olaf@425: olaf@425: struct TestNode { olaf@425: TestNode *parent; olaf@425: TestNode *prev; olaf@425: TestNode *next; olaf@425: olaf@425: TestNode *children_begin; olaf@425: TestNode *children_end; olaf@425: }; olaf@425: universe@449: void test_cx_tree_add_node(void) { olaf@425: // prepare test tree olaf@425: TestNode root; olaf@425: memset(&root, 0, sizeof(TestNode)); olaf@425: olaf@425: TestNode a; olaf@425: memset(&a, 0, sizeof(TestNode)); olaf@425: root.children_begin = &a; olaf@425: root.children_end = &a; olaf@425: a.parent = &root; universe@449: olaf@425: // new test nodes olaf@425: TestNode b; olaf@425: memset(&b, 0, sizeof(TestNode)); olaf@425: TestNode c; olaf@425: memset(&c, 0, sizeof(TestNode)); olaf@425: olaf@425: // test universe@453: cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b); universe@449: CU_ASSERT_PTR_EQUAL(b.parent, &root) universe@449: CU_ASSERT_PTR_EQUAL(b.prev, &a) universe@449: CU_ASSERT_PTR_NULL(b.next) universe@449: CU_ASSERT_PTR_EQUAL(a.next, &b) universe@453: universe@453: cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c); universe@449: CU_ASSERT_PTR_NULL(c.parent) universe@449: CU_ASSERT_PTR_NULL(c.prev) universe@449: CU_ASSERT_PTR_NULL(c.next) universe@449: CU_ASSERT_PTR_EQUAL(b.next, &c) olaf@425: } olaf@425: universe@449: void test_cx_tree_add_child_node(void) { olaf@430: // prepare test tree olaf@430: TestNode root; olaf@430: memset(&root, 0, sizeof(TestNode)); olaf@430: olaf@430: TestNode a; olaf@430: memset(&a, 0, sizeof(TestNode)); olaf@430: TestNode b; olaf@430: memset(&b, 0, sizeof(TestNode)); olaf@430: TestNode c; olaf@430: memset(&c, 0, sizeof(TestNode)); olaf@430: TestNode a1; olaf@430: memset(&a1, 0, sizeof(TestNode)); olaf@430: olaf@430: // test universe@453: cx_tree_add_child( universe@453: (void **) &root.children_begin, universe@453: (void **) &root.children_end, olaf@430: offsetof(TestNode, prev), olaf@430: offsetof(TestNode, next), universe@453: &a, universe@453: offsetof(TestNode, parent), universe@453: &root); universe@449: CU_ASSERT_PTR_EQUAL(root.children_begin, &a) universe@449: CU_ASSERT_PTR_EQUAL(root.children_end, &a) universe@449: CU_ASSERT_PTR_EQUAL(a.parent, &root) universe@449: CU_ASSERT_PTR_NULL(a.prev) universe@449: CU_ASSERT_PTR_NULL(a.next) universe@449: universe@453: cx_tree_add_child( universe@453: (void **) &root.children_begin, universe@453: (void **) &root.children_end, olaf@430: offsetof(TestNode, prev), olaf@430: offsetof(TestNode, next), universe@453: &b, universe@453: offsetof(TestNode, parent), universe@453: &root); universe@449: CU_ASSERT_PTR_NOT_NULL(root.children_begin) universe@449: CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b) universe@449: CU_ASSERT_PTR_EQUAL(root.children_end, &b) universe@449: CU_ASSERT_PTR_EQUAL(b.parent, &root) universe@449: CU_ASSERT_PTR_EQUAL(b.prev, &a) universe@453: universe@453: cx_tree_add_child( universe@453: (void **) &root.children_begin, universe@453: NULL, olaf@430: -1, olaf@430: offsetof(TestNode, next), universe@453: &c, universe@453: -1, universe@453: &root); universe@449: CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged universe@449: CU_ASSERT_PTR_EQUAL(b.next, &c) universe@449: CU_ASSERT_PTR_NULL(c.prev) universe@449: CU_ASSERT_PTR_NULL(c.next) universe@449: CU_ASSERT_PTR_NULL(c.parent) universe@453: universe@453: cx_tree_add_child( universe@453: (void **) &a.children_begin, universe@453: (void **) &a.children_end, olaf@430: offsetof(TestNode, prev), olaf@430: offsetof(TestNode, next), universe@453: &a1, universe@453: offsetof(TestNode, parent), universe@453: &a); olaf@430: CU_ASSERT_PTR_EQUAL(a.children_begin, &a1); olaf@430: CU_ASSERT_PTR_EQUAL(a1.parent, &a); universe@449: CU_ASSERT_PTR_NOT_NULL(root.children_begin) universe@449: CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1) olaf@430: } olaf@430: olaf@425: int main() { olaf@425: CU_pSuite suite = NULL; olaf@425: olaf@425: if (CUE_SUCCESS != CU_initialize_registry()) { olaf@425: return CU_get_error(); olaf@425: } olaf@425: olaf@425: suite = CU_add_suite("tree suite", NULL, NULL); olaf@425: olaf@443: CU_add_test(suite, "ll add tree node", test_cx_tree_add_node); olaf@443: CU_add_test(suite, "ll add tree child node", test_cx_tree_add_child_node); olaf@425: olaf@425: olaf@425: CU_basic_set_mode(UCX_CU_BRM); olaf@425: olaf@425: int exitcode; olaf@425: if (CU_basic_run_tests()) { olaf@425: exitcode = CU_get_error(); olaf@425: } else { olaf@425: exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; olaf@425: } olaf@425: CU_cleanup_registry(); olaf@425: return exitcode; olaf@425: }