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 | // clang-format off |
33 | auto mapResult |
34 | = seq(1) |
35 | | map([](int x) { return x * x; }) |
36 | | until([](int x) { return x > 1000 * 1000; }) |
37 | | as<std::vector<int>>(); |
38 | |
39 | auto pmapResult |
40 | = seq(1) |
41 | | pmap([](int x) { return x * x; }, 4) |
42 | | until([](int x) { return x > 1000 * 1000; }) |
43 | | as<std::vector<int>>(); |
44 | // clang-format on |
45 | |
46 | EXPECT_EQ(pmapResult, mapResult); |
47 | } |
48 | |
49 | // foreach |
50 | { |
51 | // clang-format off |
52 | auto mapResult |
53 | = seq(1, 10) |
54 | | map([](int x) { return x * x; }) |
55 | | as<std::vector<int>>(); |
56 | |
57 | auto pmapResult |
58 | = seq(1, 10) |
59 | | pmap([](int x) { return x * x; }, 4) |
60 | | as<std::vector<int>>(); |
61 | // clang-format on |
62 | |
63 | EXPECT_EQ(pmapResult, mapResult); |
64 | } |
65 | } |
66 | |
67 | TEST(Pmap, Empty) { |
68 | // apply |
69 | { |
70 | // clang-format off |
71 | auto mapResult |
72 | = seq(1) |
73 | | map([](int x) { return x * x; }) |
74 | | until([](int) { return true; }) |
75 | | as<std::vector<int>>(); |
76 | |
77 | auto pmapResult |
78 | = seq(1) |
79 | | pmap([](int x) { return x * x; }, 4) |
80 | | until([](int) { return true; }) |
81 | | as<std::vector<int>>(); |
82 | // clang-format on |
83 | |
84 | EXPECT_EQ(mapResult.size(), 0); |
85 | EXPECT_EQ(pmapResult, mapResult); |
86 | } |
87 | |
88 | // foreach |
89 | { |
90 | // clang-format off |
91 | auto mapResult |
92 | = empty<int>() |
93 | | map([](int x) { return x * x; }) |
94 | | as<std::vector<int>>(); |
95 | |
96 | auto pmapResult |
97 | = empty<int>() |
98 | | pmap([](int x) { return x * x; }, 4) |
99 | | as<std::vector<int>>(); |
100 | // clang-format on |
101 | |
102 | EXPECT_EQ(mapResult.size(), 0); |
103 | EXPECT_EQ(pmapResult, mapResult); |
104 | } |
105 | } |
106 | |
107 | TEST(Pmap, Rvalues) { |
108 | // apply |
109 | { |
110 | // clang-format off |
111 | auto mapResult |
112 | = seq(1) |
113 | | map([](int x) { return std::make_unique<int>(x); }) |
114 | | map([](std::unique_ptr<int> x) { |
115 | return std::make_unique<int>(*x * *x); }) |
116 | | map([](std::unique_ptr<int> x) { return *x; }) |
117 | | take(1000) |
118 | | sum; |
119 | |
120 | auto pmapResult |
121 | = seq(1) |
122 | | pmap([](int x) { return std::make_unique<int>(x); }) |
123 | | pmap([](std::unique_ptr<int> x) { |
124 | return std::make_unique<int>(*x * *x); }) |
125 | | pmap([](std::unique_ptr<int> x) { return *x; }) |
126 | | take(1000) |
127 | | sum; |
128 | // clang-format on |
129 | |
130 | EXPECT_EQ(pmapResult, mapResult); |
131 | } |
132 | |
133 | // foreach |
134 | { |
135 | // clang-format off |
136 | auto mapResult |
137 | = seq(1, 1000) |
138 | | map([](int x) { return std::make_unique<int>(x); }) |
139 | | map([](std::unique_ptr<int> x) { |
140 | return std::make_unique<int>(*x * *x); }) |
141 | | map([](std::unique_ptr<int> x) { return *x; }) |
142 | | sum; |
143 | |
144 | auto pmapResult |
145 | = seq(1, 1000) |
146 | | pmap([](int x) { return std::make_unique<int>(x); }) |
147 | | pmap([](std::unique_ptr<int> x) { |
148 | return std::make_unique<int>(*x * *x); }) |
149 | | pmap([](std::unique_ptr<int> x) { return *x; }) |
150 | | sum; |
151 | // clang-format on |
152 | |
153 | EXPECT_EQ(pmapResult, mapResult); |
154 | } |
155 | } |
156 | |
157 | int main(int argc, char* argv[]) { |
158 | testing::InitGoogleTest(&argc, argv); |
159 | gflags::ParseCommandLineFlags(&argc, &argv, true); |
160 | return RUN_ALL_TESTS(); |
161 | } |
162 | |