#include <folly/Foreach.h>
-#include <folly/Benchmark.h>
-#include <gtest/gtest.h>
+#include <folly/portability/GTest.h>
+
#include <map>
#include <string>
#include <vector>
EXPECT_EQ(0, n);
}
+TEST(Foreach, ForEachNested) {
+ const std::string hello = "hello";
+ size_t n = 0;
+ FOR_EACH(i, hello) {
+ FOR_EACH(j, hello) {
+ ++n;
+ }
+ }
+ auto len = hello.size();
+ EXPECT_EQ(len * len, n);
+}
+
TEST(Foreach, ForEachKV) {
std::map<std::string, int> testMap;
testMap["abc"] = 1;
}
EXPECT_EQ(10, sum);
}
-
-// Benchmarks:
-// 1. Benchmark iterating through the man with FOR_EACH, and also assign
-// iter->first and iter->second to local vars inside the FOR_EACH loop.
-// 2. Benchmark iterating through the man with FOR_EACH, but use iter->first and
-// iter->second as is, without assigning to local variables.
-// 3. Use FOR_EACH_KV loop to iterate through the map.
-
-std::map<int, std::string> bmMap; // For use in benchmarks below.
-
-void setupBenchmark(size_t iters) {
- bmMap.clear();
- for (size_t i = 0; i < iters; ++i) {
- bmMap[i] = "teststring";
- }
-}
-
-BENCHMARK(ForEachKVNoMacroAssign, iters) {
- int sumKeys = 0;
- std::string sumValues;
-
- BENCHMARK_SUSPEND {
- setupBenchmark(iters);
- }
-
- FOR_EACH (iter, bmMap) {
- const int k = iter->first;
- const std::string v = iter->second;
- sumKeys += k;
- sumValues += v;
- }
-}
-
-BENCHMARK(ForEachKVNoMacroNoAssign, iters) {
- int sumKeys = 0;
- std::string sumValues;
-
- BENCHMARK_SUSPEND {
- setupBenchmark(iters);
- }
-
- FOR_EACH (iter, bmMap) {
- sumKeys += iter->first;
- sumValues += iter->second;
- }
-}
-
-BENCHMARK(ManualLoopNoAssign, iters) {
- int sumKeys = 0;
- std::string sumValues;
-
- BENCHMARK_SUSPEND {
- setupBenchmark(iters);
- }
-
- for (auto iter = bmMap.begin(); iter != bmMap.end(); ++iter) {
- sumKeys += iter->first;
- sumValues += iter->second;
- }
-}
-
-BENCHMARK(ForEachKVMacro, iters) {
- int sumKeys = 0;
- std::string sumValues;
-
- BENCHMARK_SUSPEND {
- setupBenchmark(iters);
- }
-
- FOR_EACH_KV (k, v, bmMap) {
- sumKeys += k;
- sumValues += v;
- }
-}
-
-BENCHMARK(ForEachManual, iters) {
- int sum = 1;
- for (size_t i = 1; i < iters; ++i) {
- sum *= i;
- }
- doNotOptimizeAway(sum);
-}
-
-BENCHMARK(ForEachRange, iters) {
- int sum = 1;
- FOR_EACH_RANGE (i, 1, iters) {
- sum *= i;
- }
- doNotOptimizeAway(sum);
-}
-
-BENCHMARK(ForEachDescendingManual, iters) {
- int sum = 1;
- for (size_t i = iters; i-- > 1; ) {
- sum *= i;
- }
- doNotOptimizeAway(sum);
-}
-
-BENCHMARK(ForEachRangeR, iters) {
- int sum = 1;
- FOR_EACH_RANGE_R (i, 1U, iters) {
- sum *= i;
- }
- doNotOptimizeAway(sum);
-}
-
-int main(int argc, char** argv) {
- testing::InitGoogleTest(&argc, argv);
- auto r = RUN_ALL_TESTS();
- if (r) {
- return r;
- }
- runBenchmarks();
- return 0;
-}