test/test_tree.cpp

Tue, 04 Oct 2022 19:25:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 04 Oct 2022 19:25:07 +0200
changeset 591
7df0bcaecffa
parent 515
6d3909bf1609
permissions
-rw-r--r--

fix over-optimization of strstr

1. it's actually less performant to frequently read bytes
from an array instead of using the native word length
2. the SBO buffer should be local and not static to allow
multi-threading usage

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"
universe@515 30 #include <gtest/gtest.h>
olaf@425 31
olaf@425 32 struct TestNode {
universe@515 33 TestNode *parent = nullptr;
universe@515 34 TestNode *prev = nullptr;
universe@515 35 TestNode *next = nullptr;
universe@515 36
universe@515 37 TestNode *children_begin = nullptr;
universe@515 38 TestNode *children_end = nullptr;
olaf@425 39 };
olaf@425 40
universe@515 41 TEST(Tree, cx_tree_add_sibling) {
olaf@425 42 // prepare test tree
universe@515 43 TestNode root, a;
olaf@425 44 root.children_begin = &a;
olaf@425 45 root.children_end = &a;
olaf@425 46 a.parent = &root;
universe@449 47
olaf@425 48 // new test nodes
universe@515 49 TestNode b, c;
universe@515 50
olaf@425 51 // test
universe@453 52 cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b);
universe@515 53 EXPECT_EQ(b.parent, &root);
universe@515 54 EXPECT_EQ(b.prev, &a);
universe@515 55 EXPECT_EQ(b.next, nullptr);
universe@515 56 EXPECT_EQ(a.next, &b);
universe@453 57
universe@453 58 cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c);
universe@515 59 EXPECT_EQ(c.parent, nullptr);
universe@515 60 EXPECT_EQ(c.prev, nullptr);
universe@515 61 EXPECT_EQ(c.next, nullptr);
universe@515 62 EXPECT_EQ(b.next, &c);
olaf@425 63 }
olaf@425 64
universe@515 65 TEST(Tree, cx_tree_add_child) {
universe@515 66 TestNode root, a, b, c, a1;
universe@515 67
universe@453 68 cx_tree_add_child(
universe@453 69 (void **) &root.children_begin,
universe@453 70 (void **) &root.children_end,
olaf@430 71 offsetof(TestNode, prev),
olaf@430 72 offsetof(TestNode, next),
universe@453 73 &a,
universe@453 74 offsetof(TestNode, parent),
universe@453 75 &root);
universe@515 76 EXPECT_EQ(root.children_begin, &a);
universe@515 77 EXPECT_EQ(root.children_end, &a);
universe@515 78 EXPECT_EQ(a.parent, &root);
universe@515 79 EXPECT_EQ(a.prev, nullptr);
universe@515 80 EXPECT_EQ(a.next, nullptr);
universe@449 81
universe@453 82 cx_tree_add_child(
universe@453 83 (void **) &root.children_begin,
universe@453 84 (void **) &root.children_end,
olaf@430 85 offsetof(TestNode, prev),
olaf@430 86 offsetof(TestNode, next),
universe@453 87 &b,
universe@453 88 offsetof(TestNode, parent),
universe@453 89 &root);
universe@515 90 EXPECT_EQ(root.children_begin, &a);
universe@515 91 EXPECT_EQ(root.children_begin->next, &b);
universe@515 92 EXPECT_EQ(root.children_end, &b);
universe@515 93 EXPECT_EQ(b.parent, &root);
universe@515 94 EXPECT_EQ(b.prev, &a);
universe@453 95
universe@453 96 cx_tree_add_child(
universe@453 97 (void **) &root.children_begin,
universe@515 98 nullptr,
olaf@430 99 -1,
olaf@430 100 offsetof(TestNode, next),
universe@453 101 &c,
universe@453 102 -1,
universe@453 103 &root);
universe@515 104 EXPECT_EQ(root.children_end, &b); // children_end unchanged
universe@515 105 EXPECT_EQ(b.next, &c);
universe@515 106 EXPECT_EQ(c.prev, nullptr);
universe@515 107 EXPECT_EQ(c.next, nullptr);
universe@515 108 EXPECT_EQ(c.parent, nullptr);
universe@453 109
universe@453 110 cx_tree_add_child(
universe@453 111 (void **) &a.children_begin,
universe@453 112 (void **) &a.children_end,
olaf@430 113 offsetof(TestNode, prev),
olaf@430 114 offsetof(TestNode, next),
universe@453 115 &a1,
universe@453 116 offsetof(TestNode, parent),
universe@453 117 &a);
universe@515 118 EXPECT_EQ(a.children_begin, &a1);
universe@515 119 EXPECT_EQ(a1.parent, &a);
universe@515 120 EXPECT_EQ(root.children_begin, &a);
universe@515 121 EXPECT_EQ(root.children_begin->children_begin, &a1);
olaf@430 122 }

mercurial