2 * Copyright 2012 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>
22 #include "folly/experimental/Gen.h"
23 #include "folly/experimental/StringGen.h"
24 #include "folly/experimental/FileGen.h"
25 #include "folly/experimental/TestUtil.h"
26 #include "folly/FBVector.h"
27 #include "folly/Format.h"
28 #include "folly/dynamic.h"
30 using namespace folly::gen;
31 using namespace folly;
35 using std::unique_ptr;
39 using std::make_tuple;
40 //using std::unordered_map;
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>());
107 // cover the fenceposts of the loop unrolling
108 for (int n = 1; n < 100; ++n) {
109 EXPECT_EQ(n, seq(1, n) | count);
110 EXPECT_EQ(n + 1, seq(1) | take(n + 1) | count);
115 // cover the fenceposts of the loop unrolling
116 for (int n = 1; n < 100; ++n) {
117 EXPECT_EQ(range(0, n) | count, n);
121 TEST(Gen, FromIterators) {
122 vector<int> source {2, 3, 5, 7, 11};
123 auto gen = from(makeRange(source.begin() + 1, source.end() - 1));
124 EXPECT_EQ(3 * 5 * 7, gen | product);
128 auto source = seq(0, 10)
129 | map([](int i) { return std::make_pair(i, i * i); })
130 | as<std::map<int, int>>();
131 auto gen = fromConst(source)
132 | map([&](const std::pair<const int, int>& p) {
133 return p.second - p.first;
135 EXPECT_EQ(330, gen | sum);
139 const auto expected = vector<int>{1, 2, 4, 5, 7, 8};
142 | filter([](int x) { return x % 3; })
144 EXPECT_EQ(expected, actual);
148 auto expected = vector<int>{1, 4, 9, 16};
151 | mapped([](int x) { return x * x; })
154 EXPECT_EQ(expected, actual);
160 | mapped([](int x) { return x * x; })
163 EXPECT_EQ((vector<int>{25, 36, 49, 64}), gen | as<vector>());
169 | mapped([](int x) { return x * x; })
170 | until([](int x) { return x >= 1000; });
171 EXPECT_EQ(31, gen | count);
174 TEST(Gen, Composed) {
175 // Operator, Operator
177 filter([](Optional<int>& o) { return o.hasValue(); })
178 | map([](Optional<int>& o) -> int& { return o.value(); });
179 std::vector<Optional<int>> opts {
180 none, 4, none, 6, none
182 EXPECT_EQ(4 * 4 + 6 * 6, from(opts) | valuesOf | map(square) | sum);
184 auto sumOpt = valuesOf | sum;
185 EXPECT_EQ(10, from(opts) | sumOpt);
189 std::vector<int> nums {2, 3, 5, 7};
190 std::map<int, int> mappings { { 3, 9}, {5, 25} };
191 auto gen = from(nums) + (from(mappings) | get<1>());
192 EXPECT_EQ(51, gen | sum);
193 EXPECT_EQ(5, gen | take(2) | sum);
194 EXPECT_EQ(26, gen | take(5) | sum);
198 std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
199 auto gen = from(nums) | rconcat;
200 EXPECT_EQ(17, gen | sum);
201 EXPECT_EQ(10, gen | take(3) | sum);
204 TEST(Gen, ConcatGen) {
205 auto gen = seq(1, 10)
206 | map([](int i) { return seq(1, i); })
208 EXPECT_EQ(220, gen | sum);
209 EXPECT_EQ(10, gen | take(6) | sum);
212 TEST(Gen, ConcatAlt) {
213 std::vector<std::vector<int>> nums {{2, 3}, {5, 7}};
214 auto actual = from(nums)
215 | map([](std::vector<int>& v) { return from(v); })
219 EXPECT_EQ(expected, actual);
223 auto expected = vector<int>{0, 3, 5, 6, 7, 8, 9};
225 from({8, 6, 7, 5, 3, 0, 9})
228 EXPECT_EQ(expected, actual);
231 TEST(Gen, OrderMoved) {
232 auto expected = vector<int>{0, 9, 25, 36, 49, 64, 81};
234 from({8, 6, 7, 5, 3, 0, 9})
239 EXPECT_EQ(expected, actual);
242 TEST(Gen, OrderTake) {
243 auto expected = vector<int>{9, 8, 7};
245 from({8, 6, 7, 5, 3, 0, 9})
246 | orderByDescending(square)
249 EXPECT_EQ(expected, actual);
253 EXPECT_EQ(7, seq(1, 10)
261 auto gen = from({"three", "eleven", "four"});
263 EXPECT_EQ("eleven", gen | maxBy(&strlen));
267 fbstring expected = "facebook";
268 fbstring actual = "face";
269 from(StringPiece("book")) | appendTo(actual);
270 EXPECT_EQ(expected, actual);
273 TEST(Gen, FromRValue) {
275 // AFAICT The C++ Standard does not specify what happens to the rvalue
276 // reference of a std::vector when it is used as the 'other' for an rvalue
277 // constructor. Use fbvector because we're sure its size will be zero in
279 folly::fbvector<int> v({1,2,3,4});
281 EXPECT_EQ(v.size(), 4); // ensure that the lvalue version was called!
282 auto expected = 1 * 2 * 3 * 4;
283 EXPECT_EQ(expected, q1 | product);
285 auto q2 = from(std::move(v));
286 EXPECT_EQ(v.size(), 0); // ensure that rvalue version was called
287 EXPECT_EQ(expected, q2 | product);
291 auto q = from([] {return vector<int>({3,7,5}); }());
292 EXPECT_EQ(expected, q | max);
295 for (auto size: {5, 1024, 16384, 1<<20}) {
296 auto q1 = from(vector<int>(size, 2));
297 auto q2 = from(vector<int>(size, 3));
298 // If the rvalue specialization is broken/gone, then the compiler will
299 // (disgustingly!) just store a *reference* to the temporary object,
300 // which is bad. Try to catch this by allocating two temporary vectors
301 // of the same size, so that they'll probably use the same underlying
302 // buffer if q1's vector is destructed before q2's vector is constructed.
303 EXPECT_EQ(size * 2 + size * 3, (q1 | sum) + (q2 | sum));
307 auto q = from(set<int>{1,2,3,2,1});
308 EXPECT_EQ(q | sum, 6);
313 auto expected = vector<int>{5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
316 | orderBy([](int x) { return (5.1 - x) * (5.1 - x); })
318 EXPECT_EQ(expected, actual);
322 int expected = 2 * 3 * 4 * 5;
325 | foldl(1, multiply);
326 EXPECT_EQ(expected, actual);
330 int expected = 2 + 3 + 4 + 5;
331 auto actual = seq(2, 5) | reduce(add);
332 EXPECT_EQ(expected, actual);
335 TEST(Gen, ReduceBad) {
336 auto gen = seq(1) | take(0);
346 std::vector<unique_ptr<int>> ptrs;
347 ptrs.emplace_back(new int(1));
348 EXPECT_NE(ptrs.front().get(), nullptr);
349 auto ptrs2 = from(ptrs) | move | as<vector>();
350 EXPECT_EQ(ptrs.front().get(), nullptr);
351 EXPECT_EQ(**ptrs2.data(), 1);
357 | filter([](int x) { return x > 3; });
358 EXPECT_EQ(4, gen | first);
361 TEST(Gen, FromCopy) {
362 vector<int> v {3, 5};
364 auto copy = fromCopy(v);
365 EXPECT_EQ(8, src | sum);
366 EXPECT_EQ(8, copy | sum);
368 EXPECT_EQ(10, src | sum);
369 EXPECT_EQ(8, copy | sum);
373 std::map<int, int> pairs {
379 auto pairSrc = from(pairs);
380 auto keys = pairSrc | get<0>();
381 auto values = pairSrc | get<1>();
382 EXPECT_EQ(10, keys | sum);
383 EXPECT_EQ(30, values | sum);
384 EXPECT_EQ(30, keys | map(square) | sum);
386 EXPECT_EQ(15, keys | sum);
387 EXPECT_EQ(55, values | sum);
389 vector<tuple<int, int, int>> tuples {
392 make_tuple(3, 9, 27),
394 EXPECT_EQ(36, from(tuples) | get<2>() | sum);
398 EXPECT_TRUE(seq(0) | any);
399 EXPECT_TRUE(seq(0, 1) | any);
400 EXPECT_TRUE(from({1}) | any);
401 EXPECT_FALSE(range(0, 0) | any);
402 EXPECT_FALSE(from({1}) | take(0) | any);
405 TEST(Gen, Yielders) {
406 auto gen = GENERATOR(int) {
407 for (int i = 1; i <= 5; ++i) {
411 for (int i = 3; ; ++i) {
415 vector<int> expected {
416 1, 2, 3, 4, 5, 7, 9, 16, 25
418 EXPECT_EQ(expected, gen | take(9) | as<vector>());
421 TEST(Gen, NestedYield) {
422 auto nums = GENERATOR(int) {
423 for (int i = 1; ; ++i) {
427 auto gen = GENERATOR(int) {
428 nums | take(10) | yield;
429 seq(1, 5) | [&](int i) {
433 EXPECT_EQ(70, gen | sum);
436 TEST(Gen, MapYielders) {
439 return GENERATOR(int) {
441 for (i = 1; i < n; ++i)
448 vector<int> expected {
453 1, 2, 3, 4, 5, 4, 3, 2, 1,
455 EXPECT_EQ(expected, gen | as<vector>());
458 TEST(Gen, VirtualGen) {
459 VirtualGen<int> v(seq(1, 10));
460 EXPECT_EQ(55, v | sum);
462 EXPECT_EQ(385, v | sum);
464 EXPECT_EQ(55, v | sum);
465 EXPECT_EQ(30, v | take(4) | sum);
469 TEST(Gen, CustomType) {
473 auto gen = from({Foo{2}, Foo{3}})
474 | map([](const Foo& f) { return f.y; });
475 EXPECT_EQ(5, gen | sum);
478 TEST(Gen, NoNeedlessCopies) {
480 | map([](int x) { return unique_ptr<int>(new int(x)); })
481 | map([](unique_ptr<int> p) { return p; })
482 | map([](unique_ptr<int>&& p) { return std::move(p); })
483 | map([](const unique_ptr<int>& p) { return *p; });
484 EXPECT_EQ(15, gen | sum);
485 EXPECT_EQ(6, gen | take(3) | sum);
489 class TestIntSeq : public GenImpl<int, TestIntSeq> {
493 template <class Body>
494 bool apply(Body&& body) const {
495 for (int i = 1; i < 6; ++i) {
503 TestIntSeq(TestIntSeq&&) = default;
504 TestIntSeq& operator=(TestIntSeq&&) = default;
505 TestIntSeq(const TestIntSeq&) = delete;
506 TestIntSeq& operator=(const TestIntSeq&) = delete;
510 TEST(Gen, NoGeneratorCopies) {
511 EXPECT_EQ(15, TestIntSeq() | sum);
512 auto x = TestIntSeq() | take(3);
513 EXPECT_EQ(6, std::move(x) | sum);
516 TEST(Gen, FromArray) {
517 int source[] = {2, 3, 5, 7};
518 auto gen = from(source);
519 EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
522 TEST(Gen, FromStdArray) {
523 std::array<int,4> source {{2, 3, 5, 7}};
524 auto gen = from(source);
525 EXPECT_EQ(2 * 3 * 5 * 7, gen | product);
528 TEST(Gen, StringConcat) {
529 auto gen = seq(1, 10)
530 | map([](int n) { return folly::to<fbstring>(n); })
532 EXPECT_EQ("12345678910", gen | as<fbstring>());
540 CopyCounter() : copies(0), moves(0) {
544 CopyCounter(CopyCounter&& source) {
545 *this = std::move(source);
549 CopyCounter(const CopyCounter& source) {
558 CopyCounter& operator=(const CopyCounter& source) {
559 this->copies = source.copies + 1;
560 this->moves = source.moves;
564 CopyCounter& operator=(CopyCounter&& source) {
565 this->copies = source.copies;
566 this->moves = source.moves + 1;
571 int CopyCounter::alive = 0;
573 TEST(Gen, CopyCount) {
574 vector<CopyCounter> originals;
575 originals.emplace_back();
576 EXPECT_EQ(1, originals.size());
577 EXPECT_EQ(0, originals.back().copies);
579 vector<CopyCounter> copies = from(originals) | as<vector>();
580 EXPECT_EQ(1, copies.back().copies);
581 EXPECT_EQ(0, copies.back().moves);
583 vector<CopyCounter> moves = from(originals) | move | as<vector>();
584 EXPECT_EQ(0, moves.back().copies);
585 EXPECT_EQ(1, moves.back().moves);
588 // test dynamics with various layers of nested arrays.
590 dynamic array1 = {1, 2};
591 EXPECT_EQ(dynamic(3), from(array1) | sum);
592 dynamic array2 = {{1}, {1, 2}};
593 EXPECT_EQ(dynamic(4), from(array2) | rconcat | sum);
594 dynamic array3 = {{{1}}, {{1}, {1, 2}}};
595 EXPECT_EQ(dynamic(5), from(array3) | rconcat | rconcat | sum);
598 TEST(StringGen, EmptySplit) {
599 auto collect = eachTo<std::string>() | as<vector>();
601 auto pieces = split("", ',') | collect;
602 EXPECT_EQ(0, pieces.size());
605 // The last delimiter is eaten, just like std::getline
607 auto pieces = split(",", ',') | collect;
608 EXPECT_EQ(1, pieces.size());
609 EXPECT_EQ("", pieces[0]);
613 auto pieces = split(",,", ',') | collect;
614 EXPECT_EQ(2, pieces.size());
615 EXPECT_EQ("", pieces[0]);
616 EXPECT_EQ("", pieces[1]);
620 auto pieces = split(",,", ',') | take(1) | collect;
621 EXPECT_EQ(1, pieces.size());
622 EXPECT_EQ("", pieces[0]);
626 TEST(StringGen, Split) {
627 auto collect = eachTo<std::string>() | as<vector>();
629 auto pieces = split("hello,, world, goodbye, meow", ',') | collect;
630 EXPECT_EQ(5, pieces.size());
631 EXPECT_EQ("hello", pieces[0]);
632 EXPECT_EQ("", pieces[1]);
633 EXPECT_EQ(" world", pieces[2]);
634 EXPECT_EQ(" goodbye", pieces[3]);
635 EXPECT_EQ(" meow", pieces[4]);
639 auto pieces = split("hello,, world, goodbye, meow", ',')
641 EXPECT_EQ(3, pieces.size());
642 EXPECT_EQ("hello", pieces[0]);
643 EXPECT_EQ("", pieces[1]);
644 EXPECT_EQ(" world", pieces[2]);
648 auto pieces = split("hello,, world, goodbye, meow", ',')
650 EXPECT_EQ(5, pieces.size());
651 EXPECT_EQ("hello", pieces[0]);
652 EXPECT_EQ("", pieces[1]);
653 EXPECT_EQ(" world", pieces[2]);
657 TEST(StringGen, EmptyResplit) {
658 auto collect = eachTo<std::string>() | as<vector>();
660 auto pieces = from({""}) | resplit(',') | collect;
661 EXPECT_EQ(0, pieces.size());
664 // The last delimiter is eaten, just like std::getline
666 auto pieces = from({","}) | resplit(',') | collect;
667 EXPECT_EQ(1, pieces.size());
668 EXPECT_EQ("", pieces[0]);
672 auto pieces = from({",,"}) | resplit(',') | collect;
673 EXPECT_EQ(2, pieces.size());
674 EXPECT_EQ("", pieces[0]);
675 EXPECT_EQ("", pieces[1]);
679 TEST(StringGen, Resplit) {
680 auto collect = eachTo<std::string>() | as<vector>();
682 auto pieces = from({"hello,, world, goodbye, meow"}) |
683 resplit(',') | collect;
684 EXPECT_EQ(5, pieces.size());
685 EXPECT_EQ("hello", pieces[0]);
686 EXPECT_EQ("", pieces[1]);
687 EXPECT_EQ(" world", pieces[2]);
688 EXPECT_EQ(" goodbye", pieces[3]);
689 EXPECT_EQ(" meow", pieces[4]);
692 auto pieces = from({"hel", "lo,", ", world", ", goodbye, m", "eow"}) |
693 resplit(',') | collect;
694 EXPECT_EQ(5, pieces.size());
695 EXPECT_EQ("hello", pieces[0]);
696 EXPECT_EQ("", pieces[1]);
697 EXPECT_EQ(" world", pieces[2]);
698 EXPECT_EQ(" goodbye", pieces[3]);
699 EXPECT_EQ(" meow", pieces[4]);
703 TEST(FileGen, ByLine) {
704 auto collect = eachTo<std::string>() | as<vector>();
705 test::TemporaryFile file("ByLine");
706 static const std::string lines(
708 "This is the second line\n"
711 "a few empty lines above\n"
712 "incomplete last line");
713 EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
715 auto expected = from({lines}) | resplit('\n') | collect;
716 auto found = byLine(file.path().c_str()) | collect;
718 EXPECT_TRUE(expected == found);
721 class FileGenBufferedTest : public ::testing::TestWithParam<int> { };
723 TEST_P(FileGenBufferedTest, FileWriter) {
724 size_t bufferSize = GetParam();
725 test::TemporaryFile file("FileWriter");
727 static const std::string lines(
729 "This is the second line\n"
732 "a few empty lines above\n");
734 auto src = from({lines, lines, lines, lines, lines, lines, lines, lines});
735 auto collect = eachTo<std::string>() | as<vector>();
736 auto expected = src | resplit('\n') | collect;
738 src | eachAs<StringPiece>() | toFile(file.fd(), bufferSize);
739 auto found = byLine(file.path().c_str()) | collect;
741 EXPECT_TRUE(expected == found);
744 INSTANTIATE_TEST_CASE_P(
745 DifferentBufferSizes,
747 ::testing::Values(0, 1, 2, 4, 8, 64, 4096));
749 int main(int argc, char *argv[]) {
750 testing::InitGoogleTest(&argc, argv);
751 google::ParseCommandLineFlags(&argc, &argv, true);
752 return RUN_ALL_TESTS();