1 //===- unittests/ADT/SimpleIListTest.cpp - simple_ilist unit tests --------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/simple_ilist.h"
10 #include "gtest/gtest.h"
16 struct Node
: ilist_node
<Node
> {};
17 bool operator<(const Node
&L
, const Node
&R
) { return &L
< &R
; }
18 bool makeFalse(const Node
&, const Node
&) { return false; }
20 struct deleteNode
: std::default_delete
<Node
> {};
21 void doNothing(Node
*) {}
23 TEST(SimpleIListTest
, DefaultConstructor
) {
25 EXPECT_EQ(L
.begin(), L
.end());
26 EXPECT_TRUE(L
.empty());
27 EXPECT_EQ(0u, L
.size());
30 TEST(SimpleIListTest
, pushPopFront
) {
35 EXPECT_EQ(&A
, &L
.front());
36 EXPECT_EQ(&B
, &L
.back());
37 EXPECT_FALSE(L
.empty());
38 EXPECT_EQ(2u, L
.size());
40 // Pop front and check the new front.
42 EXPECT_EQ(&B
, &L
.front());
46 EXPECT_TRUE(L
.empty());
49 TEST(SimpleIListTest
, pushPopBack
) {
54 EXPECT_EQ(&A
, &L
.front());
55 EXPECT_EQ(&B
, &L
.back());
56 EXPECT_FALSE(L
.empty());
57 EXPECT_EQ(2u, L
.size());
59 // Pop back and check the new front.
61 EXPECT_EQ(&A
, &L
.back());
65 EXPECT_TRUE(L
.empty());
68 TEST(SimpleIListTest
, swap
) {
69 simple_ilist
<Node
> L1
, L2
;
74 EXPECT_TRUE(L1
.empty());
75 EXPECT_EQ(0u, L1
.size());
76 EXPECT_EQ(&A
, &L2
.front());
77 EXPECT_EQ(&B
, &L2
.back());
78 EXPECT_FALSE(L2
.empty());
79 EXPECT_EQ(2u, L2
.size());
82 TEST(SimpleIListTest
, insertEraseAtEnd
) {
87 EXPECT_EQ(&A
, &L
.front());
88 EXPECT_EQ(&B
, &L
.back());
89 EXPECT_FALSE(L
.empty());
90 EXPECT_EQ(2u, L
.size());
93 TEST(SimpleIListTest
, insertAtBegin
) {
96 L
.insert(L
.begin(), B
);
97 L
.insert(L
.begin(), A
);
98 EXPECT_EQ(&A
, &L
.front());
99 EXPECT_EQ(&B
, &L
.back());
100 EXPECT_FALSE(L
.empty());
101 EXPECT_EQ(2u, L
.size());
104 TEST(SimpleIListTest
, remove
) {
105 simple_ilist
<Node
> L
;
110 EXPECT_EQ(&A
, &L
.front());
111 EXPECT_EQ(&B
, &*++L
.begin());
112 EXPECT_EQ(&C
, &L
.back());
113 EXPECT_EQ(3u, L
.size());
116 EXPECT_EQ(&A
, &L
.front());
117 EXPECT_EQ(&C
, &L
.back());
118 EXPECT_EQ(2u, L
.size());
121 EXPECT_EQ(&C
, &L
.front());
122 EXPECT_EQ(1u, L
.size());
125 EXPECT_TRUE(L
.empty());
128 TEST(SimpleIListTest
, removeAndDispose
) {
129 simple_ilist
<Node
> L
;
135 EXPECT_EQ(&A
, &L
.front());
136 EXPECT_EQ(B
, &*++L
.begin());
137 EXPECT_EQ(&C
, &L
.back());
138 EXPECT_EQ(3u, L
.size());
140 L
.removeAndDispose(*B
, deleteNode());
141 EXPECT_EQ(&A
, &L
.front());
142 EXPECT_EQ(&C
, &L
.back());
143 EXPECT_EQ(2u, L
.size());
146 TEST(SimpleIListTest
, removeAndDisposeNullDeleter
) {
147 simple_ilist
<Node
> L
;
152 EXPECT_EQ(&A
, &L
.front());
153 EXPECT_EQ(&B
, &*++L
.begin());
154 EXPECT_EQ(&C
, &L
.back());
155 EXPECT_EQ(3u, L
.size());
157 L
.removeAndDispose(B
, doNothing
);
158 EXPECT_EQ(&A
, &L
.front());
159 EXPECT_EQ(&C
, &L
.back());
160 EXPECT_EQ(2u, L
.size());
163 TEST(SimpleIListTest
, erase
) {
164 simple_ilist
<Node
> L
;
169 EXPECT_EQ(&A
, &L
.front());
170 EXPECT_EQ(&B
, &*++L
.begin());
171 EXPECT_EQ(&C
, &L
.back());
172 EXPECT_EQ(3u, L
.size());
174 EXPECT_EQ(C
.getIterator(), L
.erase(B
.getIterator()));
175 EXPECT_EQ(&A
, &L
.front());
176 EXPECT_EQ(&C
, &L
.back());
177 EXPECT_EQ(2u, L
.size());
180 TEST(SimpleIListTest
, reverse_iterator
) {
181 simple_ilist
<Node
> L
;
187 auto ReverseIter
= L
.rbegin();
188 EXPECT_EQ(C
.getReverseIterator(), ReverseIter
);
190 EXPECT_EQ(B
.getReverseIterator(), ReverseIter
);
192 EXPECT_EQ(A
.getReverseIterator(), ReverseIter
);
194 EXPECT_EQ(L
.rend(), ReverseIter
);
197 TEST(SimpleIListTest
, eraseAndDispose
) {
198 simple_ilist
<Node
> L
;
204 EXPECT_EQ(&A
, &L
.front());
205 EXPECT_EQ(B
, &*++L
.begin());
206 EXPECT_EQ(&C
, &L
.back());
207 EXPECT_EQ(3u, L
.size());
209 L
.eraseAndDispose(B
->getIterator(), deleteNode());
210 EXPECT_EQ(&A
, &L
.front());
211 EXPECT_EQ(&C
, &L
.back());
212 EXPECT_EQ(2u, L
.size());
215 TEST(SimpleIListTest
, eraseAndDisposeNullDeleter
) {
216 simple_ilist
<Node
> L
;
221 EXPECT_EQ(&A
, &L
.front());
222 EXPECT_EQ(&B
, &*++L
.begin());
223 EXPECT_EQ(&C
, &L
.back());
224 EXPECT_EQ(3u, L
.size());
226 L
.eraseAndDispose(B
.getIterator(), doNothing
);
227 EXPECT_EQ(&A
, &L
.front());
228 EXPECT_EQ(&C
, &L
.back());
229 EXPECT_EQ(2u, L
.size());
232 TEST(SimpleIListTest
, eraseRange
) {
233 simple_ilist
<Node
> L
;
241 EXPECT_EQ(&A
, &*I
++);
242 EXPECT_EQ(&B
, &*I
++);
243 EXPECT_EQ(&C
, &*I
++);
244 EXPECT_EQ(&D
, &*I
++);
245 EXPECT_EQ(&E
, &*I
++);
246 EXPECT_EQ(L
.end(), I
);
247 EXPECT_EQ(5u, L
.size());
250 EXPECT_EQ(E
.getIterator(), L
.erase(B
.getIterator(), E
.getIterator()));
251 EXPECT_EQ(&A
, &L
.front());
252 EXPECT_EQ(&E
, &L
.back());
253 EXPECT_EQ(2u, L
.size());
256 TEST(SimpleIListTest
, eraseAndDisposeRange
) {
257 simple_ilist
<Node
> L
;
258 Node A
, *B
= new Node
, *C
= new Node
, *D
= new Node
, E
;
265 EXPECT_EQ(&A
, &*I
++);
269 EXPECT_EQ(&E
, &*I
++);
270 EXPECT_EQ(L
.end(), I
);
271 EXPECT_EQ(5u, L
.size());
274 EXPECT_EQ(E
.getIterator(),
275 L
.eraseAndDispose(B
->getIterator(), E
.getIterator(), deleteNode()));
276 EXPECT_EQ(&A
, &L
.front());
277 EXPECT_EQ(&E
, &L
.back());
278 EXPECT_EQ(2u, L
.size());
281 TEST(SimpleIListTest
, eraseAndDisposeRangeNullDeleter
) {
282 simple_ilist
<Node
> L
;
290 EXPECT_EQ(&A
, &*I
++);
291 EXPECT_EQ(&B
, &*I
++);
292 EXPECT_EQ(&C
, &*I
++);
293 EXPECT_EQ(&D
, &*I
++);
294 EXPECT_EQ(&E
, &*I
++);
295 EXPECT_EQ(L
.end(), I
);
296 EXPECT_EQ(5u, L
.size());
299 EXPECT_EQ(E
.getIterator(),
300 L
.eraseAndDispose(B
.getIterator(), E
.getIterator(), doNothing
));
301 EXPECT_EQ(&A
, &L
.front());
302 EXPECT_EQ(&E
, &L
.back());
303 EXPECT_EQ(2u, L
.size());
306 TEST(SimpleIListTest
, clear
) {
307 simple_ilist
<Node
> L
;
312 EXPECT_TRUE(L
.empty());
313 EXPECT_EQ(0u, L
.size());
316 TEST(SimpleIListTest
, clearAndDispose
) {
317 simple_ilist
<Node
> L
;
322 L
.clearAndDispose(deleteNode());
323 EXPECT_TRUE(L
.empty());
324 EXPECT_EQ(0u, L
.size());
327 TEST(SimpleIListTest
, clearAndDisposeNullDeleter
) {
328 simple_ilist
<Node
> L
;
332 L
.clearAndDispose(doNothing
);
333 EXPECT_TRUE(L
.empty());
334 EXPECT_EQ(0u, L
.size());
337 TEST(SimpleIListTest
, spliceList
) {
338 simple_ilist
<Node
> L1
, L2
;
349 // Splice in L2, giving [A, B, C, D].
350 L1
.splice(--L1
.end(), L2
);
351 EXPECT_TRUE(L2
.empty());
352 EXPECT_EQ(4u, L1
.size());
354 EXPECT_EQ(&A
, &*I
++);
355 EXPECT_EQ(&B
, &*I
++);
356 EXPECT_EQ(&C
, &*I
++);
357 EXPECT_EQ(&D
, &*I
++);
358 EXPECT_EQ(L1
.end(), I
);
361 TEST(SimpleIListTest
, spliceSingle
) {
362 simple_ilist
<Node
> L1
, L2
;
374 // Splice B from L2 to L1, giving [A, B, C] and [D, E].
375 L1
.splice(--L1
.end(), L2
, ++L2
.begin());
377 EXPECT_EQ(&A
, &*I
++);
378 EXPECT_EQ(&B
, &*I
++);
379 EXPECT_EQ(&C
, &*I
++);
380 EXPECT_EQ(L1
.end(), I
);
383 EXPECT_EQ(&D
, &*I
++);
384 EXPECT_EQ(&E
, &*I
++);
385 EXPECT_EQ(L2
.end(), I
);
388 TEST(SimpleIListTest
, spliceRange
) {
389 simple_ilist
<Node
> L1
, L2
;
390 Node A
, B
, C
, D
, E
, F
;
402 // Splice B from L2 to L1, giving [A, B, C, D] and [E, F].
403 L1
.splice(--L1
.end(), L2
, ++L2
.begin(), --L2
.end());
405 EXPECT_EQ(&A
, &*I
++);
406 EXPECT_EQ(&B
, &*I
++);
407 EXPECT_EQ(&C
, &*I
++);
408 EXPECT_EQ(&D
, &*I
++);
409 EXPECT_EQ(L1
.end(), I
);
412 EXPECT_EQ(&E
, &*I
++);
413 EXPECT_EQ(&F
, &*I
++);
414 EXPECT_EQ(L2
.end(), I
);
417 TEST(SimpleIListTest
, merge
) {
418 for (bool IsL1LHS
: {false, true}) {
419 simple_ilist
<Node
> L1
, L2
;
437 EXPECT_EQ(4u, L1
.size());
438 EXPECT_EQ(6u, L2
.size());
439 EXPECT_TRUE(std::is_sorted(L1
.begin(), L1
.end()));
440 EXPECT_TRUE(std::is_sorted(L2
.begin(), L2
.end()));
443 auto &LHS
= IsL1LHS
? L1
: L2
;
444 auto &RHS
= IsL1LHS
? L2
: L1
;
446 EXPECT_TRUE(RHS
.empty());
447 EXPECT_FALSE(LHS
.empty());
448 EXPECT_TRUE(std::is_sorted(LHS
.begin(), LHS
.end()));
449 auto I
= LHS
.begin();
451 EXPECT_EQ(&N
, &*I
++);
452 EXPECT_EQ(LHS
.end(), I
);
456 TEST(SimpleIListTest
, mergeIsStable
) {
457 simple_ilist
<Node
> L1
, L2
;
461 EXPECT_TRUE(L1
.empty());
462 EXPECT_TRUE(L2
.empty());
474 EXPECT_EQ(3u, L1
.size());
475 EXPECT_EQ(2u, L2
.size());
476 EXPECT_TRUE(std::is_sorted(L1
.begin(), L1
.end(), makeFalse
));
477 EXPECT_TRUE(std::is_sorted(L2
.begin(), L2
.end(), makeFalse
));
480 // Merge. Should be stable.
482 L1
.merge(L2
, makeFalse
);
483 EXPECT_TRUE(L2
.empty());
484 EXPECT_FALSE(L1
.empty());
485 EXPECT_TRUE(std::is_sorted(L1
.begin(), L1
.end(), makeFalse
));
487 EXPECT_EQ(&Ns
[0], &*I
++);
488 EXPECT_EQ(&Ns
[3], &*I
++);
489 EXPECT_EQ(&Ns
[4], &*I
++);
490 EXPECT_EQ(&Ns
[1], &*I
++);
491 EXPECT_EQ(&Ns
[2], &*I
++);
492 EXPECT_EQ(L1
.end(), I
);
494 // Merge the other way. Should be stable.
497 L2
.merge(L1
, makeFalse
);
498 EXPECT_TRUE(L1
.empty());
499 EXPECT_FALSE(L2
.empty());
500 EXPECT_TRUE(std::is_sorted(L2
.begin(), L2
.end(), makeFalse
));
502 EXPECT_EQ(&Ns
[1], &*I
++);
503 EXPECT_EQ(&Ns
[2], &*I
++);
504 EXPECT_EQ(&Ns
[0], &*I
++);
505 EXPECT_EQ(&Ns
[3], &*I
++);
506 EXPECT_EQ(&Ns
[4], &*I
++);
507 EXPECT_EQ(L2
.end(), I
);
510 TEST(SimpleIListTest
, mergeEmpty
) {
511 for (bool IsL1LHS
: {false, true}) {
512 simple_ilist
<Node
> L1
, L2
;
522 EXPECT_EQ(4u, L1
.size());
523 EXPECT_TRUE(L2
.empty());
524 EXPECT_TRUE(std::is_sorted(L1
.begin(), L1
.end()));
527 auto &LHS
= IsL1LHS
? L1
: L2
;
528 auto &RHS
= IsL1LHS
? L2
: L1
;
530 EXPECT_TRUE(RHS
.empty());
531 EXPECT_FALSE(LHS
.empty());
532 EXPECT_TRUE(std::is_sorted(LHS
.begin(), LHS
.end()));
533 auto I
= LHS
.begin();
535 EXPECT_EQ(&N
, &*I
++);
536 EXPECT_EQ(LHS
.end(), I
);
540 TEST(SimpleIListTest
, mergeBothEmpty
) {
541 simple_ilist
<Node
> L1
, L2
;
543 EXPECT_TRUE(L1
.empty());
544 EXPECT_TRUE(L2
.empty());
547 TEST(SimpleIListTest
, sort
) {
548 simple_ilist
<Node
> L
;
552 for (int I
: {3, 4, 0, 8, 1, 2, 6, 7, 9, 5})
556 EXPECT_EQ(10u, L
.size());
557 EXPECT_FALSE(std::is_sorted(L
.begin(), L
.end()));
561 EXPECT_TRUE(std::is_sorted(L
.begin(), L
.end()));
564 EXPECT_EQ(&N
, &*I
++);
565 EXPECT_EQ(L
.end(), I
);
568 TEST(SimpleIListTest
, sortIsStable
) {
569 simple_ilist
<Node
> L
;
572 // Compare such that nodes are partitioned but not fully sorted.
573 auto partition
= [&](const Node
&N
) { return &N
>= &Ns
[5]; };
574 auto compare
= [&](const Node
&L
, const Node
&R
) {
575 return partition(L
) < partition(R
);
579 for (int I
: {3, 4, 7, 8, 1, 2, 6, 0, 9, 5})
583 EXPECT_EQ(10u, L
.size());
584 EXPECT_FALSE(std::is_sorted(L
.begin(), L
.end(), compare
));
588 EXPECT_TRUE(std::is_sorted(L
.begin(), L
.end(), compare
));
590 for (int O
: {3, 4, 1, 2, 0})
591 EXPECT_EQ(&Ns
[O
], &*I
++);
592 for (int O
: {7, 8, 6, 9, 5})
593 EXPECT_EQ(&Ns
[O
], &*I
++);
594 EXPECT_EQ(L
.end(), I
);
597 TEST(SimpleIListTest
, sortEmpty
) {
598 simple_ilist
<Node
> L
;
605 struct DoubleNode
: ilist_node
<DoubleNode
, ilist_tag
<Tag1
>>,
606 ilist_node
<DoubleNode
, ilist_tag
<Tag2
>> {
607 typedef ilist_node
<DoubleNode
, ilist_tag
<Tag1
>> Node1Type
;
608 typedef ilist_node
<DoubleNode
, ilist_tag
<Tag2
>> Node2Type
;
610 Node1Type::self_iterator
getIterator1() { return Node1Type::getIterator(); }
611 Node2Type::self_iterator
getIterator2() { return Node2Type::getIterator(); }
612 Node1Type::const_self_iterator
getIterator1() const {
613 return Node1Type::getIterator();
615 Node2Type::const_self_iterator
getIterator2() const {
616 return Node2Type::getIterator();
619 typedef simple_ilist
<DoubleNode
, ilist_tag
<Tag1
>> TaggedList1Type
;
620 typedef simple_ilist
<DoubleNode
, ilist_tag
<Tag2
>> TaggedList2Type
;
622 TEST(SimpleIListTest
, TaggedLists
) {
626 // Build the two lists, sharing a couple of nodes.
628 int Order1
[] = {0, 1, 2, 3, 4, 7, 9};
629 int Order2
[] = {2, 5, 6, 7, 8, 4, 9, 1};
635 // Check that each list is correct.
636 EXPECT_EQ(sizeof(Order1
) / sizeof(int), L1
.size());
637 auto I1
= L1
.begin();
638 for (int I
: Order1
) {
639 EXPECT_EQ(Ns
[I
].getIterator1(), I1
);
640 EXPECT_EQ(&Ns
[I
], &*I1
++);
642 EXPECT_EQ(L1
.end(), I1
);
644 EXPECT_EQ(sizeof(Order2
) / sizeof(int), L2
.size());
645 auto I2
= L2
.begin();
646 for (int I
: Order2
) {
647 EXPECT_EQ(Ns
[I
].getIterator2(), I2
);
648 EXPECT_EQ(&Ns
[I
], &*I2
++);
650 EXPECT_EQ(L2
.end(), I2
);