proxygen
HashTest.cpp File Reference
#include <folly/hash/Hash.h>
#include <stdint.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <folly/MapUtil.h>
#include <folly/Range.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

struct  std::hash< TestEnum >
 
struct  std::hash< TestStruct >
 
class  TestHasher
 
struct  FNVTestParam
 
class  FNVTest
 

Enumerations

enum  TestEnum
 
enum  TestBigEnum : uint64_t
 

Functions

 TEST (Hash, Fnv32)
 
 TEST (Hash, Fnv64)
 
 TEST (Hash, Hsieh32)
 
 TEST (Hash, TWang_Mix64)
 
 TEST (Hash, TWang_Unmix64)
 
 TEST (Hash, TWang_32From64)
 
 TEST (Hash, Jenkins_Rev_Mix32)
 
 TEST (Hash, Jenkins_Rev_Unmix32)
 
 TEST (Hash, hasher)
 
 TEST (Hash, integral_types)
 
 TEST (Hash, noCachedHashCode)
 
 TEST (Hash, integer_conversion)
 
 TEST (Hash, float_types)
 
template<typename T , typename... Ts>
size_t hash_combine_test (const T &t, const Ts &...ts)
 
 TEST (Hash, pair)
 
 TEST (Hash, hash_combine)
 
 TEST (Hash, hash_bool)
 
 TEST (Hash, hash_bool10)
 
 TEST (Hash, std_tuple)
 
 TEST (Hash, enum_type)
 
 TEST (Hash, pair_folly_hash)
 
 TEST (Hash, tuple_folly_hash)
 
 TEST (Hash, hash_range)
 
 TEST (Hash, commutative_hash_combine)
 
 TEST (Hash, std_tuple_different_hash)
 
 TEST (Hash, Strings)
 
 TEST_P (FNVTest, Fnva64Buf)
 
 TEST_P (FNVTest, Fnva64)
 
 TEST_P (FNVTest, Fnva64Partial)
 
 INSTANTIATE_TEST_CASE_P (FNVTesting, FNVTest,::testing::Values(FNVTestParam{"foobar", 0x85944171f73967e8}, FNVTestParam{"chongo was here!\n", 0x46810940eff5f915}, FNVTestParam{"127.0.0.3", 0xaabafc7104d91158}, FNVTestParam{"http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash", 0xd9b957fb7fe794c5}, FNVTestParam{"http://norvig.com/21-days.html", 0x07aaa640476e0b9a}))
 
 TEST (Traits, stdHashPairAvalances)
 
 TEST (Traits, stdHashTuple2Avalances)
 
 TEST (Traits, stdHashStringAvalances)
 
 TEST (Traits, follyHashUint64Avalances)
 
 TEST (Traits, follyHasherInt64Avalances)
 
 TEST (Traits, follyHasherFloatAvalanches)
 
 TEST (Traits, follyHasherDoubleAvalanches)
 

Variables

static constexpr bool k32Bit = sizeof(std::size_t) == 4
 

Enumeration Type Documentation

enum TestBigEnum : uint64_t
strong

Definition at line 234 of file HashTest.cpp.

234  : uint64_t {
235  ITEM = 1,
236 };
enum TestEnum
strong

Definition at line 228 of file HashTest.cpp.

228  {
229  MIN = 0,
230  ITEM = 1,
231  MAX = 2,
232 };
#define MIN(a, b)
Definition: http_parser.c:46

Function Documentation

template<typename T , typename... Ts>
size_t hash_combine_test ( const T t,
const Ts &...  ts 
)

Definition at line 419 of file HashTest.cpp.

References folly::hash::hash_combine_generic(), and folly::pushmi::detail::t.

Referenced by TEST().

419  {
420  return hash_combine_generic(TestHasher{}, t, ts...);
421 }
size_t hash_combine_generic(const Hasher &) noexcept
Definition: Hash.h:634
INSTANTIATE_TEST_CASE_P ( FNVTesting  ,
FNVTest  ,
::testing::Values(FNVTestParam{"foobar", 0x85944171f73967e8}, FNVTestParam{"chongo was here!\n", 0x46810940eff5f915}, FNVTestParam{"127.0.0.3", 0xaabafc7104d91158}, FNVTestParam{"http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash", 0xd9b957fb7fe794c5}, FNVTestParam{"http://norvig.com/21-days.html", 0x07aaa640476e0b9a})   
)

Referenced by TEST_P().

TEST ( Hash  ,
Fnv32   
)

Definition at line 32 of file HashTest.cpp.

References EXPECT_EQ, folly::hash::fnv32(), folly::hash::fnv32_buf(), uint32_t, and uint8_t.

32  {
33  const char* s1 = "hello, world!";
34  const uint32_t s1_res = 3605494790UL;
35  EXPECT_EQ(fnv32(s1), s1_res);
36  EXPECT_EQ(fnv32(s1), fnv32_buf(s1, strlen(s1)));
37 
38  const char* s2 = "monkeys! m0nk3yz! ev3ry \\/\\/here~~~~";
39  const uint32_t s2_res = 1270448334UL;
40  EXPECT_EQ(fnv32(s2), s2_res);
41  EXPECT_EQ(fnv32(s2), fnv32_buf(s2, strlen(s2)));
42 
43  const char* s3 = "";
44  const uint32_t s3_res = 2166136261UL;
45  EXPECT_EQ(fnv32(s3), s3_res);
46  EXPECT_EQ(fnv32(s3), fnv32_buf(s3, strlen(s3)));
47 
48  const uint8_t s4_data[] = {0xFF, 0xFF, 0xFF, 0x00};
49  const char* s4 = reinterpret_cast<const char*>(s4_data);
50  const uint32_t s4_res = 2420936562UL;
51  EXPECT_EQ(fnv32(s4), s4_res);
52  EXPECT_EQ(fnv32(s4), fnv32_buf(s4, strlen(s4)));
53 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint32_t fnv32_buf(const void *buf, size_t n, uint32_t hash=FNV_32_HASH_START) noexcept
Definition: Hash.h:163
uint32_t fnv32(const char *buf, uint32_t hash=FNV_32_HASH_START) noexcept
Definition: Hash.h:149
TEST ( Hash  ,
Fnv64   
)

Definition at line 55 of file HashTest.cpp.

References EXPECT_EQ, EXPECT_NE, folly::hash::fnv64(), folly::hash::fnv64_buf(), int32_t, int64_t, uint64_t, and uint8_t.

55  {
56  const char* s1 = "hello, world!";
57  const uint64_t s1_res = 13991426986746681734ULL;
58  EXPECT_EQ(fnv64(s1), s1_res);
59  EXPECT_EQ(fnv64(s1), fnv64_buf(s1, strlen(s1)));
60 
61  const char* s2 = "monkeys! m0nk3yz! ev3ry \\/\\/here~~~~";
62  const uint64_t s2_res = 6091394665637302478ULL;
63  EXPECT_EQ(fnv64(s2), s2_res);
64  EXPECT_EQ(fnv64(s2), fnv64_buf(s2, strlen(s2)));
65 
66  const char* s3 = "";
67  const uint64_t s3_res = 14695981039346656037ULL;
68  EXPECT_EQ(fnv64(s3), s3_res);
69  EXPECT_EQ(fnv64(s3), fnv64_buf(s3, strlen(s3)));
70 
71  const uint8_t s4_data[] = {0xFF, 0xFF, 0xFF, 0x00};
72  const char* s4 = reinterpret_cast<const char*>(s4_data);
73  const uint64_t s4_res = 2787597222566293202ULL;
74  EXPECT_EQ(fnv64(s4), s4_res);
75  EXPECT_EQ(fnv64(s4), fnv64_buf(s4, strlen(s4)));
76 
77  // note: Use fnv64_buf to make a single hash value from multiple
78  // fields/datatypes.
79  const char* t4_a = "E Pluribus";
80  int64_t t4_b = 0xF1E2D3C4B5A69788;
81  int32_t t4_c = 0xAB12CD34;
82  const char* t4_d = "Unum";
83  uint64_t t4_res = 15571330457339273965ULL;
84  uint64_t t4_hash1 = fnv64_buf(t4_a, strlen(t4_a));
85  uint64_t t4_hash2 =
86  fnv64_buf(reinterpret_cast<void*>(&t4_b), sizeof(int64_t), t4_hash1);
87  uint64_t t4_hash3 =
88  fnv64_buf(reinterpret_cast<void*>(&t4_c), sizeof(int32_t), t4_hash2);
89  uint64_t t4_hash4 = fnv64_buf(t4_d, strlen(t4_d), t4_hash3);
90  EXPECT_EQ(t4_hash4, t4_res);
91  // note: These are probabalistic, not determinate, but c'mon.
92  // These hash values should be different, or something's not
93  // working.
94  EXPECT_NE(t4_hash1, t4_hash4);
95  EXPECT_NE(t4_hash2, t4_hash4);
96  EXPECT_NE(t4_hash3, t4_hash4);
97 }
uint64_t fnv64_buf(const void *buf, size_t n, uint64_t hash=FNV_64_HASH_START) noexcept
Definition: Hash.h:199
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint64_t fnv64(const char *buf, uint64_t hash=FNV_64_HASH_START) noexcept
Definition: Hash.h:185
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Hash  ,
Hsieh32   
)

Definition at line 99 of file HashTest.cpp.

References EXPECT_EQ, folly::hash::hsieh_hash32(), folly::hash::hsieh_hash32_buf(), and uint32_t.

99  {
100  const char* s1 = "hello, world!";
101  const uint32_t s1_res = 2918802987ul;
102  EXPECT_EQ(hsieh_hash32(s1), s1_res);
103  EXPECT_EQ(hsieh_hash32(s1), hsieh_hash32_buf(s1, strlen(s1)));
104 
105  const char* s2 = "monkeys! m0nk3yz! ev3ry \\/\\/here~~~~";
106  const uint32_t s2_res = 47373213ul;
107  EXPECT_EQ(hsieh_hash32(s2), s2_res);
108  EXPECT_EQ(hsieh_hash32(s2), hsieh_hash32_buf(s2, strlen(s2)));
109 
110  const char* s3 = "";
111  const uint32_t s3_res = 0;
112  EXPECT_EQ(hsieh_hash32(s3), s3_res);
113  EXPECT_EQ(hsieh_hash32(s3), hsieh_hash32_buf(s3, strlen(s3)));
114 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::uint32_t hsieh_hash32_buf(const void *buf, std::size_t len)
uint32_t hsieh_hash32(const char *s) noexcept
Definition: Hash.h:301
TEST ( Hash  ,
TWang_Mix64   
)

Definition at line 116 of file HashTest.cpp.

References EXPECT_EQ, folly::hash::twang_mix64(), folly::hash::twang_unmix64(), and uint64_t.

116  {
117  uint64_t i1 = 0x78a87873e2d31dafULL;
118  uint64_t i1_res = 3389151152926383528ULL;
119  EXPECT_EQ(i1_res, twang_mix64(i1));
120  EXPECT_EQ(i1, twang_unmix64(i1_res));
121 
122  uint64_t i2 = 0x0123456789abcdefULL;
123  uint64_t i2_res = 3061460455458984563ull;
124  EXPECT_EQ(i2_res, twang_mix64(i2));
125  EXPECT_EQ(i2, twang_unmix64(i2_res));
126 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint64_t twang_mix64(uint64_t key) noexcept
Definition: Hash.h:49
uint64_t twang_unmix64(uint64_t key) noexcept
Definition: Hash.h:66
TEST ( Hash  ,
TWang_Unmix64   
)

Definition at line 135 of file HashTest.cpp.

References i, and uint64_t.

135  {
136  // We'll try (1 << i), (1 << i) + 1, (1 << i) - 1
137  for (int i = 1; i < 64; i++) {
138  checkTWang((uint64_t(1) << i) - 1);
139  checkTWang(uint64_t(1) << i);
140  checkTWang((uint64_t(1) << i) + 1);
141  }
142 }
TEST ( Hash  ,
TWang_32From64   
)

Definition at line 144 of file HashTest.cpp.

References EXPECT_EQ, folly::hash::twang_32from64(), uint32_t, and uint64_t.

144  {
145  uint64_t i1 = 0x78a87873e2d31dafULL;
146  uint32_t i1_res = 1525586863ul;
147  EXPECT_EQ(twang_32from64(i1), i1_res);
148 
149  uint64_t i2 = 0x0123456789abcdefULL;
150  uint32_t i2_res = 2918899159ul;
151  EXPECT_EQ(twang_32from64(i2), i2_res);
152 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint32_t twang_32from64(uint64_t key) noexcept
Definition: Hash.h:83
TEST ( Hash  ,
Jenkins_Rev_Mix32   
)

Definition at line 154 of file HashTest.cpp.

References EXPECT_EQ, folly::hash::jenkins_rev_mix32(), folly::hash::jenkins_rev_unmix32(), and uint32_t.

154  {
155  uint32_t i1 = 3805486511ul;
156  uint32_t i1_res = 381808021ul;
157  EXPECT_EQ(i1_res, jenkins_rev_mix32(i1));
158  EXPECT_EQ(i1, jenkins_rev_unmix32(i1_res));
159 
160  uint32_t i2 = 2309737967ul;
161  uint32_t i2_res = 1834777923ul;
162  EXPECT_EQ(i2_res, jenkins_rev_mix32(i2));
163  EXPECT_EQ(i2, jenkins_rev_unmix32(i2_res));
164 }
uint32_t jenkins_rev_unmix32(uint32_t key) noexcept
Definition: Hash.h:117
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint32_t jenkins_rev_mix32(uint32_t key) noexcept
Definition: Hash.h:97
TEST ( Hash  ,
Jenkins_Rev_Unmix32   
)

Definition at line 173 of file HashTest.cpp.

References i.

173  {
174  // We'll try (1 << i), (1 << i) + 1, (1 << i) - 1
175  for (int i = 1; i < 32; i++) {
176  checkJenkins((1U << i) - 1);
177  checkJenkins(1U << i);
178  checkJenkins((1U << i) + 1);
179  }
180 }
TEST ( Hash  ,
hasher   
)

Definition at line 182 of file HashTest.cpp.

References EXPECT_EQ, folly::get_default(), and m.

182  {
183  // Basically just confirms that things compile ok.
184  std::unordered_map<int32_t, int32_t, folly::hasher<int32_t>> m;
185  m.insert(std::make_pair(4, 5));
186  EXPECT_EQ(get_default(m, 4), 5);
187 }
Map::mapped_type get_default(const Map &map, const Key &key)
Definition: MapUtil.h:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static map< string, int > m
TEST ( Hash  ,
integral_types   
)

Definition at line 189 of file HashTest.cpp.

References EXPECT_EQ, int16_t, int32_t, int64_t, int8_t, uint16_t, uint32_t, uint64_t, and uint8_t.

189  {
190  // Basically just confirms that things compile ok.
191  std::unordered_set<size_t> hashes;
193  hashes.insert(hasher((char)1));
194  hashes.insert(hasher((signed char)2));
195  hashes.insert(hasher((unsigned char)3));
196  hashes.insert(hasher((short)4));
197  hashes.insert(hasher((signed short)5));
198  hashes.insert(hasher((unsigned short)6));
199  hashes.insert(hasher((int)7));
200  hashes.insert(hasher((signed int)8));
201  hashes.insert(hasher((unsigned int)9));
202  hashes.insert(hasher((long)10));
203  hashes.insert(hasher((signed long)11));
204  hashes.insert(hasher((unsigned long)12));
205  hashes.insert(hasher((long long)13));
206  hashes.insert(hasher((signed long long)14));
207  hashes.insert(hasher((unsigned long long)15));
208  hashes.insert(hasher((int8_t)16));
209  hashes.insert(hasher((uint8_t)17));
210  hashes.insert(hasher((int16_t)18));
211  hashes.insert(hasher((uint16_t)19));
212  hashes.insert(hasher((int32_t)20));
213  hashes.insert(hasher((uint32_t)21));
214  hashes.insert(hasher((int64_t)22));
215  hashes.insert(hasher((uint64_t)23));
216  hashes.insert(hasher((size_t)24));
217 
218  size_t setSize = 24;
219 #if FOLLY_HAVE_INT128_T
220  hashes.insert(hasher((__int128_t)25));
221  hashes.insert(hasher((__uint128_t)26));
222  setSize += 2;
223 #endif
224  EXPECT_EQ(setSize, hashes.size());
225 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Hash  ,
noCachedHashCode   
)

Definition at line 360 of file HashTest.cpp.

360  {
361  testNoCachedHashCode<bool, char, folly::hasher<bool>>();
362  testNoCachedHashCode<int, char, folly::hasher<int>>();
363  testNoCachedHashCode<double, char, folly::hasher<double>>();
364  testNoCachedHashCode<TestEnum, char, folly::hasher<TestEnum>>();
365 
366  testNoCachedHashCode<bool, std::string, folly::Hash>();
367  testNoCachedHashCode<int, std::string, folly::Hash>();
368  testNoCachedHashCode<double, std::string, folly::Hash>();
369  testNoCachedHashCode<TestEnum, std::string, folly::Hash>();
370 }
TEST ( Hash  ,
integer_conversion   
)

Definition at line 372 of file HashTest.cpp.

References EXPECT_EQ, h, k, s, folly::TEST(), and uint64_t.

372  {
374  uint64_t k = 10;
375  EXPECT_EQ(h(k), h(10));
376 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
KeyT k
TEST ( Hash  ,
float_types   
)

Definition at line 392 of file HashTest.cpp.

References EXPECT_EQ, and f.

392  {
394 
395  EXPECT_EQ(hasher(0.0f), hasher(-0.0f));
396  EXPECT_EQ(hasher(0.0), hasher(-0.0));
397 
398  // Basically just confirms that things compile ok.
399  std::unordered_set<size_t> hashes;
400  hashes.insert(hasher(0.0f));
401  hashes.insert(hasher(0.1f));
402  hashes.insert(hasher(0.2));
403  hashes.insert(hasher(0.2f));
404  hashes.insert(hasher(-0.3));
405  hashes.insert(hasher(-0.3f));
406 
407  EXPECT_EQ(6, hashes.size());
408 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Hash  ,
pair   
)

Definition at line 423 of file HashTest.cpp.

References a, b, c, EXPECT_EQ, EXPECT_NE, folly::hash::hash_combine(), and hash_combine_test().

423  {
424  auto a = std::make_pair(1, 2);
425  auto b = std::make_pair(3, 4);
426  auto c = std::make_pair(1, 2);
427  auto d = std::make_pair(2, 1);
431 
432  // With composition
434  // Test order dependence
436 
437  // Test with custom hasher
439  // 3 + 4 != 1 + 2
441  // This time, thanks to a terrible hash function, these are equal
443  // With composition
445  // Test order dependence
447  // Again, 1 + 2 == 2 + 1
449 }
size_t hash_combine_test(const T &t, const Ts &...ts)
Definition: HashTest.cpp:419
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t hash_combine(const T &t, const Ts &...ts) noexcept(noexcept(hash_combine_generic(StdHasher{}, t, ts...)))
Definition: Hash.h:669
char a
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
char c
TEST ( Hash  ,
hash_combine   
)

Definition at line 451 of file HashTest.cpp.

References EXPECT_NE, EXPECT_TRUE, folly::hash::hash_combine(), and folly::pushmi::__adl::noexcept().

451  {
453  EXPECT_NE(hash_combine(1, 2), hash_combine(2, 1));
454 }
requires E e noexcept(noexcept(s.error(std::move(e))))
size_t hash_combine(const T &t, const Ts &...ts) noexcept(noexcept(hash_combine_generic(StdHasher{}, t, ts...)))
Definition: Hash.h:669
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Hash  ,
hash_bool   
)

Definition at line 456 of file HashTest.cpp.

References EXPECT_NE.

456  {
457  const auto hash = folly::Hash();
458  EXPECT_NE(hash(true), hash(false));
459 }
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Hash  ,
hash_bool10   
)

Definition at line 461 of file HashTest.cpp.

References EXPECT_EQ, and values().

461  {
462  const auto hash = folly::Hash();
463  std::set<size_t> values;
464  for (bool b1 : {false, true}) {
465  for (bool b2 : {false, true}) {
466  for (bool b3 : {false, true}) {
467  for (bool b4 : {false, true}) {
468  for (bool b5 : {false, true}) {
469  for (bool b6 : {false, true}) {
470  for (bool b7 : {false, true}) {
471  for (bool b8 : {false, true}) {
472  for (bool b9 : {false, true}) {
473  for (bool b10 : {false, true}) {
474  values.insert(
475  hash(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10));
476  }
477  }
478  }
479  }
480  }
481  }
482  }
483  }
484  }
485  }
486  EXPECT_EQ(values.size(), 1 << 10);
487 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::vector< int > values(1'000)
TEST ( Hash  ,
std_tuple   
)

Definition at line 489 of file HashTest.cpp.

References EXPECT_EQ, m, and folly::pushmi::detail::t.

489  {
490  typedef std::tuple<int64_t, std::string, int32_t> tuple3;
491  tuple3 t(42, "foo", 1);
492 
493  std::unordered_map<tuple3, std::string> m;
494  m[t] = "bar";
495  EXPECT_EQ("bar", m[t]);
496 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static map< string, int > m
TEST ( Hash  ,
enum_type   
)

Definition at line 498 of file HashTest.cpp.

References EXPECT_EQ, EXPECT_NE, int32_t, and int64_t.

498  {
499  const auto hash = folly::Hash();
500 
501  enum class Enum32 : int32_t { Foo, Bar };
502  EXPECT_EQ(hash(static_cast<int32_t>(Enum32::Foo)), hash(Enum32::Foo));
503  EXPECT_EQ(hash(static_cast<int32_t>(Enum32::Bar)), hash(Enum32::Bar));
504  EXPECT_NE(hash(Enum32::Foo), hash(Enum32::Bar));
505 
506  std::unordered_map<Enum32, std::string, folly::Hash> m32;
507  m32[Enum32::Foo] = "foo";
508  EXPECT_EQ("foo", m32[Enum32::Foo]);
509 
510  enum class Enum64 : int64_t { Foo, Bar };
511  EXPECT_EQ(hash(static_cast<int64_t>(Enum64::Foo)), hash(Enum64::Foo));
512  EXPECT_EQ(hash(static_cast<int64_t>(Enum64::Bar)), hash(Enum64::Bar));
513  EXPECT_NE(hash(Enum64::Foo), hash(Enum64::Bar));
514 
515  std::unordered_map<Enum64, std::string, folly::Hash> m64;
516  m64[Enum64::Foo] = "foo";
517  EXPECT_EQ("foo", m64[Enum64::Foo]);
518 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Hash  ,
pair_folly_hash   
)

Definition at line 520 of file HashTest.cpp.

References EXPECT_EQ, and m.

520  {
521  typedef std::pair<int64_t, int32_t> pair2;
522  pair2 p(42, 1);
523 
524  std::unordered_map<pair2, std::string, folly::Hash> m;
525  m[p] = "bar";
526  EXPECT_EQ("bar", m[p]);
527 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static map< string, int > m
TEST ( Hash  ,
tuple_folly_hash   
)

Definition at line 529 of file HashTest.cpp.

References EXPECT_EQ, folly::hash::hash_range(), m, and folly::pushmi::detail::t.

529  {
530  typedef std::tuple<int64_t, int32_t, int32_t> tuple3;
531  tuple3 t(42, 1, 1);
532 
533  std::unordered_map<tuple3, std::string, folly::Hash> m;
534  m[t] = "bar";
535  EXPECT_EQ("bar", m[t]);
536 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static map< string, int > m
TEST ( Hash  ,
hash_range   
)

Definition at line 545 of file HashTest.cpp.

References EXPECT_EQ, and EXPECT_NE.

545  {
546  EXPECT_EQ(hash_vector<int32_t>({1, 2}), hash_vector<int16_t>({1, 2}));
547  EXPECT_NE(hash_vector<int>({2, 1}), hash_vector<int>({1, 2}));
548  EXPECT_EQ(hash_vector<int>({}), hash_vector<float>({}));
549 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Hash  ,
commutative_hash_combine   
)

Definition at line 551 of file HashTest.cpp.

References folly::hash::commutative_hash_combine(), folly::hash::commutative_hash_combine_range(), folly::hash::commutative_hash_combine_range_generic(), folly::hash::commutative_hash_combine_value_generic(), EXPECT_EQ, EXPECT_NE, g(), h, i, and rd.

551  {
552  EXPECT_EQ(
554  folly::Hash{}(12345ul), folly::Hash{}, 6789ul),
556  folly::Hash{}(6789ul), folly::Hash{}, 12345ul));
557 
558  std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
559  std::random_device rd;
560  std::mt19937 g(rd());
561  auto h = commutative_hash_combine_range(v.begin(), v.end());
562  for (int i = 0; i < 100; i++) {
563  std::shuffle(v.begin(), v.end(), g);
564  EXPECT_EQ(h, commutative_hash_combine_range(v.begin(), v.end()));
565  }
566  EXPECT_NE(
567  h,
569  /* seed = */ 0xdeadbeef, folly::Hash{}, v.begin(), v.end()));
570  EXPECT_NE(
571  h, commutative_hash_combine_range(v.begin(), v.begin() + (v.size() - 1)));
572 
573  EXPECT_EQ(h, commutative_hash_combine(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
574  EXPECT_EQ(h, commutative_hash_combine(10, 2, 3, 4, 5, 6, 7, 8, 9, 1));
575 
576  EXPECT_EQ(
577  commutative_hash_combine(12345, 6789),
578  commutative_hash_combine(6789, 12345));
579 }
*than *hazptr_holder h
Definition: Hazptr.h:116
uint64_t commutative_hash_combine_range(Iter first, Iter last)
Definition: Hash.h:624
uint64_t commutative_hash_combine_range_generic(uint64_t seed, Hash const &hasher, Iter first, Iter last)
Definition: Hash.h:612
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint64_t commutative_hash_combine_value_generic(uint64_t seed, Hash const &hasher, Value const &value)
Definition: Hash.h:584
std::random_device rd
g_t g(f_t)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
uint64_t commutative_hash_combine(Value const &...value)
Definition: Hash.h:675
TEST ( Hash  ,
std_tuple_different_hash   
)

Definition at line 581 of file HashTest.cpp.

References EXPECT_NE.

581  {
582  typedef std::tuple<int64_t, std::string, int32_t> tuple3;
583  tuple3 t1(42, "foo", 1);
584  tuple3 t2(9, "bar", 3);
585  tuple3 t3(42, "foo", 3);
586 
587  EXPECT_NE(std::hash<tuple3>()(t1), std::hash<tuple3>()(t2));
588  EXPECT_NE(std::hash<tuple3>()(t1), std::hash<tuple3>()(t3));
589 }
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Hash  ,
Strings   
)

Definition at line 591 of file HashTest.cpp.

References EXPECT_EQ, EXPECT_NE, folly::range(), and folly::Range< Iter >::str().

591  {
592  using namespace folly;
593 
594  StringPiece a1 = "10050517", b1 = "51107032", a2 = "10050518",
595  b2 = "51107033", a3 = "10050519", b3 = "51107034",
596  a4 = "10050525", b4 = "51107040";
597  Range<const wchar_t*> w1 = range(L"10050517"), w2 = range(L"51107032"),
598  w3 = range(L"10050518"), w4 = range(L"51107033");
599  Hash h2;
600  EXPECT_NE(h2(a1), h2(b1));
601  EXPECT_NE(h2(a1), h2(b1));
602  EXPECT_NE(h2(a2), h2(b2));
603  EXPECT_NE(h2(a3), h2(b3));
604  EXPECT_NE(h2(ByteRange(a1)), h2(ByteRange(b1)));
605  EXPECT_NE(h2(ByteRange(a2)), h2(ByteRange(b2)));
606  EXPECT_NE(h2(ByteRange(a3)), h2(ByteRange(b3)));
607  EXPECT_NE(h2(ByteRange(a4)), h2(ByteRange(b4)));
608  EXPECT_NE(h2(w1), h2(w2));
609  EXPECT_NE(h2(w1), h2(w3));
610  EXPECT_NE(h2(w2), h2(w4));
611 
612  // Check compatibility with std::string.
613  EXPECT_EQ(h2(a1), h2(a1.str()));
614  EXPECT_EQ(h2(a2), h2(a2.str()));
615  EXPECT_EQ(h2(a3), h2(a3.str()));
616  EXPECT_EQ(h2(a4), h2(a4.str()));
617 }
std::string str() const
Definition: Range.h:591
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( Traits  ,
stdHashPairAvalances   
)

Definition at line 899 of file HashTest.cpp.

References v.

899  {
900  verifyAvalanching<std::hash<std::pair<int, int>>>(
901  std::make_pair(0, 0), [](std::pair<int, int>& v) { v.first++; });
902 }
auto v
TEST ( Traits  ,
stdHashTuple2Avalances   
)

Definition at line 904 of file HashTest.cpp.

References std::tr1::make_tuple(), and v.

904  {
905  verifyAvalanching<std::hash<std::tuple<int, int>>>(
906  std::make_tuple(0, 0),
907  [](std::tuple<int, int>& v) { std::get<0>(v) += 1; });
908 }
auto v
tuple make_tuple()
Definition: gtest-tuple.h:675
TEST ( Traits  ,
stdHashStringAvalances   
)

Definition at line 910 of file HashTest.cpp.

References i, and string.

910  {
911  verifyAvalanching<std::hash<std::string>, std::string>(
912  "00000000000000000000000000000", [](std::string& str) {
913  std::size_t i = 0;
914  while (str[i] == '1') {
915  str[i] = '0';
916  ++i;
917  }
918  str[i] = '1';
919  });
920 }
const char * string
Definition: Conv.cpp:212
TEST ( Traits  ,
follyHashUint64Avalances   
)

Definition at line 922 of file HashTest.cpp.

References uint64_t, and v.

922  {
923  verifyAvalanching<folly::Hash>(uint64_t{0}, [](uint64_t& v) { v++; });
924 }
auto v
TEST ( Traits  ,
follyHasherInt64Avalances   
)

Definition at line 926 of file HashTest.cpp.

References int64_t, and v.

926  {
927  verifyAvalanching<folly::hasher<int64_t>>(
928  int64_t{0}, [](int64_t& v) { v++; });
929 }
auto v
TEST ( Traits  ,
follyHasherFloatAvalanches   
)

Definition at line 931 of file HashTest.cpp.

References v.

931  {
932  verifyAvalanching<folly::hasher<float>>(0.0f, [](float& v) { v += 1; });
933 }
auto v
TEST ( Traits  ,
follyHasherDoubleAvalanches   
)

Definition at line 935 of file HashTest.cpp.

References v.

935  {
936  verifyAvalanching<folly::hasher<double>>(0.0, [](double& v) { v += 1; });
937 }
auto v
TEST_P ( FNVTest  ,
Fnva64Buf   
)

Definition at line 626 of file HashTest.cpp.

References EXPECT_EQ, and folly::hash::fnva64_buf().

626  {
627  EXPECT_EQ(
628  GetParam().out, fnva64_buf(GetParam().in.data(), GetParam().in.size()));
629 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint64_t fnva64_buf(const void *buf, size_t n, uint64_t hash=FNVA_64_HASH_START) noexcept
Definition: Hash.h:220
TEST_P ( FNVTest  ,
Fnva64   
)

Definition at line 631 of file HashTest.cpp.

References EXPECT_EQ, and folly::hash::fnva64().

631  {
632  EXPECT_EQ(GetParam().out, fnva64(GetParam().in));
633 }
uint64_t fnva64(const std::string &str, uint64_t hash=FNVA_64_HASH_START) noexcept
Definition: Hash.h:234
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_P ( FNVTest  ,
Fnva64Partial   
)

Definition at line 635 of file HashTest.cpp.

References folly::data(), EXPECT_EQ, folly::hash::fnva64_buf(), INSTANTIATE_TEST_CASE_P(), and folly::partial().

635  {
636  size_t partialLen = GetParam().in.size() / 2;
637  auto data = GetParam().in.data();
638  auto partial = fnva64_buf(data, partialLen);
639  EXPECT_EQ(
640  GetParam().out,
641  fnva64_buf(
642  data + partialLen, GetParam().in.size() - partialLen, partial));
643 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto partial(F &&f, Args &&...args) -> detail::partial::Partial< typename std::decay< F >::type, std::tuple< typename std::decay< Args >::type... >>
Definition: Partial.h:119
uint64_t fnva64_buf(const void *buf, size_t n, uint64_t hash=FNVA_64_HASH_START) noexcept
Definition: Hash.h:220
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43

Variable Documentation

constexpr bool k32Bit = sizeof(std::size_t) == 4
static

Definition at line 663 of file HashTest.cpp.