/*
- * 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.
* limitations under the License.
*/
-#include "folly/Foreach.h"
+#include <folly/Foreach.h>
-#include "folly/Benchmark.h"
+#include <folly/Benchmark.h>
#include <gtest/gtest.h>
#include <map>
#include <string>
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<std::string, int> testMap;
testMap["abc"] = 1;
std::map<int, std::string> 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";
}
}
BENCHMARK_SUSPEND {
setupBenchmark(iters);
- int sumKeys = 0;
- std::string sumValues = "";
}
FOR_EACH (iter, bmMap) {
}
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;
}
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;
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);
BENCHMARK(ForEachDescendingManual, iters) {
int sum = 1;
- for (auto i = iters; i-- > 1; ) {
+ for (size_t i = iters; i-- > 1; ) {
sum *= i;
}
doNotOptimizeAway(sum);
BENCHMARK(ForEachRangeR, iters) {
int sum = 1;
- FOR_EACH_RANGE_R (i, 1, iters) {
+ FOR_EACH_RANGE_R (i, 1U, iters) {
sum *= i;
}
doNotOptimizeAway(sum);