forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_iterator_wrapper.cpp
More file actions
222 lines (194 loc) · 4.77 KB
/
Copy pathtest_iterator_wrapper.cpp
File metadata and controls
222 lines (194 loc) · 4.77 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// NOTE this header tests implementation details
#include "catch.hpp"
#include "internal/iterator_wrapper.hpp"
// I'm using a std::vector of 1 int instead of just an int in order to give
// the iterator types non-trivial constructors, destructors, and assignment.
// same begin() and end() types
struct SameTypes {
struct iterator {
iterator(int) : value_(1) {}
bool operator!=(const iterator& other) const {
return value_ != other.value_;
}
iterator& operator++() {
++value_.front();
return *this;
}
const int& operator*() const {
return value_.front();
}
std::vector<int> value_; // non-trvial operations
};
iterator begin() const {
return {0};
}
iterator end() const {
return {0};
}
};
// different begin() and end() types
struct DifferentTypes {
struct iterator;
struct end_iterator;
struct iterator {
iterator() : value_{} {
REQUIRE(false);
}
iterator(int i) : value_(1, i) {}
bool operator!=(const iterator& other) const {
return value() != other.value();
}
bool operator!=(const end_iterator&) const {
return value() != 3;
}
iterator& operator++() {
++value_.front();
return *this;
}
const int& operator*() const {
return value();
}
const int& value() const {
return value_.front();
}
std::vector<int> value_;
};
struct end_iterator {
end_iterator() {
REQUIRE(false);
}
end_iterator(int) {}
bool operator!=(const end_iterator&) const {
return false;
}
bool operator!=(const iterator& other) const {
return other.value() != 3;
}
end_iterator& operator++() {
return *this;
}
const int& operator*() const {
assert(false);
return value();
}
const int& value() const {
return value_.front();
}
std::vector<int> value_{};
};
iterator begin() const {
return {0};
}
end_iterator end() const {
return {0};
}
};
// Explicit instatiations, which could cause failures if the implementation
// details of the implementation details change.
template class iter::impl::
IteratorWrapperImpl<iter::impl::iterator_type<DifferentTypes>,
iter::impl::iterator_end_type<DifferentTypes>>;
using iter::impl::IteratorWrapper;
TEST_CASE("ensure test type iterators are totally comparable", "[test_util") {
{
SameTypes s{};
auto it = s.begin();
(void)(it != it);
}
{
DifferentTypes d{};
auto b = d.begin();
auto e = d.end();
(void)(b != b);
(void)(e != e);
(void)(b != e);
(void)(e != b);
}
}
TEST_CASE(
"Operations on IteratorWrappers with SameTypes work", "[base_iterator]") {
SameTypes s;
IteratorWrapper<SameTypes> it(s.begin());
REQUIRE((std::is_same<std::decay_t<decltype(it)>,
std::decay_t<decltype(s.begin())>>{}));
REQUIRE(*it == 0);
++it;
REQUIRE(*it == 1);
}
TEST_CASE("Operations on IteratorWrappers with DifferentTypes work",
"[base_iterator]") {
DifferentTypes d;
using BI = IteratorWrapper<DifferentTypes>;
BI it(d.begin());
REQUIRE((!std::is_same<std::decay_t<decltype(it)>,
std::decay_t<decltype(d.begin())>>{}));
REQUIRE(*it == 0);
++it;
REQUIRE(*it == 1);
BI it2(d.begin());
REQUIRE(it != it2);
REQUIRE(it2 != it);
++it2;
REQUIRE_FALSE(it != it2);
REQUIRE_FALSE(it2 != it);
BI bend(d.end());
REQUIRE(it != bend);
REQUIRE(bend != it);
++it;
++it;
REQUIRE_FALSE(it != bend);
REQUIRE_FALSE(bend != it);
}
TEST_CASE(
"Can copy construct a IteratorWrapper with SameTypes", "[base_iterator]") {
SameTypes s;
using BI = IteratorWrapper<SameTypes>;
BI it(s.begin());
BI it2(it);
REQUIRE_FALSE(it != it2);
++it2;
REQUIRE(it != it2);
}
TEST_CASE(
"Can copy assign a IteratorWrapper with SameTypes", "[base_iterator]") {
SameTypes s;
using BI = IteratorWrapper<SameTypes>;
BI it(s.begin());
BI it2(s.begin());
REQUIRE_FALSE(it != it2);
++it2;
REQUIRE(it != it2);
it = it2;
REQUIRE_FALSE(it != it2);
}
TEST_CASE("Can copy construct a IteratorWrapper with DifferentTypes",
"[base_iterator]") {
using BI = IteratorWrapper<DifferentTypes>;
DifferentTypes d;
BI it(d.begin());
BI it2(it);
REQUIRE_FALSE(it != it2);
++it;
REQUIRE(it != it2);
}
TEST_CASE("Can copy construct a IteratorWrapper with DifferenTypes",
"[base_iterator]") {
using BI = IteratorWrapper<DifferentTypes>;
DifferentTypes d;
BI it(d.begin());
BI it2(it);
it = it2;
// break assignment into a different test
REQUIRE_FALSE(it != it2);
BI it_end(d.end());
REQUIRE(it != it_end);
SECTION("normal = end") {
it = it_end;
REQUIRE_FALSE(it != it_end);
}
SECTION("end = normal") {
it_end = BI{d.begin()};
REQUIRE_FALSE(it != it_end);
}
}
// TODO test move operations