2017
[folly.git] / folly / test / SpookyHashV1Test.cpp
index 2896c46135e4bf22bdddf0c741b81ef0a7c2b760..19ca545391f9c15f9301f990e01eb185853fe03e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 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.
 #define __STDC_FORMAT_MACROS 1
 #endif
 
-#include "folly/SpookyHashV1.h"
-#include "folly/Benchmark.h"
+#include <folly/SpookyHashV1.h>
+#include <folly/portability/GTest.h>
+#include <folly/portability/Time.h>
 
 #include <cinttypes>
 #include <cstdio>
 #include <cstddef>
 #include <cstring>
 #include <cstdlib>
-#include <ctime>
+
+#include <glog/logging.h>
 
 using namespace ::folly::hash;
 
 static bool failed = false;
 
-static uint64_t GetTickCount() {
+static uint64_t GetClockTickCount() {
   timespec ts;
   clock_gettime(CLOCK_REALTIME, &ts);
   return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;  // milliseconds
@@ -74,7 +76,8 @@ private:
 };
 
 // fastest conceivable hash function (for comparison)
-static void Add(const void *data, size_t length, uint64_t *hash1, uint64_t *hash2)
+static void Add(const void *data, size_t length,
+                uint64_t *hash1, uint64_t *hash2)
 {
     uint64_t *p64 = (uint64_t *)data;
     uint64_t *end = p64 + length/8;
@@ -270,41 +273,41 @@ void DoTimingBig(int seed)
         memset(buf[i], (char)seed, BUFSIZE);
     }
 
-    uint64_t a = GetTickCount();
+    uint64_t a = GetClockTickCount();
     uint64_t hash1 = seed;
     uint64_t hash2 = seed;
     for (uint64_t i=0; i<NUMBUF; ++i)
     {
         SpookyHashV1::Hash128(buf[i], BUFSIZE, &hash1, &hash2);
     }
-    uint64_t z = GetTickCount();
+    uint64_t z = GetClockTickCount();
     printf("SpookyHashV1::Hash128, uncached: time is "
            "%4" PRIu64 " milliseconds\n", z-a);
 
-    a = GetTickCount();
+    a = GetClockTickCount();
     for (uint64_t i=0; i<NUMBUF; ++i)
     {
         Add(buf[i], BUFSIZE, &hash1, &hash2);
     }
-    z = GetTickCount();
+    z = GetClockTickCount();
     printf("Addition           , uncached: time is "
            "%4" PRIu64 " milliseconds\n", z-a);
 
-    a = GetTickCount();
+    a = GetClockTickCount();
     for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
     {
         SpookyHashV1::Hash128(buf[0], 1024, &hash1, &hash2);
     }
-    z = GetTickCount();
+    z = GetClockTickCount();
     printf("SpookyHashV1::Hash128,   cached: time is "
            "%4" PRIu64 " milliseconds\n", z-a);
 
-    a = GetTickCount();
+    a = GetClockTickCount();
     for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
     {
         Add(buf[0], 1024, &hash1, &hash2);
     }
-    z = GetTickCount();
+    z = GetClockTickCount();
     printf("Addition           ,   cached: time is "
            "%4" PRIu64 " milliseconds\n", z-a);
 
@@ -333,14 +336,14 @@ void DoTimingSmall(int seed)
 
     for (int i=1; i <= BUFSIZE; i <<= 1)
     {
-        uint64_t a = GetTickCount();
+        uint64_t a = GetClockTickCount();
         uint64_t hash1 = seed;
         uint64_t hash2 = seed+i;
         for (int j=0; j<NUMITER; ++j)
         {
             SpookyHashV1::Hash128((char *)buf, i, &hash1, &hash2);
         }
-        uint64_t z = GetTickCount();
+        uint64_t z = GetClockTickCount();
         printf("%d bytes: hash is %.16" PRIx64 " %.16" PRIx64 ", "
                "time is %" PRIu64 "\n", i, hash1, hash2, z-a);
     }
@@ -426,8 +429,10 @@ void TestDeltas(int seed)
                     {
                         buf1[j/8] ^= (1 << (j%8));
                     }
-                    SpookyHashV1::Hash128(buf1, h, &measure[0][0], &measure[0][1]);
-                    SpookyHashV1::Hash128(buf2, h, &measure[1][0], &measure[1][1]);
+                    SpookyHashV1::Hash128(buf1, h,
+                            &measure[0][0], &measure[0][1]);
+                    SpookyHashV1::Hash128(buf2, h,
+                            &measure[1][0], &measure[1][1]);
                     for (int l=0; l<2; ++l) {
                         measure[2][l] = measure[0][l] ^ measure[1][l];
                         measure[3][l] = ~(measure[0][l] ^ measure[1][l]);
@@ -529,16 +534,15 @@ void TestPieces()
 }
 #undef BUFSIZE
 
-int main(int argc, const char **argv)
-{
+TEST(SpookyHashV1, Main) {
     TestResults();
     TestAlignment();
     TestPieces();
-    DoTimingBig(argc);
+    DoTimingBig(1);
     // tudorb@fb.com: Commented out slow tests
 #if 0
     DoTimingSmall(argc);
     TestDeltas(argc);
 #endif
-    return failed;
+    CHECK_EQ(failed, 0);
 }