Fix copyright lines
[folly.git] / folly / gen / test / ParallelMapTest.cpp
1 /*
2  * Copyright 2014-present 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
21 #include <folly/Memory.h>
22 #include <folly/gen/Base.h>
23 #include <folly/gen/ParallelMap.h>
24 #include <folly/portability/GTest.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 std::make_unique<int>(x); })
105         | map([](std::unique_ptr<int> x) {
106             return std::make_unique<int>(*x * *x); })
107         | map([](std::unique_ptr<int> x) { return *x; })
108         | take(1000)
109         | sum;
110
111     auto pmapResult
112         = seq(1)
113         | pmap([](int x) { return std::make_unique<int>(x); })
114         | pmap([](std::unique_ptr<int> x) {
115             return std::make_unique<int>(*x * *x); })
116         | pmap([](std::unique_ptr<int> x) { return *x; })
117         | take(1000)
118         | sum;
119
120     EXPECT_EQ(pmapResult, mapResult);
121   }
122
123   // foreach
124   {
125     auto mapResult
126         = seq(1, 1000)
127         | map([](int x) { return std::make_unique<int>(x); })
128         | map([](std::unique_ptr<int> x) {
129             return std::make_unique<int>(*x * *x); })
130         | map([](std::unique_ptr<int> x) { return *x; })
131         | sum;
132
133     auto pmapResult
134         = seq(1, 1000)
135         | pmap([](int x) { return std::make_unique<int>(x); })
136         | pmap([](std::unique_ptr<int> x) {
137             return std::make_unique<int>(*x * *x); })
138         | pmap([](std::unique_ptr<int> x) { return *x; })
139         | sum;
140
141     EXPECT_EQ(pmapResult, mapResult);
142   }
143 }
144
145 int main(int argc, char *argv[]) {
146   testing::InitGoogleTest(&argc, argv);
147   gflags::ParseCommandLineFlags(&argc, &argv, true);
148   return RUN_ALL_TESTS();
149 }