Sat, 16 Apr 2022 14:47:27 +0200
migrate tree tests to gtest
test/CMakeLists.txt | file | annotate | diff | comparison | revisions | |
test/test_tree.c | file | annotate | diff | comparison | revisions | |
test/test_tree.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/test/CMakeLists.txt Sat Apr 16 09:10:10 2022 +0200 1.2 +++ b/test/CMakeLists.txt Sat Apr 16 14:47:27 2022 +0200 1.3 @@ -10,7 +10,6 @@ 1.4 message(CHECK_PASS "found: compiling tests.") 1.5 set(TESTS 1.6 test_list 1.7 - test_tree 1.8 ) 1.9 1.10 foreach (test ${TESTS}) 1.11 @@ -40,6 +39,7 @@ 1.12 1.13 add_executable(ucxtest 1.14 test_allocator.cpp 1.15 + test_tree.cpp 1.16 selftest.cpp 1.17 ) 1.18 target_link_libraries(ucxtest PRIVATE ucx_static gtest_main)
2.1 --- a/test/test_tree.c Sat Apr 16 09:10:10 2022 +0200 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,169 +0,0 @@ 2.4 -/* 2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 - * 2.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 2.8 - * 2.9 - * Redistribution and use in source and binary forms, with or without 2.10 - * modification, are permitted provided that the following conditions are met: 2.11 - * 2.12 - * 1. Redistributions of source code must retain the above copyright 2.13 - * notice, this list of conditions and the following disclaimer. 2.14 - * 2.15 - * 2. Redistributions in binary form must reproduce the above copyright 2.16 - * notice, this list of conditions and the following disclaimer in the 2.17 - * documentation and/or other materials provided with the distribution. 2.18 - * 2.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.29 - * POSSIBILITY OF SUCH DAMAGE. 2.30 - */ 2.31 - 2.32 -#include "cx/tree.h" 2.33 -#include "test_config.h" 2.34 - 2.35 -#include <stddef.h> 2.36 - 2.37 -typedef struct TestNode TestNode; 2.38 - 2.39 -struct TestNode { 2.40 - TestNode *parent; 2.41 - TestNode *prev; 2.42 - TestNode *next; 2.43 - 2.44 - TestNode *children_begin; 2.45 - TestNode *children_end; 2.46 -}; 2.47 - 2.48 -void test_tree_add_sibling(void) { 2.49 - // prepare test tree 2.50 - TestNode root; 2.51 - memset(&root, 0, sizeof(TestNode)); 2.52 - 2.53 - TestNode a; 2.54 - memset(&a, 0, sizeof(TestNode)); 2.55 - root.children_begin = &a; 2.56 - root.children_end = &a; 2.57 - a.parent = &root; 2.58 - 2.59 - // new test nodes 2.60 - TestNode b; 2.61 - memset(&b, 0, sizeof(TestNode)); 2.62 - TestNode c; 2.63 - memset(&c, 0, sizeof(TestNode)); 2.64 - 2.65 - // test 2.66 - cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b); 2.67 - CU_ASSERT_PTR_EQUAL(b.parent, &root) 2.68 - CU_ASSERT_PTR_EQUAL(b.prev, &a) 2.69 - CU_ASSERT_PTR_NULL(b.next) 2.70 - CU_ASSERT_PTR_EQUAL(a.next, &b) 2.71 - 2.72 - cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c); 2.73 - CU_ASSERT_PTR_NULL(c.parent) 2.74 - CU_ASSERT_PTR_NULL(c.prev) 2.75 - CU_ASSERT_PTR_NULL(c.next) 2.76 - CU_ASSERT_PTR_EQUAL(b.next, &c) 2.77 -} 2.78 - 2.79 -void test_tree_add_child(void) { 2.80 - // prepare test tree 2.81 - TestNode root; 2.82 - memset(&root, 0, sizeof(TestNode)); 2.83 - 2.84 - TestNode a; 2.85 - memset(&a, 0, sizeof(TestNode)); 2.86 - TestNode b; 2.87 - memset(&b, 0, sizeof(TestNode)); 2.88 - TestNode c; 2.89 - memset(&c, 0, sizeof(TestNode)); 2.90 - TestNode a1; 2.91 - memset(&a1, 0, sizeof(TestNode)); 2.92 - 2.93 - // test 2.94 - cx_tree_add_child( 2.95 - (void **) &root.children_begin, 2.96 - (void **) &root.children_end, 2.97 - offsetof(TestNode, prev), 2.98 - offsetof(TestNode, next), 2.99 - &a, 2.100 - offsetof(TestNode, parent), 2.101 - &root); 2.102 - CU_ASSERT_PTR_EQUAL(root.children_begin, &a) 2.103 - CU_ASSERT_PTR_EQUAL(root.children_end, &a) 2.104 - CU_ASSERT_PTR_EQUAL(a.parent, &root) 2.105 - CU_ASSERT_PTR_NULL(a.prev) 2.106 - CU_ASSERT_PTR_NULL(a.next) 2.107 - 2.108 - cx_tree_add_child( 2.109 - (void **) &root.children_begin, 2.110 - (void **) &root.children_end, 2.111 - offsetof(TestNode, prev), 2.112 - offsetof(TestNode, next), 2.113 - &b, 2.114 - offsetof(TestNode, parent), 2.115 - &root); 2.116 - CU_ASSERT_PTR_NOT_NULL(root.children_begin) 2.117 - CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b) 2.118 - CU_ASSERT_PTR_EQUAL(root.children_end, &b) 2.119 - CU_ASSERT_PTR_EQUAL(b.parent, &root) 2.120 - CU_ASSERT_PTR_EQUAL(b.prev, &a) 2.121 - 2.122 - cx_tree_add_child( 2.123 - (void **) &root.children_begin, 2.124 - NULL, 2.125 - -1, 2.126 - offsetof(TestNode, next), 2.127 - &c, 2.128 - -1, 2.129 - &root); 2.130 - CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged 2.131 - CU_ASSERT_PTR_EQUAL(b.next, &c) 2.132 - CU_ASSERT_PTR_NULL(c.prev) 2.133 - CU_ASSERT_PTR_NULL(c.next) 2.134 - CU_ASSERT_PTR_NULL(c.parent) 2.135 - 2.136 - cx_tree_add_child( 2.137 - (void **) &a.children_begin, 2.138 - (void **) &a.children_end, 2.139 - offsetof(TestNode, prev), 2.140 - offsetof(TestNode, next), 2.141 - &a1, 2.142 - offsetof(TestNode, parent), 2.143 - &a); 2.144 - CU_ASSERT_PTR_EQUAL(a.children_begin, &a1); 2.145 - CU_ASSERT_PTR_EQUAL(a1.parent, &a); 2.146 - CU_ASSERT_PTR_NOT_NULL(root.children_begin) 2.147 - CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1) 2.148 -} 2.149 - 2.150 -int main() { 2.151 - CU_pSuite suite = NULL; 2.152 - 2.153 - if (CUE_SUCCESS != CU_initialize_registry()) { 2.154 - return CU_get_error(); 2.155 - } 2.156 - 2.157 - suite = CU_add_suite("tree suite", NULL, NULL); 2.158 - 2.159 - cu_add_test(suite, test_tree_add_sibling); 2.160 - cu_add_test(suite, test_tree_add_child); 2.161 - 2.162 - CU_basic_set_mode(UCX_CU_BRM); 2.163 - 2.164 - int exitcode; 2.165 - if (CU_basic_run_tests()) { 2.166 - exitcode = CU_get_error(); 2.167 - } else { 2.168 - exitcode = CU_get_number_of_failures() == 0 ? 0 : 1; 2.169 - } 2.170 - CU_cleanup_registry(); 2.171 - return exitcode; 2.172 -}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/test_tree.cpp Sat Apr 16 14:47:27 2022 +0200 3.3 @@ -0,0 +1,122 @@ 3.4 +/* 3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 + * 3.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 3.8 + * 3.9 + * Redistribution and use in source and binary forms, with or without 3.10 + * modification, are permitted provided that the following conditions are met: 3.11 + * 3.12 + * 1. Redistributions of source code must retain the above copyright 3.13 + * notice, this list of conditions and the following disclaimer. 3.14 + * 3.15 + * 2. Redistributions in binary form must reproduce the above copyright 3.16 + * notice, this list of conditions and the following disclaimer in the 3.17 + * documentation and/or other materials provided with the distribution. 3.18 + * 3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 + * POSSIBILITY OF SUCH DAMAGE. 3.30 + */ 3.31 + 3.32 +#include "cx/tree.h" 3.33 +#include <gtest/gtest.h> 3.34 + 3.35 +struct TestNode { 3.36 + TestNode *parent = nullptr; 3.37 + TestNode *prev = nullptr; 3.38 + TestNode *next = nullptr; 3.39 + 3.40 + TestNode *children_begin = nullptr; 3.41 + TestNode *children_end = nullptr; 3.42 +}; 3.43 + 3.44 +TEST(Tree, cx_tree_add_sibling) { 3.45 + // prepare test tree 3.46 + TestNode root, a; 3.47 + root.children_begin = &a; 3.48 + root.children_end = &a; 3.49 + a.parent = &root; 3.50 + 3.51 + // new test nodes 3.52 + TestNode b, c; 3.53 + 3.54 + // test 3.55 + cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b); 3.56 + EXPECT_EQ(b.parent, &root); 3.57 + EXPECT_EQ(b.prev, &a); 3.58 + EXPECT_EQ(b.next, nullptr); 3.59 + EXPECT_EQ(a.next, &b); 3.60 + 3.61 + cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c); 3.62 + EXPECT_EQ(c.parent, nullptr); 3.63 + EXPECT_EQ(c.prev, nullptr); 3.64 + EXPECT_EQ(c.next, nullptr); 3.65 + EXPECT_EQ(b.next, &c); 3.66 +} 3.67 + 3.68 +TEST(Tree, cx_tree_add_child) { 3.69 + TestNode root, a, b, c, a1; 3.70 + 3.71 + cx_tree_add_child( 3.72 + (void **) &root.children_begin, 3.73 + (void **) &root.children_end, 3.74 + offsetof(TestNode, prev), 3.75 + offsetof(TestNode, next), 3.76 + &a, 3.77 + offsetof(TestNode, parent), 3.78 + &root); 3.79 + EXPECT_EQ(root.children_begin, &a); 3.80 + EXPECT_EQ(root.children_end, &a); 3.81 + EXPECT_EQ(a.parent, &root); 3.82 + EXPECT_EQ(a.prev, nullptr); 3.83 + EXPECT_EQ(a.next, nullptr); 3.84 + 3.85 + cx_tree_add_child( 3.86 + (void **) &root.children_begin, 3.87 + (void **) &root.children_end, 3.88 + offsetof(TestNode, prev), 3.89 + offsetof(TestNode, next), 3.90 + &b, 3.91 + offsetof(TestNode, parent), 3.92 + &root); 3.93 + EXPECT_EQ(root.children_begin, &a); 3.94 + EXPECT_EQ(root.children_begin->next, &b); 3.95 + EXPECT_EQ(root.children_end, &b); 3.96 + EXPECT_EQ(b.parent, &root); 3.97 + EXPECT_EQ(b.prev, &a); 3.98 + 3.99 + cx_tree_add_child( 3.100 + (void **) &root.children_begin, 3.101 + nullptr, 3.102 + -1, 3.103 + offsetof(TestNode, next), 3.104 + &c, 3.105 + -1, 3.106 + &root); 3.107 + EXPECT_EQ(root.children_end, &b); // children_end unchanged 3.108 + EXPECT_EQ(b.next, &c); 3.109 + EXPECT_EQ(c.prev, nullptr); 3.110 + EXPECT_EQ(c.next, nullptr); 3.111 + EXPECT_EQ(c.parent, nullptr); 3.112 + 3.113 + cx_tree_add_child( 3.114 + (void **) &a.children_begin, 3.115 + (void **) &a.children_end, 3.116 + offsetof(TestNode, prev), 3.117 + offsetof(TestNode, next), 3.118 + &a1, 3.119 + offsetof(TestNode, parent), 3.120 + &a); 3.121 + EXPECT_EQ(a.children_begin, &a1); 3.122 + EXPECT_EQ(a1.parent, &a); 3.123 + EXPECT_EQ(root.children_begin, &a); 3.124 + EXPECT_EQ(root.children_begin->children_begin, &a1); 3.125 +}