Debian Codegraph @

llvm-project/llvm/unittests/ADT/IteratorTest.cpp

raw
1 //===- IteratorTest.cpp - Unit tests for iterator utilities ---------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/ilist.h"
10 #include "llvm/ADT/iterator.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "gtest/gtest.h"
15 #include <optional>
16
17 using namespace llvm;
18
19 namespace {
20
21 template <int> struct Shadow;
22
23 struct WeirdIter
24 : llvm::iterator_facade_base<WeirdIter, std::input_iterator_tag, Shadow<0>,
25 Shadow<1>, Shadow<2>, Shadow<3>> {};
26
27 struct AdaptedIter : iterator_adaptor_base<AdaptedIter, WeirdIter> {};
28
29 // Test that iterator_adaptor_base forwards typedefs, if value_type is
30 // unchanged.
31 static_assert(std::is_same_v<typename AdaptedIter::value_type, Shadow<0>>, "");
32 static_assert(std::is_same_v<typename AdaptedIter::difference_type, Shadow<1>>,
33 "");
34 static_assert(std::is_same_v<typename AdaptedIter::pointer, Shadow<2>>, "");
35 static_assert(std::is_same_v<typename AdaptedIter::reference, Shadow<3>>, "");
36
37 // Ensure that pointe{e,r}_iterator adaptors correctly forward the category of
38 // the underlying iterator.
39
40 using RandomAccessIter = SmallVectorImpl<int*>::iterator;
41 using BidiIter = ilist<int*>::iterator;
42
43 template<class T>
44 using pointee_iterator_defaulted = pointee_iterator<T>;
45 template<class T>
46 using pointer_iterator_defaulted = pointer_iterator<T>;
47
48 // Ensures that an iterator and its adaptation have the same iterator_category.
49 template<template<typename> class A, typename It>
50 using IsAdaptedIterCategorySame =
51 std::is_same<typename std::iterator_traits<It>::iterator_category,
52 typename std::iterator_traits<A<It>>::iterator_category>;
53
54 // Check that dereferencing works correctly adapting pointers and proxies.
55 template <class T>
56 struct PointerWrapper : public iterator_adaptor_base<PointerWrapper<T>, T *> {
57 PointerWrapper(T *I) : PointerWrapper::iterator_adaptor_base(I) {}
58 };
59 struct IntProxy {
60 int &I;
61 IntProxy(int &I) : I(I) {}
62 void operator=(int NewValue) { I = NewValue; }
63 };
64 struct ConstIntProxy {
65 const int &I;
66 ConstIntProxy(const int &I) : I(I) {}
67 };
68 template <class T, class ProxyT>
69 struct PointerProxyWrapper
70 : public iterator_adaptor_base<PointerProxyWrapper<T, ProxyT>, T *,
71 std::random_access_iterator_tag, T,
72 ptrdiff_t, T *, ProxyT> {
73 PointerProxyWrapper(T *I) : PointerProxyWrapper::iterator_adaptor_base(I) {}
74 };
75 using IntIterator = PointerWrapper<int>;
76 using ConstIntIterator = PointerWrapper<const int>;
77 using IntProxyIterator = PointerProxyWrapper<int, IntProxy>;
78 using ConstIntProxyIterator = PointerProxyWrapper<const int, ConstIntProxy>;
79
80 // There should only be a single (const-qualified) operator*, operator->, and
81 // operator[]. This test confirms that there isn't a non-const overload. Rather
82 // than adding those, users should double-check that T, PointerT, and ReferenceT
83 // have the right constness, and/or make fields mutable.
84 static_assert(&IntIterator::operator* == &IntIterator::operator*, "");
85 static_assert(&IntIterator::operator-> == &IntIterator::operator->, "");
86 static_assert(&IntIterator::operator[] == &IntIterator::operator[], "");
87
88 template <class T, std::enable_if_t<std::is_assignable_v<T, int>, bool> = false>
89 constexpr bool canAssignFromInt(T &&) {
90 return true;
91 }
92 template <class T,
93 std::enable_if_t<!std::is_assignable_v<T, int>, bool> = false>
94 constexpr bool canAssignFromInt(T &&) {
95 return false;
96 }
97
98 TEST(IteratorAdaptorTest, Dereference) {
99 int Number = 1;
100
101 // Construct some iterators and check whether they can be assigned to.
102 IntIterator I(&Number);
103 const IntIterator IC(&Number);
104 ConstIntIterator CI(&Number);
105 const ConstIntIterator CIC(&Number);
106 EXPECT_EQ(true, canAssignFromInt(*I)); // int *
107 EXPECT_EQ(true, canAssignFromInt(*IC)); // int *const
108 EXPECT_EQ(false, canAssignFromInt(*CI)); // const int *
109 EXPECT_EQ(false, canAssignFromInt(*CIC)); // const int *const
110
111 // Prove that dereference and assignment work.
112 EXPECT_EQ(1, *I);
113 EXPECT_EQ(1, *IC);
114 EXPECT_EQ(1, *CI);
115 EXPECT_EQ(1, *CIC);
116 *I = 2;
117 EXPECT_EQ(2, Number);
118 *IC = 3;
119 EXPECT_EQ(3, Number);
120
121 // Construct some proxy iterators and check whether they can be assigned to.
122 IntProxyIterator P(&Number);
123 const IntProxyIterator PC(&Number);
124 ConstIntProxyIterator CP(&Number);
125 const ConstIntProxyIterator CPC(&Number);
126 EXPECT_EQ(true, canAssignFromInt(*P)); // int *
127 EXPECT_EQ(true, canAssignFromInt(*PC)); // int *const
128 EXPECT_EQ(false, canAssignFromInt(*CP)); // const int *
129 EXPECT_EQ(false, canAssignFromInt(*CPC)); // const int *const
130
131 // Prove that dereference and assignment work.
132 EXPECT_EQ(3, (*P).I);
133 EXPECT_EQ(3, (*PC).I);
134 EXPECT_EQ(3, (*CP).I);
135 EXPECT_EQ(3, (*CPC).I);
136 *P = 4;
137 EXPECT_EQ(4, Number);
138 *PC = 5;
139 EXPECT_EQ(5, Number);
140 }
141
142 // pointeE_iterator
143 static_assert(IsAdaptedIterCategorySame<pointee_iterator_defaulted,
144 RandomAccessIter>::value, "");
145 static_assert(IsAdaptedIterCategorySame<pointee_iterator_defaulted,
146 BidiIter>::value, "");
147 // pointeR_iterator
148 static_assert(IsAdaptedIterCategorySame<pointer_iterator_defaulted,
149 RandomAccessIter>::value, "");
150 static_assert(IsAdaptedIterCategorySame<pointer_iterator_defaulted,
151 BidiIter>::value, "");
152
153 TEST(PointeeIteratorTest, Basic) {
154 int arr[4] = {1, 2, 3, 4};
155 SmallVector<int *, 4> V;
156 V.push_back(&arr[0]);
157 V.push_back(&arr[1]);
158 V.push_back(&arr[2]);
159 V.push_back(&arr[3]);
160
161 typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator>
162 test_iterator;
163
164 test_iterator Begin, End;
165 Begin = V.begin();
166 End = test_iterator(V.end());
167
168 test_iterator I = Begin;
169 for (int i = 0; i < 4; ++i) {
170 EXPECT_EQ(*V[i], *I);
171
172 EXPECT_EQ(I, Begin + i);
173 EXPECT_EQ(I, std::next(Begin, i));
174 test_iterator J = Begin;
175 J += i;
176 EXPECT_EQ(I, J);
177 EXPECT_EQ(*V[i], Begin[i]);
178
179 EXPECT_NE(I, End);
180 EXPECT_GT(End, I);
181 EXPECT_LT(I, End);
182 EXPECT_GE(I, Begin);
183 EXPECT_LE(Begin, I);
184
185 EXPECT_EQ(i, I - Begin);
186 EXPECT_EQ(i, std::distance(Begin, I));
187 EXPECT_EQ(Begin, I - i);
188
189 test_iterator K = I++;
190 EXPECT_EQ(K, std::prev(I));
191 }
192 EXPECT_EQ(End, I);
193 }
194
195 TEST(PointeeIteratorTest, SmartPointer) {
196 SmallVector<std::unique_ptr<int>, 4> V;
197 V.push_back(std::make_unique<int>(1));
198 V.push_back(std::make_unique<int>(2));
199 V.push_back(std::make_unique<int>(3));
200 V.push_back(std::make_unique<int>(4));
201
202 typedef pointee_iterator<
203 SmallVectorImpl<std::unique_ptr<int>>::const_iterator>
204 test_iterator;
205
206 test_iterator Begin, End;
207 Begin = V.begin();
208 End = test_iterator(V.end());
209
210 test_iterator I = Begin;
211 for (int i = 0; i < 4; ++i) {
212 EXPECT_EQ(*V[i], *I);
213
214 EXPECT_EQ(I, Begin + i);
215 EXPECT_EQ(I, std::next(Begin, i));
216 test_iterator J = Begin;
217 J += i;
218 EXPECT_EQ(I, J);
219 EXPECT_EQ(*V[i], Begin[i]);
220
221 EXPECT_NE(I, End);
222 EXPECT_GT(End, I);
223 EXPECT_LT(I, End);
224 EXPECT_GE(I, Begin);
225 EXPECT_LE(Begin, I);
226
227 EXPECT_EQ(i, I - Begin);
228 EXPECT_EQ(i, std::distance(Begin, I));
229 EXPECT_EQ(Begin, I - i);
230
231 test_iterator K = I++;
232 EXPECT_EQ(K, std::prev(I));
233 }
234 EXPECT_EQ(End, I);
235 }
236
237 TEST(PointeeIteratorTest, Range) {
238 int A[] = {1, 2, 3, 4};
239 SmallVector<int *, 4> V{&A[0], &A[1], &A[2], &A[3]};
240
241 int I = 0;
242 for (int II : make_pointee_range(V))
243 EXPECT_EQ(A[I++], II);
244 }
245
246 TEST(PointeeIteratorTest, PointeeType) {
247 struct S {
248 int X;
249 bool operator==(const S &RHS) const { return X == RHS.X; };
250 };
251 S A[] = {S{0}, S{1}};
252 SmallVector<S *, 2> V{&A[0], &A[1]};
253
254 pointee_iterator<SmallVectorImpl<S *>::const_iterator, const S> I = V.begin();
255 for (int j = 0; j < 2; ++j, ++I) {
256 EXPECT_EQ(*V[j], *I);
257 }
258 }
259
260 TEST(FilterIteratorTest, Lambda) {
261 auto IsOdd = [](int N) { return N % 2 == 1; };
262 int A[] = {0, 1, 2, 3, 4, 5, 6};
263 auto Range = make_filter_range(A, IsOdd);
264 SmallVector<int, 3> Actual(Range.begin(), Range.end());
265 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
266 }
267
268 TEST(FilterIteratorTest, Enumerate) {
269 auto IsOdd = [](auto N) { return N.value() % 2 == 1; };
270 int A[] = {0, 1, 2, 3, 4, 5, 6};
271 auto Enumerate = llvm::enumerate(A);
272 SmallVector<int> Actual;
273 for (auto IndexedValue : make_filter_range(Enumerate, IsOdd))
274 Actual.push_back(IndexedValue.value());
275 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
276 }
277
278 TEST(FilterIteratorTest, CallableObject) {
279 int Counter = 0;
280 struct Callable {
281 int &Counter;
282
283 Callable(int &Counter) : Counter(Counter) {}
284
285 bool operator()(int N) {
286 Counter++;
287 return N % 2 == 1;
288 }
289 };
290 Callable IsOdd(Counter);
291 int A[] = {0, 1, 2, 3, 4, 5, 6};
292 auto Range = make_filter_range(A, IsOdd);
293 EXPECT_EQ(2, Counter);
294 SmallVector<int, 3> Actual(Range.begin(), Range.end());
295 EXPECT_GE(Counter, 7);
296 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
297 }
298
299 TEST(FilterIteratorTest, FunctionPointer) {
300 bool (*IsOdd)(int) = [](int N) { return N % 2 == 1; };
301 int A[] = {0, 1, 2, 3, 4, 5, 6};
302 auto Range = make_filter_range(A, IsOdd);
303 SmallVector<int, 3> Actual(Range.begin(), Range.end());
304 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
305 }
306
307 TEST(FilterIteratorTest, Composition) {
308 auto IsOdd = [](int N) { return N % 2 == 1; };
309 std::unique_ptr<int> A[] = {std::make_unique<int>(0), std::make_unique<int>(1),
310 std::make_unique<int>(2), std::make_unique<int>(3),
311 std::make_unique<int>(4), std::make_unique<int>(5),
312 std::make_unique<int>(6)};
313 using PointeeIterator = pointee_iterator<std::unique_ptr<int> *>;
314 auto Range = make_filter_range(
315 make_range(PointeeIterator(std::begin(A)), PointeeIterator(std::end(A))),
316 IsOdd);
317 SmallVector<int, 3> Actual(Range.begin(), Range.end());
318 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
319 }
320
321 TEST(FilterIteratorTest, InputIterator) {
322 struct InputIterator
323 : iterator_adaptor_base<InputIterator, int *, std::input_iterator_tag> {
324 InputIterator(int *It) : InputIterator::iterator_adaptor_base(It) {}
325 };
326
327 auto IsOdd = [](int N) { return N % 2 == 1; };
328 int A[] = {0, 1, 2, 3, 4, 5, 6};
329 auto Range = make_filter_range(
330 make_range(InputIterator(std::begin(A)), InputIterator(std::end(A))),
331 IsOdd);
332 SmallVector<int, 3> Actual(Range.begin(), Range.end());
333 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
334 }
335
336 TEST(FilterIteratorTest, ReverseFilterRange) {
337 auto IsOdd = [](int N) { return N % 2 == 1; };
338 int A[] = {0, 1, 2, 3, 4, 5, 6};
339
340 // Check basic reversal.
341 auto Range = reverse(make_filter_range(A, IsOdd));
342 SmallVector<int, 3> Actual(Range.begin(), Range.end());
343 EXPECT_EQ((SmallVector<int, 3>{5, 3, 1}), Actual);
344
345 // Check that the reverse of the reverse is the original.
346 auto Range2 = reverse(reverse(make_filter_range(A, IsOdd)));
347 SmallVector<int, 3> Actual2(Range2.begin(), Range2.end());
348 EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual2);
349
350 // Check empty ranges.
351 auto Range3 = reverse(make_filter_range(ArrayRef<int>(), IsOdd));
352 SmallVector<int, 0> Actual3(Range3.begin(), Range3.end());
353 EXPECT_EQ((SmallVector<int, 0>{}), Actual3);
354
355 // Check that we don't skip the first element, provided it isn't filtered
356 // away.
357 auto IsEven = [](int N) { return N % 2 == 0; };
358 auto Range4 = reverse(make_filter_range(A, IsEven));
359 SmallVector<int, 4> Actual4(Range4.begin(), Range4.end());
360 EXPECT_EQ((SmallVector<int, 4>{6, 4, 2, 0}), Actual4);
361 }
362
363 TEST(PointerIterator, Basic) {
364 int A[] = {1, 2, 3, 4};
365 pointer_iterator<int *> Begin(std::begin(A)), End(std::end(A));
366 EXPECT_EQ(A, *Begin);
367 ++Begin;
368 EXPECT_EQ(A + 1, *Begin);
369 ++Begin;
370 EXPECT_EQ(A + 2, *Begin);
371 ++Begin;
372 EXPECT_EQ(A + 3, *Begin);
373 ++Begin;
374 EXPECT_EQ(Begin, End);
375 }
376
377 TEST(PointerIterator, Const) {
378 int A[] = {1, 2, 3, 4};
379 const pointer_iterator<int *> Begin(std::begin(A));
380 EXPECT_EQ(A, *Begin);
381 EXPECT_EQ(A + 1, std::next(*Begin, 1));
382 EXPECT_EQ(A + 2, std::next(*Begin, 2));
383 EXPECT_EQ(A + 3, std::next(*Begin, 3));
384 EXPECT_EQ(A + 4, std::next(*Begin, 4));
385 }
386
387 TEST(PointerIterator, Range) {
388 int A[] = {1, 2, 3, 4};
389 int I = 0;
390 for (int *P : make_pointer_range(A))
391 EXPECT_EQ(A + I++, P);
392 }
393
394 TEST(ZipIteratorTest, Basic) {
395 using namespace std;
396 const SmallVector<unsigned, 6> pi{3, 1, 4, 1, 5, 9};
397 SmallVector<bool, 6> odd{1, 1, 0, 1, 1, 1};
398 const char message[] = "yynyyy\0";
399 std::array<int, 2> shortArr = {42, 43};
400
401 for (auto tup : zip(pi, odd, message)) {
402 EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup));
403 EXPECT_EQ(get<0>(tup) & 0x01 ? 'y' : 'n', get<2>(tup));
404 }
405
406 // Note the rvalue.
407 for (auto tup : zip(pi, SmallVector<bool, 0>{1, 1, 0, 1, 1})) {
408 EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup));
409 }
410
411 // Iterate until we run out elements in the *shortest* range.
412 for (auto [idx, elem] : enumerate(zip(odd, shortArr))) {
413 EXPECT_LT(idx, static_cast<size_t>(2));
414 }
415 for (auto [idx, elem] : enumerate(zip(shortArr, odd))) {
416 EXPECT_LT(idx, static_cast<size_t>(2));
417 }
418 }
419
420 TEST(ZipIteratorTest, ZipEqualBasic) {
421 const SmallVector<unsigned, 6> pi = {3, 1, 4, 1, 5, 8};
422 const SmallVector<bool, 6> vals = {1, 1, 0, 1, 1, 0};
423 unsigned iters = 0;
424
425 for (auto [lhs, rhs] : zip_equal(vals, pi)) {
426 EXPECT_EQ(lhs, rhs & 0x01);
427 ++iters;
428 }
429
430 EXPECT_EQ(iters, 6u);
431 }
432
433 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
434 // Check that an assertion is triggered when ranges passed to `zip_equal` differ
435 // in length.
436 TEST(ZipIteratorTest, ZipEqualNotEqual) {
437 const SmallVector<unsigned, 6> pi = {3, 1, 4, 1, 5, 8};
438 const SmallVector<bool, 2> vals = {1, 1};
439
440 EXPECT_DEATH(zip_equal(pi, vals), "Iteratees do not have equal length");
441 EXPECT_DEATH(zip_equal(vals, pi), "Iteratees do not have equal length");
442 EXPECT_DEATH(zip_equal(pi, pi, vals), "Iteratees do not have equal length");
443 EXPECT_DEATH(zip_equal(vals, vals, pi), "Iteratees do not have equal length");
444 }
445 #endif
446
447 TEST(ZipIteratorTest, ZipFirstBasic) {
448 using namespace std;
449 const SmallVector<unsigned, 6> pi{3, 1, 4, 1, 5, 9};
450 unsigned iters = 0;
451
452 for (auto tup : zip_first(SmallVector<bool, 0>{1, 1, 0, 1}, pi)) {
453 EXPECT_EQ(get<0>(tup), get<1>(tup) & 0x01);
454 iters += 1;
455 }
456
457 EXPECT_EQ(iters, 4u);
458 }
459
460 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
461 // Make sure that we can detect when the first range is not the shortest.
462 TEST(ZipIteratorTest, ZipFirstNotShortest) {
463 const std::array<unsigned, 6> longer = {};
464 const std::array<unsigned, 4> shorter = {};
465
466 EXPECT_DEATH(zip_first(longer, shorter),
467 "First iteratee is not the shortest");
468 EXPECT_DEATH(zip_first(longer, shorter, longer),
469 "First iteratee is not the shortest");
470 EXPECT_DEATH(zip_first(longer, longer, shorter),
471 "First iteratee is not the shortest");
472 }
473 #endif
474
475 TEST(ZipIteratorTest, ZipLongestBasic) {
476 using namespace std;
477 const vector<unsigned> pi{3, 1, 4, 1, 5, 9};
478 const vector<StringRef> e{"2", "7", "1", "8"};
479
480 {
481 // Check left range longer than right.
482 const vector<tuple<optional<unsigned>, optional<StringRef>>> expected{
483 make_tuple(3, StringRef("2")), make_tuple(1, StringRef("7")),
484 make_tuple(4, StringRef("1")), make_tuple(1, StringRef("8")),
485 make_tuple(5, std::nullopt), make_tuple(9, std::nullopt)};
486 size_t iters = 0;
487 for (auto tup : zip_longest(pi, e)) {
488 EXPECT_EQ(tup, expected[iters]);
489 iters += 1;
490 }
491 EXPECT_EQ(iters, expected.size());
492 }
493
494 {
495 // Check right range longer than left.
496 const vector<tuple<optional<StringRef>, optional<unsigned>>> expected{
497 make_tuple(StringRef("2"), 3), make_tuple(StringRef("7"), 1),
498 make_tuple(StringRef("1"), 4), make_tuple(StringRef("8"), 1),
499 make_tuple(std::nullopt, 5), make_tuple(std::nullopt, 9)};
500 size_t iters = 0;
501 for (auto tup : zip_longest(e, pi)) {
502 EXPECT_EQ(tup, expected[iters]);
503 iters += 1;
504 }
505 EXPECT_EQ(iters, expected.size());
506 }
507 }
508
509 TEST(ZipIteratorTest, Mutability) {
510 using namespace std;
511 const SmallVector<unsigned, 4> pi{3, 1, 4, 1, 5, 9};
512 char message[] = "hello zip\0";
513
514 for (auto tup : zip(pi, message, message)) {
515 EXPECT_EQ(get<1>(tup), get<2>(tup));
516 get<2>(tup) = get<0>(tup) & 0x01 ? 'y' : 'n';
517 }
518
519 // note the rvalue
520 for (auto tup : zip(message, "yynyyyzip\0")) {
521 EXPECT_EQ(get<0>(tup), get<1>(tup));
522 }
523 }
524
525 TEST(ZipIteratorTest, ZipFirstMutability) {
526 using namespace std;
527 vector<unsigned> pi{3, 1, 4, 1, 5, 9};
528 unsigned iters = 0;
529
530 for (auto tup : zip_first(SmallVector<bool, 0>{1, 1, 0, 1}, pi)) {
531 get<1>(tup) = get<0>(tup);
532 iters += 1;
533 }
534
535 EXPECT_EQ(iters, 4u);
536
537 for (auto tup : zip_first(SmallVector<bool, 0>{1, 1, 0, 1}, pi)) {
538 EXPECT_EQ(get<0>(tup), get<1>(tup));
539 }
540 }
541
542 TEST(ZipIteratorTest, Filter) {
543 using namespace std;
544 vector<unsigned> pi{3, 1, 4, 1, 5, 9};
545
546 unsigned iters = 0;
547 // pi is length 6, but the zip RHS is length 7.
548 auto zipped = zip_first(pi, vector<bool>{1, 1, 0, 1, 1, 1, 0});
549 for (auto tup : make_filter_range(
550 zipped, [](decltype(zipped)::value_type t) { return get<1>(t); })) {
551 EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup));
552 get<0>(tup) += 1;
553 iters += 1;
554 }
555
556 // Should have skipped pi[2].
557 EXPECT_EQ(iters, 5u);
558
559 // Ensure that in-place mutation works.
560 EXPECT_TRUE(all_of(pi, [](unsigned n) { return (n & 0x01) == 0; }));
561 }
562
563 TEST(ZipIteratorTest, Reverse) {
564 using namespace std;
565 vector<unsigned> ascending{0, 1, 2, 3, 4, 5};
566
567 auto zipped = zip_first(ascending, vector<bool>{0, 1, 0, 1, 0, 1});
568 unsigned last = 6;
569 for (auto tup : reverse(zipped)) {
570 // Check that this is in reverse.
571 EXPECT_LT(get<0>(tup), last);
572 last = get<0>(tup);
573 EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup));
574 }
575
576 auto odds = [](decltype(zipped)::value_type tup) { return get<1>(tup); };
577 last = 6;
578 for (auto tup : make_filter_range(reverse(zipped), odds)) {
579 EXPECT_LT(get<0>(tup), last);
580 last = get<0>(tup);
581 EXPECT_TRUE(get<0>(tup) & 0x01);
582 get<0>(tup) += 1;
583 }
584
585 // Ensure that in-place mutation works.
586 EXPECT_TRUE(all_of(ascending, [](unsigned n) { return (n & 0x01) == 0; }));
587 }
588
589 TEST(RangeTest, Distance) {
590 std::vector<int> v1;
591 std::vector<int> v2{1, 2, 3};
592
593 EXPECT_EQ(std::distance(v1.begin(), v1.end()), size(v1));
594 EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
595 }
596
597 } // anonymous namespace