forked from boostorg/iterator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_iterator_test.cpp
More file actions
393 lines (314 loc) · 11.5 KB
/
transform_iterator_test.cpp
File metadata and controls
393 lines (314 loc) · 11.5 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// (C) Copyright Jeremy Siek 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Revision History
// 22 Nov 2002 Thomas Witt
// Added interoperability check.
// 28 Oct 2002 Jeremy Siek
// Updated for new iterator adaptors.
// 08 Mar 2001 Jeremy Siek
// Moved test of transform iterator into its own file. It to
// to be in iterator_adaptor_test.cpp.
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <algorithm>
#include <iterator>
#include <vector>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/iterator_concepts.hpp>
#include <boost/iterator/new_iterator_tests.hpp>
#include <boost/pending/iterator_tests.hpp>
#include <boost/concept_check.hpp>
#if defined(__cpp_lib_ranges) && ( __cpp_lib_ranges >= 202002L )
#include <ranges>
#endif
#include "static_assert_same.hpp"
struct mult_functor {
// Functors used with transform_iterator must be
// DefaultConstructible, as the transform_iterator must be
// DefaultConstructible to satisfy the requirements for
// TrivialIterator.
mult_functor() { }
mult_functor(int aa) : a(aa) { }
int operator()(int b) const { return a * b; }
int a;
};
struct adaptable_mult_functor
: mult_functor
{
typedef int result_type;
typedef int argument_type;
// Functors used with transform_iterator must be
// DefaultConstructible, as the transform_iterator must be
// DefaultConstructible to satisfy the requirements for
// TrivialIterator.
adaptable_mult_functor() { }
adaptable_mult_functor(int aa) : mult_functor(aa) { }
};
struct identity {
template <class T>
T&& operator()(T&& x) const {
return static_cast<T&&>(x);
}
};
struct reference_to_value {
template <class T>
typename std::remove_reference<T>::type operator()(T&& x) const {
return x;
}
};
struct const_select_first
{
typedef int const& result_type;
int const& operator()(std::pair<int, int>const& p) const
{
return p.first;
}
};
struct select_first
: const_select_first // derivation to allow conversions
{
typedef int& result_type;
int& operator()(std::pair<int, int>& p) const
{
return p.first;
}
};
struct select_second
{
typedef int& result_type;
int& operator()(std::pair<int, int>& p) const
{
return p.second;
}
};
struct value_select_first
{
typedef int result_type;
int operator()(std::pair<int, int>const& p) const
{
return p.first;
}
};
int mult_2(int arg)
{
return arg*2;
}
struct polymorphic_mult_functor
{
//Implement result_of protocol
template <class FArgs> struct result;
template <class F, class T> struct result<const F(T )> {typedef T type;};
template <class F, class T> struct result<const F(T& )> {typedef T type;};
template <class F, class T> struct result<const F(const T&)> {typedef T type;};
template <class F, class T> struct result<F(T )> {typedef void type;};
template <class F, class T> struct result<F(T& )> {typedef void type;};
template <class F, class T> struct result<F(const T&)> {typedef void type;};
template <class T>
T operator()(const T& _arg) const {return _arg*2;}
template <class T>
void operator()(const T& _arg) { BOOST_ASSERT(0); }
};
int
main()
{
const int N = 10;
// Concept checks
{
typedef boost::transform_iterator<adaptable_mult_functor, int*> iter_t;
typedef boost::transform_iterator<adaptable_mult_functor, int const*> c_iter_t;
boost::function_requires< boost_concepts::InteroperableIteratorConcept<iter_t, c_iter_t> >();
}
// Test transform_iterator
{
int x[N], y[N];
for (int k = 0; k < N; ++k)
x[k] = k;
std::copy(x, x + N, y);
for (int k2 = 0; k2 < N; ++k2)
x[k2] = x[k2] * 2;
typedef boost::transform_iterator<adaptable_mult_functor, int*> iter_t;
iter_t i(y, adaptable_mult_functor(2));
boost::input_iterator_test(i, x[0], x[1]);
boost::input_iterator_test(iter_t(&y[0], adaptable_mult_functor(2)), x[0], x[1]);
boost::random_access_readable_iterator_test(i, N, x);
}
// Test transform_iterator non adaptable functor
{
int x[N], y[N];
for (int k = 0; k < N; ++k)
x[k] = k;
std::copy(x, x + N, y);
for (int k2 = 0; k2 < N; ++k2)
x[k2] = x[k2] * 2;
typedef boost::transform_iterator<mult_functor, int*, int> iter_t;
iter_t i(y, mult_functor(2));
boost::input_iterator_test(i, x[0], x[1]);
boost::input_iterator_test(iter_t(&y[0], mult_functor(2)), x[0], x[1]);
boost::random_access_readable_iterator_test(i, N, x);
}
// Test transform_iterator default argument handling
{
{
typedef boost::transform_iterator<adaptable_mult_functor, int*, float> iter_t;
STATIC_ASSERT_SAME(iter_t::reference, float);
STATIC_ASSERT_SAME(iter_t::value_type, float);
}
{
typedef boost::transform_iterator<adaptable_mult_functor, int*, boost::use_default, float> iter_t;
STATIC_ASSERT_SAME(iter_t::reference, int);
STATIC_ASSERT_SAME(iter_t::value_type, float);
}
{
typedef boost::transform_iterator<adaptable_mult_functor, int*, float, double> iter_t;
STATIC_ASSERT_SAME(iter_t::reference, float);
STATIC_ASSERT_SAME(iter_t::value_type, double);
}
}
// Test transform_iterator with function pointers
{
int x[N], y[N];
for (int k = 0; k < N; ++k)
x[k] = k;
std::copy(x, x + N, y);
for (int k2 = 0; k2 < N; ++k2)
x[k2] = x[k2] * 2;
boost::input_iterator_test(
boost::make_transform_iterator(y, mult_2), x[0], x[1]);
boost::input_iterator_test(
boost::make_transform_iterator(&y[0], mult_2), x[0], x[1]);
boost::random_access_readable_iterator_test(
boost::make_transform_iterator(y, mult_2), N, x);
}
// Test transform_iterator as projection iterator
{
typedef std::pair<int, int> pair_t;
int x[N];
int y[N];
pair_t values[N];
for(int i = 0; i < N; ++i) {
x[i] = i;
y[i] = N - (i + 1);
}
std::copy(
x
, x + N
, boost::make_transform_iterator((pair_t*)values, select_first())
);
std::copy(
y
, y + N
, boost::make_transform_iterator((pair_t*)values, select_second())
);
boost::random_access_readable_iterator_test(
boost::make_transform_iterator((pair_t*)values, value_select_first())
, N
, x
);
boost::random_access_readable_iterator_test(
boost::make_transform_iterator((pair_t*)values, const_select_first())
, N, x
);
boost::constant_lvalue_iterator_test(
boost::make_transform_iterator((pair_t*)values, const_select_first()), x[0]);
boost::non_const_lvalue_iterator_test(
boost::make_transform_iterator((pair_t*)values, select_first()), x[0], 17);
boost::const_nonconst_iterator_test(
++boost::make_transform_iterator((pair_t*)values, select_first())
, boost::make_transform_iterator((pair_t*)values, const_select_first())
);
}
// Test transform_iterator with polymorphic object function
{
int x[N], y[N];
for (int k = 0; k < N; ++k)
x[k] = k;
std::copy(x, x + N, y);
for (int k2 = 0; k2 < N; ++k2)
x[k2] = x[k2] * 2;
boost::input_iterator_test(
boost::make_transform_iterator(y, polymorphic_mult_functor()), x[0], x[1]);
boost::input_iterator_test(
boost::make_transform_iterator(&y[0], polymorphic_mult_functor()), x[0], x[1]);
boost::random_access_readable_iterator_test(
boost::make_transform_iterator(y, polymorphic_mult_functor()), N, x);
}
{
using Iter = boost::iterators::transform_iterator<identity, typename std::vector<int>::iterator>;
using ConstIter = boost::iterators::transform_iterator<identity, typename std::vector<int>::const_iterator>;
static_assert(
std::is_same<decltype(*std::declval<Iter>()), int&>::value,
"Transform iterator with identity must dereference into int reference."
);
static_assert(
std::is_same<decltype(*std::declval<ConstIter>()), const int&>::value,
"Transform iterator with identity must dereference into const int reference."
);
static_assert(
std::is_same<decltype(std::declval<Iter>()[std::declval<std::size_t>()]), int&>::value,
"Transform iterator over iterator with identity must return int reference for operator[] call."
);
static_assert(
std::is_same<decltype(std::declval<ConstIter>()[std::declval<std::size_t>()]), const int&>::value,
"Transform iterator over const iterator with identity must return int reference for operator[] call."
);
#if defined(__cpp_lib_concepts) && ( __cpp_lib_concepts >= 202002L )
static_assert(std::random_access_iterator<Iter>);
static_assert(std::random_access_iterator<ConstIter>);
static_assert(std::output_iterator<Iter, int>);
static_assert(std::input_iterator<Iter>);
static_assert(!std::output_iterator<ConstIter, int>);
static_assert(std::input_iterator<ConstIter>);
#endif
auto nums = std::vector<int>{1, 2, 3, 4, 5, 6};
auto iter1 = boost::iterators::make_transform_iterator<identity>(nums.begin());
auto iter2 = boost::iterators::make_transform_iterator<identity>(nums.end());
const auto found3 = std::lower_bound(iter1, iter2, 3);
BOOST_TEST(*found3 == 3);
#if defined(__cpp_lib_ranges)
auto found3rng = std::ranges::lower_bound(iter1, iter2, 3);
BOOST_TEST(*found3rng == 3);
#endif
*std::prev(iter2) = 7;
BOOST_TEST(nums.back() == 7);
}
{
using Iter = boost::iterators::transform_iterator<reference_to_value, typename std::vector<int>::iterator>;
using ConstIter = boost::iterators::transform_iterator<reference_to_value, typename std::vector<int>::const_iterator>;
static_assert(
std::is_same<decltype(*std::declval<Iter>()), int>::value,
"Transform iterator with identity must dereference into int reference."
);
static_assert(
std::is_same<decltype(*std::declval<ConstIter>()), int>::value,
"Transform iterator with identity must dereference into const int reference."
);
static_assert(
std::is_same<decltype(std::declval<Iter>()[std::declval<std::size_t>()]), int>::value,
"Transform iterator over iterator with identity must return int reference for operator[] call."
);
static_assert(
std::is_same<decltype(std::declval<ConstIter>()[std::declval<std::size_t>()]), int>::value,
"Transform iterator over const iterator with identity must return int reference for operator[] call."
);
#if defined(__cpp_lib_concepts) && ( __cpp_lib_concepts >= 202002L )
static_assert(std::random_access_iterator<Iter>);
static_assert(std::random_access_iterator<ConstIter>);
static_assert(!std::output_iterator<Iter, int>);
static_assert(std::input_iterator<Iter>);
static_assert(!std::output_iterator<ConstIter, int>);
static_assert(std::input_iterator<ConstIter>);
#endif
auto nums = std::vector<int>{1, 2, 3, 4, 5, 6};
auto iter1 = boost::iterators::make_transform_iterator<identity>(nums.begin());
auto iter2 = boost::iterators::make_transform_iterator<identity>(nums.end());
const auto found3 = std::lower_bound(iter1, iter2, 3);
BOOST_TEST(*found3 == 3);
#if defined(__cpp_lib_ranges) && ( __cpp_lib_ranges >= 202002L )
auto found3rng = std::ranges::lower_bound(iter1, iter2, 3);
BOOST_TEST(*found3rng == 3);
#endif
}
return boost::report_errors();
}