1 //===- unittests/ADT/IListIteratorTest.cpp - ilist_iterator unit tests ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/simple_ilist.h"
11 #include "gtest/gtest.h"
17 struct Node
: ilist_node
<Node
> {};
19 TEST(IListIteratorTest
, DefaultConstructor
) {
20 simple_ilist
<Node
>::iterator I
;
21 simple_ilist
<Node
>::reverse_iterator RI
;
22 simple_ilist
<Node
>::const_iterator CI
;
23 simple_ilist
<Node
>::const_reverse_iterator CRI
;
24 EXPECT_EQ(nullptr, I
.getNodePtr());
25 EXPECT_EQ(nullptr, CI
.getNodePtr());
26 EXPECT_EQ(nullptr, RI
.getNodePtr());
27 EXPECT_EQ(nullptr, CRI
.getNodePtr());
36 EXPECT_EQ(I
, RI
.getReverse());
37 EXPECT_EQ(RI
, I
.getReverse());
40 TEST(IListIteratorTest
, Empty
) {
43 // Check iterators of L.
44 EXPECT_EQ(L
.begin(), L
.end());
45 EXPECT_EQ(L
.rbegin(), L
.rend());
47 // Reverse of end should be rend (since the sentinel sits on both sides).
48 EXPECT_EQ(L
.end(), L
.rend().getReverse());
49 EXPECT_EQ(L
.rend(), L
.end().getReverse());
51 // Iterators shouldn't match default constructors.
52 simple_ilist
<Node
>::iterator I
;
53 simple_ilist
<Node
>::reverse_iterator RI
;
54 EXPECT_NE(I
, L
.begin());
55 EXPECT_NE(I
, L
.end());
56 EXPECT_NE(RI
, L
.rbegin());
57 EXPECT_NE(RI
, L
.rend());
60 TEST(IListIteratorTest
, OneNodeList
) {
65 // Check address of reference.
66 EXPECT_EQ(&A
, &*L
.begin());
67 EXPECT_EQ(&A
, &*L
.rbegin());
69 // Check that the handle matches.
70 EXPECT_EQ(L
.rbegin().getNodePtr(), L
.begin().getNodePtr());
73 EXPECT_EQ(L
.end(), ++L
.begin());
74 EXPECT_EQ(L
.begin(), --L
.end());
75 EXPECT_EQ(L
.rend(), ++L
.rbegin());
76 EXPECT_EQ(L
.rbegin(), --L
.rend());
79 EXPECT_EQ(L
.rbegin(), L
.begin().getReverse());
80 EXPECT_EQ(L
.begin(), L
.rbegin().getReverse());
83 TEST(IListIteratorTest
, TwoNodeList
) {
90 EXPECT_EQ(&A
, &*L
.begin());
91 EXPECT_EQ(&B
, &*++L
.begin());
92 EXPECT_EQ(L
.end(), ++++L
.begin());
93 EXPECT_EQ(&B
, &*L
.rbegin());
94 EXPECT_EQ(&A
, &*++L
.rbegin());
95 EXPECT_EQ(L
.rend(), ++++L
.rbegin());
98 EXPECT_EQ(++L
.rbegin(), L
.begin().getReverse());
99 EXPECT_EQ(L
.rbegin(), (++L
.begin()).getReverse());
100 EXPECT_EQ(++L
.begin(), L
.rbegin().getReverse());
101 EXPECT_EQ(L
.begin(), (++L
.rbegin()).getReverse());
104 TEST(IListIteratorTest
, CheckEraseForward
) {
105 simple_ilist
<Node
> L
;
107 L
.insert(L
.end(), A
);
108 L
.insert(L
.end(), B
);
116 EXPECT_EQ(L
.end(), I
);
119 TEST(IListIteratorTest
, CheckEraseReverse
) {
120 simple_ilist
<Node
> L
;
122 L
.insert(L
.end(), A
);
123 L
.insert(L
.end(), B
);
126 auto RI
= L
.rbegin();
131 EXPECT_EQ(L
.rend(), RI
);