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"
12 class Node
: public LinkNode
<Node
> {
14 explicit Node(int id
) : id_(id
) {}
16 int id() const { return id_
; }
22 class MultipleInheritanceNodeBase
{
24 MultipleInheritanceNodeBase() : field_taking_up_space_(0) {}
25 int field_taking_up_space_
;
28 class MultipleInheritanceNode
: public MultipleInheritanceNodeBase
,
29 public LinkNode
<MultipleInheritanceNode
> {
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
) {
40 for (const LinkNode
<Node
>* node
= (forward
? list
.head() : list
.tail());
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());
48 EXPECT_EQ(num_nodes
, i
);
51 void ExpectListContents(const LinkedList
<Node
>& list
,
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
);
78 EXPECT_EQ(&n1
, list
.head());
79 EXPECT_EQ(&n1
, list
.tail());
81 const int expected
[] = {1};
82 ExpectListContents(list
, arraysize(expected
), expected
);
88 EXPECT_EQ(&n1
, list
.head());
89 EXPECT_EQ(&n2
, list
.tail());
91 const int expected
[] = {1, 2};
92 ExpectListContents(list
, arraysize(expected
), expected
);
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
;
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.
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.
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.
151 EXPECT_EQ(&n2
, list
.head());
152 EXPECT_EQ(&n4
, list
.tail());
154 const int expected
[] = {2, 4};
155 ExpectListContents(list
, arraysize(expected
), expected
);
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.
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
;
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
;
229 EXPECT_EQ(&n1
, list
.head());
230 EXPECT_EQ(&n2
, list
.tail());
232 const int expected
[] = {1, 2};
233 ExpectListContents(list
, arraysize(expected
), expected
);
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
);
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
;
271 EXPECT_FALSE(list
.empty());
274 TEST(LinkedList
, EmptiedListIsEmptyAgain
) {
275 LinkedList
<Node
> list
;
281 EXPECT_TRUE(list
.empty());
284 TEST(LinkedList
, NodesCanBeReused
) {
285 LinkedList
<Node
> list1
;
286 LinkedList
<Node
> list2
;
293 EXPECT_EQ(list2
.head()->value(), &n
);
296 TEST(LinkedList
, RemovedNodeHasNullNextPrevious
) {
297 LinkedList
<Node
> list
;
303 EXPECT_EQ(NULL
, n
.next());
304 EXPECT_EQ(NULL
, n
.previous());