1 //===- unittests/ADT/IListIteratorTest.cpp - ilist_iterator unit tests ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/simple_ilist.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
15 namespace {
16
17 struct Node : ilist_node<Node> {};
18
TEST(IListIteratorTest,DefaultConstructor)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());
28 EXPECT_EQ(I, I);
29 EXPECT_EQ(I, CI);
30 EXPECT_EQ(CI, I);
31 EXPECT_EQ(CI, CI);
32 EXPECT_EQ(RI, RI);
33 EXPECT_EQ(RI, CRI);
34 EXPECT_EQ(CRI, RI);
35 EXPECT_EQ(CRI, CRI);
36 EXPECT_EQ(I, RI.getReverse());
37 EXPECT_EQ(RI, I.getReverse());
38 }
39
TEST(IListIteratorTest,Empty)40 TEST(IListIteratorTest, Empty) {
41 simple_ilist<Node> L;
42
43 // Check iterators of L.
44 EXPECT_EQ(L.begin(), L.end());
45 EXPECT_EQ(L.rbegin(), L.rend());
46
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());
50
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());
58 }
59
TEST(IListIteratorTest,OneNodeList)60 TEST(IListIteratorTest, OneNodeList) {
61 simple_ilist<Node> L;
62 Node A;
63 L.insert(L.end(), A);
64
65 // Check address of reference.
66 EXPECT_EQ(&A, &*L.begin());
67 EXPECT_EQ(&A, &*L.rbegin());
68
69 // Check that the handle matches.
70 EXPECT_EQ(L.rbegin().getNodePtr(), L.begin().getNodePtr());
71
72 // Check iteration.
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());
77
78 // Check conversions.
79 EXPECT_EQ(L.rbegin(), L.begin().getReverse());
80 EXPECT_EQ(L.begin(), L.rbegin().getReverse());
81 }
82
TEST(IListIteratorTest,TwoNodeList)83 TEST(IListIteratorTest, TwoNodeList) {
84 simple_ilist<Node> L;
85 Node A, B;
86 L.insert(L.end(), A);
87 L.insert(L.end(), B);
88
89 // Check order.
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());
96
97 // Check conversions.
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());
102 }
103
TEST(IListIteratorTest,CheckEraseForward)104 TEST(IListIteratorTest, CheckEraseForward) {
105 simple_ilist<Node> L;
106 Node A, B;
107 L.insert(L.end(), A);
108 L.insert(L.end(), B);
109
110 // Erase nodes.
111 auto I = L.begin();
112 EXPECT_EQ(&A, &*I);
113 L.remove(*I++);
114 EXPECT_EQ(&B, &*I);
115 L.remove(*I++);
116 EXPECT_EQ(L.end(), I);
117 }
118
TEST(IListIteratorTest,CheckEraseReverse)119 TEST(IListIteratorTest, CheckEraseReverse) {
120 simple_ilist<Node> L;
121 Node A, B;
122 L.insert(L.end(), A);
123 L.insert(L.end(), B);
124
125 // Erase nodes.
126 auto RI = L.rbegin();
127 EXPECT_EQ(&B, &*RI);
128 L.remove(*RI++);
129 EXPECT_EQ(&A, &*RI);
130 L.remove(*RI++);
131 EXPECT_EQ(L.rend(), RI);
132 }
133
TEST(IListIteratorTest,ReverseConstructor)134 TEST(IListIteratorTest, ReverseConstructor) {
135 simple_ilist<Node> L;
136 const simple_ilist<Node> &CL = L;
137 Node A, B;
138 L.insert(L.end(), A);
139 L.insert(L.end(), B);
140
141 // Save typing.
142 typedef simple_ilist<Node>::iterator iterator;
143 typedef simple_ilist<Node>::reverse_iterator reverse_iterator;
144 typedef simple_ilist<Node>::const_iterator const_iterator;
145 typedef simple_ilist<Node>::const_reverse_iterator const_reverse_iterator;
146
147 // Check conversion values.
148 EXPECT_EQ(L.begin(), iterator(L.rend()));
149 EXPECT_EQ(++L.begin(), iterator(++L.rbegin()));
150 EXPECT_EQ(L.end(), iterator(L.rbegin()));
151 EXPECT_EQ(L.rbegin(), reverse_iterator(L.end()));
152 EXPECT_EQ(++L.rbegin(), reverse_iterator(++L.begin()));
153 EXPECT_EQ(L.rend(), reverse_iterator(L.begin()));
154
155 // Check const iterator constructors.
156 EXPECT_EQ(CL.begin(), const_iterator(L.rend()));
157 EXPECT_EQ(CL.begin(), const_iterator(CL.rend()));
158 EXPECT_EQ(CL.rbegin(), const_reverse_iterator(L.end()));
159 EXPECT_EQ(CL.rbegin(), const_reverse_iterator(CL.end()));
160
161 // Confirm lack of implicit conversions.
162 static_assert(!std::is_convertible<iterator, reverse_iterator>::value,
163 "unexpected implicit conversion");
164 static_assert(!std::is_convertible<reverse_iterator, iterator>::value,
165 "unexpected implicit conversion");
166 static_assert(
167 !std::is_convertible<const_iterator, const_reverse_iterator>::value,
168 "unexpected implicit conversion");
169 static_assert(
170 !std::is_convertible<const_reverse_iterator, const_iterator>::value,
171 "unexpected implicit conversion");
172 }
173
174 } // end namespace
175