X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FForeachTest.cpp;h=4d97b0b0ddfdbd1509ab03b3ef3f76f20286429a;hb=587e4b4d914c477b79120fb6559571d2dc5d040d;hp=700e87c092658b9d49e2931229975485071ee60f;hpb=27494a20393fa45072e7d526d358835f3abe312a;p=folly.git diff --git a/folly/test/ForeachTest.cpp b/folly/test/ForeachTest.cpp index 700e87c0..4d97b0b0 100644 --- a/folly/test/ForeachTest.cpp +++ b/folly/test/ForeachTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,9 +14,9 @@ * limitations under the License. */ -#include "folly/Foreach.h" +#include -#include "folly/Benchmark.h" +#include #include #include #include @@ -26,6 +26,20 @@ using namespace folly; using namespace folly::detail; +TEST(Foreach, ForEachRvalue) { + const char* const hello = "hello"; + int n = 0; + FOR_EACH(it, std::string(hello)) { + ++n; + } + EXPECT_EQ(strlen(hello), n); + FOR_EACH_R(it, std::string(hello)) { + --n; + EXPECT_EQ(hello[n], *it); + } + EXPECT_EQ(0, n); +} + TEST(Foreach, ForEachKV) { std::map testMap; testMap["abc"] = 1; @@ -161,9 +175,9 @@ TEST(Foreach, ForEachRangeR) { std::map bmMap; // For use in benchmarks below. -void setupBenchmark(int iters) { +void setupBenchmark(size_t iters) { bmMap.clear(); - for (int i = 0; i < iters; ++i) { + for (size_t i = 0; i < iters; ++i) { bmMap[i] = "teststring"; } } @@ -174,8 +188,6 @@ BENCHMARK(ForEachKVNoMacroAssign, iters) { BENCHMARK_SUSPEND { setupBenchmark(iters); - int sumKeys = 0; - std::string sumValues = ""; } FOR_EACH (iter, bmMap) { @@ -201,11 +213,12 @@ BENCHMARK(ForEachKVNoMacroNoAssign, iters) { } BENCHMARK(ManualLoopNoAssign, iters) { + int sumKeys = 0; + std::string sumValues; + BENCHMARK_SUSPEND { setupBenchmark(iters); } - int sumKeys = 0; - std::string sumValues; for (auto iter = bmMap.begin(); iter != bmMap.end(); ++iter) { sumKeys += iter->first; @@ -214,11 +227,12 @@ BENCHMARK(ManualLoopNoAssign, iters) { } BENCHMARK(ForEachKVMacro, iters) { + int sumKeys = 0; + std::string sumValues; + BENCHMARK_SUSPEND { setupBenchmark(iters); } - int sumKeys = 0; - std::string sumValues; FOR_EACH_KV (k, v, bmMap) { sumKeys += k; @@ -228,7 +242,7 @@ BENCHMARK(ForEachKVMacro, iters) { BENCHMARK(ForEachManual, iters) { int sum = 1; - for (auto i = 1; i < iters; ++i) { + for (size_t i = 1; i < iters; ++i) { sum *= i; } doNotOptimizeAway(sum); @@ -244,7 +258,7 @@ BENCHMARK(ForEachRange, iters) { BENCHMARK(ForEachDescendingManual, iters) { int sum = 1; - for (auto i = iters; i-- > 1; ) { + for (size_t i = iters; i-- > 1; ) { sum *= i; } doNotOptimizeAway(sum); @@ -252,7 +266,7 @@ BENCHMARK(ForEachDescendingManual, iters) { BENCHMARK(ForEachRangeR, iters) { int sum = 1; - FOR_EACH_RANGE_R (i, 1, iters) { + FOR_EACH_RANGE_R (i, 1U, iters) { sum *= i; } doNotOptimizeAway(sum);