ParallelMapTest.cpp 3.9 KB

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