Add dbghelp.dll that works on XP, and include in base.isolate
[chromium-blink-merge.git] / base / containers / linked_list_unittest.cc
blob93a9f385084c0e649ff1c44e7fe547e1dad4a155
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/containers/linked_list.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace base {
10 namespace {
12 class Node : public LinkNode<Node> {
13 public:
14 explicit Node(int id) : id_(id) {}
16 int id() const { return id_; }
18 private:
19 int id_;
22 class MultipleInheritanceNodeBase {
23 public:
24 MultipleInheritanceNodeBase() : field_taking_up_space_(0) {}
25 int field_taking_up_space_;
28 class MultipleInheritanceNode : public MultipleInheritanceNodeBase,
29 public LinkNode<MultipleInheritanceNode> {
30 public:
31 MultipleInheritanceNode() {}
34 // Checks that when iterating |list| (either from head to tail, or from
35 // tail to head, as determined by |forward|), we get back |node_ids|,
36 // which is an array of size |num_nodes|.
37 void ExpectListContentsForDirection(const LinkedList<Node>& list,
38 int num_nodes, const int* node_ids, bool forward) {
39 int i = 0;
40 for (const LinkNode<Node>* node = (forward ? list.head() : list.tail());
41 node != list.end();
42 node = (forward ? node->next() : node->previous())) {
43 ASSERT_LT(i, num_nodes);
44 int index_of_id = forward ? i : num_nodes - i - 1;
45 EXPECT_EQ(node_ids[index_of_id], node->value()->id());
46 ++i;
48 EXPECT_EQ(num_nodes, i);
51 void ExpectListContents(const LinkedList<Node>& list,
52 int num_nodes,
53 const int* node_ids) {
55 SCOPED_TRACE("Iterating forward (from head to tail)");
56 ExpectListContentsForDirection(list, num_nodes, node_ids, true);
59 SCOPED_TRACE("Iterating backward (from tail to head)");
60 ExpectListContentsForDirection(list, num_nodes, node_ids, false);
64 TEST(LinkedList, Empty) {
65 LinkedList<Node> list;
66 EXPECT_EQ(list.end(), list.head());
67 EXPECT_EQ(list.end(), list.tail());
68 ExpectListContents(list, 0, NULL);
71 TEST(LinkedList, Append) {
72 LinkedList<Node> list;
73 ExpectListContents(list, 0, NULL);
75 Node n1(1);
76 list.Append(&n1);
78 EXPECT_EQ(&n1, list.head());
79 EXPECT_EQ(&n1, list.tail());
81 const int expected[] = {1};
82 ExpectListContents(list, arraysize(expected), expected);
85 Node n2(2);
86 list.Append(&n2);
88 EXPECT_EQ(&n1, list.head());
89 EXPECT_EQ(&n2, list.tail());
91 const int expected[] = {1, 2};
92 ExpectListContents(list, arraysize(expected), expected);
95 Node n3(3);
96 list.Append(&n3);
98 EXPECT_EQ(&n1, list.head());
99 EXPECT_EQ(&n3, list.tail());
101 const int expected[] = {1, 2, 3};
102 ExpectListContents(list, arraysize(expected), expected);
106 TEST(LinkedList, RemoveFromList) {
107 LinkedList<Node> list;
109 Node n1(1);
110 Node n2(2);
111 Node n3(3);
112 Node n4(4);
113 Node n5(5);
115 list.Append(&n1);
116 list.Append(&n2);
117 list.Append(&n3);
118 list.Append(&n4);
119 list.Append(&n5);
121 EXPECT_EQ(&n1, list.head());
122 EXPECT_EQ(&n5, list.tail());
124 const int expected[] = {1, 2, 3, 4, 5};
125 ExpectListContents(list, arraysize(expected), expected);
128 // Remove from the middle.
129 n3.RemoveFromList();
131 EXPECT_EQ(&n1, list.head());
132 EXPECT_EQ(&n5, list.tail());
134 const int expected[] = {1, 2, 4, 5};
135 ExpectListContents(list, arraysize(expected), expected);
138 // Remove from the tail.
139 n5.RemoveFromList();
141 EXPECT_EQ(&n1, list.head());
142 EXPECT_EQ(&n4, list.tail());
144 const int expected[] = {1, 2, 4};
145 ExpectListContents(list, arraysize(expected), expected);
148 // Remove from the head.
149 n1.RemoveFromList();
151 EXPECT_EQ(&n2, list.head());
152 EXPECT_EQ(&n4, list.tail());
154 const int expected[] = {2, 4};
155 ExpectListContents(list, arraysize(expected), expected);
158 // Empty the list.
159 n2.RemoveFromList();
160 n4.RemoveFromList();
162 ExpectListContents(list, 0, NULL);
163 EXPECT_EQ(list.end(), list.head());
164 EXPECT_EQ(list.end(), list.tail());
166 // Fill the list once again.
167 list.Append(&n1);
168 list.Append(&n2);
169 list.Append(&n3);
170 list.Append(&n4);
171 list.Append(&n5);
173 EXPECT_EQ(&n1, list.head());
174 EXPECT_EQ(&n5, list.tail());
176 const int expected[] = {1, 2, 3, 4, 5};
177 ExpectListContents(list, arraysize(expected), expected);
181 TEST(LinkedList, InsertBefore) {
182 LinkedList<Node> list;
184 Node n1(1);
185 Node n2(2);
186 Node n3(3);
187 Node n4(4);
189 list.Append(&n1);
190 list.Append(&n2);
192 EXPECT_EQ(&n1, list.head());
193 EXPECT_EQ(&n2, list.tail());
195 const int expected[] = {1, 2};
196 ExpectListContents(list, arraysize(expected), expected);
199 n3.InsertBefore(&n2);
201 EXPECT_EQ(&n1, list.head());
202 EXPECT_EQ(&n2, list.tail());
204 const int expected[] = {1, 3, 2};
205 ExpectListContents(list, arraysize(expected), expected);
208 n4.InsertBefore(&n1);
210 EXPECT_EQ(&n4, list.head());
211 EXPECT_EQ(&n2, list.tail());
213 const int expected[] = {4, 1, 3, 2};
214 ExpectListContents(list, arraysize(expected), expected);
218 TEST(LinkedList, InsertAfter) {
219 LinkedList<Node> list;
221 Node n1(1);
222 Node n2(2);
223 Node n3(3);
224 Node n4(4);
226 list.Append(&n1);
227 list.Append(&n2);
229 EXPECT_EQ(&n1, list.head());
230 EXPECT_EQ(&n2, list.tail());
232 const int expected[] = {1, 2};
233 ExpectListContents(list, arraysize(expected), expected);
236 n3.InsertAfter(&n2);
238 EXPECT_EQ(&n1, list.head());
239 EXPECT_EQ(&n3, list.tail());
241 const int expected[] = {1, 2, 3};
242 ExpectListContents(list, arraysize(expected), expected);
245 n4.InsertAfter(&n1);
247 EXPECT_EQ(&n1, list.head());
248 EXPECT_EQ(&n3, list.tail());
250 const int expected[] = {1, 4, 2, 3};
251 ExpectListContents(list, arraysize(expected), expected);
255 TEST(LinkedList, MultipleInheritanceNode) {
256 MultipleInheritanceNode node;
257 EXPECT_EQ(&node, node.value());
260 TEST(LinkedList, EmptyListIsEmpty) {
261 LinkedList<Node> list;
262 EXPECT_TRUE(list.empty());
265 TEST(LinkedList, NonEmptyListIsNotEmpty) {
266 LinkedList<Node> list;
268 Node n(1);
269 list.Append(&n);
271 EXPECT_FALSE(list.empty());
274 TEST(LinkedList, EmptiedListIsEmptyAgain) {
275 LinkedList<Node> list;
277 Node n(1);
278 list.Append(&n);
279 n.RemoveFromList();
281 EXPECT_TRUE(list.empty());
284 TEST(LinkedList, NodesCanBeReused) {
285 LinkedList<Node> list1;
286 LinkedList<Node> list2;
288 Node n(1);
289 list1.Append(&n);
290 n.RemoveFromList();
291 list2.Append(&n);
293 EXPECT_EQ(list2.head()->value(), &n);
296 TEST(LinkedList, RemovedNodeHasNullNextPrevious) {
297 LinkedList<Node> list;
299 Node n(1);
300 list.Append(&n);
301 n.RemoveFromList();
303 EXPECT_EQ(NULL, n.next());
304 EXPECT_EQ(NULL, n.previous());
307 } // namespace
308 } // namespace base