| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "wtf/TreeNode.h" | 26 #include "wtf/TreeNode.h" |
| 27 | 27 |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "wtf/PassRefPtr.h" | 29 #include "wtf/PassRefPtr.h" |
| 30 #include "wtf/RefCounted.h" | 30 #include "wtf/RefCounted.h" |
| 31 #include "wtf/RefPtr.h" | 31 #include "wtf/RefPtr.h" |
| 32 | 32 |
| 33 namespace WTF { | 33 namespace WTF { |
| 34 | 34 |
| 35 class TestTree : public RefCounted<TestTree>, public TreeNode<TestTree> { | 35 class TestTree : public RefCounted<TestTree>, public TreeNode<TestTree> { |
| 36 public: | 36 public: |
| 37 static PassRefPtr<TestTree> create() { return adoptRef(new TestTree()); } | 37 static PassRefPtr<TestTree> create() { return adoptRef(new TestTree()); } |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 TEST(TreeNodeTest, AppendChild) | 40 TEST(TreeNodeTest, AppendChild) { |
| 41 { | 41 RefPtr<TestTree> root = TestTree::create(); |
| 42 RefPtr<TestTree> root = TestTree::create(); | 42 RefPtr<TestTree> firstChild = TestTree::create(); |
| 43 RefPtr<TestTree> firstChild = TestTree::create(); | 43 RefPtr<TestTree> lastChild = TestTree::create(); |
| 44 RefPtr<TestTree> lastChild = TestTree::create(); | |
| 45 | 44 |
| 46 root->appendChild(firstChild.get()); | 45 root->appendChild(firstChild.get()); |
| 47 EXPECT_EQ(root->firstChild(), firstChild.get()); | 46 EXPECT_EQ(root->firstChild(), firstChild.get()); |
| 48 EXPECT_EQ(root->lastChild(), firstChild.get()); | 47 EXPECT_EQ(root->lastChild(), firstChild.get()); |
| 49 EXPECT_EQ(firstChild->parent(), root.get()); | 48 EXPECT_EQ(firstChild->parent(), root.get()); |
| 50 | 49 |
| 51 root->appendChild(lastChild.get()); | 50 root->appendChild(lastChild.get()); |
| 52 EXPECT_EQ(root->firstChild(), firstChild.get()); | 51 EXPECT_EQ(root->firstChild(), firstChild.get()); |
| 53 EXPECT_EQ(root->lastChild(), lastChild.get()); | 52 EXPECT_EQ(root->lastChild(), lastChild.get()); |
| 54 EXPECT_EQ(lastChild->previous(), firstChild.get()); | 53 EXPECT_EQ(lastChild->previous(), firstChild.get()); |
| 55 EXPECT_EQ(firstChild->next(), lastChild.get()); | 54 EXPECT_EQ(firstChild->next(), lastChild.get()); |
| 56 EXPECT_EQ(lastChild->parent(), root.get()); | 55 EXPECT_EQ(lastChild->parent(), root.get()); |
| 57 } | 56 } |
| 58 | 57 |
| 59 TEST(TreeNodeTest, InsertBefore) | 58 TEST(TreeNodeTest, InsertBefore) { |
| 60 { | 59 RefPtr<TestTree> root = TestTree::create(); |
| 61 RefPtr<TestTree> root = TestTree::create(); | 60 RefPtr<TestTree> firstChild = TestTree::create(); |
| 62 RefPtr<TestTree> firstChild = TestTree::create(); | 61 RefPtr<TestTree> middleChild = TestTree::create(); |
| 63 RefPtr<TestTree> middleChild = TestTree::create(); | 62 RefPtr<TestTree> lastChild = TestTree::create(); |
| 64 RefPtr<TestTree> lastChild = TestTree::create(); | |
| 65 | 63 |
| 66 // Inserting single node | 64 // Inserting single node |
| 67 root->insertBefore(lastChild.get(), 0); | 65 root->insertBefore(lastChild.get(), 0); |
| 68 EXPECT_EQ(lastChild->parent(), root.get()); | 66 EXPECT_EQ(lastChild->parent(), root.get()); |
| 69 EXPECT_EQ(root->firstChild(), lastChild.get()); | 67 EXPECT_EQ(root->firstChild(), lastChild.get()); |
| 70 EXPECT_EQ(root->lastChild(), lastChild.get()); | 68 EXPECT_EQ(root->lastChild(), lastChild.get()); |
| 71 | 69 |
| 72 // Then prepend | 70 // Then prepend |
| 73 root->insertBefore(firstChild.get(), lastChild.get()); | 71 root->insertBefore(firstChild.get(), lastChild.get()); |
| 74 EXPECT_EQ(firstChild->parent(), root.get()); | 72 EXPECT_EQ(firstChild->parent(), root.get()); |
| 75 EXPECT_EQ(root->firstChild(), firstChild.get()); | 73 EXPECT_EQ(root->firstChild(), firstChild.get()); |
| 76 EXPECT_EQ(root->lastChild(), lastChild.get()); | 74 EXPECT_EQ(root->lastChild(), lastChild.get()); |
| 77 EXPECT_EQ(firstChild->next(), lastChild.get()); | 75 EXPECT_EQ(firstChild->next(), lastChild.get()); |
| 78 EXPECT_EQ(firstChild.get(), lastChild->previous()); | 76 EXPECT_EQ(firstChild.get(), lastChild->previous()); |
| 79 | 77 |
| 80 // Inserting in the middle | 78 // Inserting in the middle |
| 81 root->insertBefore(middleChild.get(), lastChild.get()); | 79 root->insertBefore(middleChild.get(), lastChild.get()); |
| 82 EXPECT_EQ(middleChild->parent(), root.get()); | 80 EXPECT_EQ(middleChild->parent(), root.get()); |
| 83 EXPECT_EQ(root->firstChild(), firstChild.get()); | 81 EXPECT_EQ(root->firstChild(), firstChild.get()); |
| 84 EXPECT_EQ(root->lastChild(), lastChild.get()); | 82 EXPECT_EQ(root->lastChild(), lastChild.get()); |
| 85 EXPECT_EQ(middleChild->previous(), firstChild.get()); | 83 EXPECT_EQ(middleChild->previous(), firstChild.get()); |
| 86 EXPECT_EQ(middleChild->next(), lastChild.get()); | 84 EXPECT_EQ(middleChild->next(), lastChild.get()); |
| 87 EXPECT_EQ(firstChild->next(), middleChild.get()); | 85 EXPECT_EQ(firstChild->next(), middleChild.get()); |
| 88 EXPECT_EQ(lastChild->previous(), middleChild.get()); | 86 EXPECT_EQ(lastChild->previous(), middleChild.get()); |
| 89 | |
| 90 } | 87 } |
| 91 | 88 |
| 92 TEST(TreeNodeTest, RemoveSingle) | 89 TEST(TreeNodeTest, RemoveSingle) { |
| 93 { | 90 RefPtr<TestTree> root = TestTree::create(); |
| 94 RefPtr<TestTree> root = TestTree::create(); | 91 RefPtr<TestTree> child = TestTree::create(); |
| 95 RefPtr<TestTree> child = TestTree::create(); | 92 RefPtr<TestTree> nullNode; |
| 96 RefPtr<TestTree> nullNode; | |
| 97 | 93 |
| 98 root->appendChild(child.get()); | 94 root->appendChild(child.get()); |
| 99 root->removeChild(child.get()); | 95 root->removeChild(child.get()); |
| 100 EXPECT_EQ(child->next(), nullNode.get()); | 96 EXPECT_EQ(child->next(), nullNode.get()); |
| 101 EXPECT_EQ(child->previous(), nullNode.get()); | 97 EXPECT_EQ(child->previous(), nullNode.get()); |
| 102 EXPECT_EQ(child->parent(), nullNode.get()); | 98 EXPECT_EQ(child->parent(), nullNode.get()); |
| 103 EXPECT_EQ(root->firstChild(), nullNode.get()); | 99 EXPECT_EQ(root->firstChild(), nullNode.get()); |
| 104 EXPECT_EQ(root->lastChild(), nullNode.get()); | 100 EXPECT_EQ(root->lastChild(), nullNode.get()); |
| 105 } | 101 } |
| 106 | 102 |
| 107 class Trio { | 103 class Trio { |
| 108 public: | 104 public: |
| 109 Trio() | 105 Trio() |
| 110 : root(TestTree::create()) | 106 : root(TestTree::create()), |
| 111 , firstChild(TestTree::create()) | 107 firstChild(TestTree::create()), |
| 112 , middleChild(TestTree::create()) | 108 middleChild(TestTree::create()), |
| 113 , lastChild(TestTree::create()) | 109 lastChild(TestTree::create()) {} |
| 114 { | |
| 115 } | |
| 116 | 110 |
| 117 void appendChildren() | 111 void appendChildren() { |
| 118 { | 112 root->appendChild(firstChild.get()); |
| 119 root->appendChild(firstChild.get()); | 113 root->appendChild(middleChild.get()); |
| 120 root->appendChild(middleChild.get()); | 114 root->appendChild(lastChild.get()); |
| 121 root->appendChild(lastChild.get()); | 115 } |
| 122 } | |
| 123 | 116 |
| 124 RefPtr<TestTree> root; | 117 RefPtr<TestTree> root; |
| 125 RefPtr<TestTree> firstChild; | 118 RefPtr<TestTree> firstChild; |
| 126 RefPtr<TestTree> middleChild; | 119 RefPtr<TestTree> middleChild; |
| 127 RefPtr<TestTree> lastChild; | 120 RefPtr<TestTree> lastChild; |
| 128 }; | 121 }; |
| 129 | 122 |
| 130 TEST(TreeNodeTest, RemoveMiddle) | 123 TEST(TreeNodeTest, RemoveMiddle) { |
| 131 { | 124 Trio trio; |
| 132 Trio trio; | 125 trio.appendChildren(); |
| 133 trio.appendChildren(); | |
| 134 | 126 |
| 135 trio.root->removeChild(trio.middleChild.get()); | 127 trio.root->removeChild(trio.middleChild.get()); |
| 136 EXPECT_TRUE(trio.middleChild->orphan()); | 128 EXPECT_TRUE(trio.middleChild->orphan()); |
| 137 EXPECT_EQ(trio.firstChild->next(), trio.lastChild.get()); | 129 EXPECT_EQ(trio.firstChild->next(), trio.lastChild.get()); |
| 138 EXPECT_EQ(trio.lastChild->previous(), trio.firstChild.get()); | 130 EXPECT_EQ(trio.lastChild->previous(), trio.firstChild.get()); |
| 139 EXPECT_EQ(trio.root->firstChild(), trio.firstChild.get()); | 131 EXPECT_EQ(trio.root->firstChild(), trio.firstChild.get()); |
| 140 EXPECT_EQ(trio.root->lastChild(), trio.lastChild.get()); | 132 EXPECT_EQ(trio.root->lastChild(), trio.lastChild.get()); |
| 141 } | 133 } |
| 142 | 134 |
| 143 TEST(TreeNodeTest, RemoveLast) | 135 TEST(TreeNodeTest, RemoveLast) { |
| 144 { | 136 RefPtr<TestTree> nullNode; |
| 145 RefPtr<TestTree> nullNode; | 137 Trio trio; |
| 146 Trio trio; | 138 trio.appendChildren(); |
| 147 trio.appendChildren(); | |
| 148 | 139 |
| 149 trio.root->removeChild(trio.lastChild.get()); | 140 trio.root->removeChild(trio.lastChild.get()); |
| 150 EXPECT_TRUE(trio.lastChild->orphan()); | 141 EXPECT_TRUE(trio.lastChild->orphan()); |
| 151 EXPECT_EQ(trio.middleChild->next(), nullNode.get()); | 142 EXPECT_EQ(trio.middleChild->next(), nullNode.get()); |
| 152 EXPECT_EQ(trio.root->firstChild(), trio.firstChild.get()); | 143 EXPECT_EQ(trio.root->firstChild(), trio.firstChild.get()); |
| 153 EXPECT_EQ(trio.root->lastChild(), trio.middleChild.get()); | 144 EXPECT_EQ(trio.root->lastChild(), trio.middleChild.get()); |
| 154 } | 145 } |
| 155 | 146 |
| 156 TEST(TreeNodeTest, RemoveFirst) | 147 TEST(TreeNodeTest, RemoveFirst) { |
| 157 { | 148 RefPtr<TestTree> nullNode; |
| 158 RefPtr<TestTree> nullNode; | 149 Trio trio; |
| 159 Trio trio; | 150 trio.appendChildren(); |
| 160 trio.appendChildren(); | |
| 161 | 151 |
| 162 trio.root->removeChild(trio.firstChild.get()); | 152 trio.root->removeChild(trio.firstChild.get()); |
| 163 EXPECT_TRUE(trio.firstChild->orphan()); | 153 EXPECT_TRUE(trio.firstChild->orphan()); |
| 164 EXPECT_EQ(trio.middleChild->previous(), nullNode.get()); | 154 EXPECT_EQ(trio.middleChild->previous(), nullNode.get()); |
| 165 EXPECT_EQ(trio.root->firstChild(), trio.middleChild.get()); | 155 EXPECT_EQ(trio.root->firstChild(), trio.middleChild.get()); |
| 166 EXPECT_EQ(trio.root->lastChild(), trio.lastChild.get()); | 156 EXPECT_EQ(trio.root->lastChild(), trio.lastChild.get()); |
| 167 } | 157 } |
| 168 | 158 |
| 169 TEST(TreeNodeTest, TakeChildrenFrom) | 159 TEST(TreeNodeTest, TakeChildrenFrom) { |
| 170 { | 160 RefPtr<TestTree> newParent = TestTree::create(); |
| 171 RefPtr<TestTree> newParent = TestTree::create(); | 161 Trio trio; |
| 172 Trio trio; | 162 trio.appendChildren(); |
| 173 trio.appendChildren(); | |
| 174 | 163 |
| 175 newParent->takeChildrenFrom(trio.root.get()); | 164 newParent->takeChildrenFrom(trio.root.get()); |
| 176 | 165 |
| 177 EXPECT_FALSE(trio.root->hasChildren()); | 166 EXPECT_FALSE(trio.root->hasChildren()); |
| 178 EXPECT_TRUE(newParent->hasChildren()); | 167 EXPECT_TRUE(newParent->hasChildren()); |
| 179 EXPECT_EQ(trio.firstChild.get(), newParent->firstChild()); | 168 EXPECT_EQ(trio.firstChild.get(), newParent->firstChild()); |
| 180 EXPECT_EQ(trio.middleChild.get(), newParent->firstChild()->next()); | 169 EXPECT_EQ(trio.middleChild.get(), newParent->firstChild()->next()); |
| 181 EXPECT_EQ(trio.lastChild.get(), newParent->lastChild()); | 170 EXPECT_EQ(trio.lastChild.get(), newParent->lastChild()); |
| 182 } | 171 } |
| 183 | 172 |
| 184 class TrioWithGrandChild : public Trio { | 173 class TrioWithGrandChild : public Trio { |
| 185 public: | 174 public: |
| 186 TrioWithGrandChild() | 175 TrioWithGrandChild() : grandChild(TestTree::create()) {} |
| 187 : grandChild(TestTree::create()) | |
| 188 { | |
| 189 } | |
| 190 | 176 |
| 191 void appendChildren() | 177 void appendChildren() { |
| 192 { | 178 Trio::appendChildren(); |
| 193 Trio::appendChildren(); | 179 middleChild->appendChild(grandChild.get()); |
| 194 middleChild->appendChild(grandChild.get()); | 180 } |
| 195 } | |
| 196 | 181 |
| 197 RefPtr<TestTree> grandChild; | 182 RefPtr<TestTree> grandChild; |
| 198 }; | 183 }; |
| 199 | 184 |
| 200 TEST(TreeNodeTest, TraverseNext) | 185 TEST(TreeNodeTest, TraverseNext) { |
| 201 { | 186 TrioWithGrandChild trio; |
| 202 TrioWithGrandChild trio; | 187 trio.appendChildren(); |
| 203 trio.appendChildren(); | |
| 204 | 188 |
| 205 TestTree* order[] = { | 189 TestTree* order[] = {trio.root.get(), trio.firstChild.get(), |
| 206 trio.root.get(), trio.firstChild.get(), trio.middleChild.get(), | 190 trio.middleChild.get(), trio.grandChild.get(), |
| 207 trio.grandChild.get(), trio.lastChild.get() | 191 trio.lastChild.get()}; |
| 208 }; | |
| 209 | 192 |
| 210 unsigned orderIndex = 0; | 193 unsigned orderIndex = 0; |
| 211 for (TestTree* node = trio.root.get(); node; node = traverseNext(node), orde
rIndex++) | 194 for (TestTree *node = trio.root.get(); node; |
| 212 EXPECT_EQ(node, order[orderIndex]); | 195 node = traverseNext(node), orderIndex++) |
| 213 EXPECT_EQ(orderIndex, sizeof(order) / sizeof(TestTree*)); | 196 EXPECT_EQ(node, order[orderIndex]); |
| 197 EXPECT_EQ(orderIndex, sizeof(order) / sizeof(TestTree*)); |
| 214 } | 198 } |
| 215 | 199 |
| 216 TEST(TreeNodeTest, TraverseNextPostORder) | 200 TEST(TreeNodeTest, TraverseNextPostORder) { |
| 217 { | 201 TrioWithGrandChild trio; |
| 218 TrioWithGrandChild trio; | 202 trio.appendChildren(); |
| 219 trio.appendChildren(); | |
| 220 | 203 |
| 204 TestTree* order[] = {trio.firstChild.get(), trio.grandChild.get(), |
| 205 trio.middleChild.get(), trio.lastChild.get(), |
| 206 trio.root.get()}; |
| 221 | 207 |
| 222 TestTree* order[] = { | 208 unsigned orderIndex = 0; |
| 223 trio.firstChild.get(), | 209 for (TestTree *node = traverseFirstPostOrder(trio.root.get()); node; |
| 224 trio.grandChild.get(), trio.middleChild.get(), trio.lastChild.get(), tri
o.root.get() | 210 node = traverseNextPostOrder(node), orderIndex++) |
| 225 }; | 211 EXPECT_EQ(node, order[orderIndex]); |
| 226 | 212 EXPECT_EQ(orderIndex, sizeof(order) / sizeof(TestTree*)); |
| 227 unsigned orderIndex = 0; | |
| 228 for (TestTree* node = traverseFirstPostOrder(trio.root.get()); node; node =
traverseNextPostOrder(node), orderIndex++) | |
| 229 EXPECT_EQ(node, order[orderIndex]); | |
| 230 EXPECT_EQ(orderIndex, sizeof(order) / sizeof(TestTree*)); | |
| 231 | |
| 232 } | 213 } |
| 233 | 214 |
| 234 } // namespace WTF | 215 } // namespace WTF |
| OLD | NEW |