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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
//===------ MappedIteratorTest.cpp - Unit tests for mapped_iterator -------===//
//
// 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/STLExtras.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
template <typename T> class MappedIteratorTestBasic : public testing::Test {};
struct Plus1Lambda {
auto operator()() const {
return [](int X) { return X + 1; };
}
};
struct Plus1LambdaWithCapture {
const int One = 1;
auto operator()() const {
return [=](int X) { return X + One; };
}
};
struct Plus1FunctionRef {
static int plus1(int X) { return X + 1; }
using FuncT = int (&)(int);
FuncT operator()() const { return (FuncT)*plus1; }
};
struct Plus1FunctionPtr {
static int plus1(int X) { return X + 1; }
using FuncT = int (*)(int);
FuncT operator()() const { return plus1; }
};
struct Plus1Functor {
struct Plus1 {
int operator()(int X) const { return X + 1; }
};
auto operator()() const { return Plus1(); }
};
struct Plus1FunctorNotDefaultConstructible {
class PlusN {
const int N;
public:
PlusN(int NArg) : N(NArg) {}
int operator()(int X) const { return X + N; }
};
auto operator()() const { return PlusN(1); }
};
// clang-format off
using FunctionTypes =
::testing::Types<
Plus1Lambda,
Plus1LambdaWithCapture,
Plus1FunctionRef,
Plus1FunctionPtr,
Plus1Functor,
Plus1FunctorNotDefaultConstructible
>;
// clang-format on
TYPED_TEST_SUITE(MappedIteratorTestBasic, FunctionTypes, );
template <typename T> using GetFuncT = decltype(std::declval<T>().operator()());
TYPED_TEST(MappedIteratorTestBasic, DefaultConstruct) {
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<typename std::vector<int>::iterator, FuncT>;
TypeParam GetCallable;
auto Func = GetCallable();
(void)Func;
constexpr bool DefaultConstruct =
std::is_default_constructible_v<callable_detail::Callable<FuncT>>;
EXPECT_TRUE(DefaultConstruct);
EXPECT_TRUE(std::is_default_constructible_v<IterT>);
if constexpr (std::is_default_constructible_v<IterT>) {
IterT I;
(void)I;
}
}
TYPED_TEST(MappedIteratorTestBasic, CopyConstruct) {
std::vector<int> V({0});
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<decltype(V)::iterator, FuncT>;
EXPECT_TRUE(std::is_copy_constructible_v<IterT>);
if constexpr (std::is_copy_constructible_v<IterT>) {
TypeParam GetCallable;
IterT I1(V.begin(), GetCallable());
IterT I2(I1);
EXPECT_EQ(I2, I1) << "copy constructed iterator is a different position";
}
}
TYPED_TEST(MappedIteratorTestBasic, MoveConstruct) {
std::vector<int> V({0});
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<decltype(V)::iterator, FuncT>;
EXPECT_TRUE(std::is_move_constructible_v<IterT>);
if constexpr (std::is_move_constructible_v<IterT>) {
TypeParam GetCallable;
IterT I1(V.begin(), GetCallable());
IterT I2(V.begin(), GetCallable());
IterT I3(std::move(I2));
EXPECT_EQ(I3, I1) << "move constructed iterator is a different position";
}
}
TYPED_TEST(MappedIteratorTestBasic, CopyAssign) {
std::vector<int> V({0});
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<decltype(V)::iterator, FuncT>;
EXPECT_TRUE(std::is_copy_assignable_v<IterT>);
if constexpr (std::is_copy_assignable_v<IterT>) {
TypeParam GetCallable;
IterT I1(V.begin(), GetCallable());
IterT I2(V.end(), GetCallable());
I2 = I1;
EXPECT_EQ(I2, I1) << "copy assigned iterator is a different position";
}
}
TYPED_TEST(MappedIteratorTestBasic, MoveAssign) {
std::vector<int> V({0});
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<decltype(V)::iterator, FuncT>;
EXPECT_TRUE(std::is_move_assignable_v<IterT>);
if constexpr (std::is_move_assignable_v<IterT>) {
TypeParam GetCallable;
IterT I1(V.begin(), GetCallable());
IterT I2(V.begin(), GetCallable());
IterT I3(V.end(), GetCallable());
I3 = std::move(I2);
EXPECT_EQ(I3, I1) << "move assigned iterator is a different position";
}
}
TYPED_TEST(MappedIteratorTestBasic, GetFunction) {
std::vector<int> V({0});
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<decltype(V)::iterator, FuncT>;
TypeParam GetCallable;
IterT I(V.begin(), GetCallable());
EXPECT_EQ(I.getFunction()(200), 201);
}
TYPED_TEST(MappedIteratorTestBasic, GetCurrent) {
std::vector<int> V({0});
using FuncT = GetFuncT<TypeParam>;
using IterT = mapped_iterator<decltype(V)::iterator, FuncT>;
TypeParam GetCallable;
IterT I(V.begin(), GetCallable());
EXPECT_EQ(I.getCurrent(), V.begin());
EXPECT_EQ(std::next(I).getCurrent(), V.end());
}
TYPED_TEST(MappedIteratorTestBasic, ApplyFunctionOnDereference) {
std::vector<int> V({0});
TypeParam GetCallable;
auto I = map_iterator(V.begin(), GetCallable());
EXPECT_EQ(*I, 1) << "should have applied function in dereference";
}
TEST(MappedIteratorTest, ApplyFunctionOnArrow) {
struct S {
int Z = 0;
};
std::vector<int> V({0});
S Y;
S *P = &Y;
auto I = map_iterator(V.begin(), [&](int X) -> S & { return *(P + X); });
I->Z = 42;
EXPECT_EQ(Y.Z, 42) << "should have applied function during arrow";
}
TEST(MappedIteratorTest, FunctionPreservesReferences) {
std::vector<int> V({1});
std::map<int, int> M({{1, 1}});
auto I = map_iterator(V.begin(), [&](int X) -> int & { return M[X]; });
*I = 42;
EXPECT_EQ(M[1], 42) << "assignment should have modified M";
}
TEST(MappedIteratorTest, CustomIteratorApplyFunctionOnDereference) {
struct CustomMapIterator
: public llvm::mapped_iterator_base<CustomMapIterator,
std::vector<int>::iterator, int> {
using BaseT::BaseT;
/// Map the element to the iterator result type.
int mapElement(int X) const { return X + 1; }
};
std::vector<int> V({0});
CustomMapIterator I(V.begin());
EXPECT_EQ(*I, 1) << "should have applied function in dereference";
}
TEST(MappedIteratorTest, CustomIteratorApplyFunctionOnArrow) {
struct S {
int Z = 0;
};
struct CustomMapIterator
: public llvm::mapped_iterator_base<CustomMapIterator,
std::vector<int>::iterator, S &> {
CustomMapIterator(std::vector<int>::iterator it, S *P) : BaseT(it), P(P) {}
/// Map the element to the iterator result type.
S &mapElement(int X) const { return *(P + X); }
S *P;
};
std::vector<int> V({0});
S Y;
CustomMapIterator I(V.begin(), &Y);
I->Z = 42;
EXPECT_EQ(Y.Z, 42) << "should have applied function during arrow";
}
TEST(MappedIteratorTest, CustomIteratorFunctionPreservesReferences) {
struct CustomMapIterator
: public llvm::mapped_iterator_base<CustomMapIterator,
std::vector<int>::iterator, int &> {
CustomMapIterator(std::vector<int>::iterator it, std::map<int, int> &M)
: BaseT(it), M(M) {}
/// Map the element to the iterator result type.
int &mapElement(int X) const { return M[X]; }
std::map<int, int> &M;
};
std::vector<int> V({1});
std::map<int, int> M({{1, 1}});
auto I = CustomMapIterator(V.begin(), M);
*I = 42;
EXPECT_EQ(M[1], 42) << "assignment should have modified M";
}
} // anonymous namespace
|