2 * Copyright 2014 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/gen/Base.h"
23 #include "folly/gen/Parallel.h"
25 using namespace folly;
26 using namespace folly::gen;
29 const auto square = [](int i) { return i * i; };
30 const auto even = [](int i) { return 0 == i % 2; };
31 static auto sleepyWork = [](int i) {
32 const auto sleepyTime = std::chrono::microseconds(100);
33 std::this_thread::sleep_for(sleepyTime);
37 static auto isPrime = [](int n) {
41 for (int d = 3; d * d <= n; d += 2) {
52 std::unique_ptr<T> operator()(T t) const {
53 return std::unique_ptr<T>(new T(std::move(t)));
57 static auto primes = seq(1, 1 << 14)
59 | as<vector<size_t>>();
61 static auto primeFactors = [](int n) {
63 | filter([&](int d) { return 0 == n % d; })
67 TEST(ParallelTest, Serial) {
69 seq(1,10) | map(square) | filter(even) | sum,
70 seq(1,10) | parallel(map(square) | filter(even)) | sum);
73 auto heavyWork = map(primeFactors);
75 TEST(ParallelTest, ComputeBound64) {
77 EXPECT_EQ(seq<size_t>(1, length) | heavyWork | sum,
78 seq<size_t>(1, length) | parallel(heavyWork) | sum);
81 TEST(ParallelTest, Take) {
84 EXPECT_EQ(seq(1, length) | take(limit) | count,
85 seq(1, length) | parallel(heavyWork) | take(limit) | count);
89 TEST(ParallelTest, Unique) {
90 auto uniqued = from(primes) | map(makeUnique) | as<vector>();
91 EXPECT_EQ(primes.size(),
92 from(primes) | parallel(map(makeUnique)) |
93 parallel(dereference | map(makeUnique)) | dereference | count);
95 from(primes) | parallel(map(makeUnique)) |
96 parallel(dereference | map(makeUnique)) | dereference |
100 TEST(ParallelTest, PSum) {
101 EXPECT_EQ(from(primes) | map(sleepyWork) | sum,
102 from(primes) | parallel(map(sleepyWork) | sub(sum)) | sum);
105 int main(int argc, char *argv[]) {
106 testing::InitGoogleTest(&argc, argv);
107 google::ParseCommandLineFlags(&argc, &argv, true);
108 return RUN_ALL_TESTS();