IListIteratorTest.cpp
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//===- unittests/ADT/IListIteratorTest.cpp - ilist_iterator unit tests ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/simple_ilist.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
struct Node : ilist_node<Node> {};
TEST(IListIteratorTest, DefaultConstructor) {
simple_ilist<Node>::iterator I;
simple_ilist<Node>::reverse_iterator RI;
simple_ilist<Node>::const_iterator CI;
simple_ilist<Node>::const_reverse_iterator CRI;
EXPECT_EQ(nullptr, I.getNodePtr());
EXPECT_EQ(nullptr, CI.getNodePtr());
EXPECT_EQ(nullptr, RI.getNodePtr());
EXPECT_EQ(nullptr, CRI.getNodePtr());
EXPECT_EQ(I, I);
EXPECT_EQ(I, CI);
EXPECT_EQ(CI, I);
EXPECT_EQ(CI, CI);
EXPECT_EQ(RI, RI);
EXPECT_EQ(RI, CRI);
EXPECT_EQ(CRI, RI);
EXPECT_EQ(CRI, CRI);
EXPECT_EQ(I, RI.getReverse());
EXPECT_EQ(RI, I.getReverse());
}
TEST(IListIteratorTest, Empty) {
simple_ilist<Node> L;
// Check iterators of L.
EXPECT_EQ(L.begin(), L.end());
EXPECT_EQ(L.rbegin(), L.rend());
// Reverse of end should be rend (since the sentinel sits on both sides).
EXPECT_EQ(L.end(), L.rend().getReverse());
EXPECT_EQ(L.rend(), L.end().getReverse());
// Iterators shouldn't match default constructors.
simple_ilist<Node>::iterator I;
simple_ilist<Node>::reverse_iterator RI;
EXPECT_NE(I, L.begin());
EXPECT_NE(I, L.end());
EXPECT_NE(RI, L.rbegin());
EXPECT_NE(RI, L.rend());
}
TEST(IListIteratorTest, OneNodeList) {
simple_ilist<Node> L;
Node A;
L.insert(L.end(), A);
// Check address of reference.
EXPECT_EQ(&A, &*L.begin());
EXPECT_EQ(&A, &*L.rbegin());
// Check that the handle matches.
EXPECT_EQ(L.rbegin().getNodePtr(), L.begin().getNodePtr());
// Check iteration.
EXPECT_EQ(L.end(), ++L.begin());
EXPECT_EQ(L.begin(), --L.end());
EXPECT_EQ(L.rend(), ++L.rbegin());
EXPECT_EQ(L.rbegin(), --L.rend());
// Check conversions.
EXPECT_EQ(L.rbegin(), L.begin().getReverse());
EXPECT_EQ(L.begin(), L.rbegin().getReverse());
}
TEST(IListIteratorTest, TwoNodeList) {
simple_ilist<Node> L;
Node A, B;
L.insert(L.end(), A);
L.insert(L.end(), B);
// Check order.
EXPECT_EQ(&A, &*L.begin());
EXPECT_EQ(&B, &*++L.begin());
EXPECT_EQ(L.end(), ++++L.begin());
EXPECT_EQ(&B, &*L.rbegin());
EXPECT_EQ(&A, &*++L.rbegin());
EXPECT_EQ(L.rend(), ++++L.rbegin());
// Check conversions.
EXPECT_EQ(++L.rbegin(), L.begin().getReverse());
EXPECT_EQ(L.rbegin(), (++L.begin()).getReverse());
EXPECT_EQ(++L.begin(), L.rbegin().getReverse());
EXPECT_EQ(L.begin(), (++L.rbegin()).getReverse());
}
TEST(IListIteratorTest, CheckEraseForward) {
simple_ilist<Node> L;
Node A, B;
L.insert(L.end(), A);
L.insert(L.end(), B);
// Erase nodes.
auto I = L.begin();
EXPECT_EQ(&A, &*I);
L.remove(*I++);
EXPECT_EQ(&B, &*I);
L.remove(*I++);
EXPECT_EQ(L.end(), I);
}
TEST(IListIteratorTest, CheckEraseReverse) {
simple_ilist<Node> L;
Node A, B;
L.insert(L.end(), A);
L.insert(L.end(), B);
// Erase nodes.
auto RI = L.rbegin();
EXPECT_EQ(&B, &*RI);
L.remove(*RI++);
EXPECT_EQ(&A, &*RI);
L.remove(*RI++);
EXPECT_EQ(L.rend(), RI);
}
TEST(IListIteratorTest, ReverseConstructor) {
simple_ilist<Node> L;
const simple_ilist<Node> &CL = L;
Node A, B;
L.insert(L.end(), A);
L.insert(L.end(), B);
// Save typing.
typedef simple_ilist<Node>::iterator iterator;
typedef simple_ilist<Node>::reverse_iterator reverse_iterator;
typedef simple_ilist<Node>::const_iterator const_iterator;
typedef simple_ilist<Node>::const_reverse_iterator const_reverse_iterator;
// Check conversion values.
EXPECT_EQ(L.begin(), iterator(L.rend()));
EXPECT_EQ(++L.begin(), iterator(++L.rbegin()));
EXPECT_EQ(L.end(), iterator(L.rbegin()));
EXPECT_EQ(L.rbegin(), reverse_iterator(L.end()));
EXPECT_EQ(++L.rbegin(), reverse_iterator(++L.begin()));
EXPECT_EQ(L.rend(), reverse_iterator(L.begin()));
// Check const iterator constructors.
EXPECT_EQ(CL.begin(), const_iterator(L.rend()));
EXPECT_EQ(CL.begin(), const_iterator(CL.rend()));
EXPECT_EQ(CL.rbegin(), const_reverse_iterator(L.end()));
EXPECT_EQ(CL.rbegin(), const_reverse_iterator(CL.end()));
// Confirm lack of implicit conversions.
static_assert(!std::is_convertible<iterator, reverse_iterator>::value,
"unexpected implicit conversion");
static_assert(!std::is_convertible<reverse_iterator, iterator>::value,
"unexpected implicit conversion");
static_assert(
!std::is_convertible<const_iterator, const_reverse_iterator>::value,
"unexpected implicit conversion");
static_assert(
!std::is_convertible<const_reverse_iterator, const_iterator>::value,
"unexpected implicit conversion");
}
} // end namespace