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: int content; olaf@425: }; olaf@425: olaf@425: void test_cx_tree_add_node() { 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; olaf@425: a.content = 1; olaf@425: 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 olaf@425: b.content = 2; olaf@425: int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b); olaf@425: CU_ASSERT_EQUAL(ret, 0); olaf@425: CU_ASSERT_PTR_EQUAL(b.parent, &root); olaf@425: CU_ASSERT_PTR_EQUAL(b.prev, &a); olaf@425: CU_ASSERT_PTR_EQUAL(b.next, NULL); olaf@425: CU_ASSERT_PTR_EQUAL(a.next, &b); olaf@425: olaf@425: c.content = 3; olaf@425: ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c); olaf@425: CU_ASSERT_EQUAL(ret, 0); olaf@425: CU_ASSERT_PTR_EQUAL(c.parent, NULL); olaf@425: CU_ASSERT_PTR_EQUAL(c.prev, NULL); olaf@425: CU_ASSERT_PTR_EQUAL(c.next, NULL); olaf@425: CU_ASSERT_PTR_EQUAL(b.next, &c); olaf@425: } olaf@425: 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: if (NULL == suite) { olaf@425: CU_cleanup_registry(); olaf@425: return CU_get_error(); olaf@425: } olaf@425: olaf@425: if ( olaf@425: !CU_add_test(suite, "ll add tree node", test_cx_tree_add_node) olaf@425: ) { olaf@425: CU_cleanup_registry(); olaf@425: return CU_get_error(); olaf@425: } 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: }