2 * Copyright 2015 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.
16 #include <gtest/gtest.h>
21 #include <folly/Range.h>
22 #include <folly/FBVector.h>
23 #include <folly/experimental/TestUtil.h>
24 #include <folly/gen/Base.h>
25 #include <folly/gen/Combine.h>
27 using namespace folly::gen;
28 using namespace folly;
33 auto even = [](int i) -> bool { return i % 2 == 0; };
34 auto odd = [](int i) -> bool { return i % 2 == 1; };
36 TEST(CombineGen, Interleave) {
37 { // large (infinite) base, small container
38 auto base = seq(1) | filter(odd);
39 auto toInterleave = seq(1, 6) | filter(even);
40 auto interleaved = base | interleave(toInterleave | as<vector>());
41 EXPECT_EQ(interleaved | as<vector>(), vector<int>({1, 2, 3, 4, 5, 6}));
43 { // small base, large container
44 auto base = seq(1) | filter(odd) | take(3);
45 auto toInterleave = seq(1) | filter(even) | take(50);
46 auto interleaved = base | interleave(toInterleave | as<vector>());
47 EXPECT_EQ(interleaved | as<vector>(),
48 vector<int>({1, 2, 3, 4, 5, 6}));
52 TEST(CombineGen, Zip) {
54 // We rely on std::move(fbvector) emptying the source vector
55 auto zippee = fbvector<string>{"one", "two", "three"};
60 ASSERT_EQ(combined.size(), 3);
61 EXPECT_EQ(std::get<0>(combined[0]), 1);
62 EXPECT_EQ(std::get<1>(combined[0]), "one");
63 EXPECT_EQ(std::get<0>(combined[1]), 2);
64 EXPECT_EQ(std::get<1>(combined[1]), "two");
65 EXPECT_EQ(std::get<0>(combined[2]), 3);
66 EXPECT_EQ(std::get<1>(combined[2]), "three");
67 ASSERT_FALSE(zippee.empty());
68 EXPECT_FALSE(zippee.front().empty()); // shouldn't have been move'd
71 { // same as top, but using std::move.
73 | zip(std::move(zippee))
75 ASSERT_EQ(combined.size(), 3);
76 EXPECT_EQ(std::get<0>(combined[0]), 1);
77 EXPECT_TRUE(zippee.empty());
80 { // same as top, but base is truncated
81 auto baseFinite = seq(1) | take(1);
82 auto combined = baseFinite
83 | zip(vector<string>{"one", "two", "three"})
85 ASSERT_EQ(combined.size(), 1);
86 EXPECT_EQ(std::get<0>(combined[0]), 1);
87 EXPECT_EQ(std::get<1>(combined[0]), "one");
91 TEST(CombineGen, TupleFlatten) {
92 vector<tuple<int,string>> intStringTupleVec{
93 tuple<int,string>{1, "1"},
94 tuple<int,string>{2, "2"},
95 tuple<int,string>{3, "3"},
98 vector<tuple<char>> charTupleVec{
105 vector<double> doubleVec{
113 auto zipped1 = from(intStringTupleVec)
115 | assert_type<tuple<tuple<int, string>, tuple<char>>>()
117 EXPECT_EQ(std::get<0>(zipped1[0]), std::make_tuple(1, "1"));
118 EXPECT_EQ(std::get<1>(zipped1[0]), std::make_tuple('A'));
120 auto zipped2 = from(zipped1)
122 | assert_type<tuple<int, string, char>&&>()
124 ASSERT_EQ(zipped2.size(), 3);
125 EXPECT_EQ(zipped2[0], std::make_tuple(1, "1", 'A'));
127 auto zipped3 = from(charTupleVec)
128 | zip(intStringTupleVec)
130 | assert_type<tuple<char, int, string>&&>()
132 ASSERT_EQ(zipped3.size(), 3);
133 EXPECT_EQ(zipped3[0], std::make_tuple('A', 1, "1"));
135 auto zipped4 = from(intStringTupleVec)
138 | assert_type<tuple<int, string, double>&&>()
140 ASSERT_EQ(zipped4.size(), 3);
141 EXPECT_EQ(zipped4[0], std::make_tuple(1, "1", 1.0));
143 auto zipped5 = from(doubleVec)
145 | assert_type<tuple<double, double>>()
146 | tuple_flatten // essentially a no-op
147 | assert_type<tuple<double, double>&&>()
149 ASSERT_EQ(zipped5.size(), 5);
150 EXPECT_EQ(zipped5[0], std::make_tuple(1.0, 1.0));
152 auto zipped6 = from(intStringTupleVec)
157 | assert_type<tuple<int, string, char, double>&&>()
159 ASSERT_EQ(zipped6.size(), 3);
160 EXPECT_EQ(zipped6[0], std::make_tuple(1, "1", 'A', 1.0));
163 int main(int argc, char *argv[]) {
164 testing::InitGoogleTest(&argc, argv);
165 gflags::ParseCommandLineFlags(&argc, &argv, true);
166 return RUN_ALL_TESTS();