xref: /aosp_15_r20/external/webrtc/third_party/abseil-cpp/absl/hash/hash_test.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/hash/hash.h"
16 
17 #include <algorithm>
18 #include <array>
19 #include <bitset>
20 #include <cstring>
21 #include <deque>
22 #include <forward_list>
23 #include <functional>
24 #include <initializer_list>
25 #include <iterator>
26 #include <limits>
27 #include <list>
28 #include <map>
29 #include <memory>
30 #include <numeric>
31 #include <random>
32 #include <set>
33 #include <string>
34 #include <tuple>
35 #include <type_traits>
36 #include <unordered_map>
37 #include <unordered_set>
38 #include <utility>
39 #include <vector>
40 
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
43 #include "absl/container/btree_map.h"
44 #include "absl/container/btree_set.h"
45 #include "absl/container/flat_hash_map.h"
46 #include "absl/container/flat_hash_set.h"
47 #include "absl/container/node_hash_map.h"
48 #include "absl/container/node_hash_set.h"
49 #include "absl/hash/hash_testing.h"
50 #include "absl/hash/internal/spy_hash_state.h"
51 #include "absl/meta/type_traits.h"
52 #include "absl/numeric/int128.h"
53 #include "absl/strings/cord_test_helpers.h"
54 
55 namespace {
56 
57 // Utility wrapper of T for the purposes of testing the `AbslHash` type erasure
58 // mechanism.  `TypeErasedValue<T>` can be constructed with a `T`, and can
59 // be compared and hashed.  However, all hashing goes through the hashing
60 // type-erasure framework.
61 template <typename T>
62 class TypeErasedValue {
63  public:
64   TypeErasedValue() = default;
65   TypeErasedValue(const TypeErasedValue&) = default;
66   TypeErasedValue(TypeErasedValue&&) = default;
TypeErasedValue(const T & n)67   explicit TypeErasedValue(const T& n) : n_(n) {}
68 
69   template <typename H>
AbslHashValue(H hash_state,const TypeErasedValue & v)70   friend H AbslHashValue(H hash_state, const TypeErasedValue& v) {
71     v.HashValue(absl::HashState::Create(&hash_state));
72     return hash_state;
73   }
74 
HashValue(absl::HashState state) const75   void HashValue(absl::HashState state) const {
76     absl::HashState::combine(std::move(state), n_);
77   }
78 
operator ==(const TypeErasedValue & rhs) const79   bool operator==(const TypeErasedValue& rhs) const { return n_ == rhs.n_; }
operator !=(const TypeErasedValue & rhs) const80   bool operator!=(const TypeErasedValue& rhs) const { return !(*this == rhs); }
81 
82  private:
83   T n_;
84 };
85 
86 // A TypeErasedValue refinement, for containers.  It exposes the wrapped
87 // `value_type` and is constructible from an initializer list.
88 template <typename T>
89 class TypeErasedContainer : public TypeErasedValue<T> {
90  public:
91   using value_type = typename T::value_type;
92   TypeErasedContainer() = default;
93   TypeErasedContainer(const TypeErasedContainer&) = default;
94   TypeErasedContainer(TypeErasedContainer&&) = default;
TypeErasedContainer(const T & n)95   explicit TypeErasedContainer(const T& n) : TypeErasedValue<T>(n) {}
TypeErasedContainer(std::initializer_list<value_type> init_list)96   TypeErasedContainer(std::initializer_list<value_type> init_list)
97       : TypeErasedContainer(T(init_list.begin(), init_list.end())) {}
98   // one-argument constructor of value type T, to appease older toolchains that
99   // get confused by one-element initializer lists in some contexts
TypeErasedContainer(const value_type & v)100   explicit TypeErasedContainer(const value_type& v)
101       : TypeErasedContainer(T(&v, &v + 1)) {}
102 };
103 
104 template <typename T>
105 using TypeErasedVector = TypeErasedContainer<std::vector<T>>;
106 
107 using absl::Hash;
108 using absl::hash_internal::SpyHashState;
109 
110 template <typename T>
111 class HashValueIntTest : public testing::Test {
112 };
113 TYPED_TEST_SUITE_P(HashValueIntTest);
114 
115 template <typename T>
SpyHash(const T & value)116 SpyHashState SpyHash(const T& value) {
117   return SpyHashState::combine(SpyHashState(), value);
118 }
119 
120 // Helper trait to verify if T is hashable. We use absl::Hash's poison status to
121 // detect it.
122 template <typename T>
123 using is_hashable = std::is_default_constructible<absl::Hash<T>>;
124 
TYPED_TEST_P(HashValueIntTest,BasicUsage)125 TYPED_TEST_P(HashValueIntTest, BasicUsage) {
126   EXPECT_TRUE((is_hashable<TypeParam>::value));
127 
128   TypeParam n = 42;
129   EXPECT_EQ(SpyHash(n), SpyHash(TypeParam{42}));
130   EXPECT_NE(SpyHash(n), SpyHash(TypeParam{0}));
131   EXPECT_NE(SpyHash(std::numeric_limits<TypeParam>::max()),
132             SpyHash(std::numeric_limits<TypeParam>::min()));
133 }
134 
TYPED_TEST_P(HashValueIntTest,FastPath)135 TYPED_TEST_P(HashValueIntTest, FastPath) {
136   // Test the fast-path to make sure the values are the same.
137   TypeParam n = 42;
138   EXPECT_EQ(absl::Hash<TypeParam>{}(n),
139             absl::Hash<std::tuple<TypeParam>>{}(std::tuple<TypeParam>(n)));
140 }
141 
142 REGISTER_TYPED_TEST_SUITE_P(HashValueIntTest, BasicUsage, FastPath);
143 using IntTypes = testing::Types<unsigned char, char, int, int32_t, int64_t,
144                                 uint32_t, uint64_t, size_t>;
145 INSTANTIATE_TYPED_TEST_SUITE_P(My, HashValueIntTest, IntTypes);
146 
147 enum LegacyEnum { kValue1, kValue2, kValue3 };
148 
149 enum class EnumClass { kValue4, kValue5, kValue6 };
150 
TEST(HashValueTest,EnumAndBool)151 TEST(HashValueTest, EnumAndBool) {
152   EXPECT_TRUE((is_hashable<LegacyEnum>::value));
153   EXPECT_TRUE((is_hashable<EnumClass>::value));
154   EXPECT_TRUE((is_hashable<bool>::value));
155 
156   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
157       LegacyEnum::kValue1, LegacyEnum::kValue2, LegacyEnum::kValue3)));
158   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
159       EnumClass::kValue4, EnumClass::kValue5, EnumClass::kValue6)));
160   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
161       std::make_tuple(true, false)));
162 }
163 
TEST(HashValueTest,FloatingPoint)164 TEST(HashValueTest, FloatingPoint) {
165   EXPECT_TRUE((is_hashable<float>::value));
166   EXPECT_TRUE((is_hashable<double>::value));
167   EXPECT_TRUE((is_hashable<long double>::value));
168 
169   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
170       std::make_tuple(42.f, 0.f, -0.f, std::numeric_limits<float>::infinity(),
171                       -std::numeric_limits<float>::infinity())));
172 
173   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
174       std::make_tuple(42., 0., -0., std::numeric_limits<double>::infinity(),
175                       -std::numeric_limits<double>::infinity())));
176 
177   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
178       // Add some values with small exponent to test that NORMAL values also
179       // append their category.
180       .5L, 1.L, 2.L, 4.L, 42.L, 0.L, -0.L,
181       17 * static_cast<long double>(std::numeric_limits<double>::max()),
182       std::numeric_limits<long double>::infinity(),
183       -std::numeric_limits<long double>::infinity())));
184 }
185 
TEST(HashValueTest,Pointer)186 TEST(HashValueTest, Pointer) {
187   EXPECT_TRUE((is_hashable<int*>::value));
188   EXPECT_TRUE((is_hashable<int(*)(char, float)>::value));
189   EXPECT_TRUE((is_hashable<void(*)(int, int, ...)>::value));
190 
191   int i;
192   int* ptr = &i;
193   int* n = nullptr;
194 
195   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
196       std::make_tuple(&i, ptr, nullptr, ptr + 1, n)));
197 }
198 
TEST(HashValueTest,PointerAlignment)199 TEST(HashValueTest, PointerAlignment) {
200   // We want to make sure that pointer alignment will not cause bits to be
201   // stuck.
202 
203   constexpr size_t kTotalSize = 1 << 20;
204   std::unique_ptr<char[]> data(new char[kTotalSize]);
205   constexpr size_t kLog2NumValues = 5;
206   constexpr size_t kNumValues = 1 << kLog2NumValues;
207 
208   for (size_t align = 1; align < kTotalSize / kNumValues;
209        align < 8 ? align += 1 : align < 1024 ? align += 8 : align += 32) {
210     SCOPED_TRACE(align);
211     ASSERT_LE(align * kNumValues, kTotalSize);
212 
213     size_t bits_or = 0;
214     size_t bits_and = ~size_t{};
215 
216     for (size_t i = 0; i < kNumValues; ++i) {
217       size_t hash = absl::Hash<void*>()(data.get() + i * align);
218       bits_or |= hash;
219       bits_and &= hash;
220     }
221 
222     // Limit the scope to the bits we would be using for Swisstable.
223     constexpr size_t kMask = (1 << (kLog2NumValues + 7)) - 1;
224     size_t stuck_bits = (~bits_or | bits_and) & kMask;
225     EXPECT_EQ(stuck_bits, 0u) << "0x" << std::hex << stuck_bits;
226   }
227 }
228 
TEST(HashValueTest,PointerToMember)229 TEST(HashValueTest, PointerToMember) {
230   struct Bass {
231     void q() {}
232   };
233 
234   struct A : Bass {
235     virtual ~A() = default;
236     virtual void vfa() {}
237 
238     static auto pq() -> void (A::*)() { return &A::q; }
239   };
240 
241   struct B : Bass {
242     virtual ~B() = default;
243     virtual void vfb() {}
244 
245     static auto pq() -> void (B::*)() { return &B::q; }
246   };
247 
248   struct Foo : A, B {
249     void f1() {}
250     void f2() const {}
251 
252     int g1() & { return 0; }
253     int g2() const & { return 0; }
254     int g3() && { return 0; }
255     int g4() const && { return 0; }
256 
257     int h1() & { return 0; }
258     int h2() const & { return 0; }
259     int h3() && { return 0; }
260     int h4() const && { return 0; }
261 
262     int a;
263     int b;
264 
265     const int c = 11;
266     const int d = 22;
267   };
268 
269   EXPECT_TRUE((is_hashable<float Foo::*>::value));
270   EXPECT_TRUE((is_hashable<double (Foo::*)(int, int)&&>::value));
271 
272   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
273       std::make_tuple(&Foo::a, &Foo::b, static_cast<int Foo::*>(nullptr))));
274 
275   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
276       std::make_tuple(&Foo::c, &Foo::d, static_cast<const int Foo::*>(nullptr),
277                       &Foo::a, &Foo::b)));
278 
279   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
280       &Foo::f1, static_cast<void (Foo::*)()>(nullptr))));
281 
282   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
283       &Foo::f2, static_cast<void (Foo::*)() const>(nullptr))));
284 
285   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
286       &Foo::g1, &Foo::h1, static_cast<int (Foo::*)() &>(nullptr))));
287 
288   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
289       &Foo::g2, &Foo::h2, static_cast<int (Foo::*)() const &>(nullptr))));
290 
291   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
292       &Foo::g3, &Foo::h3, static_cast<int (Foo::*)() &&>(nullptr))));
293 
294   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
295       &Foo::g4, &Foo::h4, static_cast<int (Foo::*)() const &&>(nullptr))));
296 
297   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
298       std::make_tuple(static_cast<void (Foo::*)()>(&Foo::vfa),
299                       static_cast<void (Foo::*)()>(&Foo::vfb),
300                       static_cast<void (Foo::*)()>(nullptr))));
301 
302   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
303       std::make_tuple(static_cast<void (Foo::*)()>(Foo::A::pq()),
304                       static_cast<void (Foo::*)()>(Foo::B::pq()),
305                       static_cast<void (Foo::*)()>(nullptr))));
306 }
307 
TEST(HashValueTest,PairAndTuple)308 TEST(HashValueTest, PairAndTuple) {
309   EXPECT_TRUE((is_hashable<std::pair<int, int>>::value));
310   EXPECT_TRUE((is_hashable<std::pair<const int&, const int&>>::value));
311   EXPECT_TRUE((is_hashable<std::tuple<int&, int&>>::value));
312   EXPECT_TRUE((is_hashable<std::tuple<int&&, int&&>>::value));
313 
314   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
315       std::make_pair(0, 42), std::make_pair(0, 42), std::make_pair(42, 0),
316       std::make_pair(0, 0), std::make_pair(42, 42), std::make_pair(1, 42))));
317 
318   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
319       std::make_tuple(std::make_tuple(0, 0, 0), std::make_tuple(0, 0, 42),
320                       std::make_tuple(0, 23, 0), std::make_tuple(17, 0, 0),
321                       std::make_tuple(42, 0, 0), std::make_tuple(3, 9, 9),
322                       std::make_tuple(0, 0, -42))));
323 
324   // Test that tuples of lvalue references work (so we need a few lvalues):
325   int a = 0, b = 1, c = 17, d = 23;
326   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
327       std::tie(a, a), std::tie(a, b), std::tie(b, c), std::tie(c, d))));
328 
329   // Test that tuples of rvalue references work:
330   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
331       std::forward_as_tuple(0, 0, 0), std::forward_as_tuple(0, 0, 42),
332       std::forward_as_tuple(0, 23, 0), std::forward_as_tuple(17, 0, 0),
333       std::forward_as_tuple(42, 0, 0), std::forward_as_tuple(3, 9, 9),
334       std::forward_as_tuple(0, 0, -42))));
335 }
336 
TEST(HashValueTest,CombineContiguousWorks)337 TEST(HashValueTest, CombineContiguousWorks) {
338   std::vector<std::tuple<int>> v1 = {std::make_tuple(1), std::make_tuple(3)};
339   std::vector<std::tuple<int>> v2 = {std::make_tuple(1), std::make_tuple(2)};
340 
341   auto vh1 = SpyHash(v1);
342   auto vh2 = SpyHash(v2);
343   EXPECT_NE(vh1, vh2);
344 }
345 
346 struct DummyDeleter {
347   template <typename T>
operator ()__anon061a8a600111::DummyDeleter348   void operator() (T* ptr) {}
349 };
350 
351 struct SmartPointerEq {
352   template <typename T, typename U>
operator ()__anon061a8a600111::SmartPointerEq353   bool operator()(const T& t, const U& u) const {
354     return GetPtr(t) == GetPtr(u);
355   }
356 
357   template <typename T>
GetPtr__anon061a8a600111::SmartPointerEq358   static auto GetPtr(const T& t) -> decltype(&*t) {
359     return t ? &*t : nullptr;
360   }
361 
GetPtr__anon061a8a600111::SmartPointerEq362   static std::nullptr_t GetPtr(std::nullptr_t) { return nullptr; }
363 };
364 
TEST(HashValueTest,SmartPointers)365 TEST(HashValueTest, SmartPointers) {
366   EXPECT_TRUE((is_hashable<std::unique_ptr<int>>::value));
367   EXPECT_TRUE((is_hashable<std::unique_ptr<int, DummyDeleter>>::value));
368   EXPECT_TRUE((is_hashable<std::shared_ptr<int>>::value));
369 
370   int i, j;
371   std::unique_ptr<int, DummyDeleter> unique1(&i);
372   std::unique_ptr<int, DummyDeleter> unique2(&i);
373   std::unique_ptr<int, DummyDeleter> unique_other(&j);
374   std::unique_ptr<int, DummyDeleter> unique_null;
375 
376   std::shared_ptr<int> shared1(&i, DummyDeleter());
377   std::shared_ptr<int> shared2(&i, DummyDeleter());
378   std::shared_ptr<int> shared_other(&j, DummyDeleter());
379   std::shared_ptr<int> shared_null;
380 
381   // Sanity check of the Eq function.
382   ASSERT_TRUE(SmartPointerEq{}(unique1, shared1));
383   ASSERT_FALSE(SmartPointerEq{}(unique1, shared_other));
384   ASSERT_TRUE(SmartPointerEq{}(unique_null, nullptr));
385   ASSERT_FALSE(SmartPointerEq{}(shared2, nullptr));
386 
387   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
388       std::forward_as_tuple(&i, nullptr,                    //
389                             unique1, unique2, unique_null,  //
390                             absl::make_unique<int>(),       //
391                             shared1, shared2, shared_null,  //
392                             std::make_shared<int>()),
393       SmartPointerEq{}));
394 }
395 
TEST(HashValueTest,FunctionPointer)396 TEST(HashValueTest, FunctionPointer) {
397   using Func = int (*)();
398   EXPECT_TRUE(is_hashable<Func>::value);
399 
400   Func p1 = [] { return 2; }, p2 = [] { return 1; };
401   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
402       std::make_tuple(p1, p2, nullptr)));
403 }
404 
405 struct WrapInTuple {
406   template <typename T>
operator ()__anon061a8a600111::WrapInTuple407   std::tuple<int, T, size_t> operator()(const T& t) const {
408     return std::make_tuple(7, t, 0xdeadbeef);
409   }
410 };
411 
FlatCord(absl::string_view sv)412 absl::Cord FlatCord(absl::string_view sv) {
413   absl::Cord c(sv);
414   c.Flatten();
415   return c;
416 }
417 
FragmentedCord(absl::string_view sv)418 absl::Cord FragmentedCord(absl::string_view sv) {
419   if (sv.size() < 2) {
420     return absl::Cord(sv);
421   }
422   size_t halfway = sv.size() / 2;
423   std::vector<absl::string_view> parts = {sv.substr(0, halfway),
424                                           sv.substr(halfway)};
425   return absl::MakeFragmentedCord(parts);
426 }
427 
TEST(HashValueTest,Strings)428 TEST(HashValueTest, Strings) {
429   EXPECT_TRUE((is_hashable<std::string>::value));
430 
431   const std::string small = "foo";
432   const std::string dup = "foofoo";
433   const std::string large = std::string(2048, 'x');  // multiple of chunk size
434   const std::string huge = std::string(5000, 'a');   // not a multiple
435 
436   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(  //
437       std::string(), absl::string_view(), absl::Cord(),                     //
438       std::string(""), absl::string_view(""), absl::Cord(""),               //
439       std::string(small), absl::string_view(small), absl::Cord(small),      //
440       std::string(dup), absl::string_view(dup), absl::Cord(dup),            //
441       std::string(large), absl::string_view(large), absl::Cord(large),      //
442       std::string(huge), absl::string_view(huge), FlatCord(huge),           //
443       FragmentedCord(huge))));
444 
445   // Also check that nested types maintain the same hash.
446   const WrapInTuple t{};
447   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(  //
448       t(std::string()), t(absl::string_view()), t(absl::Cord()),            //
449       t(std::string("")), t(absl::string_view("")), t(absl::Cord("")),      //
450       t(std::string(small)), t(absl::string_view(small)),                   //
451           t(absl::Cord(small)),                                             //
452       t(std::string(dup)), t(absl::string_view(dup)), t(absl::Cord(dup)),   //
453       t(std::string(large)), t(absl::string_view(large)),                   //
454           t(absl::Cord(large)),                                             //
455       t(std::string(huge)), t(absl::string_view(huge)),                     //
456           t(FlatCord(huge)), t(FragmentedCord(huge)))));
457 
458   // Make sure that hashing a `const char*` does not use its string-value.
459   EXPECT_NE(SpyHash(static_cast<const char*>("ABC")),
460             SpyHash(absl::string_view("ABC")));
461 }
462 
TEST(HashValueTest,WString)463 TEST(HashValueTest, WString) {
464   EXPECT_TRUE((is_hashable<std::wstring>::value));
465 
466   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
467       std::wstring(), std::wstring(L"ABC"), std::wstring(L"ABC"),
468       std::wstring(L"Some other different string"),
469       std::wstring(L"Iñtërnâtiônàlizætiøn"))));
470 }
471 
TEST(HashValueTest,U16String)472 TEST(HashValueTest, U16String) {
473   EXPECT_TRUE((is_hashable<std::u16string>::value));
474 
475   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
476       std::u16string(), std::u16string(u"ABC"), std::u16string(u"ABC"),
477       std::u16string(u"Some other different string"),
478       std::u16string(u"Iñtërnâtiônàlizætiøn"))));
479 }
480 
TEST(HashValueTest,U32String)481 TEST(HashValueTest, U32String) {
482   EXPECT_TRUE((is_hashable<std::u32string>::value));
483 
484   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
485       std::u32string(), std::u32string(U"ABC"), std::u32string(U"ABC"),
486       std::u32string(U"Some other different string"),
487       std::u32string(U"Iñtërnâtiônàlizætiøn"))));
488 }
489 
TEST(HashValueTest,StdArray)490 TEST(HashValueTest, StdArray) {
491   EXPECT_TRUE((is_hashable<std::array<int, 3>>::value));
492 
493   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
494       std::make_tuple(std::array<int, 3>{}, std::array<int, 3>{{0, 23, 42}})));
495 }
496 
TEST(HashValueTest,StdBitset)497 TEST(HashValueTest, StdBitset) {
498   EXPECT_TRUE((is_hashable<std::bitset<257>>::value));
499 
500   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
501       {std::bitset<2>("00"), std::bitset<2>("01"), std::bitset<2>("10"),
502        std::bitset<2>("11")}));
503   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
504       {std::bitset<5>("10101"), std::bitset<5>("10001"), std::bitset<5>()}));
505 
506   constexpr int kNumBits = 256;
507   std::array<std::string, 6> bit_strings;
508   bit_strings.fill(std::string(kNumBits, '1'));
509   bit_strings[1][0] = '0';
510   bit_strings[2][1] = '0';
511   bit_strings[3][kNumBits / 3] = '0';
512   bit_strings[4][kNumBits - 2] = '0';
513   bit_strings[5][kNumBits - 1] = '0';
514   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
515       {std::bitset<kNumBits>(bit_strings[0].c_str()),
516        std::bitset<kNumBits>(bit_strings[1].c_str()),
517        std::bitset<kNumBits>(bit_strings[2].c_str()),
518        std::bitset<kNumBits>(bit_strings[3].c_str()),
519        std::bitset<kNumBits>(bit_strings[4].c_str()),
520        std::bitset<kNumBits>(bit_strings[5].c_str())}));
521 }  // namespace
522 
523 // Dummy type with unordered equality and hashing semantics.  This preserves
524 // input order internally, and is used below to ensure we get test coverage
525 // for equal sequences with different iteraton orders.
526 template <typename T>
527 class UnorderedSequence {
528  public:
529   UnorderedSequence() = default;
530   template <typename TT>
UnorderedSequence(std::initializer_list<TT> l)531   UnorderedSequence(std::initializer_list<TT> l)
532       : values_(l.begin(), l.end()) {}
533   template <typename ForwardIterator,
534             typename std::enable_if<!std::is_integral<ForwardIterator>::value,
535                                     bool>::type = true>
UnorderedSequence(ForwardIterator begin,ForwardIterator end)536   UnorderedSequence(ForwardIterator begin, ForwardIterator end)
537       : values_(begin, end) {}
538   // one-argument constructor of value type T, to appease older toolchains that
539   // get confused by one-element initializer lists in some contexts
UnorderedSequence(const T & v)540   explicit UnorderedSequence(const T& v) : values_(&v, &v + 1) {}
541 
542   using value_type = T;
543 
size() const544   size_t size() const { return values_.size(); }
begin() const545   typename std::vector<T>::const_iterator begin() const {
546     return values_.begin();
547   }
end() const548   typename std::vector<T>::const_iterator end() const { return values_.end(); }
549 
operator ==(const UnorderedSequence & lhs,const UnorderedSequence & rhs)550   friend bool operator==(const UnorderedSequence& lhs,
551                          const UnorderedSequence& rhs) {
552     return lhs.size() == rhs.size() &&
553            std::is_permutation(lhs.begin(), lhs.end(), rhs.begin());
554   }
operator !=(const UnorderedSequence & lhs,const UnorderedSequence & rhs)555   friend bool operator!=(const UnorderedSequence& lhs,
556                          const UnorderedSequence& rhs) {
557     return !(lhs == rhs);
558   }
559   template <typename H>
AbslHashValue(H h,const UnorderedSequence & u)560   friend H AbslHashValue(H h, const UnorderedSequence& u) {
561     return H::combine(H::combine_unordered(std::move(h), u.begin(), u.end()),
562                       u.size());
563   }
564 
565  private:
566   std::vector<T> values_;
567 };
568 
569 template <typename T>
570 class HashValueSequenceTest : public testing::Test {
571 };
572 TYPED_TEST_SUITE_P(HashValueSequenceTest);
573 
TYPED_TEST_P(HashValueSequenceTest,BasicUsage)574 TYPED_TEST_P(HashValueSequenceTest, BasicUsage) {
575   EXPECT_TRUE((is_hashable<TypeParam>::value));
576 
577   using IntType = typename TypeParam::value_type;
578   auto a = static_cast<IntType>(0);
579   auto b = static_cast<IntType>(23);
580   auto c = static_cast<IntType>(42);
581 
582   std::vector<TypeParam> exemplars = {
583       TypeParam(),        TypeParam(),        TypeParam{a, b, c},
584       TypeParam{a, c, b}, TypeParam{c, a, b}, TypeParam{a},
585       TypeParam{a, a},    TypeParam{a, a, a}, TypeParam{a, a, b},
586       TypeParam{a, b, a}, TypeParam{b, a, a}, TypeParam{a, b},
587       TypeParam{b, c}};
588   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(exemplars));
589 }
590 
591 REGISTER_TYPED_TEST_SUITE_P(HashValueSequenceTest, BasicUsage);
592 using IntSequenceTypes = testing::Types<
593     std::deque<int>, std::forward_list<int>, std::list<int>, std::vector<int>,
594     std::vector<bool>, TypeErasedContainer<std::vector<int>>, std::set<int>,
595     std::multiset<int>, UnorderedSequence<int>,
596     TypeErasedContainer<UnorderedSequence<int>>, std::unordered_set<int>,
597     std::unordered_multiset<int>, absl::flat_hash_set<int>,
598     absl::node_hash_set<int>, absl::btree_set<int>>;
599 INSTANTIATE_TYPED_TEST_SUITE_P(My, HashValueSequenceTest, IntSequenceTypes);
600 
601 template <typename T>
602 class HashValueNestedSequenceTest : public testing::Test {};
603 TYPED_TEST_SUITE_P(HashValueNestedSequenceTest);
604 
TYPED_TEST_P(HashValueNestedSequenceTest,BasicUsage)605 TYPED_TEST_P(HashValueNestedSequenceTest, BasicUsage) {
606   using T = TypeParam;
607   using V = typename T::value_type;
608   std::vector<T> exemplars = {
609       // empty case
610       T{},
611       // sets of empty sets
612       T{V{}}, T{V{}, V{}}, T{V{}, V{}, V{}},
613       // multisets of different values
614       T{V{1}}, T{V{1, 1}, V{1, 1}}, T{V{1, 1, 1}, V{1, 1, 1}, V{1, 1, 1}},
615       // various orderings of same nested sets
616       T{V{}, V{1, 2}}, T{V{}, V{2, 1}}, T{V{1, 2}, V{}}, T{V{2, 1}, V{}},
617       // various orderings of various nested sets, case 2
618       T{V{1, 2}, V{3, 4}}, T{V{1, 2}, V{4, 3}}, T{V{1, 3}, V{2, 4}},
619       T{V{1, 3}, V{4, 2}}, T{V{1, 4}, V{2, 3}}, T{V{1, 4}, V{3, 2}},
620       T{V{2, 3}, V{1, 4}}, T{V{2, 3}, V{4, 1}}, T{V{2, 4}, V{1, 3}},
621       T{V{2, 4}, V{3, 1}}, T{V{3, 4}, V{1, 2}}, T{V{3, 4}, V{2, 1}}};
622   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(exemplars));
623 }
624 
625 REGISTER_TYPED_TEST_SUITE_P(HashValueNestedSequenceTest, BasicUsage);
626 template <typename T>
627 using TypeErasedSet = TypeErasedContainer<UnorderedSequence<T>>;
628 
629 using NestedIntSequenceTypes = testing::Types<
630     std::vector<std::vector<int>>, std::vector<UnorderedSequence<int>>,
631     std::vector<TypeErasedSet<int>>, UnorderedSequence<std::vector<int>>,
632     UnorderedSequence<UnorderedSequence<int>>,
633     UnorderedSequence<TypeErasedSet<int>>, TypeErasedSet<std::vector<int>>,
634     TypeErasedSet<UnorderedSequence<int>>, TypeErasedSet<TypeErasedSet<int>>>;
635 INSTANTIATE_TYPED_TEST_SUITE_P(My, HashValueNestedSequenceTest,
636                               NestedIntSequenceTypes);
637 
638 // Private type that only supports AbslHashValue to make sure our chosen hash
639 // implementation is recursive within absl::Hash.
640 // It uses std::abs() on the value to provide different bitwise representations
641 // of the same logical value.
642 struct Private {
643   int i;
644   template <typename H>
AbslHashValue(H h,Private p)645   friend H AbslHashValue(H h, Private p) {
646     return H::combine(std::move(h), std::abs(p.i));
647   }
648 
operator ==(Private a,Private b)649   friend bool operator==(Private a, Private b) {
650     return std::abs(a.i) == std::abs(b.i);
651   }
652 
operator <<(std::ostream & o,Private p)653   friend std::ostream& operator<<(std::ostream& o, Private p) {
654     return o << p.i;
655   }
656 };
657 
658 // Test helper for combine_piecewise_buffer.  It holds a string_view to the
659 // buffer-to-be-hashed.  Its AbslHashValue specialization will split up its
660 // contents at the character offsets requested.
661 class PiecewiseHashTester {
662  public:
663   // Create a hash view of a buffer to be hashed contiguously.
PiecewiseHashTester(absl::string_view buf)664   explicit PiecewiseHashTester(absl::string_view buf)
665       : buf_(buf), piecewise_(false), split_locations_() {}
666 
667   // Create a hash view of a buffer to be hashed piecewise, with breaks at the
668   // given locations.
PiecewiseHashTester(absl::string_view buf,std::set<size_t> split_locations)669   PiecewiseHashTester(absl::string_view buf, std::set<size_t> split_locations)
670       : buf_(buf),
671         piecewise_(true),
672         split_locations_(std::move(split_locations)) {}
673 
674   template <typename H>
AbslHashValue(H h,const PiecewiseHashTester & p)675   friend H AbslHashValue(H h, const PiecewiseHashTester& p) {
676     if (!p.piecewise_) {
677       return H::combine_contiguous(std::move(h), p.buf_.data(), p.buf_.size());
678     }
679     absl::hash_internal::PiecewiseCombiner combiner;
680     if (p.split_locations_.empty()) {
681       h = combiner.add_buffer(std::move(h), p.buf_.data(), p.buf_.size());
682       return combiner.finalize(std::move(h));
683     }
684     size_t begin = 0;
685     for (size_t next : p.split_locations_) {
686       absl::string_view chunk = p.buf_.substr(begin, next - begin);
687       h = combiner.add_buffer(std::move(h), chunk.data(), chunk.size());
688       begin = next;
689     }
690     absl::string_view last_chunk = p.buf_.substr(begin);
691     if (!last_chunk.empty()) {
692       h = combiner.add_buffer(std::move(h), last_chunk.data(),
693                               last_chunk.size());
694     }
695     return combiner.finalize(std::move(h));
696   }
697 
698  private:
699   absl::string_view buf_;
700   bool piecewise_;
701   std::set<size_t> split_locations_;
702 };
703 
704 // Dummy object that hashes as two distinct contiguous buffers, "foo" followed
705 // by "bar"
706 struct DummyFooBar {
707   template <typename H>
AbslHashValue(H h,const DummyFooBar &)708   friend H AbslHashValue(H h, const DummyFooBar&) {
709     const char* foo = "foo";
710     const char* bar = "bar";
711     h = H::combine_contiguous(std::move(h), foo, 3);
712     h = H::combine_contiguous(std::move(h), bar, 3);
713     return h;
714   }
715 };
716 
TEST(HashValueTest,CombinePiecewiseBuffer)717 TEST(HashValueTest, CombinePiecewiseBuffer) {
718   absl::Hash<PiecewiseHashTester> hash;
719 
720   // Check that hashing an empty buffer through the piecewise API works.
721   EXPECT_EQ(hash(PiecewiseHashTester("")), hash(PiecewiseHashTester("", {})));
722 
723   // Similarly, small buffers should give consistent results
724   EXPECT_EQ(hash(PiecewiseHashTester("foobar")),
725             hash(PiecewiseHashTester("foobar", {})));
726   EXPECT_EQ(hash(PiecewiseHashTester("foobar")),
727             hash(PiecewiseHashTester("foobar", {3})));
728 
729   // But hashing "foobar" in pieces gives a different answer than hashing "foo"
730   // contiguously, then "bar" contiguously.
731   EXPECT_NE(hash(PiecewiseHashTester("foobar", {3})),
732             absl::Hash<DummyFooBar>()(DummyFooBar{}));
733 
734   // Test hashing a large buffer incrementally, broken up in several different
735   // ways.  Arrange for breaks on and near the stride boundaries to look for
736   // off-by-one errors in the implementation.
737   //
738   // This test is run on a buffer that is a multiple of the stride size, and one
739   // that isn't.
740   for (size_t big_buffer_size : {1024u * 2 + 512u, 1024u * 3}) {
741     SCOPED_TRACE(big_buffer_size);
742     std::string big_buffer;
743     for (size_t i = 0; i < big_buffer_size; ++i) {
744       // Arbitrary string
745       big_buffer.push_back(32 + (i * (i / 3)) % 64);
746     }
747     auto big_buffer_hash = hash(PiecewiseHashTester(big_buffer));
748 
749     const int possible_breaks = 9;
750     size_t breaks[possible_breaks] = {1,    512,  1023, 1024, 1025,
751                                       1536, 2047, 2048, 2049};
752     for (unsigned test_mask = 0; test_mask < (1u << possible_breaks);
753          ++test_mask) {
754       SCOPED_TRACE(test_mask);
755       std::set<size_t> break_locations;
756       for (int j = 0; j < possible_breaks; ++j) {
757         if (test_mask & (1u << j)) {
758           break_locations.insert(breaks[j]);
759         }
760       }
761       EXPECT_EQ(
762           hash(PiecewiseHashTester(big_buffer, std::move(break_locations))),
763           big_buffer_hash);
764     }
765   }
766 }
767 
TEST(HashValueTest,PrivateSanity)768 TEST(HashValueTest, PrivateSanity) {
769   // Sanity check that Private is working as the tests below expect it to work.
770   EXPECT_TRUE(is_hashable<Private>::value);
771   EXPECT_NE(SpyHash(Private{0}), SpyHash(Private{1}));
772   EXPECT_EQ(SpyHash(Private{1}), SpyHash(Private{1}));
773 }
774 
TEST(HashValueTest,Optional)775 TEST(HashValueTest, Optional) {
776   EXPECT_TRUE(is_hashable<absl::optional<Private>>::value);
777 
778   using O = absl::optional<Private>;
779   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
780       std::make_tuple(O{}, O{{1}}, O{{-1}}, O{{10}})));
781 }
782 
TEST(HashValueTest,Variant)783 TEST(HashValueTest, Variant) {
784   using V = absl::variant<Private, std::string>;
785   EXPECT_TRUE(is_hashable<V>::value);
786 
787   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
788       V(Private{1}), V(Private{-1}), V(Private{2}), V("ABC"), V("BCD"))));
789 
790 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
791   struct S {};
792   EXPECT_FALSE(is_hashable<absl::variant<S>>::value);
793 #endif
794 }
795 
796 template <typename T>
797 class HashValueAssociativeMapTest : public testing::Test {};
798 TYPED_TEST_SUITE_P(HashValueAssociativeMapTest);
799 
TYPED_TEST_P(HashValueAssociativeMapTest,BasicUsage)800 TYPED_TEST_P(HashValueAssociativeMapTest, BasicUsage) {
801   using M = TypeParam;
802   using V = typename M::value_type;
803   std::vector<M> exemplars{M{},
804                            M{V{0, "foo"}},
805                            M{V{1, "foo"}},
806                            M{V{0, "bar"}},
807                            M{V{1, "bar"}},
808                            M{V{0, "foo"}, V{42, "bar"}},
809                            M{V{42, "bar"}, V{0, "foo"}},
810                            M{V{1, "foo"}, V{42, "bar"}},
811                            M{V{1, "foo"}, V{43, "bar"}},
812                            M{V{1, "foo"}, V{43, "baz"}}};
813   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(exemplars));
814 }
815 
816 REGISTER_TYPED_TEST_SUITE_P(HashValueAssociativeMapTest, BasicUsage);
817 using AssociativeMapTypes = testing::Types<
818     std::map<int, std::string>, std::unordered_map<int, std::string>,
819     absl::flat_hash_map<int, std::string>,
820     absl::node_hash_map<int, std::string>, absl::btree_map<int, std::string>,
821     UnorderedSequence<std::pair<const int, std::string>>>;
822 INSTANTIATE_TYPED_TEST_SUITE_P(My, HashValueAssociativeMapTest,
823                               AssociativeMapTypes);
824 
825 template <typename T>
826 class HashValueAssociativeMultimapTest : public testing::Test {};
827 TYPED_TEST_SUITE_P(HashValueAssociativeMultimapTest);
828 
TYPED_TEST_P(HashValueAssociativeMultimapTest,BasicUsage)829 TYPED_TEST_P(HashValueAssociativeMultimapTest, BasicUsage) {
830   using MM = TypeParam;
831   using V = typename MM::value_type;
832   std::vector<MM> exemplars{MM{},
833                             MM{V{0, "foo"}},
834                             MM{V{1, "foo"}},
835                             MM{V{0, "bar"}},
836                             MM{V{1, "bar"}},
837                             MM{V{0, "foo"}, V{0, "bar"}},
838                             MM{V{0, "bar"}, V{0, "foo"}},
839                             MM{V{0, "foo"}, V{42, "bar"}},
840                             MM{V{1, "foo"}, V{42, "bar"}},
841                             MM{V{1, "foo"}, V{1, "foo"}, V{43, "bar"}},
842                             MM{V{1, "foo"}, V{43, "bar"}, V{1, "foo"}},
843                             MM{V{1, "foo"}, V{43, "baz"}}};
844   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(exemplars));
845 }
846 
847 REGISTER_TYPED_TEST_SUITE_P(HashValueAssociativeMultimapTest, BasicUsage);
848 using AssociativeMultimapTypes =
849     testing::Types<std::multimap<int, std::string>,
850                    std::unordered_multimap<int, std::string>>;
851 INSTANTIATE_TYPED_TEST_SUITE_P(My, HashValueAssociativeMultimapTest,
852                               AssociativeMultimapTypes);
853 
TEST(HashValueTest,ReferenceWrapper)854 TEST(HashValueTest, ReferenceWrapper) {
855   EXPECT_TRUE(is_hashable<std::reference_wrapper<Private>>::value);
856 
857   Private p1{1}, p10{10};
858   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
859       p1, p10, std::ref(p1), std::ref(p10), std::cref(p1), std::cref(p10))));
860 
861   EXPECT_TRUE(is_hashable<std::reference_wrapper<int>>::value);
862   int one = 1, ten = 10;
863   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
864       one, ten, std::ref(one), std::ref(ten), std::cref(one), std::cref(ten))));
865 
866   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
867       std::make_tuple(std::tuple<std::reference_wrapper<int>>(std::ref(one)),
868                       std::tuple<std::reference_wrapper<int>>(std::ref(ten)),
869                       std::tuple<int>(one), std::tuple<int>(ten))));
870 }
871 
872 template <typename T, typename = void>
873 struct IsHashCallable : std::false_type {};
874 
875 template <typename T>
876 struct IsHashCallable<T, absl::void_t<decltype(std::declval<absl::Hash<T>>()(
877                             std::declval<const T&>()))>> : std::true_type {};
878 
879 template <typename T, typename = void>
880 struct IsAggregateInitializable : std::false_type {};
881 
882 template <typename T>
883 struct IsAggregateInitializable<T, absl::void_t<decltype(T{})>>
884     : std::true_type {};
885 
TEST(IsHashableTest,ValidHash)886 TEST(IsHashableTest, ValidHash) {
887   EXPECT_TRUE((is_hashable<int>::value));
888   EXPECT_TRUE(std::is_default_constructible<absl::Hash<int>>::value);
889   EXPECT_TRUE(std::is_copy_constructible<absl::Hash<int>>::value);
890   EXPECT_TRUE(std::is_move_constructible<absl::Hash<int>>::value);
891   EXPECT_TRUE(absl::is_copy_assignable<absl::Hash<int>>::value);
892   EXPECT_TRUE(absl::is_move_assignable<absl::Hash<int>>::value);
893   EXPECT_TRUE(IsHashCallable<int>::value);
894   EXPECT_TRUE(IsAggregateInitializable<absl::Hash<int>>::value);
895 }
896 
897 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
TEST(IsHashableTest,PoisonHash)898 TEST(IsHashableTest, PoisonHash) {
899   struct X {};
900   EXPECT_FALSE((is_hashable<X>::value));
901   EXPECT_FALSE(std::is_default_constructible<absl::Hash<X>>::value);
902   EXPECT_FALSE(std::is_copy_constructible<absl::Hash<X>>::value);
903   EXPECT_FALSE(std::is_move_constructible<absl::Hash<X>>::value);
904   EXPECT_FALSE(absl::is_copy_assignable<absl::Hash<X>>::value);
905   EXPECT_FALSE(absl::is_move_assignable<absl::Hash<X>>::value);
906   EXPECT_FALSE(IsHashCallable<X>::value);
907 #if !defined(__GNUC__) || __GNUC__ < 9
908   // This doesn't compile on GCC 9.
909   EXPECT_FALSE(IsAggregateInitializable<absl::Hash<X>>::value);
910 #endif
911 }
912 #endif  // ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
913 
914 // Hashable types
915 //
916 // These types exist simply to exercise various AbslHashValue behaviors, so
917 // they are named by what their AbslHashValue overload does.
918 struct NoOp {
919   template <typename HashCode>
AbslHashValue(HashCode h,NoOp n)920   friend HashCode AbslHashValue(HashCode h, NoOp n) {
921     return h;
922   }
923 };
924 
925 struct EmptyCombine {
926   template <typename HashCode>
AbslHashValue(HashCode h,EmptyCombine e)927   friend HashCode AbslHashValue(HashCode h, EmptyCombine e) {
928     return HashCode::combine(std::move(h));
929   }
930 };
931 
932 template <typename Int>
933 struct CombineIterative {
934   template <typename HashCode>
AbslHashValue(HashCode h,CombineIterative c)935   friend HashCode AbslHashValue(HashCode h, CombineIterative c) {
936     for (int i = 0; i < 5; ++i) {
937       h = HashCode::combine(std::move(h), Int(i));
938     }
939     return h;
940   }
941 };
942 
943 template <typename Int>
944 struct CombineVariadic {
945   template <typename HashCode>
AbslHashValue(HashCode h,CombineVariadic c)946   friend HashCode AbslHashValue(HashCode h, CombineVariadic c) {
947     return HashCode::combine(std::move(h), Int(0), Int(1), Int(2), Int(3),
948                              Int(4));
949   }
950 };
951 enum class InvokeTag {
952   kUniquelyRepresented,
953   kHashValue,
954 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
955   kLegacyHash,
956 #endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
957   kStdHash,
958   kNone
959 };
960 
961 template <InvokeTag T>
962 using InvokeTagConstant = std::integral_constant<InvokeTag, T>;
963 
964 template <InvokeTag... Tags>
965 struct MinTag;
966 
967 template <InvokeTag a, InvokeTag b, InvokeTag... Tags>
968 struct MinTag<a, b, Tags...> : MinTag<(a < b ? a : b), Tags...> {};
969 
970 template <InvokeTag a>
971 struct MinTag<a> : InvokeTagConstant<a> {};
972 
973 template <InvokeTag... Tags>
974 struct CustomHashType {
CustomHashType__anon061a8a600111::CustomHashType975   explicit CustomHashType(size_t val) : value(val) {}
976   size_t value;
977 };
978 
979 template <InvokeTag allowed, InvokeTag... tags>
980 struct EnableIfContained
981     : std::enable_if<absl::disjunction<
982           std::integral_constant<bool, allowed == tags>...>::value> {};
983 
984 template <
985     typename H, InvokeTag... Tags,
986     typename = typename EnableIfContained<InvokeTag::kHashValue, Tags...>::type>
AbslHashValue(H state,CustomHashType<Tags...> t)987 H AbslHashValue(H state, CustomHashType<Tags...> t) {
988   static_assert(MinTag<Tags...>::value == InvokeTag::kHashValue, "");
989   return H::combine(std::move(state),
990                     t.value + static_cast<int>(InvokeTag::kHashValue));
991 }
992 
993 }  // namespace
994 
995 namespace absl {
996 ABSL_NAMESPACE_BEGIN
997 namespace hash_internal {
998 template <InvokeTag... Tags>
999 struct is_uniquely_represented<
1000     CustomHashType<Tags...>,
1001     typename EnableIfContained<InvokeTag::kUniquelyRepresented, Tags...>::type>
1002     : std::true_type {};
1003 }  // namespace hash_internal
1004 ABSL_NAMESPACE_END
1005 }  // namespace absl
1006 
1007 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
1008 namespace ABSL_INTERNAL_LEGACY_HASH_NAMESPACE {
1009 template <InvokeTag... Tags>
1010 struct hash<CustomHashType<Tags...>> {
1011   template <InvokeTag... TagsIn, typename = typename EnableIfContained<
1012                                      InvokeTag::kLegacyHash, TagsIn...>::type>
operator ()ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash1013   size_t operator()(CustomHashType<TagsIn...> t) const {
1014     static_assert(MinTag<Tags...>::value == InvokeTag::kLegacyHash, "");
1015     return t.value + static_cast<int>(InvokeTag::kLegacyHash);
1016   }
1017 };
1018 }  // namespace ABSL_INTERNAL_LEGACY_HASH_NAMESPACE
1019 #endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
1020 
1021 namespace std {
1022 template <InvokeTag... Tags>  // NOLINT
1023 struct hash<CustomHashType<Tags...>> {
1024   template <InvokeTag... TagsIn, typename = typename EnableIfContained<
1025                                      InvokeTag::kStdHash, TagsIn...>::type>
operator ()std::hash1026   size_t operator()(CustomHashType<TagsIn...> t) const {
1027     static_assert(MinTag<Tags...>::value == InvokeTag::kStdHash, "");
1028     return t.value + static_cast<int>(InvokeTag::kStdHash);
1029   }
1030 };
1031 }  // namespace std
1032 
1033 namespace {
1034 
1035 template <typename... T>
TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>,T...)1036 void TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>, T...) {
1037   using type = CustomHashType<T::value...>;
1038   SCOPED_TRACE(testing::PrintToString(std::vector<InvokeTag>{T::value...}));
1039   EXPECT_TRUE(is_hashable<type>());
1040   EXPECT_TRUE(is_hashable<const type>());
1041   EXPECT_TRUE(is_hashable<const type&>());
1042 
1043   const size_t offset = static_cast<int>(std::min({T::value...}));
1044   EXPECT_EQ(SpyHash(type(7)), SpyHash(size_t{7 + offset}));
1045 }
1046 
TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>)1047 void TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>) {
1048 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
1049   // is_hashable is false if we don't support any of the hooks.
1050   using type = CustomHashType<>;
1051   EXPECT_FALSE(is_hashable<type>());
1052   EXPECT_FALSE(is_hashable<const type>());
1053   EXPECT_FALSE(is_hashable<const type&>());
1054 #endif  // ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
1055 }
1056 
1057 template <InvokeTag Tag, typename... T>
TestCustomHashType(InvokeTagConstant<Tag> tag,T...t)1058 void TestCustomHashType(InvokeTagConstant<Tag> tag, T... t) {
1059   constexpr auto next = static_cast<InvokeTag>(static_cast<int>(Tag) + 1);
1060   TestCustomHashType(InvokeTagConstant<next>(), tag, t...);
1061   TestCustomHashType(InvokeTagConstant<next>(), t...);
1062 }
1063 
TEST(HashTest,CustomHashType)1064 TEST(HashTest, CustomHashType) {
1065   TestCustomHashType(InvokeTagConstant<InvokeTag{}>());
1066 }
1067 
TEST(HashTest,NoOpsAreEquivalent)1068 TEST(HashTest, NoOpsAreEquivalent) {
1069   EXPECT_EQ(Hash<NoOp>()({}), Hash<NoOp>()({}));
1070   EXPECT_EQ(Hash<NoOp>()({}), Hash<EmptyCombine>()({}));
1071 }
1072 
1073 template <typename T>
1074 class HashIntTest : public testing::Test {
1075 };
1076 TYPED_TEST_SUITE_P(HashIntTest);
1077 
TYPED_TEST_P(HashIntTest,BasicUsage)1078 TYPED_TEST_P(HashIntTest, BasicUsage) {
1079   EXPECT_NE(Hash<NoOp>()({}), Hash<TypeParam>()(0));
1080   EXPECT_NE(Hash<NoOp>()({}),
1081             Hash<TypeParam>()(std::numeric_limits<TypeParam>::max()));
1082   if (std::numeric_limits<TypeParam>::min() != 0) {
1083     EXPECT_NE(Hash<NoOp>()({}),
1084               Hash<TypeParam>()(std::numeric_limits<TypeParam>::min()));
1085   }
1086 
1087   EXPECT_EQ(Hash<CombineIterative<TypeParam>>()({}),
1088             Hash<CombineVariadic<TypeParam>>()({}));
1089 }
1090 
1091 REGISTER_TYPED_TEST_SUITE_P(HashIntTest, BasicUsage);
1092 using IntTypes = testing::Types<unsigned char, char, int, int32_t, int64_t,
1093                                 uint32_t, uint64_t, size_t>;
1094 INSTANTIATE_TYPED_TEST_SUITE_P(My, HashIntTest, IntTypes);
1095 
1096 struct StructWithPadding {
1097   char c;
1098   int i;
1099 
1100   template <typename H>
AbslHashValue(H hash_state,const StructWithPadding & s)1101   friend H AbslHashValue(H hash_state, const StructWithPadding& s) {
1102     return H::combine(std::move(hash_state), s.c, s.i);
1103   }
1104 };
1105 
1106 static_assert(sizeof(StructWithPadding) > sizeof(char) + sizeof(int),
1107               "StructWithPadding doesn't have padding");
1108 static_assert(std::is_standard_layout<StructWithPadding>::value, "");
1109 
1110 // This check has to be disabled because libstdc++ doesn't support it.
1111 // static_assert(std::is_trivially_constructible<StructWithPadding>::value, "");
1112 
1113 template <typename T>
1114 struct ArraySlice {
1115   T* begin;
1116   T* end;
1117 
1118   template <typename H>
AbslHashValue(H hash_state,const ArraySlice & slice)1119   friend H AbslHashValue(H hash_state, const ArraySlice& slice) {
1120     for (auto t = slice.begin; t != slice.end; ++t) {
1121       hash_state = H::combine(std::move(hash_state), *t);
1122     }
1123     return hash_state;
1124   }
1125 };
1126 
TEST(HashTest,HashNonUniquelyRepresentedType)1127 TEST(HashTest, HashNonUniquelyRepresentedType) {
1128   // Create equal StructWithPadding objects that are known to have non-equal
1129   // padding bytes.
1130   static const size_t kNumStructs = 10;
1131   unsigned char buffer1[kNumStructs * sizeof(StructWithPadding)];
1132   std::memset(buffer1, 0, sizeof(buffer1));
1133   auto* s1 = reinterpret_cast<StructWithPadding*>(buffer1);
1134 
1135   unsigned char buffer2[kNumStructs * sizeof(StructWithPadding)];
1136   std::memset(buffer2, 255, sizeof(buffer2));
1137   auto* s2 = reinterpret_cast<StructWithPadding*>(buffer2);
1138   for (size_t i = 0; i < kNumStructs; ++i) {
1139     SCOPED_TRACE(i);
1140     s1[i].c = s2[i].c = static_cast<char>('0' + i);
1141     s1[i].i = s2[i].i = static_cast<int>(i);
1142     ASSERT_FALSE(memcmp(buffer1 + i * sizeof(StructWithPadding),
1143                         buffer2 + i * sizeof(StructWithPadding),
1144                         sizeof(StructWithPadding)) == 0)
1145         << "Bug in test code: objects do not have unequal"
1146         << " object representations";
1147   }
1148 
1149   EXPECT_EQ(Hash<StructWithPadding>()(s1[0]), Hash<StructWithPadding>()(s2[0]));
1150   EXPECT_EQ(Hash<ArraySlice<StructWithPadding>>()({s1, s1 + kNumStructs}),
1151             Hash<ArraySlice<StructWithPadding>>()({s2, s2 + kNumStructs}));
1152 }
1153 
TEST(HashTest,StandardHashContainerUsage)1154 TEST(HashTest, StandardHashContainerUsage) {
1155   std::unordered_map<int, std::string, Hash<int>> map = {{0, "foo"},
1156                                                          {42, "bar"}};
1157 
1158   EXPECT_NE(map.find(0), map.end());
1159   EXPECT_EQ(map.find(1), map.end());
1160   EXPECT_NE(map.find(0u), map.end());
1161 }
1162 
1163 struct ConvertibleFromNoOp {
ConvertibleFromNoOp__anon061a8a600411::ConvertibleFromNoOp1164   ConvertibleFromNoOp(NoOp) {}  // NOLINT(runtime/explicit)
1165 
1166   template <typename H>
AbslHashValue(H hash_state,ConvertibleFromNoOp)1167   friend H AbslHashValue(H hash_state, ConvertibleFromNoOp) {
1168     return H::combine(std::move(hash_state), 1);
1169   }
1170 };
1171 
TEST(HashTest,HeterogeneousCall)1172 TEST(HashTest, HeterogeneousCall) {
1173   EXPECT_NE(Hash<ConvertibleFromNoOp>()(NoOp()),
1174             Hash<NoOp>()(NoOp()));
1175 }
1176 
TEST(IsUniquelyRepresentedTest,SanityTest)1177 TEST(IsUniquelyRepresentedTest, SanityTest) {
1178   using absl::hash_internal::is_uniquely_represented;
1179 
1180   EXPECT_TRUE(is_uniquely_represented<unsigned char>::value);
1181   EXPECT_TRUE(is_uniquely_represented<int>::value);
1182   EXPECT_FALSE(is_uniquely_represented<bool>::value);
1183   EXPECT_FALSE(is_uniquely_represented<int*>::value);
1184 }
1185 
1186 struct IntAndString {
1187   int i;
1188   std::string s;
1189 
1190   template <typename H>
AbslHashValue(H hash_state,IntAndString int_and_string)1191   friend H AbslHashValue(H hash_state, IntAndString int_and_string) {
1192     return H::combine(std::move(hash_state), int_and_string.s,
1193                       int_and_string.i);
1194   }
1195 };
1196 
TEST(HashTest,SmallValueOn64ByteBoundary)1197 TEST(HashTest, SmallValueOn64ByteBoundary) {
1198   Hash<IntAndString>()(IntAndString{0, std::string(63, '0')});
1199 }
1200 
TEST(HashTest,TypeErased)1201 TEST(HashTest, TypeErased) {
1202   EXPECT_TRUE((is_hashable<TypeErasedValue<size_t>>::value));
1203   EXPECT_TRUE((is_hashable<std::pair<TypeErasedValue<size_t>, int>>::value));
1204 
1205   EXPECT_EQ(SpyHash(TypeErasedValue<size_t>(7)), SpyHash(size_t{7}));
1206   EXPECT_NE(SpyHash(TypeErasedValue<size_t>(7)), SpyHash(size_t{13}));
1207 
1208   EXPECT_EQ(SpyHash(std::make_pair(TypeErasedValue<size_t>(7), 17)),
1209             SpyHash(std::make_pair(size_t{7}, 17)));
1210 
1211   absl::flat_hash_set<absl::flat_hash_set<int>> ss = {{1, 2}, {3, 4}};
1212   TypeErasedContainer<absl::flat_hash_set<absl::flat_hash_set<int>>> es = {
1213       absl::flat_hash_set<int>{1, 2}, {3, 4}};
1214   absl::flat_hash_set<TypeErasedContainer<absl::flat_hash_set<int>>> se = {
1215       {1, 2}, {3, 4}};
1216   EXPECT_EQ(SpyHash(ss), SpyHash(es));
1217   EXPECT_EQ(SpyHash(ss), SpyHash(se));
1218 }
1219 
1220 struct ValueWithBoolConversion {
operator bool__anon061a8a600411::ValueWithBoolConversion1221   operator bool() const { return false; }
1222   int i;
1223 };
1224 
1225 }  // namespace
1226 namespace std {
1227 template <>
1228 struct hash<ValueWithBoolConversion> {
operator ()std::hash1229   size_t operator()(ValueWithBoolConversion v) {
1230     return static_cast<size_t>(v.i);
1231   }
1232 };
1233 }  // namespace std
1234 
1235 namespace {
1236 
TEST(HashTest,DoesNotUseImplicitConversionsToBool)1237 TEST(HashTest, DoesNotUseImplicitConversionsToBool) {
1238   EXPECT_NE(absl::Hash<ValueWithBoolConversion>()(ValueWithBoolConversion{0}),
1239             absl::Hash<ValueWithBoolConversion>()(ValueWithBoolConversion{1}));
1240 }
1241 
TEST(HashOf,MatchesHashForSingleArgument)1242 TEST(HashOf, MatchesHashForSingleArgument) {
1243   std::string s = "forty two";
1244   int i = 42;
1245   double d = 42.0;
1246   std::tuple<int, int> t{4, 2};
1247 
1248   EXPECT_EQ(absl::HashOf(s), absl::Hash<std::string>{}(s));
1249   EXPECT_EQ(absl::HashOf(i), absl::Hash<int>{}(i));
1250   EXPECT_EQ(absl::HashOf(d), absl::Hash<double>{}(d));
1251   EXPECT_EQ(absl::HashOf(t), (absl::Hash<std::tuple<int, int>>{}(t)));
1252 }
1253 
TEST(HashOf,MatchesHashOfTupleForMultipleArguments)1254 TEST(HashOf, MatchesHashOfTupleForMultipleArguments) {
1255   std::string hello = "hello";
1256   std::string world = "world";
1257 
1258   EXPECT_EQ(absl::HashOf(), absl::HashOf(std::make_tuple()));
1259   EXPECT_EQ(absl::HashOf(hello), absl::HashOf(std::make_tuple(hello)));
1260   EXPECT_EQ(absl::HashOf(hello, world),
1261             absl::HashOf(std::make_tuple(hello, world)));
1262 }
1263 
1264 template <typename T>
1265 std::true_type HashOfExplicitParameter(decltype(absl::HashOf<T>(0))) {
1266   return {};
1267 }
1268 template <typename T>
HashOfExplicitParameter(size_t)1269 std::false_type HashOfExplicitParameter(size_t) {
1270   return {};
1271 }
1272 
TEST(HashOf,CantPassExplicitTemplateParameters)1273 TEST(HashOf, CantPassExplicitTemplateParameters) {
1274   EXPECT_FALSE(HashOfExplicitParameter<int>(0));
1275 }
1276 
1277 }  // namespace
1278