Use std::chrono::high_resolution_clock for folly::Benchmark
[folly.git] / folly / test / VarintTest.cpp
index 812b888baea3a4d08250f4bed7507c88f1eb6965..8b8a1856cbf80c3181f0bb5540fbcf173dc86901 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <vector>
 
 #include <glog/logging.h>
-#include <gtest/gtest.h>
 
 #include <folly/Benchmark.h>
 #include <folly/Random.h>
+#include <folly/portability/GTest.h>
 
 DEFINE_int32(random_seed, folly::randomNumberSeed(), "random seed");
 
@@ -65,6 +65,24 @@ void testVarint(uint64_t val, std::initializer_list<uint8_t> bytes) {
   }
 }
 
+TEST(Varint, Interface) {
+  // Make sure decodeVarint() accepts all of StringPiece, MutableStringPiece,
+  // ByteRange, and MutableByteRange.
+  char c = 0;
+
+  StringPiece sp(&c, 1);
+  EXPECT_EQ(decodeVarint(sp), 0);
+
+  MutableStringPiece msp(&c, 1);
+  EXPECT_EQ(decodeVarint(msp), 0);
+
+  ByteRange br(reinterpret_cast<unsigned char*>(&c), 1);
+  EXPECT_EQ(decodeVarint(br), 0);
+
+  MutableByteRange mbr(reinterpret_cast<unsigned char*>(&c), 1);
+  EXPECT_EQ(decodeVarint(mbr), 0);
+}
+
 TEST(Varint, Simple) {
   testVarint(0, {0});
   testVarint(1, {1});
@@ -115,7 +133,7 @@ void generateRandomValues() {
   for (size_t i = 0; i < kNumValues; ++i) {
     int n = numBytes(rng);
     uint64_t val = 0;
-    for (size_t j = 0; j < n; ++j) {
+    for (int j = 0; j < n; ++j) {
       val = (val << 8) + byte(rng);
     }
     gValues[i] = val;
@@ -145,7 +163,6 @@ void generateRandomValues() {
 BENCHMARK(VarintEncoding, iters) {
   uint8_t* start = &(*gEncoded.begin());
   uint8_t* p = start;
-  bool empty = (iters == 0);
   while (iters--) {
     p = start;
     for (auto& v : gValues) {
@@ -172,7 +189,7 @@ BENCHMARK(VarintDecoding, iters) {
 
 int main(int argc, char *argv[]) {
   testing::InitGoogleTest(&argc, argv);
-  google::ParseCommandLineFlags(&argc, &argv, true);
+  gflags::ParseCommandLineFlags(&argc, &argv, true);
   google::InitGoogleLogging(argv[0]);
   int ret = RUN_ALL_TESTS();
   if (ret == 0) {
@@ -181,4 +198,3 @@ int main(int argc, char *argv[]) {
   }
   return ret;
 }
-