1 //===- unittests/ADT/IListIteratorTest.cpp - ilist_iterator 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
> {};
18 TEST(IListIteratorTest
, DefaultConstructor
) {
19 simple_ilist
<Node
>::iterator I
;
20 simple_ilist
<Node
>::reverse_iterator RI
;
21 simple_ilist
<Node
>::const_iterator CI
;
22 simple_ilist
<Node
>::const_reverse_iterator CRI
;
23 EXPECT_EQ(nullptr, I
.getNodePtr());
24 EXPECT_EQ(nullptr, CI
.getNodePtr());
25 EXPECT_EQ(nullptr, RI
.getNodePtr());
26 EXPECT_EQ(nullptr, CRI
.getNodePtr());
35 EXPECT_EQ(I
, RI
.getReverse());
36 EXPECT_EQ(RI
, I
.getReverse());
39 TEST(IListIteratorTest
, Empty
) {
42 // Check iterators of L.
43 EXPECT_EQ(L
.begin(), L
.end());
44 EXPECT_EQ(L
.rbegin(), L
.rend());
46 // Reverse of end should be rend (since the sentinel sits on both sides).
47 EXPECT_EQ(L
.end(), L
.rend().getReverse());
48 EXPECT_EQ(L
.rend(), L
.end().getReverse());
50 // Iterators shouldn't match default constructors.
51 simple_ilist
<Node
>::iterator I
;
52 simple_ilist
<Node
>::reverse_iterator RI
;
53 EXPECT_NE(I
, L
.begin());
54 EXPECT_NE(I
, L
.end());
55 EXPECT_NE(RI
, L
.rbegin());
56 EXPECT_NE(RI
, L
.rend());
59 TEST(IListIteratorTest
, OneNodeList
) {
64 // Check address of reference.
65 EXPECT_EQ(&A
, &*L
.begin());
66 EXPECT_EQ(&A
, &*L
.rbegin());
68 // Check that the handle matches.
69 EXPECT_EQ(L
.rbegin().getNodePtr(), L
.begin().getNodePtr());
72 EXPECT_EQ(L
.end(), ++L
.begin());
73 EXPECT_EQ(L
.begin(), --L
.end());
74 EXPECT_EQ(L
.rend(), ++L
.rbegin());
75 EXPECT_EQ(L
.rbegin(), --L
.rend());
78 EXPECT_EQ(L
.rbegin(), L
.begin().getReverse());
79 EXPECT_EQ(L
.begin(), L
.rbegin().getReverse());
82 TEST(IListIteratorTest
, TwoNodeList
) {
89 EXPECT_EQ(&A
, &*L
.begin());
90 EXPECT_EQ(&B
, &*++L
.begin());
91 EXPECT_EQ(L
.end(), ++++L
.begin());
92 EXPECT_EQ(&B
, &*L
.rbegin());
93 EXPECT_EQ(&A
, &*++L
.rbegin());
94 EXPECT_EQ(L
.rend(), ++++L
.rbegin());
97 EXPECT_EQ(++L
.rbegin(), L
.begin().getReverse());
98 EXPECT_EQ(L
.rbegin(), (++L
.begin()).getReverse());
99 EXPECT_EQ(++L
.begin(), L
.rbegin().getReverse());
100 EXPECT_EQ(L
.begin(), (++L
.rbegin()).getReverse());
103 TEST(IListIteratorTest
, CheckEraseForward
) {
104 simple_ilist
<Node
> L
;
106 L
.insert(L
.end(), A
);
107 L
.insert(L
.end(), B
);
115 EXPECT_EQ(L
.end(), I
);
118 TEST(IListIteratorTest
, CheckEraseReverse
) {
119 simple_ilist
<Node
> L
;
121 L
.insert(L
.end(), A
);
122 L
.insert(L
.end(), B
);
125 auto RI
= L
.rbegin();
130 EXPECT_EQ(L
.rend(), RI
);
133 TEST(IListIteratorTest
, ReverseConstructor
) {
134 simple_ilist
<Node
> L
;
135 const simple_ilist
<Node
> &CL
= L
;
137 L
.insert(L
.end(), A
);
138 L
.insert(L
.end(), B
);
141 typedef simple_ilist
<Node
>::iterator iterator
;
142 typedef simple_ilist
<Node
>::reverse_iterator reverse_iterator
;
143 typedef simple_ilist
<Node
>::const_iterator const_iterator
;
144 typedef simple_ilist
<Node
>::const_reverse_iterator const_reverse_iterator
;
146 // Check conversion values.
147 EXPECT_EQ(L
.begin(), iterator(L
.rend()));
148 EXPECT_EQ(++L
.begin(), iterator(++L
.rbegin()));
149 EXPECT_EQ(L
.end(), iterator(L
.rbegin()));
150 EXPECT_EQ(L
.rbegin(), reverse_iterator(L
.end()));
151 EXPECT_EQ(++L
.rbegin(), reverse_iterator(++L
.begin()));
152 EXPECT_EQ(L
.rend(), reverse_iterator(L
.begin()));
154 // Check const iterator constructors.
155 EXPECT_EQ(CL
.begin(), const_iterator(L
.rend()));
156 EXPECT_EQ(CL
.begin(), const_iterator(CL
.rend()));
157 EXPECT_EQ(CL
.rbegin(), const_reverse_iterator(L
.end()));
158 EXPECT_EQ(CL
.rbegin(), const_reverse_iterator(CL
.end()));
160 // Confirm lack of implicit conversions.
161 static_assert(!std::is_convertible
<iterator
, reverse_iterator
>::value
,
162 "unexpected implicit conversion");
163 static_assert(!std::is_convertible
<reverse_iterator
, iterator
>::value
,
164 "unexpected implicit conversion");
166 !std::is_convertible
<const_iterator
, const_reverse_iterator
>::value
,
167 "unexpected implicit conversion");
169 !std::is_convertible
<const_reverse_iterator
, const_iterator
>::value
,
170 "unexpected implicit conversion");