7b29f6cd6d060ee6133c5427c2f894c43f58b668
[folly.git] / folly / gen / test / ParallelMapTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <vector>
18
19 #include <glog/logging.h>
20 #include <gtest/gtest.h>
21
22 #include "folly/Memory.h"
23 #include "folly/gen/Base.h"
24 #include "folly/gen/ParallelMap.h"
25
26 using namespace folly;
27 using namespace folly::gen;
28
29 TEST(Pmap, InfiniteEquivalent) {
30   // apply
31   {
32     auto mapResult
33       = seq(1)
34       | map([](int x) { return x * x; })
35       | until([](int x) { return x > 1000 * 1000; })
36       | as<std::vector<int>>();
37
38     auto pmapResult
39       = seq(1)
40       | pmap([](int x) { return x * x; }, 4)
41       | until([](int x) { return x > 1000 * 1000; })
42       | as<std::vector<int>>();
43
44     EXPECT_EQ(pmapResult, mapResult);
45   }
46
47   // foreach
48   {
49     auto mapResult
50       = seq(1, 10)
51       | map([](int x) { return x * x; })
52       | as<std::vector<int>>();
53
54     auto pmapResult
55       = seq(1, 10)
56       | pmap([](int x) { return x * x; }, 4)
57       | as<std::vector<int>>();
58
59     EXPECT_EQ(pmapResult, mapResult);
60   }
61 }
62
63 TEST(Pmap, Empty) {
64   // apply
65   {
66     auto mapResult
67       = seq(1)
68       | map([](int x) { return x * x; })
69       | until([](int) { return true; })
70       | as<std::vector<int>>();
71
72     auto pmapResult
73       = seq(1)
74       | pmap([](int x) { return x * x; }, 4)
75       | until([](int) { return true; })
76       | as<std::vector<int>>();
77
78     EXPECT_EQ(mapResult.size(), 0);
79     EXPECT_EQ(pmapResult, mapResult);
80   }
81
82   // foreach
83   {
84     auto mapResult
85       = empty<int>()
86       | map([](int x) { return x * x; })
87       | as<std::vector<int>>();
88
89     auto pmapResult
90       = empty<int>()
91       | pmap([](int x) { return x * x; }, 4)
92       | as<std::vector<int>>();
93
94     EXPECT_EQ(mapResult.size(), 0);
95     EXPECT_EQ(pmapResult, mapResult);
96   }
97 }
98
99 TEST(Pmap, Rvalues) {
100   // apply
101   {
102     auto mapResult
103       = seq(1)
104       | map([](int x) { return make_unique<int>(x); })
105       | map([](std::unique_ptr<int> x) { return make_unique<int>(*x * *x); })
106       | map([](std::unique_ptr<int> x) { return *x; })
107       | take(1000)
108       | sum;
109
110     auto pmapResult
111       = seq(1)
112       | pmap([](int x) { return make_unique<int>(x); })
113       | pmap([](std::unique_ptr<int> x) { return make_unique<int>(*x * *x); })
114       | pmap([](std::unique_ptr<int> x) { return *x; })
115       | take(1000)
116       | sum;
117
118     EXPECT_EQ(pmapResult, mapResult);
119   }
120
121   // foreach
122   {
123     auto mapResult
124       = seq(1, 1000)
125       | map([](int x) { return make_unique<int>(x); })
126       | map([](std::unique_ptr<int> x) { return make_unique<int>(*x * *x); })
127       | map([](std::unique_ptr<int> x) { return *x; })
128       | sum;
129
130     auto pmapResult
131       = seq(1, 1000)
132       | pmap([](int x) { return make_unique<int>(x); })
133       | pmap([](std::unique_ptr<int> x) { return make_unique<int>(*x * *x); })
134       | pmap([](std::unique_ptr<int> x) { return *x; })
135       | sum;
136
137     EXPECT_EQ(pmapResult, mapResult);
138   }
139 }
140
141 int main(int argc, char *argv[]) {
142   testing::InitGoogleTest(&argc, argv);
143   google::ParseCommandLineFlags(&argc, &argv, true);
144   return RUN_ALL_TESTS();
145 }