Revert D5408572: replace getnameinfo with inet_ntop in v6 string formatting
[folly.git] / folly / gen / test / ParallelTest.cpp
1 /*
2  * Copyright 2017 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 <glog/logging.h>
18
19 #include <iostream>
20 #include <array>
21 #include <vector>
22
23 #include <folly/gen/Base.h>
24 #include <folly/gen/Parallel.h>
25 #include <folly/portability/GTest.h>
26
27 using namespace folly;
28 using namespace folly::gen;
29 using std::vector;
30
31 const auto square = [](int i) { return i * i; };
32 const auto even = [](int i) { return 0 == i % 2; };
33 static auto sleepyWork = [](int i) {
34   const auto sleepyTime = std::chrono::microseconds(100);
35   std::this_thread::sleep_for(sleepyTime);
36   return i;
37 };
38
39 static auto isPrime = [](int n) {
40   if (n < 2) {
41     return false;
42   } else if (n > 2) {
43     for (int d = 3; d * d <= n; d += 2) {
44       if (0 == n % d) {
45         return false;
46       }
47     }
48   }
49   return true;
50 };
51
52 struct {
53   template <class T>
54   std::unique_ptr<T> operator()(T t) const {
55     return std::unique_ptr<T>(new T(std::move(t)));
56   }
57 } makeUnique;
58
59 static auto primes = seq(1, 1 << 14)
60                    | filter(isPrime)
61                    | as<vector<size_t>>();
62
63 static auto primeFactors = [](int n) {
64   return from(primes)
65        | filter([&](int d) { return 0 == n % d; })
66        | count;
67 };
68
69 TEST(ParallelTest, Serial) {
70   EXPECT_EQ(
71             seq(1,10) | map(square) | filter(even) | sum,
72             seq(1,10) | parallel(map(square) | filter(even)) | sum);
73 }
74
75 auto heavyWork = map(primeFactors);
76
77 TEST(ParallelTest, ComputeBound64) {
78   int length = 1 << 10;
79   EXPECT_EQ(seq<size_t>(1, length) | heavyWork | sum,
80             seq<size_t>(1, length) | parallel(heavyWork) | sum);
81 }
82
83 TEST(ParallelTest, Take) {
84   int length = 1 << 18;
85   int limit = 1 << 14;
86   EXPECT_EQ(seq(1, length) | take(limit) | count,
87             seq(1, length) | parallel(heavyWork) | take(limit) | count);
88 }
89
90
91 TEST(ParallelTest, Unique) {
92   auto uniqued = from(primes) | map(makeUnique) | as<vector>();
93   EXPECT_EQ(primes.size(),
94             from(primes) | parallel(map(makeUnique)) |
95                 parallel(dereference | map(makeUnique)) | dereference | count);
96   EXPECT_EQ(2,
97             from(primes) | parallel(map(makeUnique)) |
98                 parallel(dereference | map(makeUnique)) | dereference |
99                 take(2) | count);
100 }
101
102 TEST(ParallelTest, PSum) {
103   EXPECT_EQ(from(primes) | map(sleepyWork) | sum,
104             from(primes) | parallel(map(sleepyWork) | sub(sum)) | sum);
105 }
106
107 int main(int argc, char *argv[]) {
108   testing::InitGoogleTest(&argc, argv);
109   gflags::ParseCommandLineFlags(&argc, &argv, true);
110   return RUN_ALL_TESTS();
111 }