/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
// SpookyHash: a 128-bit noncryptographic hash function
// By Bob Jenkins, public domain
-#include "folly/SpookyHashV1.h"
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS 1
+#endif
+
+#include <folly/SpookyHashV1.h>
+#include <folly/Benchmark.h>
+
+#include <cinttypes>
#include <cstdio>
#include <cstddef>
#include <cstring>
#include <cstdlib>
#include <ctime>
+#include <gtest/gtest.h>
+
using namespace ::folly::hash;
static bool failed = false;
};
// 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;
SpookyHashV1::Hash128(buf[i], BUFSIZE, &hash1, &hash2);
}
uint64_t z = GetTickCount();
- printf("SpookyHashV1::Hash128, uncached: time is %4lu milliseconds\n", z-a);
+ printf("SpookyHashV1::Hash128, uncached: time is "
+ "%4" PRIu64 " milliseconds\n", z-a);
a = GetTickCount();
for (uint64_t i=0; i<NUMBUF; ++i)
Add(buf[i], BUFSIZE, &hash1, &hash2);
}
z = GetTickCount();
- printf("Addition , uncached: time is %4lu milliseconds\n", z-a);
+ printf("Addition , uncached: time is "
+ "%4" PRIu64 " milliseconds\n", z-a);
a = GetTickCount();
for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
SpookyHashV1::Hash128(buf[0], 1024, &hash1, &hash2);
}
z = GetTickCount();
- printf("SpookyHashV1::Hash128, cached: time is %4lu milliseconds\n", z-a);
+ printf("SpookyHashV1::Hash128, cached: time is "
+ "%4" PRIu64 " milliseconds\n", z-a);
a = GetTickCount();
for (uint64_t i=0; i<NUMBUF*BUFSIZE/1024; ++i)
Add(buf[0], 1024, &hash1, &hash2);
}
z = GetTickCount();
- printf("Addition , cached: time is %4lu milliseconds\n", z-a);
+ printf("Addition , cached: time is "
+ "%4" PRIu64 " milliseconds\n", z-a);
for (int i=0; i<NUMBUF; ++i)
{
#define NUMITER 10000000
void DoTimingSmall(int seed)
{
- printf("\ntesting timing of hashing up to %d cached aligned bytes %d times ...\n",
- BUFSIZE, NUMITER);
+ printf("\ntesting timing of hashing up to %d cached aligned bytes %d "
+ "times ...\n", BUFSIZE, NUMITER);
uint64_t buf[BUFSIZE/8];
for (int i=0; i<BUFSIZE/8; ++i)
SpookyHashV1::Hash128((char *)buf, i, &hash1, &hash2);
}
uint64_t z = GetTickCount();
- printf("%d bytes: hash is %.16lx %.16lx, time is %lu\n",
- i, hash1, hash2, z-a);
+ printf("%d bytes: hash is %.16" PRIx64 " %.16" PRIx64 ", "
+ "time is %" PRIu64 "\n", i, hash1, hash2, z-a);
}
}
#undef BUFSIZE
#define MEASURES 6
void TestDeltas(int seed)
{
- printf("\nall 1 or 2 bit input deltas get %d tries to flip every output bit ...\n", TRIES);
+ printf("\nall 1 or 2 bit input deltas get %d tries to flip every output "
+ "bit ...\n", TRIES);
Random random;
random.Init((uint64_t)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]);
if (a != c)
{
- printf("wrong a %d: %.16lx %.16lx\n", i, a,c);
+ printf("wrong a %d: %.16" PRIx64 " %.16" PRIx64 "\n", i, a,c);
failed = true;
}
if (b != d)
{
- printf("wrong b %d: %.16lx %.16lx\n", i, b,d);
+ printf("wrong b %d: %.16" PRIx64 " %.16" PRIx64 "\n", i, b,d);
failed = true;
}
state.Final(&c, &d);
if (a != c)
{
- printf("wrong a %d %d: %.16lx %.16lx\n", j, i, a,c);
+ printf("wrong a %d %d: %.16" PRIx64 " %.16" PRIx64 "\n",
+ j, i, a,c);
failed = true;
}
if (b != d)
{
- printf("wrong b %d %d: %.16lx %.16lx\n", j, i, b,d);
+ printf("wrong b %d %d: %.16" PRIx64 " %.16" PRIx64 "\n",
+ j, i, b,d);
failed = true;
}
}
}
#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);
}