2 * Copyright 2017 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>
24 #include <folly/FBVector.h>
25 #include <folly/MapUtil.h>
26 #include <folly/Memory.h>
27 #include <folly/String.h>
28 #include <folly/dynamic.h>
29 #include <folly/experimental/TestUtil.h>
30 #include <folly/gen/Base.h>
31 #include <folly/portability/GTest.h>
33 using namespace folly::gen;
34 using namespace folly;
35 using std::make_tuple;
41 using std::unique_ptr;
44 #define EXPECT_SAME(A, B) \
45 static_assert(std::is_same<A, B>::value, "Mismatched: " #A ", " #B)
46 EXPECT_SAME(int&&, typename ArgumentReference<int>::type);
47 EXPECT_SAME(int&, typename ArgumentReference<int&>::type);
48 EXPECT_SAME(const int&, typename ArgumentReference<const int&>::type);
49 EXPECT_SAME(const int&, typename ArgumentReference<const int>::type);
52 ostream& operator<<(ostream& os, const set<T>& values) {
53 return os << from(values);
57 ostream& operator<<(ostream& os, const vector<T>& values) {
59 for (auto& value : values) {
60 if (&value != &values.front()) {
68 auto square = [](int x) { return x * x; };
69 auto add = [](int a, int b) { return a + b; };
70 auto multiply = [](int a, int b) { return a * b; };
72 auto product = foldl(1, multiply);
74 template <typename A, typename B>
75 ostream& operator<<(ostream& os, const pair<A, B>& pair) {
76 return os << "(" << pair.first << ", " << pair.second << ")";
80 auto gen = seq(1, 10);
81 EXPECT_EQ(10, gen | count);
82 EXPECT_EQ(5, gen | take(5) | count);
86 auto gen = seq(1, 10);
87 EXPECT_EQ((1 + 10) * 10 / 2, gen | sum);
88 EXPECT_EQ((1 + 5) * 5 / 2, gen | take(5) | sum);
94 gen | [&](int x) { accum += x; };
97 gen | take(3) | [&](int x) { accum2 += x; };
102 auto expected = vector<int>{4, 9, 16};
103 auto gen = from({2, 3, 4}) | map(square);
104 EXPECT_EQ((vector<int>{4, 9, 16}), gen | as<vector>());
105 EXPECT_EQ((vector<int>{4, 9}), gen | take(2) | as<vector>());
110 Counter(int start = 0)
114 int count() const { return c; }
115 int incr() { return ++c; }
117 int& ref() { return c; }
118 const int& ref() const { return c; }
122 auto counters = seq(1, 10) | eachAs<Counter>() | as<vector>();
123 EXPECT_EQ(10 * (1 + 10) / 2,
125 | member(&Counter::count)
127 EXPECT_EQ(10 * (1 + 10) / 2,
130 | member(&Counter::count)
132 EXPECT_EQ(10 * (2 + 11) / 2,
134 | member(&Counter::incr)
136 EXPECT_EQ(10 * (3 + 12) / 2,
139 | member(&Counter::incr)
141 EXPECT_EQ(10 * (3 + 12) / 2,
143 | member(&Counter::count)
146 // type-verifications
147 auto m = empty<Counter&>();
148 auto c = empty<const Counter&>();
149 m | member(&Counter::incr) | assert_type<int&&>();
150 m | member(&Counter::count) | assert_type<int&&>();
151 m | member(&Counter::count) | assert_type<int&&>();
152 m | member<Const>(&Counter::ref) | assert_type<const int&>();
153 m | member<Mutable>(&Counter::ref) | assert_type<int&>();
154 c | member<Const>(&Counter::ref) | assert_type<const int&>();
159 X() : a(2), b(3), c(4), d(b) {}
164 int& d; // can't access this with a field pointer.
167 std::vector<X> xs(1);
168 EXPECT_EQ(2, from(xs)
171 EXPECT_EQ(3, from(xs)
174 EXPECT_EQ(4, from(xs)
177 EXPECT_EQ(2, seq(&xs[0], &xs[0])
181 empty<X&>() | field(&X::a) | assert_type<const int&>();
182 empty<X*>() | field(&X::a) | assert_type<const int&>();
183 empty<X&>() | field(&X::b) | assert_type<int&>();
184 empty<X*>() | field(&X::b) | assert_type<int&>();
185 empty<X&>() | field(&X::c) | assert_type<int&>();
186 empty<X*>() | field(&X::c) | assert_type<int&>();
188 empty<X&&>() | field(&X::a) | assert_type<const int&&>();
189 empty<X&&>() | field(&X::b) | assert_type<int&&>();
190 empty<X&&>() | field(&X::c) | assert_type<int&&>();
191 // references don't imply ownership so they're not moved
193 empty<const X&>() | field(&X::a) | assert_type<const int&>();
194 empty<const X*>() | field(&X::a) | assert_type<const int&>();
195 empty<const X&>() | field(&X::b) | assert_type<const int&>();
196 empty<const X*>() | field(&X::b) | assert_type<const int&>();
197 // 'mutable' has no effect on field pointers, by C++ spec
198 empty<const X&>() | field(&X::c) | assert_type<const int&>();
199 empty<const X*>() | field(&X::c) | assert_type<const int&>();
201 // can't form pointer-to-reference field: empty<X&>() | field(&X::d)
205 // cover the fenceposts of the loop unrolling
206 for (int n = 1; n < 100; ++n) {
207 EXPECT_EQ(n, seq(1, n) | count);
208 EXPECT_EQ(n + 1, seq(1) | take(n + 1) | count);
212 TEST(Gen, SeqWithStep) {
213 EXPECT_EQ(75, seq(5, 25, 5) | sum);
216 TEST(Gen, SeqWithStepArray) {
217 const std::array<int, 6> arr{{1, 2, 3, 4, 5, 6}};
218 EXPECT_EQ(9, seq(&arr[0], &arr[5], 2)
219 | map([](const int *i) { return *i; })
224 // cover the fenceposts of the loop unrolling
225 for (int n = 1; n < 100; ++n) {
226 EXPECT_EQ(gen::range(0, n) | count, n);
230 TEST(Gen, RangeWithStep) {
231 EXPECT_EQ(50, range(5, 25, 5) | sum);
234 TEST(Gen, FromIterators) {
235 vector<int> source {2, 3, 5, 7, 11};
236 auto gen = from(folly::range(source.begin() + 1, source.end() - 1));
237 EXPECT_EQ(3 * 5 * 7, gen | product);
241 auto source = seq(0, 10)
242 | map([](int i) { return std::make_pair(i, i * i); })
243 | as<std::map<int, int>>();
244 auto gen = fromConst(source)
245 | map([&](const std::pair<const int, int>& p) {
246 return p.second - p.first;
248 EXPECT_EQ(330, gen | sum);
252 const auto expected = vector<int>{1, 2, 4, 5, 7, 8};
255 | filter([](int x) { return x % 3; })
257 EXPECT_EQ(expected, actual);
260 TEST(Gen, FilterDefault) {
262 // Default filter should remove 0s
263 const auto expected = vector<int>{1, 1, 2, 3};
265 from({0, 1, 1, 0, 2, 3, 0})
268 EXPECT_EQ(expected, actual);
271 // Default filter should remove nullptrs
275 const auto expected = vector<int*>{&a, &b, &c};
277 from({(int*)nullptr, &a, &b, &c, (int*)nullptr})
280 EXPECT_EQ(expected, actual);
283 // Default filter on Optionals should remove folly::null
284 const auto expected =
285 vector<Optional<int>>{Optional<int>(5), Optional<int>(0)};
287 from({Optional<int>(5), Optional<int>(), Optional<int>(0)})
290 EXPECT_EQ(expected, actual);
294 TEST(Gen, FilterSink) {
297 | map([](int x) { return vector<int>{x}; })
298 | filter([](vector<int> v) { return !v.empty(); })
300 EXPECT_FALSE(from(actual) | rconcat | isEmpty);
303 TEST(Gen, Contains) {
308 EXPECT_TRUE(gen | contains(49));
309 EXPECT_FALSE(gen | contains(50));
313 seq(1) // infinite, to prove laziness
315 | eachTo<std::string>();
317 // std::string gen, const char* needle
318 EXPECT_TRUE(gen | take(9999) | contains("49"));
324 auto expected = vector<int>{1, 4, 9, 16};
327 | mapped([](int x) { return x * x; })
330 EXPECT_EQ(expected, actual);
333 auto expected = vector<int>{ 0, 1, 4, 5, 8 };
335 = ((seq(0) | take(2)) +
340 EXPECT_EQ(expected, actual);
343 auto expected = vector<int>{ 0, 1, 4, 5, 8 };
347 return seq(i * 4) | take(2);
352 EXPECT_EQ(expected, actual);
357 EXPECT_THROW(take(limit - 6), std::invalid_argument);
364 EXPECT_THROW(stride(0), std::invalid_argument);
367 auto expected = vector<int>{1, 2, 3, 4};
372 EXPECT_EQ(expected, actual);
375 auto expected = vector<int>{1, 3, 5, 7};
380 EXPECT_EQ(expected, actual);
383 auto expected = vector<int>{1, 4, 7, 10};
388 EXPECT_EQ(expected, actual);
391 auto expected = vector<int>{1, 3, 5, 7, 9, 1, 4, 7, 10};
393 = ((seq(1, 10) | stride(2)) +
394 (seq(1, 10) | stride(3)))
396 EXPECT_EQ(expected, actual);
398 EXPECT_EQ(500, seq(1) | take(1000) | stride(2) | count);
399 EXPECT_EQ(10, seq(1) | take(1000) | stride(2) | take(10) | count);
403 std::mt19937 rnd(42);
408 std::unordered_map<int,int> hits;
409 const int kNumIters = 80;
410 for (int i = 0; i < kNumIters; i++) {
411 auto vec = sampler | as<vector<int>>();
412 EXPECT_EQ(vec.size(), 50);
413 auto uniq = fromConst(vec) | as<set<int>>();
414 EXPECT_EQ(uniq.size(), vec.size()); // sampling without replacement
420 // In 80 separate samples of our range, we should have seen every value
421 // at least once and no value all 80 times. (The odds of either of those
422 // events is 1/2^80).
423 EXPECT_EQ(hits.size(), 100);
424 for (auto hit: hits) {
425 EXPECT_GT(hit.second, 0);
426 EXPECT_LT(hit.second, kNumIters);
432 EXPECT_EQ((small | sum), 15);
433 EXPECT_EQ((small | take(3) | count), 3);
439 | mapped([](int x) { return x * x; })
442 EXPECT_EQ((vector<int>{25, 36, 49, 64}), gen | as<vector>());
447 auto expected = vector<int>{1, 4, 9, 16};
450 | mapped([](int x) { return x * x; })
451 | until([](int x) { return x > 20; })
453 EXPECT_EQ(expected, actual);
456 auto expected = vector<int>{ 0, 1, 4, 5, 8 };
458 = ((seq(0) | until([](int i) { return i > 1; })) +
459 (seq(4) | until([](int i) { return i > 5; })) +
460 (seq(8) | until([](int i) { return i > 9; })))
461 | until([](int i) { return i > 8; })
463 EXPECT_EQ(expected, actual);
467 auto expected = vector<int>{ 0, 1, 5, 6, 10 };
471 return seq(i * 5) | until([=](int j) { return j > i * 5 + 1; });
474 | until([](int i) { return i > 10; })
476 EXPECT_EQ(expected, actual);
482 auto increment = [](int& i) { ++i; };
483 auto clone = map([](int i) { return i; });
486 auto actual = seq(0) | clone | visit(increment) | take(4) | sum;
487 EXPECT_EQ(expected, actual);
491 auto actual = seq(0, 3) | clone | visit(increment) | sum;
492 EXPECT_EQ(expected, actual);
495 std::vector<int> x2, x4;
496 std::vector<int> expected2{0, 1, 4, 9};
497 std::vector<int> expected4{0, 1, 16, 81};
499 auto tee = [](std::vector<int>& container) {
500 return visit([&](int value) { container.push_back(value); });
503 98, seq(0, 3) | map(square) | tee(x2) | map(square) | tee(x4) | sum);
504 EXPECT_EQ(expected2, x2);
505 EXPECT_EQ(expected4, x4);
509 TEST(Gen, Composed) {
510 // Operator, Operator
512 filter([](Optional<int>& o) { return o.hasValue(); })
513 | map([](Optional<int>& o) -> int& { return o.value(); });
514 std::vector<Optional<int>> opts {
515 none, 4, none, 6, none
517 EXPECT_EQ(4 * 4 + 6 * 6, from(opts) | valuesOf | map(square) | sum);
519 auto sumOpt = valuesOf | sum;
520 EXPECT_EQ(10, from(opts) | sumOpt);
524 std::vector<int> nums {2, 3, 5, 7};
525 std::map<int, int> mappings { { 3, 9}, {5, 25} };
526 auto gen = from(nums) + (from(mappings) | get<1>());
527 EXPECT_EQ(51, gen | sum);
528 EXPECT_EQ(5, gen | take(2) | sum);
529 EXPECT_EQ(26, gen | take(5) | sum);
533 std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
534 auto gen = from(nums) | rconcat;
535 EXPECT_EQ(17, gen | sum);
536 EXPECT_EQ(10, gen | take(3) | sum);
539 TEST(Gen, ConcatGen) {
540 auto gen = seq(1, 10)
541 | map([](int i) { return seq(1, i); })
543 EXPECT_EQ(220, gen | sum);
544 EXPECT_EQ(10, gen | take(6) | sum);
547 TEST(Gen, ConcatAlt) {
548 std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
549 auto actual = from(nums)
550 | map([](std::vector<int>& v) { return from(v); })
554 EXPECT_EQ(expected, actual);
558 auto expected = vector<int>{0, 3, 5, 6, 7, 8, 9};
560 from({8, 6, 7, 5, 3, 0, 9})
563 EXPECT_EQ(expected, actual);
566 TEST(Gen, OrderMoved) {
567 auto expected = vector<int>{0, 9, 25, 36, 49, 64, 81};
569 from({8, 6, 7, 5, 3, 0, 9})
574 EXPECT_EQ(expected, actual);
577 TEST(Gen, OrderTake) {
578 auto expected = vector<int>{9, 8, 7};
580 from({8, 6, 7, 5, 3, 0, 9})
581 | orderByDescending(square)
584 EXPECT_EQ(expected, actual);
587 TEST(Gen, Distinct) {
588 auto expected = vector<int>{3, 1, 2};
590 from({3, 1, 3, 2, 1, 2, 3})
593 EXPECT_EQ(expected, actual);
596 TEST(Gen, DistinctBy) { // 0 1 4 9 6 5 6 9 4 1 0
597 auto expected = vector<int>{0, 1, 2, 3, 4, 5};
600 | distinctBy([](int i) { return i * i % 10; })
602 EXPECT_EQ(expected, actual);
605 TEST(Gen, DistinctMove) { // 0 1 4 9 6 5 6 9 4 1 0
606 auto expected = vector<int>{0, 1, 2, 3, 4, 5};
609 | mapped([](int i) { return std::unique_ptr<int>(new int(i)); })
610 // see comment below about selector parameters for Distinct
611 | distinctBy([](const std::unique_ptr<int>& pi) { return *pi * *pi % 10; })
612 | mapped([](std::unique_ptr<int> pi) { return *pi; })
615 // NOTE(tjackson): the following line intentionally doesn't work:
616 // | distinctBy([](std::unique_ptr<int> pi) { return *pi * *pi % 10; })
617 // This is because distinctBy because the selector intentionally requires a
618 // const reference. If it required a move-reference, the value might get
619 // gutted by the selector before said value could be passed to downstream
621 EXPECT_EQ(expected, actual);
624 TEST(Gen, DistinctInfinite) {
625 // distinct should be able to handle an infinite sequence, provided that, of
626 // of cource, is it eventually made finite before returning the result.
627 auto expected = seq(0) | take(5) | as<vector>(); // 0 1 2 3 4
630 seq(0) // 0 1 2 3 4 5 6 7 ...
631 | mapped([](int i) { return i / 2; }) // 0 0 1 1 2 2 3 3 ...
632 | distinct // 0 1 2 3 4 5 6 7 ...
633 | take(5) // 0 1 2 3 4
636 EXPECT_EQ(expected, actual);
639 TEST(Gen, DistinctByInfinite) {
640 // Similarly to the DistinctInfinite test case, distinct by should be able to
641 // handle infinite sequences. Note that depending on how many values we take()
642 // at the end, the sequence may infinite loop. This is fine becasue we cannot
643 // solve the halting problem.
644 auto expected = vector<int>{1, 2};
646 seq(1) // 1 2 3 4 5 6 7 8 ...
647 | distinctBy([](int i) { return i % 2; }) // 1 2 (but might by infinite)
650 // Note that if we had take(3), this would infinite loop
652 EXPECT_EQ(expected, actual);
656 EXPECT_EQ(7, seq(1, 10)
657 | minBy([](int i) -> double {
665 auto gen = from({"three", "eleven", "four"});
667 EXPECT_EQ("eleven", gen | maxBy(&strlen) | unwrap);
671 auto odds = seq(2,10) | filter([](int i){ return i % 2; });
673 EXPECT_EQ(3, odds | min);
677 auto odds = seq(2,10) | filter([](int i){ return i % 2; });
679 EXPECT_EQ(9, odds | max);
683 string expected = "facebook";
684 string actual = "face";
685 from(StringPiece("book")) | appendTo(actual);
686 EXPECT_EQ(expected, actual);
689 TEST(Gen, FromRValue) {
691 // AFAICT The C++ Standard does not specify what happens to the rvalue
692 // reference of a std::vector when it is used as the 'other' for an rvalue
693 // constructor. Use fbvector because we're sure its size will be zero in
695 fbvector<int> v({1, 2, 3, 4});
697 EXPECT_EQ(v.size(), 4); // ensure that the lvalue version was called!
698 auto expected = 1 * 2 * 3 * 4;
699 EXPECT_EQ(expected, q1 | product);
701 auto q2 = from(std::move(v));
702 EXPECT_EQ(v.size(), 0); // ensure that rvalue version was called
703 EXPECT_EQ(expected, q2 | product);
707 auto q = from([] { return vector<int>({3, 7, 5}); }());
708 EXPECT_EQ(expected, q | max);
711 for (auto size : {5, 1024, 16384, 1 << 20}) {
712 auto q1 = from(vector<int>(size, 2));
713 auto q2 = from(vector<int>(size, 3));
714 // If the rvalue specialization is broken/gone, then the compiler will
715 // (disgustingly!) just store a *reference* to the temporary object,
716 // which is bad. Try to catch this by allocating two temporary vectors
717 // of the same size, so that they'll probably use the same underlying
718 // buffer if q1's vector is destructed before q2's vector is constructed.
719 EXPECT_EQ(size * 2 + size * 3, (q1 | sum) + (q2 | sum));
723 auto q = from(set<int>{1,2,3,2,1});
724 EXPECT_EQ(q | sum, 6);
729 auto expected = vector<int>{5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
732 | orderBy([](int x) { return (5.1 - x) * (5.1 - x); })
734 EXPECT_EQ(expected, actual);
736 expected = seq(1, 10) | as<vector>();
739 | map([] (int x) { return 11 - x; })
742 EXPECT_EQ(expected, actual);
746 int expected = 2 * 3 * 4 * 5;
749 | foldl(1, multiply);
750 EXPECT_EQ(expected, actual);
754 int expected = 2 + 3 + 4 + 5;
755 auto actual = seq(2, 5) | reduce(add);
756 EXPECT_EQ(expected, actual | unwrap);
759 TEST(Gen, ReduceBad) {
760 auto gen = seq(1) | take(0);
761 auto actual = gen | reduce(add);
762 EXPECT_FALSE(actual); // Empty sequences are okay, they just yeild 'none'
766 std::vector<unique_ptr<int>> ptrs;
767 ptrs.emplace_back(new int(1));
768 EXPECT_NE(ptrs.front().get(), nullptr);
769 auto ptrs2 = from(ptrs) | move | as<vector>();
770 EXPECT_EQ(ptrs.front().get(), nullptr);
771 EXPECT_EQ(**ptrs2.data(), 1);
775 auto gen = seq(0) | filter([](int x) { return x > 3; });
776 EXPECT_EQ(4, gen | first | unwrap);
779 TEST(Gen, FromCopy) {
780 vector<int> v {3, 5};
782 auto copy = fromCopy(v);
783 EXPECT_EQ(8, src | sum);
784 EXPECT_EQ(8, copy | sum);
786 EXPECT_EQ(10, src | sum);
787 EXPECT_EQ(8, copy | sum);
791 std::map<int, int> pairs {
797 auto pairSrc = from(pairs);
798 auto keys = pairSrc | get<0>();
799 auto values = pairSrc | get<1>();
800 EXPECT_EQ(10, keys | sum);
801 EXPECT_EQ(30, values | sum);
802 EXPECT_EQ(30, keys | map(square) | sum);
804 EXPECT_EQ(15, keys | sum);
805 EXPECT_EQ(55, values | sum);
807 vector<tuple<int, int, int>> tuples {
810 make_tuple(3, 9, 27),
812 EXPECT_EQ(36, from(tuples) | get<2>() | sum);
815 TEST(Gen, notEmpty) {
816 EXPECT_TRUE(seq(0, 1) | notEmpty);
817 EXPECT_TRUE(just(1) | notEmpty);
818 EXPECT_FALSE(gen::range(0, 0) | notEmpty);
819 EXPECT_FALSE(from({1}) | take(0) | notEmpty);
823 EXPECT_FALSE(seq(0, 1) | isEmpty);
824 EXPECT_FALSE(just(1) | isEmpty);
825 EXPECT_TRUE(gen::range(0, 0) | isEmpty);
826 EXPECT_TRUE(from({1}) | take(0) | isEmpty);
830 EXPECT_TRUE(seq(0, 10) | any([](int i) { return i == 7; }));
831 EXPECT_FALSE(seq(0, 10) | any([](int i) { return i == 11; }));
835 EXPECT_TRUE(seq(0, 10) | all([](int i) { return i < 11; }));
836 EXPECT_FALSE(seq(0, 10) | all([](int i) { return i < 5; }));
837 EXPECT_FALSE(seq(0) | take(9999) | all([](int i) { return i < 10; }));
839 // empty lists satisfies all
840 EXPECT_TRUE(seq(0) | take(0) | all([](int i) { return i < 50; }));
841 EXPECT_TRUE(seq(0) | take(0) | all([](int i) { return i > 50; }));
844 TEST(Gen, Yielders) {
845 auto gen = GENERATOR(int) {
846 for (int i = 1; i <= 5; ++i) {
850 for (int i = 3; ; ++i) {
854 vector<int> expected {
855 1, 2, 3, 4, 5, 7, 9, 16, 25
857 EXPECT_EQ(expected, gen | take(9) | as<vector>());
860 TEST(Gen, NestedYield) {
861 auto nums = GENERATOR(int) {
862 for (int i = 1; ; ++i) {
866 auto gen = GENERATOR(int) {
867 nums | take(10) | yield;
868 seq(1, 5) | [&](int i) {
872 EXPECT_EQ(70, gen | sum);
875 TEST(Gen, MapYielders) {
878 return GENERATOR(int) {
880 for (i = 1; i < n; ++i) {
883 for (; i >= 1; --i) {
889 vector<int> expected {
894 1, 2, 3, 4, 5, 4, 3, 2, 1,
896 EXPECT_EQ(expected, gen | as<vector>());
899 TEST(Gen, VirtualGen) {
900 VirtualGen<int> v(seq(1, 10));
901 EXPECT_EQ(55, v | sum);
903 EXPECT_EQ(385, v | sum);
905 EXPECT_EQ(55, v | sum);
906 EXPECT_EQ(30, v | take(4) | sum);
910 TEST(Gen, CustomType) {
914 auto gen = from({Foo{2}, Foo{3}})
915 | map([](const Foo& f) { return f.y; });
916 EXPECT_EQ(5, gen | sum);
919 TEST(Gen, NoNeedlessCopies) {
921 | map([](int x) { return unique_ptr<int>(new int(x)); })
922 | map([](unique_ptr<int> p) { return p; })
923 | map([](unique_ptr<int>&& p) { return std::move(p); })
924 | map([](const unique_ptr<int>& p) { return *p; });
925 EXPECT_EQ(15, gen | sum);
926 EXPECT_EQ(6, gen | take(3) | sum);
931 class TestIntSeq : public GenImpl<int, TestIntSeq> {
935 template <class Body>
936 bool apply(Body&& body) const {
937 for (int i = 1; i < 6; ++i) {
945 TestIntSeq(TestIntSeq&&) noexcept = default;
946 TestIntSeq& operator=(TestIntSeq&&) noexcept = default;
947 TestIntSeq(const TestIntSeq&) = delete;
948 TestIntSeq& operator=(const TestIntSeq&) = delete;
953 TEST(Gen, NoGeneratorCopies) {
954 EXPECT_EQ(15, TestIntSeq() | sum);
955 auto x = TestIntSeq() | take(3);
956 EXPECT_EQ(6, std::move(x) | sum);
959 TEST(Gen, FromArray) {
960 int source[] = {2, 3, 5, 7};
961 auto gen = from(source);
962 EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
965 TEST(Gen, FromStdArray) {
966 std::array<int,4> source {{2, 3, 5, 7}};
967 auto gen = from(source);
968 EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
971 TEST(Gen, StringConcat) {
972 auto gen = seq(1, 10)
975 EXPECT_EQ("12345678910", gen | as<string>());
983 CopyCounter() : copies(0), moves(0) {
987 CopyCounter(CopyCounter&& source) noexcept {
988 *this = std::move(source);
992 CopyCounter(const CopyCounter& source) {
1001 CopyCounter& operator=(const CopyCounter& source) {
1002 this->copies = source.copies + 1;
1003 this->moves = source.moves;
1007 CopyCounter& operator=(CopyCounter&& source) {
1008 this->copies = source.copies;
1009 this->moves = source.moves + 1;
1014 int CopyCounter::alive = 0;
1016 TEST(Gen, CopyCount) {
1017 vector<CopyCounter> originals;
1018 originals.emplace_back();
1019 EXPECT_EQ(1, originals.size());
1020 EXPECT_EQ(0, originals.back().copies);
1022 vector<CopyCounter> copies = from(originals) | as<vector>();
1023 EXPECT_EQ(1, copies.back().copies);
1024 EXPECT_EQ(0, copies.back().moves);
1026 vector<CopyCounter> moves = from(originals) | move | as<vector>();
1027 EXPECT_EQ(0, moves.back().copies);
1028 EXPECT_EQ(1, moves.back().moves);
1031 // test dynamics with various layers of nested arrays.
1032 TEST(Gen, Dynamic) {
1033 dynamic array1 = dynamic::array(1, 2);
1034 EXPECT_EQ(dynamic(3), from(array1) | sum);
1035 dynamic array2 = folly::dynamic::array(
1036 folly::dynamic::array(1), folly::dynamic::array(1, 2));
1037 EXPECT_EQ(dynamic(4), from(array2) | rconcat | sum);
1038 dynamic array3 = folly::dynamic::array(
1039 folly::dynamic::array(folly::dynamic::array(1)),
1040 folly::dynamic::array(
1041 folly::dynamic::array(1), folly::dynamic::array(1, 2)));
1042 EXPECT_EQ(dynamic(5), from(array3) | rconcat | rconcat | sum);
1045 TEST(Gen, DynamicObject) {
1046 const dynamic obj = dynamic::object(1, 2)(3, 4);
1047 EXPECT_EQ(dynamic(4), from(obj.keys()) | sum);
1048 EXPECT_EQ(dynamic(6), from(obj.values()) | sum);
1049 EXPECT_EQ(dynamic(4), from(obj.items()) | get<0>() | sum);
1050 EXPECT_EQ(dynamic(6), from(obj.items()) | get<1>() | sum);
1053 TEST(Gen, Collect) {
1054 auto s = from({7, 6, 5, 4, 3}) | as<set<int>>();
1055 EXPECT_EQ(s.size(), 5);
1061 auto s = from({1, 2});
1062 EXPECT_EQ((vector<int> { 1, 2, 1, 2, 1 }),
1063 s | cycle | take(5) | as<vector>());
1066 auto s = from({1, 2});
1067 EXPECT_EQ((vector<int> { 1, 2, 1, 2 }),
1068 s | cycle(2) | as<vector>());
1071 auto s = from({1, 2, 3});
1072 EXPECT_EQ((vector<int> { 1, 2, 1, 2, 1 }),
1073 s | take(2) | cycle | take(5) | as<vector>());
1076 auto s = empty<int>();
1077 EXPECT_EQ((vector<int> { }),
1078 s | cycle | take(4) | as<vector>());
1082 int* pcount = &count;
1083 auto countdown = GENERATOR(int) {
1084 ASSERT_GE(*pcount, 0)
1085 << "Cycle should have stopped when it didnt' get values!";
1086 for (int i = 1; i <= *pcount; ++i) {
1092 EXPECT_EQ((vector<int> { 1, 2, 3, 1, 2, 1}),
1093 s | cycle | take(7) | as<vector>());
1094 // take necessary as cycle returns an infinite generator
1098 TEST(Gen, Dereference) {
1100 const int x = 4, y = 2;
1101 auto s = from(std::initializer_list<const int*>({&x, nullptr, &y}));
1102 EXPECT_EQ(6, s | dereference | sum);
1105 vector<int> a { 1, 2 };
1106 vector<int> b { 3, 4 };
1107 vector<vector<int>*> pv { &a, nullptr, &b };
1110 | [&](vector<int>& v) {
1113 EXPECT_EQ(3, a.size());
1114 EXPECT_EQ(3, b.size());
1115 EXPECT_EQ(5, a.back());
1116 EXPECT_EQ(5, b.back());
1119 vector<std::map<int, int>> maps {
1136 | map([](std::map<int, int>& m) {
1137 return get_ptr(m, 3);
1143 vector<unique_ptr<int>> ups;
1144 ups.emplace_back(new int(3));
1146 ups.emplace_back(new int(7));
1147 EXPECT_EQ(10, from(ups) | dereference | sum);
1148 EXPECT_EQ(10, from(ups) | move | dereference | sum);
1153 struct DereferenceWrapper {
1155 string& operator*() & {
1158 string&& operator*() && {
1159 return std::move(data);
1161 explicit operator bool() {
1165 bool operator==(const DereferenceWrapper& a, const DereferenceWrapper& b) {
1166 return a.data == b.data;
1168 void PrintTo(const DereferenceWrapper& a, std::ostream* o) {
1169 *o << "Wrapper{\"" << cEscape<string>(a.data) << "\"}";
1173 TEST(Gen, DereferenceWithLValueRef) {
1174 auto original = vector<DereferenceWrapper>{{"foo"}, {"bar"}};
1175 auto copy = original;
1176 auto expected = vector<string>{"foo", "bar"};
1177 auto actual = from(original) | dereference | as<vector>();
1178 EXPECT_EQ(expected, actual);
1179 EXPECT_EQ(copy, original);
1182 TEST(Gen, DereferenceWithRValueRef) {
1183 auto original = vector<DereferenceWrapper>{{"foo"}, {"bar"}};
1184 auto empty = vector<DereferenceWrapper>{{}, {}};
1185 auto expected = vector<string>{"foo", "bar"};
1186 auto actual = from(original) | move | dereference | as<vector>();
1187 EXPECT_EQ(expected, actual);
1188 EXPECT_EQ(empty, original);
1191 TEST(Gen, Indirect) {
1193 EXPECT_EQ(&vs[0], from(vs) | indirect | first | unwrap);
1197 using std::runtime_error;
1198 EXPECT_THROW(from({"1", "a", "3"})
1203 from({"1", "a", "3"})
1204 | guard<runtime_error>([](runtime_error&, const char*) {
1205 return true; // continue
1210 from({"1", "a", "3"})
1211 | guard<runtime_error>([](runtime_error&, const char*) {
1212 return false; // break
1216 EXPECT_THROW(from({"1", "a", "3"})
1217 | guard<runtime_error>([](runtime_error&, const char* v) {
1229 EXPECT_EQ((vector<vector<int>> { {1} }),
1230 seq(1, 1) | batch(5) | as<vector>());
1231 EXPECT_EQ((vector<vector<int>> { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11} }),
1232 seq(1, 11) | batch(3) | as<vector>());
1233 EXPECT_THROW(seq(1, 1) | batch(0) | as<vector>(),
1234 std::invalid_argument);
1237 TEST(Gen, BatchMove) {
1238 auto expected = vector<vector<int>>{ {0, 1}, {2, 3}, {4} };
1241 | mapped([](int i) { return std::unique_ptr<int>(new int(i)); })
1243 | mapped([](std::vector<std::unique_ptr<int>>& pVector) {
1244 std::vector<int> iVector;
1245 for (const auto& p : pVector) {
1246 iVector.push_back(*p);
1251 EXPECT_EQ(expected, actual);
1255 auto expected = seq(0, 10) | as<std::vector>();
1256 for (size_t windowSize = 1; windowSize <= 20; ++windowSize) {
1258 auto actual = seq(0, 10) |
1259 mapped([](int i) { return std::unique_ptr<int>(new int(i)); }) |
1260 window(4) | dereference | as<std::vector>();
1261 EXPECT_EQ(expected, actual) << windowSize;
1263 for (size_t windowSize = 1; windowSize <= 20; ++windowSize) {
1265 auto actual = seq(0) |
1266 mapped([](int i) { return std::unique_ptr<int>(new int(i)); }) |
1267 take(11) | window(4) | dereference | as<std::vector>();
1268 EXPECT_EQ(expected, actual) << windowSize;
1270 for (size_t windowSize = 1; windowSize <= 20; ++windowSize) {
1272 auto actual = seq(0) |
1273 mapped([](int i) { return std::unique_ptr<int>(new int(i)); }) |
1274 window(4) | take(11) | dereference | as<std::vector>();
1275 EXPECT_EQ(expected, actual) << windowSize;
1283 EXPECT_EQ(&x, j | indirect | first | unwrap);
1285 EXPECT_EQ(4, j | sum);
1291 EXPECT_EQ(&x, j | indirect | first | unwrap);
1293 EXPECT_EQ(5, j | sum);
1297 auto j = just(std::move(x));
1298 EXPECT_NE(&x, j | indirect | first | unwrap);
1300 EXPECT_EQ(3, j | sum);
1304 TEST(Gen, GroupBy) {
1305 vector<string> strs{"zero", "one", "two", "three", "four",
1306 "five", "six", "seven", "eight", "nine"};
1308 auto gb = from(strs) | groupBy([](const string& str) { return str.size(); });
1310 EXPECT_EQ(10, gb | mapOp(count) | sum);
1311 EXPECT_EQ(3, gb | count);
1313 vector<string> mode{"zero", "four", "five", "nine"};
1315 gb | maxBy([](const Group<size_t, string>& g) { return g.size(); })
1319 vector<string> largest{"three", "seven", "eight"};
1321 gb | maxBy([](const Group<size_t, string>& g) { return g.key(); })
1329 EXPECT_EQ(4, o | unwrap);
1330 EXPECT_THROW(e | unwrap, OptionalEmptyException);
1332 auto oup = folly::make_optional(std::make_unique<int>(5));
1333 // optional has a value, and that value is non-null
1334 EXPECT_TRUE(bool(oup | unwrap));
1335 EXPECT_EQ(5, *(oup | unwrap));
1336 EXPECT_TRUE(oup.hasValue()); // still has a pointer (null or not)
1337 EXPECT_TRUE(bool(oup.value())); // that value isn't null
1339 auto moved1 = std::move(oup) | unwrapOr(std::make_unique<int>(6));
1340 // oup still has a value, but now it's now nullptr since the pointer was moved
1342 EXPECT_TRUE(oup.hasValue());
1343 EXPECT_FALSE(oup.value());
1344 EXPECT_TRUE(bool(moved1));
1345 EXPECT_EQ(5, *moved1);
1347 auto moved2 = std::move(oup) | unwrapOr(std::make_unique<int>(7));
1348 // oup's still-valid nullptr value wins here, the pointer to 7 doesn't apply
1349 EXPECT_FALSE(moved2);
1352 auto moved3 = std::move(oup) | unwrapOr(std::make_unique<int>(8));
1353 // oup is empty now, so the unwrapOr comes into play.
1354 EXPECT_TRUE(bool(moved3));
1355 EXPECT_EQ(8, *moved3);
1358 // mixed types, with common type matching optional
1359 Optional<double> full(3.3);
1360 decltype(full) empty;
1361 auto fallback = unwrapOr(4);
1362 EXPECT_EQ(3.3, full | fallback);
1363 EXPECT_EQ(3.3, std::move(full) | fallback);
1364 EXPECT_EQ(3.3, full | std::move(fallback));
1365 EXPECT_EQ(3.3, std::move(full) | std::move(fallback));
1366 EXPECT_EQ(4.0, empty | fallback);
1367 EXPECT_EQ(4.0, std::move(empty) | fallback);
1368 EXPECT_EQ(4.0, empty | std::move(fallback));
1369 EXPECT_EQ(4.0, std::move(empty) | std::move(fallback));
1373 // mixed types, with common type matching fallback
1374 Optional<int> full(3);
1375 decltype(full) empty;
1376 auto fallback = unwrapOr(5.0); // type: double
1377 // if we chose 'int' as the common type, we'd see truncation here
1378 EXPECT_EQ(1.5, (full | fallback) / 2);
1379 EXPECT_EQ(1.5, (std::move(full) | fallback) / 2);
1380 EXPECT_EQ(1.5, (full | std::move(fallback)) / 2);
1381 EXPECT_EQ(1.5, (std::move(full) | std::move(fallback)) / 2);
1382 EXPECT_EQ(2.5, (empty | fallback) / 2);
1383 EXPECT_EQ(2.5, (std::move(empty) | fallback) / 2);
1384 EXPECT_EQ(2.5, (empty | std::move(fallback)) / 2);
1385 EXPECT_EQ(2.5, (std::move(empty) | std::move(fallback)) / 2);
1389 auto opt = folly::make_optional(std::make_shared<int>(8));
1390 auto fallback = unwrapOr(std::make_unique<int>(9));
1391 // fallback must be std::move'd to be used
1392 EXPECT_EQ(8, *(opt | std::move(fallback)));
1393 EXPECT_TRUE(bool(opt.value())); // shared_ptr copied out, not moved
1394 EXPECT_TRUE(bool(opt)); // value still present
1395 EXPECT_TRUE(bool(fallback.value())); // fallback value not needed
1397 EXPECT_EQ(8, *(std::move(opt) | std::move(fallback)));
1398 EXPECT_FALSE(opt.value()); // shared_ptr moved out
1399 EXPECT_TRUE(bool(opt)); // gutted value still present
1400 EXPECT_TRUE(bool(fallback.value())); // fallback value not needed
1404 EXPECT_FALSE(opt); // opt is empty now
1405 EXPECT_EQ(9, *(std::move(opt) | std::move(fallback)));
1406 EXPECT_FALSE(fallback.value()); // fallback moved out!
1410 // test with nullptr
1411 vector<int> v{1, 2};
1412 EXPECT_EQ(&v[1], from(v) | indirect | max | unwrap);
1414 EXPECT_FALSE(from(v) | indirect | max | unwrapOr(nullptr));
1418 // mixed type determined by fallback
1419 Optional<std::nullptr_t> empty;
1421 EXPECT_EQ(&x, empty | unwrapOr(&x));
1425 int main(int argc, char *argv[]) {
1426 testing::InitGoogleTest(&argc, argv);
1427 gflags::ParseCommandLineFlags(&argc, &argv, true);
1428 return RUN_ALL_TESTS();