2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <glog/logging.h>
18 #include <gtest/gtest.h>
24 #include "folly/FBVector.h"
25 #include "folly/MapUtil.h"
26 #include "folly/Memory.h"
27 #include "folly/dynamic.h"
28 #include "folly/gen/Base.h"
29 #include "folly/experimental/TestUtil.h"
31 using namespace folly::gen;
32 using namespace folly;
33 using std::make_tuple;
39 using std::unique_ptr;
42 #define EXPECT_SAME(A, B) \
43 static_assert(std::is_same<A, B>::value, "Mismatched: " #A ", " #B)
44 EXPECT_SAME(int&&, typename ArgumentReference<int>::type);
45 EXPECT_SAME(int&, typename ArgumentReference<int&>::type);
46 EXPECT_SAME(const int&, typename ArgumentReference<const int&>::type);
47 EXPECT_SAME(const int&, typename ArgumentReference<const int>::type);
50 ostream& operator<<(ostream& os, const set<T>& values) {
51 return os << from(values);
55 ostream& operator<<(ostream& os, const vector<T>& values) {
57 for (auto& value : values) {
58 if (&value != &values.front()) {
66 auto square = [](int x) { return x * x; };
67 auto add = [](int a, int b) { return a + b; };
68 auto multiply = [](int a, int b) { return a * b; };
70 auto product = foldl(1, multiply);
72 template<typename A, typename B>
73 ostream& operator<<(ostream& os, const pair<A, B>& pair) {
74 return os << "(" << pair.first << ", " << pair.second << ")";
78 auto gen = seq(1, 10);
79 EXPECT_EQ(10, gen | count);
80 EXPECT_EQ(5, gen | take(5) | count);
84 auto gen = seq(1, 10);
85 EXPECT_EQ((1 + 10) * 10 / 2, gen | sum);
86 EXPECT_EQ((1 + 5) * 5 / 2, gen | take(5) | sum);
92 gen | [&](int x) { accum += x; };
95 gen | take(3) | [&](int x) { accum2 += x; };
100 auto expected = vector<int>{4, 9, 16};
101 auto gen = from({2, 3, 4}) | map(square);
102 EXPECT_EQ((vector<int>{4, 9, 16}), gen | as<vector>());
103 EXPECT_EQ((vector<int>{4, 9}), gen | take(2) | as<vector>());
108 Counter(int start = 0)
112 int count() const { return c; }
113 int incr() { return ++c; }
115 int& ref() { return c; }
116 const int& ref() const { return c; }
120 auto counters = seq(1, 10) | eachAs<Counter>() | as<vector>();
121 EXPECT_EQ(10 * (1 + 10) / 2,
123 | member(&Counter::count)
125 EXPECT_EQ(10 * (1 + 10) / 2,
127 | mapped([](const Counter& c) { return &c; })
128 | member(&Counter::count)
130 EXPECT_EQ(10 * (2 + 11) / 2,
132 | member(&Counter::incr)
134 EXPECT_EQ(10 * (3 + 12) / 2,
136 | mapped([](Counter& c) { return &c; })
137 | member(&Counter::incr)
139 EXPECT_EQ(10 * (3 + 12) / 2,
141 | member(&Counter::count)
144 // type-verifications
145 auto m = empty<Counter&>();
146 auto c = empty<const Counter&>();
147 m | member(&Counter::incr) | assert_type<int&&>();
148 m | member(&Counter::count) | assert_type<int&&>();
149 m | member(&Counter::count) | assert_type<int&&>();
150 m | member<Const>(&Counter::ref) | assert_type<const int&>();
151 m | member<Mutable>(&Counter::ref) | assert_type<int&>();
152 c | member<Const>(&Counter::ref) | assert_type<const int&>();
157 X() : a(2), b(3), c(4), d(b) {}
162 int& d; // can't access this with a field pointer.
165 std::vector<X> xs(1);
166 EXPECT_EQ(2, from(xs)
169 EXPECT_EQ(3, from(xs)
172 EXPECT_EQ(4, from(xs)
175 EXPECT_EQ(2, seq(&xs[0], &xs[0])
179 empty<X&>() | field(&X::a) | assert_type<const int&>();
180 empty<X*>() | field(&X::a) | assert_type<const int&>();
181 empty<X&>() | field(&X::b) | assert_type<int&>();
182 empty<X*>() | field(&X::b) | assert_type<int&>();
183 empty<X&>() | field(&X::c) | assert_type<int&>();
184 empty<X*>() | field(&X::c) | assert_type<int&>();
186 empty<X&&>() | field(&X::a) | assert_type<const int&&>();
187 empty<X&&>() | field(&X::b) | assert_type<int&&>();
188 empty<X&&>() | field(&X::c) | assert_type<int&&>();
189 // references don't imply ownership so they're not moved
191 empty<const X&>() | field(&X::a) | assert_type<const int&>();
192 empty<const X*>() | field(&X::a) | assert_type<const int&>();
193 empty<const X&>() | field(&X::b) | assert_type<const int&>();
194 empty<const X*>() | field(&X::b) | assert_type<const int&>();
195 // 'mutable' has no effect on field pointers, by C++ spec
196 empty<const X&>() | field(&X::c) | assert_type<const int&>();
197 empty<const X*>() | field(&X::c) | assert_type<const int&>();
199 // can't form pointer-to-reference field: empty<X&>() | field(&X::d)
203 // cover the fenceposts of the loop unrolling
204 for (int n = 1; n < 100; ++n) {
205 EXPECT_EQ(n, seq(1, n) | count);
206 EXPECT_EQ(n + 1, seq(1) | take(n + 1) | count);
210 TEST(Gen, SeqWithStep) {
211 EXPECT_EQ(75, seq(5, 25, 5) | sum);
214 TEST(Gen, SeqWithStepArray) {
215 const std::array<int, 6> arr{{1, 2, 3, 4, 5, 6}};
216 EXPECT_EQ(9, seq(&arr[0], &arr[5], 2)
217 | map([](const int *i) { return *i; })
222 // cover the fenceposts of the loop unrolling
223 for (int n = 1; n < 100; ++n) {
224 EXPECT_EQ(gen::range(0, n) | count, n);
228 TEST(Gen, RangeWithStep) {
229 EXPECT_EQ(50, range(5, 25, 5) | sum);
232 TEST(Gen, FromIterators) {
233 vector<int> source {2, 3, 5, 7, 11};
234 auto gen = from(folly::range(source.begin() + 1, source.end() - 1));
235 EXPECT_EQ(3 * 5 * 7, gen | product);
239 auto source = seq(0, 10)
240 | map([](int i) { return std::make_pair(i, i * i); })
241 | as<std::map<int, int>>();
242 auto gen = fromConst(source)
243 | map([&](const std::pair<const int, int>& p) {
244 return p.second - p.first;
246 EXPECT_EQ(330, gen | sum);
250 const auto expected = vector<int>{1, 2, 4, 5, 7, 8};
253 | filter([](int x) { return x % 3; })
255 EXPECT_EQ(expected, actual);
258 TEST(Gen, Contains) {
263 EXPECT_TRUE(gen | contains(49));
264 EXPECT_FALSE(gen | contains(50));
268 seq(1) // infinite, to prove laziness
270 | eachTo<std::string>();
272 // std::string gen, const char* needle
273 EXPECT_TRUE(gen | take(9999) | contains("49"));
279 auto expected = vector<int>{1, 4, 9, 16};
282 | mapped([](int x) { return x * x; })
285 EXPECT_EQ(expected, actual);
288 auto expected = vector<int>{ 0, 1, 4, 5, 8 };
290 = ((seq(0) | take(2)) +
295 EXPECT_EQ(expected, actual);
298 auto expected = vector<int>{ 0, 1, 4, 5, 8 };
302 return seq(i * 4) | take(2);
307 EXPECT_EQ(expected, actual);
312 std::mt19937 rnd(42);
317 std::unordered_map<int,int> hits;
318 const int kNumIters = 80;
319 for (int i = 0; i < kNumIters; i++) {
320 auto vec = sampler | as<vector<int>>();
321 EXPECT_EQ(vec.size(), 50);
322 auto uniq = fromConst(vec) | as<set<int>>();
323 EXPECT_EQ(uniq.size(), vec.size()); // sampling without replacement
329 // In 80 separate samples of our range, we should have seen every value
330 // at least once and no value all 80 times. (The odds of either of those
331 // events is 1/2^80).
332 EXPECT_EQ(hits.size(), 100);
333 for (auto hit: hits) {
334 EXPECT_GT(hit.second, 0);
335 EXPECT_LT(hit.second, kNumIters);
341 EXPECT_EQ((small | sum), 15);
342 EXPECT_EQ((small | take(3) | count), 3);
348 | mapped([](int x) { return x * x; })
351 EXPECT_EQ((vector<int>{25, 36, 49, 64}), gen | as<vector>());
356 auto expected = vector<int>{1, 4, 9, 16};
359 | mapped([](int x) { return x * x; })
360 | until([](int x) { return x > 20; })
362 EXPECT_EQ(expected, actual);
365 auto expected = vector<int>{ 0, 1, 4, 5, 8 };
367 = ((seq(0) | until([](int i) { return i > 1; })) +
368 (seq(4) | until([](int i) { return i > 5; })) +
369 (seq(8) | until([](int i) { return i > 9; })))
370 | until([](int i) { return i > 8; })
372 EXPECT_EQ(expected, actual);
376 auto expected = vector<int>{ 0, 1, 5, 6, 10 };
380 return seq(i * 5) | until([=](int j) { return j > i * 5 + 1; });
383 | until([](int i) { return i > 10; })
385 EXPECT_EQ(expected, actual);
390 TEST(Gen, Composed) {
391 // Operator, Operator
393 filter([](Optional<int>& o) { return o.hasValue(); })
394 | map([](Optional<int>& o) -> int& { return o.value(); });
395 std::vector<Optional<int>> opts {
396 none, 4, none, 6, none
398 EXPECT_EQ(4 * 4 + 6 * 6, from(opts) | valuesOf | map(square) | sum);
400 auto sumOpt = valuesOf | sum;
401 EXPECT_EQ(10, from(opts) | sumOpt);
405 std::vector<int> nums {2, 3, 5, 7};
406 std::map<int, int> mappings { { 3, 9}, {5, 25} };
407 auto gen = from(nums) + (from(mappings) | get<1>());
408 EXPECT_EQ(51, gen | sum);
409 EXPECT_EQ(5, gen | take(2) | sum);
410 EXPECT_EQ(26, gen | take(5) | sum);
414 std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
415 auto gen = from(nums) | rconcat;
416 EXPECT_EQ(17, gen | sum);
417 EXPECT_EQ(10, gen | take(3) | sum);
420 TEST(Gen, ConcatGen) {
421 auto gen = seq(1, 10)
422 | map([](int i) { return seq(1, i); })
424 EXPECT_EQ(220, gen | sum);
425 EXPECT_EQ(10, gen | take(6) | sum);
428 TEST(Gen, ConcatAlt) {
429 std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
430 auto actual = from(nums)
431 | map([](std::vector<int>& v) { return from(v); })
435 EXPECT_EQ(expected, actual);
439 auto expected = vector<int>{0, 3, 5, 6, 7, 8, 9};
441 from({8, 6, 7, 5, 3, 0, 9})
444 EXPECT_EQ(expected, actual);
447 TEST(Gen, OrderMoved) {
448 auto expected = vector<int>{0, 9, 25, 36, 49, 64, 81};
450 from({8, 6, 7, 5, 3, 0, 9})
455 EXPECT_EQ(expected, actual);
458 TEST(Gen, OrderTake) {
459 auto expected = vector<int>{9, 8, 7};
461 from({8, 6, 7, 5, 3, 0, 9})
462 | orderByDescending(square)
465 EXPECT_EQ(expected, actual);
468 TEST(Gen, Distinct) {
469 auto expected = vector<int>{3, 1, 2};
471 from({3, 1, 3, 2, 1, 2, 3})
474 EXPECT_EQ(expected, actual);
477 TEST(Gen, DistinctBy) { // 0 1 4 9 6 5 6 9 4 1 0
478 auto expected = vector<int>{0, 1, 2, 3, 4, 5};
481 | distinctBy([](int i) { return i * i % 10; })
483 EXPECT_EQ(expected, actual);
486 TEST(Gen, DistinctMove) { // 0 1 4 9 6 5 6 9 4 1 0
487 auto expected = vector<int>{0, 1, 2, 3, 4, 5};
490 | mapped([](int i) { return std::unique_ptr<int>(new int(i)); })
491 // see comment below about selector parameters for Distinct
492 | distinctBy([](const std::unique_ptr<int>& pi) { return *pi * *pi % 10; })
493 | mapped([](std::unique_ptr<int> pi) { return *pi; })
496 // NOTE(tjackson): the following line intentionally doesn't work:
497 // | distinctBy([](std::unique_ptr<int> pi) { return *pi * *pi % 10; })
498 // This is because distinctBy because the selector intentionally requires a
499 // const reference. If it required a move-reference, the value might get
500 // gutted by the selector before said value could be passed to downstream
502 EXPECT_EQ(expected, actual);
506 EXPECT_EQ(7, seq(1, 10)
507 | minBy([](int i) -> double {
514 auto gen = from({"three", "eleven", "four"});
516 EXPECT_EQ("eleven", gen | maxBy(&strlen));
520 string expected = "facebook";
521 string actual = "face";
522 from(StringPiece("book")) | appendTo(actual);
523 EXPECT_EQ(expected, actual);
526 TEST(Gen, FromRValue) {
528 // AFAICT The C++ Standard does not specify what happens to the rvalue
529 // reference of a std::vector when it is used as the 'other' for an rvalue
530 // constructor. Use fbvector because we're sure its size will be zero in
532 fbvector<int> v({1,2,3,4});
534 EXPECT_EQ(v.size(), 4); // ensure that the lvalue version was called!
535 auto expected = 1 * 2 * 3 * 4;
536 EXPECT_EQ(expected, q1 | product);
538 auto q2 = from(std::move(v));
539 EXPECT_EQ(v.size(), 0); // ensure that rvalue version was called
540 EXPECT_EQ(expected, q2 | product);
544 auto q = from([] {return vector<int>({3,7,5}); }());
545 EXPECT_EQ(expected, q | max);
548 for (auto size: {5, 1024, 16384, 1<<20}) {
549 auto q1 = from(vector<int>(size, 2));
550 auto q2 = from(vector<int>(size, 3));
551 // If the rvalue specialization is broken/gone, then the compiler will
552 // (disgustingly!) just store a *reference* to the temporary object,
553 // which is bad. Try to catch this by allocating two temporary vectors
554 // of the same size, so that they'll probably use the same underlying
555 // buffer if q1's vector is destructed before q2's vector is constructed.
556 EXPECT_EQ(size * 2 + size * 3, (q1 | sum) + (q2 | sum));
560 auto q = from(set<int>{1,2,3,2,1});
561 EXPECT_EQ(q | sum, 6);
566 auto expected = vector<int>{5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
569 | orderBy([](int x) { return (5.1 - x) * (5.1 - x); })
571 EXPECT_EQ(expected, actual);
575 int expected = 2 * 3 * 4 * 5;
578 | foldl(1, multiply);
579 EXPECT_EQ(expected, actual);
583 int expected = 2 + 3 + 4 + 5;
584 auto actual = seq(2, 5) | reduce(add);
585 EXPECT_EQ(expected, actual);
588 TEST(Gen, ReduceBad) {
589 auto gen = seq(1) | take(0);
599 std::vector<unique_ptr<int>> ptrs;
600 ptrs.emplace_back(new int(1));
601 EXPECT_NE(ptrs.front().get(), nullptr);
602 auto ptrs2 = from(ptrs) | move | as<vector>();
603 EXPECT_EQ(ptrs.front().get(), nullptr);
604 EXPECT_EQ(**ptrs2.data(), 1);
610 | filter([](int x) { return x > 3; });
611 EXPECT_EQ(4, gen | first);
614 TEST(Gen, FromCopy) {
615 vector<int> v {3, 5};
617 auto copy = fromCopy(v);
618 EXPECT_EQ(8, src | sum);
619 EXPECT_EQ(8, copy | sum);
621 EXPECT_EQ(10, src | sum);
622 EXPECT_EQ(8, copy | sum);
626 std::map<int, int> pairs {
632 auto pairSrc = from(pairs);
633 auto keys = pairSrc | get<0>();
634 auto values = pairSrc | get<1>();
635 EXPECT_EQ(10, keys | sum);
636 EXPECT_EQ(30, values | sum);
637 EXPECT_EQ(30, keys | map(square) | sum);
639 EXPECT_EQ(15, keys | sum);
640 EXPECT_EQ(55, values | sum);
642 vector<tuple<int, int, int>> tuples {
645 make_tuple(3, 9, 27),
647 EXPECT_EQ(36, from(tuples) | get<2>() | sum);
651 EXPECT_TRUE(seq(0) | any);
652 EXPECT_TRUE(seq(0, 1) | any);
653 EXPECT_TRUE(seq(0, 10) | any([](int i) { return i == 7; }));
654 EXPECT_FALSE(seq(0, 10) | any([](int i) { return i == 11; }));
656 EXPECT_TRUE(from({1}) | any);
657 EXPECT_FALSE(gen::range(0, 0) | any);
658 EXPECT_FALSE(from({1}) | take(0) | any);
662 EXPECT_TRUE(seq(0, 10) | all([](int i) { return i < 11; }));
663 EXPECT_FALSE(seq(0, 10) | all([](int i) { return i < 5; }));
664 EXPECT_FALSE(seq(0) | take(9999) | all([](int i) { return i < 10; }));
666 // empty lists satisfies all
667 EXPECT_TRUE(seq(0) | take(0) | all([](int i) { return i < 50; }));
668 EXPECT_TRUE(seq(0) | take(0) | all([](int i) { return i > 50; }));
671 TEST(Gen, Yielders) {
672 auto gen = GENERATOR(int) {
673 for (int i = 1; i <= 5; ++i) {
677 for (int i = 3; ; ++i) {
681 vector<int> expected {
682 1, 2, 3, 4, 5, 7, 9, 16, 25
684 EXPECT_EQ(expected, gen | take(9) | as<vector>());
687 TEST(Gen, NestedYield) {
688 auto nums = GENERATOR(int) {
689 for (int i = 1; ; ++i) {
693 auto gen = GENERATOR(int) {
694 nums | take(10) | yield;
695 seq(1, 5) | [&](int i) {
699 EXPECT_EQ(70, gen | sum);
702 TEST(Gen, MapYielders) {
705 return GENERATOR(int) {
707 for (i = 1; i < n; ++i)
714 vector<int> expected {
719 1, 2, 3, 4, 5, 4, 3, 2, 1,
721 EXPECT_EQ(expected, gen | as<vector>());
724 TEST(Gen, VirtualGen) {
725 VirtualGen<int> v(seq(1, 10));
726 EXPECT_EQ(55, v | sum);
728 EXPECT_EQ(385, v | sum);
730 EXPECT_EQ(55, v | sum);
731 EXPECT_EQ(30, v | take(4) | sum);
735 TEST(Gen, CustomType) {
739 auto gen = from({Foo{2}, Foo{3}})
740 | map([](const Foo& f) { return f.y; });
741 EXPECT_EQ(5, gen | sum);
744 TEST(Gen, NoNeedlessCopies) {
746 | map([](int x) { return unique_ptr<int>(new int(x)); })
747 | map([](unique_ptr<int> p) { return p; })
748 | map([](unique_ptr<int>&& p) { return std::move(p); })
749 | map([](const unique_ptr<int>& p) { return *p; });
750 EXPECT_EQ(15, gen | sum);
751 EXPECT_EQ(6, gen | take(3) | sum);
756 class TestIntSeq : public GenImpl<int, TestIntSeq> {
760 template <class Body>
761 bool apply(Body&& body) const {
762 for (int i = 1; i < 6; ++i) {
770 TestIntSeq(TestIntSeq&&) = default;
771 TestIntSeq& operator=(TestIntSeq&&) = default;
772 TestIntSeq(const TestIntSeq&) = delete;
773 TestIntSeq& operator=(const TestIntSeq&) = delete;
778 TEST(Gen, NoGeneratorCopies) {
779 EXPECT_EQ(15, TestIntSeq() | sum);
780 auto x = TestIntSeq() | take(3);
781 EXPECT_EQ(6, std::move(x) | sum);
784 TEST(Gen, FromArray) {
785 int source[] = {2, 3, 5, 7};
786 auto gen = from(source);
787 EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
790 TEST(Gen, FromStdArray) {
791 std::array<int,4> source {{2, 3, 5, 7}};
792 auto gen = from(source);
793 EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
796 TEST(Gen, StringConcat) {
797 auto gen = seq(1, 10)
800 EXPECT_EQ("12345678910", gen | as<string>());
808 CopyCounter() : copies(0), moves(0) {
812 CopyCounter(CopyCounter&& source) {
813 *this = std::move(source);
817 CopyCounter(const CopyCounter& source) {
826 CopyCounter& operator=(const CopyCounter& source) {
827 this->copies = source.copies + 1;
828 this->moves = source.moves;
832 CopyCounter& operator=(CopyCounter&& source) {
833 this->copies = source.copies;
834 this->moves = source.moves + 1;
839 int CopyCounter::alive = 0;
841 TEST(Gen, CopyCount) {
842 vector<CopyCounter> originals;
843 originals.emplace_back();
844 EXPECT_EQ(1, originals.size());
845 EXPECT_EQ(0, originals.back().copies);
847 vector<CopyCounter> copies = from(originals) | as<vector>();
848 EXPECT_EQ(1, copies.back().copies);
849 EXPECT_EQ(0, copies.back().moves);
851 vector<CopyCounter> moves = from(originals) | move | as<vector>();
852 EXPECT_EQ(0, moves.back().copies);
853 EXPECT_EQ(1, moves.back().moves);
856 // test dynamics with various layers of nested arrays.
858 dynamic array1 = {1, 2};
859 EXPECT_EQ(dynamic(3), from(array1) | sum);
860 dynamic array2 = {{1}, {1, 2}};
861 EXPECT_EQ(dynamic(4), from(array2) | rconcat | sum);
862 dynamic array3 = {{{1}}, {{1}, {1, 2}}};
863 EXPECT_EQ(dynamic(5), from(array3) | rconcat | rconcat | sum);
866 TEST(Gen, DynamicObject) {
867 const dynamic obj = dynamic::object(1, 2)(3, 4);
868 EXPECT_EQ(dynamic(4), from(obj.keys()) | sum);
869 EXPECT_EQ(dynamic(6), from(obj.values()) | sum);
870 EXPECT_EQ(dynamic(4), from(obj.items()) | get<0>() | sum);
871 EXPECT_EQ(dynamic(6), from(obj.items()) | get<1>() | sum);
875 auto s = from({7, 6, 5, 4, 3}) | as<set<int>>();
876 EXPECT_EQ(s.size(), 5);
882 auto s = from({1, 2});
883 EXPECT_EQ((vector<int> { 1, 2, 1, 2, 1 }),
884 s | cycle | take(5) | as<vector>());
887 auto s = from({1, 2});
888 EXPECT_EQ((vector<int> { 1, 2, 1, 2 }),
889 s | cycle(2) | as<vector>());
892 auto s = from({1, 2, 3});
893 EXPECT_EQ((vector<int> { 1, 2, 1, 2, 1 }),
894 s | take(2) | cycle | take(5) | as<vector>());
897 auto s = empty<int>();
898 EXPECT_EQ((vector<int> { }),
899 s | cycle | take(4) | as<vector>());
903 int* pcount = &count;
904 auto countdown = GENERATOR(int) {
905 ASSERT_GE(*pcount, 0)
906 << "Cycle should have stopped when it didnt' get values!";
907 for (int i = 1; i <= *pcount; ++i) {
913 EXPECT_EQ((vector<int> { 1, 2, 3, 1, 2, 1}),
914 s | cycle | as<vector>());
918 TEST(Gen, Dereference) {
920 const int x = 4, y = 2;
921 auto s = from(std::initializer_list<const int*>({&x, nullptr, &y}));
922 EXPECT_EQ(6, s | dereference | sum);
925 vector<int> a { 1, 2 };
926 vector<int> b { 3, 4 };
927 vector<vector<int>*> pv { &a, nullptr, &b };
930 | [&](vector<int>& v) {
933 EXPECT_EQ(3, a.size());
934 EXPECT_EQ(3, b.size());
935 EXPECT_EQ(5, a.back());
936 EXPECT_EQ(5, b.back());
939 vector<std::map<int, int>> maps {
956 | map([](std::map<int, int>& m) {
957 return get_ptr(m, 3);
963 vector<unique_ptr<int>> ups;
964 ups.emplace_back(new int(3));
966 ups.emplace_back(new int(7));
967 EXPECT_EQ(10, from(ups) | dereference | sum);
968 EXPECT_EQ(10, from(ups) | move | dereference | sum);
973 using std::runtime_error;
974 EXPECT_THROW(from({"1", "a", "3"})
979 from({"1", "a", "3"})
980 | guard<runtime_error>([](runtime_error&, const char*) {
981 return true; // continue
986 from({"1", "a", "3"})
987 | guard<runtime_error>([](runtime_error&, const char*) {
988 return false; // break
992 EXPECT_THROW(from({"1", "a", "3"})
993 | guard<runtime_error>([](runtime_error&, const char* v) {
1005 EXPECT_EQ((vector<vector<int>> { {1} }),
1006 seq(1, 1) | batch(5) | as<vector>());
1007 EXPECT_EQ((vector<vector<int>> { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11} }),
1008 seq(1, 11) | batch(3) | as<vector>());
1009 EXPECT_THROW(seq(1, 1) | batch(0) | as<vector>(),
1010 std::invalid_argument);
1013 TEST(Gen, BatchMove) {
1014 auto expected = vector<vector<int>>{ {0, 1}, {2, 3}, {4} };
1017 | mapped([](int i) { return std::unique_ptr<int>(new int(i)); })
1019 | mapped([](std::vector<std::unique_ptr<int>>& pVector) {
1020 std::vector<int> iVector;
1021 for (const auto& p : pVector) {
1022 iVector.push_back(*p);
1027 EXPECT_EQ(expected, actual);
1030 int main(int argc, char *argv[]) {
1031 testing::InitGoogleTest(&argc, argv);
1032 google::ParseCommandLineFlags(&argc, &argv, true);
1033 return RUN_ALL_TESTS();