blob: 18f6c41a64807636d32bd9613631db5dbd646583 [file] [log] [blame]
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +00001//===- unittests/ADT/IListTest.cpp - ilist unit tests ---------------------===//
Daniel Dunbar959ae592010-05-12 21:35:19 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar959ae592010-05-12 21:35:19 +00006//
7//===----------------------------------------------------------------------===//
8
Daniel Dunbar959ae592010-05-12 21:35:19 +00009#include "llvm/ADT/ilist.h"
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruthb034cb72013-01-02 10:26:28 +000011#include "llvm/ADT/ilist_node.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000012#include "gtest/gtest.h"
13#include <ostream>
Daniel Dunbar959ae592010-05-12 21:35:19 +000014
15using namespace llvm;
16
17namespace {
18
19struct Node : ilist_node<Node> {
20 int Value;
21
22 Node() {}
David Blaikie0bc56da2015-03-04 17:01:18 +000023 Node(int Value) : Value(Value) {}
24 Node(const Node&) = default;
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +000025 ~Node() { Value = -1; }
Daniel Dunbar959ae592010-05-12 21:35:19 +000026};
27
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +000028TEST(IListTest, Basic) {
Daniel Dunbar959ae592010-05-12 21:35:19 +000029 ilist<Node> List;
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +000030 List.push_back(new Node(1));
Daniel Dunbar959ae592010-05-12 21:35:19 +000031 EXPECT_EQ(1, List.back().Value);
Duncan P. N. Exon Smith4042d912015-11-11 02:26:42 +000032 EXPECT_EQ(nullptr, List.getPrevNode(List.back()));
33 EXPECT_EQ(nullptr, List.getNextNode(List.back()));
Daniel Dunbar959ae592010-05-12 21:35:19 +000034
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +000035 List.push_back(new Node(2));
Daniel Dunbar959ae592010-05-12 21:35:19 +000036 EXPECT_EQ(2, List.back().Value);
Duncan P. N. Exon Smith4042d912015-11-11 02:26:42 +000037 EXPECT_EQ(2, List.getNextNode(List.front())->Value);
38 EXPECT_EQ(1, List.getPrevNode(List.back())->Value);
Daniel Dunbar2842f252010-05-13 18:35:02 +000039
40 const ilist<Node> &ConstList = List;
41 EXPECT_EQ(2, ConstList.back().Value);
Duncan P. N. Exon Smith4042d912015-11-11 02:26:42 +000042 EXPECT_EQ(2, ConstList.getNextNode(ConstList.front())->Value);
43 EXPECT_EQ(1, ConstList.getPrevNode(ConstList.back())->Value);
Daniel Dunbar959ae592010-05-12 21:35:19 +000044}
45
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +000046TEST(IListTest, cloneFrom) {
47 Node L1Nodes[] = {Node(0), Node(1)};
48 Node L2Nodes[] = {Node(0), Node(1)};
49 ilist<Node> L1, L2, L3;
50
51 // Build L1 from L1Nodes.
52 L1.push_back(&L1Nodes[0]);
53 L1.push_back(&L1Nodes[1]);
54
55 // Build L2 from L2Nodes, based on L1 nodes.
56 L2.cloneFrom(L1, [&](const Node &N) { return &L2Nodes[N.Value]; });
57
58 // Add a node to L3 to be deleted, and then rebuild L3 by copying L1.
59 L3.push_back(new Node(7));
60 L3.cloneFrom(L1, [](const Node &N) { return new Node(N); });
61
62 EXPECT_EQ(2u, L1.size());
63 EXPECT_EQ(&L1Nodes[0], &L1.front());
64 EXPECT_EQ(&L1Nodes[1], &L1.back());
65 EXPECT_EQ(2u, L2.size());
66 EXPECT_EQ(&L2Nodes[0], &L2.front());
67 EXPECT_EQ(&L2Nodes[1], &L2.back());
68 EXPECT_EQ(2u, L3.size());
69 EXPECT_EQ(0, L3.front().Value);
70 EXPECT_EQ(1, L3.back().Value);
71
72 // Don't free nodes on the stack.
73 L1.clearAndLeakNodesUnsafely();
74 L2.clearAndLeakNodesUnsafely();
75}
76
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +000077TEST(IListTest, SpliceOne) {
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000078 ilist<Node> List;
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +000079 List.push_back(new Node(1));
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000080
81 // The single-element splice operation supports noops.
82 List.splice(List.begin(), List, List.begin());
83 EXPECT_EQ(1u, List.size());
84 EXPECT_EQ(1, List.front().Value);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000085 EXPECT_TRUE(std::next(List.begin()) == List.end());
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000086
87 // Altenative noop. Move the first element behind itself.
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +000088 List.push_back(new Node(2));
89 List.push_back(new Node(3));
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000090 List.splice(std::next(List.begin()), List, List.begin());
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000091 EXPECT_EQ(3u, List.size());
92 EXPECT_EQ(1, List.front().Value);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000093 EXPECT_EQ(2, std::next(List.begin())->Value);
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000094 EXPECT_EQ(3, List.back().Value);
95}
96
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +000097TEST(IListTest, SpliceSwap) {
Duncan P. N. Exon Smith47416612016-08-17 02:08:08 +000098 ilist<Node> L;
99 Node N0(0);
100 Node N1(1);
101 L.insert(L.end(), &N0);
102 L.insert(L.end(), &N1);
103 EXPECT_EQ(0, L.front().Value);
104 EXPECT_EQ(1, L.back().Value);
105
106 L.splice(L.begin(), L, ++L.begin());
107 EXPECT_EQ(1, L.front().Value);
108 EXPECT_EQ(0, L.back().Value);
109
110 L.clearAndLeakNodesUnsafely();
111}
112
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000113TEST(IListTest, SpliceSwapOtherWay) {
Duncan P. N. Exon Smith47416612016-08-17 02:08:08 +0000114 ilist<Node> L;
115 Node N0(0);
116 Node N1(1);
117 L.insert(L.end(), &N0);
118 L.insert(L.end(), &N1);
119 EXPECT_EQ(0, L.front().Value);
120 EXPECT_EQ(1, L.back().Value);
121
122 L.splice(L.end(), L, L.begin());
123 EXPECT_EQ(1, L.front().Value);
124 EXPECT_EQ(0, L.back().Value);
125
126 L.clearAndLeakNodesUnsafely();
127}
128
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000129TEST(IListTest, UnsafeClear) {
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +0000130 ilist<Node> List;
131
132 // Before even allocating a sentinel.
133 List.clearAndLeakNodesUnsafely();
134 EXPECT_EQ(0u, List.size());
135
136 // Empty list with sentinel.
137 ilist<Node>::iterator E = List.end();
138 List.clearAndLeakNodesUnsafely();
139 EXPECT_EQ(0u, List.size());
140 // The sentinel shouldn't change.
141 EXPECT_TRUE(E == List.end());
142
143 // List with contents.
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +0000144 List.push_back(new Node(1));
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +0000145 ASSERT_EQ(1u, List.size());
Duncan P. N. Exon Smithc8925b12015-10-20 18:30:20 +0000146 Node *N = &*List.begin();
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +0000147 EXPECT_EQ(1, N->Value);
148 List.clearAndLeakNodesUnsafely();
149 EXPECT_EQ(0u, List.size());
150 ASSERT_EQ(1, N->Value);
151 delete N;
152
153 // List is still functional.
Duncan P. N. Exon Smithb5da0052016-09-11 23:43:43 +0000154 List.push_back(new Node(5));
155 List.push_back(new Node(6));
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +0000156 ASSERT_EQ(2u, List.size());
157 EXPECT_EQ(5, List.front().Value);
158 EXPECT_EQ(6, List.back().Value);
159}
160
Duncan P. N. Exon Smith11cb5382016-08-19 20:17:23 +0000161struct Empty {};
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000162TEST(IListTest, HasObsoleteCustomizationTrait) {
Duncan P. N. Exon Smith11cb5382016-08-19 20:17:23 +0000163 // Negative test for HasObsoleteCustomization.
164 static_assert(!ilist_detail::HasObsoleteCustomization<Empty, Node>::value,
165 "Empty has no customizations");
Daniel Dunbar959ae592010-05-12 21:35:19 +0000166}
Duncan P. N. Exon Smith11cb5382016-08-19 20:17:23 +0000167
168struct GetNext {
169 Node *getNext(Node *);
170};
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000171TEST(IListTest, HasGetNextTrait) {
Duncan P. N. Exon Smith11cb5382016-08-19 20:17:23 +0000172 static_assert(ilist_detail::HasGetNext<GetNext, Node>::value,
173 "GetNext has a getNext(Node*)");
174 static_assert(ilist_detail::HasObsoleteCustomization<GetNext, Node>::value,
175 "Empty should be obsolete because of getNext()");
176
177 // Negative test for HasGetNext.
178 static_assert(!ilist_detail::HasGetNext<Empty, Node>::value,
179 "Empty does not have a getNext(Node*)");
180}
181
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000182struct CreateSentinel {
183 Node *createSentinel();
184};
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000185TEST(IListTest, HasCreateSentinelTrait) {
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000186 static_assert(ilist_detail::HasCreateSentinel<CreateSentinel>::value,
187 "CreateSentinel has a getNext(Node*)");
188 static_assert(
189 ilist_detail::HasObsoleteCustomization<CreateSentinel, Node>::value,
190 "Empty should be obsolete because of createSentinel()");
191
192 // Negative test for HasCreateSentinel.
193 static_assert(!ilist_detail::HasCreateSentinel<Empty>::value,
194 "Empty does not have a createSentinel()");
195}
196
197struct NodeWithCallback : ilist_node<NodeWithCallback> {
198 int Value = 0;
199 bool IsInList = false;
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000200 bool WasTransferred = false;
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000201
202 NodeWithCallback() = default;
203 NodeWithCallback(int Value) : Value(Value) {}
204 NodeWithCallback(const NodeWithCallback &) = delete;
205};
206
207} // end namespace
208
209namespace llvm {
Reid Klecknere80799e2019-01-23 22:59:52 +0000210// These nodes are stack-allocated for testing purposes, so don't let the ilist
211// own or delete them.
212template <> struct ilist_alloc_traits<NodeWithCallback> {
213 static void deleteNode(NodeWithCallback *) {}
214};
215
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000216template <> struct ilist_callback_traits<NodeWithCallback> {
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000217 void addNodeToList(NodeWithCallback *N) { N->IsInList = true; }
218 void removeNodeFromList(NodeWithCallback *N) { N->IsInList = false; }
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000219 template <class Iterator>
220 void transferNodesFromList(ilist_callback_traits &Other, Iterator First,
221 Iterator Last) {
222 for (; First != Last; ++First) {
223 First->WasTransferred = true;
224 Other.removeNodeFromList(&*First);
225 addNodeToList(&*First);
226 }
227 }
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000228};
229} // end namespace llvm
230
231namespace {
232
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000233TEST(IListTest, addNodeToList) {
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000234 ilist<NodeWithCallback> L1, L2;
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000235 NodeWithCallback N(7);
236 ASSERT_FALSE(N.IsInList);
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000237 ASSERT_FALSE(N.WasTransferred);
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000238
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000239 L1.insert(L1.begin(), &N);
240 ASSERT_EQ(1u, L1.size());
241 ASSERT_EQ(&N, &L1.front());
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000242 ASSERT_TRUE(N.IsInList);
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000243 ASSERT_FALSE(N.WasTransferred);
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000244
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000245 L2.splice(L2.end(), L1);
246 ASSERT_EQ(&N, &L2.front());
247 ASSERT_TRUE(N.IsInList);
248 ASSERT_TRUE(N.WasTransferred);
249
250 L1.remove(&N);
251 ASSERT_EQ(0u, L1.size());
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000252 ASSERT_FALSE(N.IsInList);
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000253 ASSERT_TRUE(N.WasTransferred);
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000254}
255
Reid Klecknere80799e2019-01-23 22:59:52 +0000256TEST(IListTest, sameListSplice) {
257 NodeWithCallback N1(1);
258 NodeWithCallback N2(2);
259 ASSERT_FALSE(N1.WasTransferred);
260 ASSERT_FALSE(N2.WasTransferred);
261
262 ilist<NodeWithCallback> L1;
263 L1.insert(L1.end(), &N1);
264 L1.insert(L1.end(), &N2);
265 ASSERT_EQ(2u, L1.size());
266 ASSERT_EQ(&N1, &L1.front());
267 ASSERT_FALSE(N1.WasTransferred);
268 ASSERT_FALSE(N2.WasTransferred);
269
270 // Swap the nodes with splice inside the same list. Check that we get the
271 // transfer callback.
272 L1.splice(L1.begin(), L1, std::next(L1.begin()), L1.end());
273 ASSERT_EQ(2u, L1.size());
274 ASSERT_EQ(&N1, &L1.back());
275 ASSERT_EQ(&N2, &L1.front());
276 ASSERT_FALSE(N1.WasTransferred);
277 ASSERT_TRUE(N2.WasTransferred);
278}
279
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000280struct PrivateNode : private ilist_node<PrivateNode> {
Duncan P. N. Exon Smith34c4d2a2016-09-10 16:55:06 +0000281 friend struct llvm::ilist_detail::NodeAccess;
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000282
283 int Value = 0;
284
285 PrivateNode() = default;
286 PrivateNode(int Value) : Value(Value) {}
287 PrivateNode(const PrivateNode &) = delete;
288};
289
Duncan P. N. Exon Smithfbdb2012016-08-30 00:18:43 +0000290TEST(IListTest, privateNode) {
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000291 // Instantiate various APIs to be sure they're callable when ilist_node is
292 // inherited privately.
Duncan P. N. Exon Smithe974f572016-09-03 01:06:08 +0000293 ilist<PrivateNode> L;
294 PrivateNode N(7);
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000295 L.insert(L.begin(), &N);
296 ++L.begin();
297 (void)*L.begin();
298 (void)(L.begin() == L.end());
299
Duncan P. N. Exon Smithe974f572016-09-03 01:06:08 +0000300 ilist<PrivateNode> L2;
Duncan P. N. Exon Smith64093a32016-08-19 20:40:12 +0000301 L2.splice(L2.end(), L);
302 L2.remove(&N);
303}
304
Duncan P. N. Exon Smith11cb5382016-08-19 20:17:23 +0000305} // end namespace