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