Specialize and implement setThreadName only on some platforms
[folly.git] / folly / test / BitsTest.cpp
index 99bdcb484f1e0ee9148e9d315e13744fdcdc2d46..528fc07cfa7882fabc64c48c71a013c1bc0e5862 100644 (file)
@@ -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.
 // @author Tudor Bosman (tudorb@fb.com)
 
 #include <gflags/gflags.h>
-#include "folly/Bits.h"
-#include "folly/Benchmark.h"
+#include <folly/Bits.h>
+#include <folly/Benchmark.h>
 #include <gtest/gtest.h>
 
 using namespace folly;
 
+// Test constexpr-ness.
+#ifndef __clang__
+static_assert(findFirstSet(2u) == 2, "findFirstSet");
+static_assert(findLastSet(2u) == 2, "findLastSet");
+static_assert(nextPowTwo(2u) == 2, "nextPowTwo");
+static_assert(isPowTwo(2u), "isPowTwo");
+#endif  // __clang__
+
 namespace {
 
 template <class INT>
@@ -109,9 +117,42 @@ TEST(Bits, nextPowTwoClz) {
   testPowTwo(nextPowTwo);
 }
 
-int x; // prevent the loop from getting optimized away
 BENCHMARK(nextPowTwoClz, iters) {
-  x = folly::nextPowTwo(iters);
+  for (unsigned long i = 0; i < iters; ++i) {
+    auto x = folly::nextPowTwo(iters);
+    folly::doNotOptimizeAway(x);
+  }
+}
+
+TEST(Bits, isPowTwo) {
+  EXPECT_FALSE(isPowTwo(0u));
+  EXPECT_TRUE(isPowTwo(1ul));
+  EXPECT_TRUE(isPowTwo(2ull));
+  EXPECT_FALSE(isPowTwo(3ul));
+  EXPECT_TRUE(isPowTwo(4ul));
+  EXPECT_FALSE(isPowTwo(5ul));
+  EXPECT_TRUE(isPowTwo(8ul));
+  EXPECT_FALSE(isPowTwo(15u));
+  EXPECT_TRUE(isPowTwo(16u));
+  EXPECT_FALSE(isPowTwo(17u));
+  EXPECT_FALSE(isPowTwo(511ul));
+  EXPECT_TRUE(isPowTwo(512ul));
+  EXPECT_FALSE(isPowTwo(513ul));
+  EXPECT_FALSE(isPowTwo((1ul<<31) - 1));
+  EXPECT_TRUE(isPowTwo(1ul<<31));
+  EXPECT_FALSE(isPowTwo((1ul<<31) + 1));
+  EXPECT_FALSE(isPowTwo((1ull<<63) - 1));
+  EXPECT_TRUE(isPowTwo(1ull<<63));
+  EXPECT_FALSE(isPowTwo((1ull<<63) + 1));
+}
+
+BENCHMARK_DRAW_LINE();
+BENCHMARK(isPowTwo, iters) {
+  bool b;
+  for (unsigned long i = 0; i < iters; ++i) {
+    b = folly::isPowTwo(i);
+    folly::doNotOptimizeAway(b);
+  }
 }
 
 TEST(Bits, popcount) {
@@ -123,7 +164,7 @@ TEST(Bits, popcount) {
 
 int main(int argc, char** argv) {
   testing::InitGoogleTest(&argc, argv);
-  google::ParseCommandLineFlags(&argc, &argv, true);
+  gflags::ParseCommandLineFlags(&argc, &argv, true);
   auto ret = RUN_ALL_TESTS();
   if (!ret && FLAGS_benchmark) {
     folly::runBenchmarks();