| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "wtf/TreeNode.h" | |
| 27 | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 #include "wtf/PassRefPtr.h" | |
| 30 #include "wtf/RefCounted.h" | |
| 31 #include "wtf/RefPtr.h" | |
| 32 | |
| 33 namespace WTF { | |
| 34 | |
| 35 class TestTree : public RefCounted<TestTree>, public TreeNode<TestTree> { | |
| 36 public: | |
| 37 static PassRefPtr<TestTree> create() { return adoptRef(new TestTree()); } | |
| 38 }; | |
| 39 | |
| 40 TEST(TreeNodeTest, AppendChild) { | |
| 41 RefPtr<TestTree> root = TestTree::create(); | |
| 42 RefPtr<TestTree> firstChild = TestTree::create(); | |
| 43 RefPtr<TestTree> lastChild = TestTree::create(); | |
| 44 | |
| 45 root->appendChild(firstChild.get()); | |
| 46 EXPECT_EQ(root->firstChild(), firstChild.get()); | |
| 47 EXPECT_EQ(root->lastChild(), firstChild.get()); | |
| 48 EXPECT_EQ(firstChild->parent(), root.get()); | |
| 49 | |
| 50 root->appendChild(lastChild.get()); | |
| 51 EXPECT_EQ(root->firstChild(), firstChild.get()); | |
| 52 EXPECT_EQ(root->lastChild(), lastChild.get()); | |
| 53 EXPECT_EQ(lastChild->previous(), firstChild.get()); | |
| 54 EXPECT_EQ(firstChild->next(), lastChild.get()); | |
| 55 EXPECT_EQ(lastChild->parent(), root.get()); | |
| 56 } | |
| 57 | |
| 58 TEST(TreeNodeTest, InsertBefore) { | |
| 59 RefPtr<TestTree> root = TestTree::create(); | |
| 60 RefPtr<TestTree> firstChild = TestTree::create(); | |
| 61 RefPtr<TestTree> middleChild = TestTree::create(); | |
| 62 RefPtr<TestTree> lastChild = TestTree::create(); | |
| 63 | |
| 64 // Inserting single node | |
| 65 root->insertBefore(lastChild.get(), 0); | |
| 66 EXPECT_EQ(lastChild->parent(), root.get()); | |
| 67 EXPECT_EQ(root->firstChild(), lastChild.get()); | |
| 68 EXPECT_EQ(root->lastChild(), lastChild.get()); | |
| 69 | |
| 70 // Then prepend | |
| 71 root->insertBefore(firstChild.get(), lastChild.get()); | |
| 72 EXPECT_EQ(firstChild->parent(), root.get()); | |
| 73 EXPECT_EQ(root->firstChild(), firstChild.get()); | |
| 74 EXPECT_EQ(root->lastChild(), lastChild.get()); | |
| 75 EXPECT_EQ(firstChild->next(), lastChild.get()); | |
| 76 EXPECT_EQ(firstChild.get(), lastChild->previous()); | |
| 77 | |
| 78 // Inserting in the middle | |
| 79 root->insertBefore(middleChild.get(), lastChild.get()); | |
| 80 EXPECT_EQ(middleChild->parent(), root.get()); | |
| 81 EXPECT_EQ(root->firstChild(), firstChild.get()); | |
| 82 EXPECT_EQ(root->lastChild(), lastChild.get()); | |
| 83 EXPECT_EQ(middleChild->previous(), firstChild.get()); | |
| 84 EXPECT_EQ(middleChild->next(), lastChild.get()); | |
| 85 EXPECT_EQ(firstChild->next(), middleChild.get()); | |
| 86 EXPECT_EQ(lastChild->previous(), middleChild.get()); | |
| 87 } | |
| 88 | |
| 89 TEST(TreeNodeTest, RemoveSingle) { | |
| 90 RefPtr<TestTree> root = TestTree::create(); | |
| 91 RefPtr<TestTree> child = TestTree::create(); | |
| 92 RefPtr<TestTree> nullNode; | |
| 93 | |
| 94 root->appendChild(child.get()); | |
| 95 root->removeChild(child.get()); | |
| 96 EXPECT_EQ(child->next(), nullNode.get()); | |
| 97 EXPECT_EQ(child->previous(), nullNode.get()); | |
| 98 EXPECT_EQ(child->parent(), nullNode.get()); | |
| 99 EXPECT_EQ(root->firstChild(), nullNode.get()); | |
| 100 EXPECT_EQ(root->lastChild(), nullNode.get()); | |
| 101 } | |
| 102 | |
| 103 class Trio { | |
| 104 public: | |
| 105 Trio() | |
| 106 : root(TestTree::create()), | |
| 107 firstChild(TestTree::create()), | |
| 108 middleChild(TestTree::create()), | |
| 109 lastChild(TestTree::create()) {} | |
| 110 | |
| 111 void appendChildren() { | |
| 112 root->appendChild(firstChild.get()); | |
| 113 root->appendChild(middleChild.get()); | |
| 114 root->appendChild(lastChild.get()); | |
| 115 } | |
| 116 | |
| 117 RefPtr<TestTree> root; | |
| 118 RefPtr<TestTree> firstChild; | |
| 119 RefPtr<TestTree> middleChild; | |
| 120 RefPtr<TestTree> lastChild; | |
| 121 }; | |
| 122 | |
| 123 TEST(TreeNodeTest, RemoveMiddle) { | |
| 124 Trio trio; | |
| 125 trio.appendChildren(); | |
| 126 | |
| 127 trio.root->removeChild(trio.middleChild.get()); | |
| 128 EXPECT_TRUE(trio.middleChild->orphan()); | |
| 129 EXPECT_EQ(trio.firstChild->next(), trio.lastChild.get()); | |
| 130 EXPECT_EQ(trio.lastChild->previous(), trio.firstChild.get()); | |
| 131 EXPECT_EQ(trio.root->firstChild(), trio.firstChild.get()); | |
| 132 EXPECT_EQ(trio.root->lastChild(), trio.lastChild.get()); | |
| 133 } | |
| 134 | |
| 135 TEST(TreeNodeTest, RemoveLast) { | |
| 136 RefPtr<TestTree> nullNode; | |
| 137 Trio trio; | |
| 138 trio.appendChildren(); | |
| 139 | |
| 140 trio.root->removeChild(trio.lastChild.get()); | |
| 141 EXPECT_TRUE(trio.lastChild->orphan()); | |
| 142 EXPECT_EQ(trio.middleChild->next(), nullNode.get()); | |
| 143 EXPECT_EQ(trio.root->firstChild(), trio.firstChild.get()); | |
| 144 EXPECT_EQ(trio.root->lastChild(), trio.middleChild.get()); | |
| 145 } | |
| 146 | |
| 147 TEST(TreeNodeTest, RemoveFirst) { | |
| 148 RefPtr<TestTree> nullNode; | |
| 149 Trio trio; | |
| 150 trio.appendChildren(); | |
| 151 | |
| 152 trio.root->removeChild(trio.firstChild.get()); | |
| 153 EXPECT_TRUE(trio.firstChild->orphan()); | |
| 154 EXPECT_EQ(trio.middleChild->previous(), nullNode.get()); | |
| 155 EXPECT_EQ(trio.root->firstChild(), trio.middleChild.get()); | |
| 156 EXPECT_EQ(trio.root->lastChild(), trio.lastChild.get()); | |
| 157 } | |
| 158 | |
| 159 TEST(TreeNodeTest, TakeChildrenFrom) { | |
| 160 RefPtr<TestTree> newParent = TestTree::create(); | |
| 161 Trio trio; | |
| 162 trio.appendChildren(); | |
| 163 | |
| 164 newParent->takeChildrenFrom(trio.root.get()); | |
| 165 | |
| 166 EXPECT_FALSE(trio.root->hasChildren()); | |
| 167 EXPECT_TRUE(newParent->hasChildren()); | |
| 168 EXPECT_EQ(trio.firstChild.get(), newParent->firstChild()); | |
| 169 EXPECT_EQ(trio.middleChild.get(), newParent->firstChild()->next()); | |
| 170 EXPECT_EQ(trio.lastChild.get(), newParent->lastChild()); | |
| 171 } | |
| 172 | |
| 173 class TrioWithGrandChild : public Trio { | |
| 174 public: | |
| 175 TrioWithGrandChild() : grandChild(TestTree::create()) {} | |
| 176 | |
| 177 void appendChildren() { | |
| 178 Trio::appendChildren(); | |
| 179 middleChild->appendChild(grandChild.get()); | |
| 180 } | |
| 181 | |
| 182 RefPtr<TestTree> grandChild; | |
| 183 }; | |
| 184 | |
| 185 TEST(TreeNodeTest, TraverseNext) { | |
| 186 TrioWithGrandChild trio; | |
| 187 trio.appendChildren(); | |
| 188 | |
| 189 TestTree* order[] = {trio.root.get(), trio.firstChild.get(), | |
| 190 trio.middleChild.get(), trio.grandChild.get(), | |
| 191 trio.lastChild.get()}; | |
| 192 | |
| 193 unsigned orderIndex = 0; | |
| 194 for (TestTree *node = trio.root.get(); node; | |
| 195 node = traverseNext(node), orderIndex++) | |
| 196 EXPECT_EQ(node, order[orderIndex]); | |
| 197 EXPECT_EQ(orderIndex, sizeof(order) / sizeof(TestTree*)); | |
| 198 } | |
| 199 | |
| 200 TEST(TreeNodeTest, TraverseNextPostORder) { | |
| 201 TrioWithGrandChild trio; | |
| 202 trio.appendChildren(); | |
| 203 | |
| 204 TestTree* order[] = {trio.firstChild.get(), trio.grandChild.get(), | |
| 205 trio.middleChild.get(), trio.lastChild.get(), | |
| 206 trio.root.get()}; | |
| 207 | |
| 208 unsigned orderIndex = 0; | |
| 209 for (TestTree *node = traverseFirstPostOrder(trio.root.get()); node; | |
| 210 node = traverseNextPostOrder(node), orderIndex++) | |
| 211 EXPECT_EQ(node, order[orderIndex]); | |
| 212 EXPECT_EQ(orderIndex, sizeof(order) / sizeof(TestTree*)); | |
| 213 } | |
| 214 | |
| 215 } // namespace WTF | |
| OLD | NEW |