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 <array>
18 #include <bitset>
19 #include <cstring>
20 #include <deque>
21 #include <forward_list>
22 #include <functional>
23 #include <iterator>
24 #include <limits>
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <numeric>
29 #include <random>
30 #include <set>
31 #include <string>
32 #include <tuple>
33 #include <type_traits>
34 #include <unordered_map>
35 #include <utility>
36 #include <vector>
37
38 #include "gmock/gmock.h"
39 #include "gtest/gtest.h"
40 #include "absl/container/flat_hash_set.h"
41 #include "absl/hash/hash_testing.h"
42 #include "absl/hash/internal/spy_hash_state.h"
43 #include "absl/meta/type_traits.h"
44 #include "absl/numeric/int128.h"
45 #include "absl/strings/cord_test_helpers.h"
46
47 namespace {
48
49 using absl::Hash;
50 using absl::hash_internal::SpyHashState;
51
52 template <typename T>
53 class HashValueIntTest : public testing::Test {
54 };
55 TYPED_TEST_SUITE_P(HashValueIntTest);
56
57 template <typename T>
SpyHash(const T & value)58 SpyHashState SpyHash(const T& value) {
59 return SpyHashState::combine(SpyHashState(), value);
60 }
61
62 // Helper trait to verify if T is hashable. We use absl::Hash's poison status to
63 // detect it.
64 template <typename T>
65 using is_hashable = std::is_default_constructible<absl::Hash<T>>;
66
TYPED_TEST_P(HashValueIntTest,BasicUsage)67 TYPED_TEST_P(HashValueIntTest, BasicUsage) {
68 EXPECT_TRUE((is_hashable<TypeParam>::value));
69
70 TypeParam n = 42;
71 EXPECT_EQ(SpyHash(n), SpyHash(TypeParam{42}));
72 EXPECT_NE(SpyHash(n), SpyHash(TypeParam{0}));
73 EXPECT_NE(SpyHash(std::numeric_limits<TypeParam>::max()),
74 SpyHash(std::numeric_limits<TypeParam>::min()));
75 }
76
TYPED_TEST_P(HashValueIntTest,FastPath)77 TYPED_TEST_P(HashValueIntTest, FastPath) {
78 // Test the fast-path to make sure the values are the same.
79 TypeParam n = 42;
80 EXPECT_EQ(absl::Hash<TypeParam>{}(n),
81 absl::Hash<std::tuple<TypeParam>>{}(std::tuple<TypeParam>(n)));
82 }
83
84 REGISTER_TYPED_TEST_CASE_P(HashValueIntTest, BasicUsage, FastPath);
85 using IntTypes = testing::Types<unsigned char, char, int, int32_t, int64_t,
86 uint32_t, uint64_t, size_t>;
87 INSTANTIATE_TYPED_TEST_CASE_P(My, HashValueIntTest, IntTypes);
88
89 enum LegacyEnum { kValue1, kValue2, kValue3 };
90
91 enum class EnumClass { kValue4, kValue5, kValue6 };
92
TEST(HashValueTest,EnumAndBool)93 TEST(HashValueTest, EnumAndBool) {
94 EXPECT_TRUE((is_hashable<LegacyEnum>::value));
95 EXPECT_TRUE((is_hashable<EnumClass>::value));
96 EXPECT_TRUE((is_hashable<bool>::value));
97
98 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
99 LegacyEnum::kValue1, LegacyEnum::kValue2, LegacyEnum::kValue3)));
100 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
101 EnumClass::kValue4, EnumClass::kValue5, EnumClass::kValue6)));
102 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
103 std::make_tuple(true, false)));
104 }
105
TEST(HashValueTest,FloatingPoint)106 TEST(HashValueTest, FloatingPoint) {
107 EXPECT_TRUE((is_hashable<float>::value));
108 EXPECT_TRUE((is_hashable<double>::value));
109 EXPECT_TRUE((is_hashable<long double>::value));
110
111 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
112 std::make_tuple(42.f, 0.f, -0.f, std::numeric_limits<float>::infinity(),
113 -std::numeric_limits<float>::infinity())));
114
115 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
116 std::make_tuple(42., 0., -0., std::numeric_limits<double>::infinity(),
117 -std::numeric_limits<double>::infinity())));
118
119 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
120 // Add some values with small exponent to test that NORMAL values also
121 // append their category.
122 .5L, 1.L, 2.L, 4.L, 42.L, 0.L, -0.L,
123 17 * static_cast<long double>(std::numeric_limits<double>::max()),
124 std::numeric_limits<long double>::infinity(),
125 -std::numeric_limits<long double>::infinity())));
126 }
127
TEST(HashValueTest,Pointer)128 TEST(HashValueTest, Pointer) {
129 EXPECT_TRUE((is_hashable<int*>::value));
130
131 int i;
132 int* ptr = &i;
133 int* n = nullptr;
134
135 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
136 std::make_tuple(&i, ptr, nullptr, ptr + 1, n)));
137 }
138
TEST(HashValueTest,PointerAlignment)139 TEST(HashValueTest, PointerAlignment) {
140 // We want to make sure that pointer alignment will not cause bits to be
141 // stuck.
142
143 constexpr size_t kTotalSize = 1 << 20;
144 std::unique_ptr<char[]> data(new char[kTotalSize]);
145 constexpr size_t kLog2NumValues = 5;
146 constexpr size_t kNumValues = 1 << kLog2NumValues;
147
148 for (size_t align = 1; align < kTotalSize / kNumValues;
149 align < 8 ? align += 1 : align < 1024 ? align += 8 : align += 32) {
150 SCOPED_TRACE(align);
151 ASSERT_LE(align * kNumValues, kTotalSize);
152
153 size_t bits_or = 0;
154 size_t bits_and = ~size_t{};
155
156 for (size_t i = 0; i < kNumValues; ++i) {
157 size_t hash = absl::Hash<void*>()(data.get() + i * align);
158 bits_or |= hash;
159 bits_and &= hash;
160 }
161
162 // Limit the scope to the bits we would be using for Swisstable.
163 constexpr size_t kMask = (1 << (kLog2NumValues + 7)) - 1;
164 size_t stuck_bits = (~bits_or | bits_and) & kMask;
165 EXPECT_EQ(stuck_bits, 0) << "0x" << std::hex << stuck_bits;
166 }
167 }
168
TEST(HashValueTest,PairAndTuple)169 TEST(HashValueTest, PairAndTuple) {
170 EXPECT_TRUE((is_hashable<std::pair<int, int>>::value));
171 EXPECT_TRUE((is_hashable<std::pair<const int&, const int&>>::value));
172 EXPECT_TRUE((is_hashable<std::tuple<int&, int&>>::value));
173 EXPECT_TRUE((is_hashable<std::tuple<int&&, int&&>>::value));
174
175 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
176 std::make_pair(0, 42), std::make_pair(0, 42), std::make_pair(42, 0),
177 std::make_pair(0, 0), std::make_pair(42, 42), std::make_pair(1, 42))));
178
179 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
180 std::make_tuple(std::make_tuple(0, 0, 0), std::make_tuple(0, 0, 42),
181 std::make_tuple(0, 23, 0), std::make_tuple(17, 0, 0),
182 std::make_tuple(42, 0, 0), std::make_tuple(3, 9, 9),
183 std::make_tuple(0, 0, -42))));
184
185 // Test that tuples of lvalue references work (so we need a few lvalues):
186 int a = 0, b = 1, c = 17, d = 23;
187 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
188 std::tie(a, a), std::tie(a, b), std::tie(b, c), std::tie(c, d))));
189
190 // Test that tuples of rvalue references work:
191 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
192 std::forward_as_tuple(0, 0, 0), std::forward_as_tuple(0, 0, 42),
193 std::forward_as_tuple(0, 23, 0), std::forward_as_tuple(17, 0, 0),
194 std::forward_as_tuple(42, 0, 0), std::forward_as_tuple(3, 9, 9),
195 std::forward_as_tuple(0, 0, -42))));
196 }
197
TEST(HashValueTest,CombineContiguousWorks)198 TEST(HashValueTest, CombineContiguousWorks) {
199 std::vector<std::tuple<int>> v1 = {std::make_tuple(1), std::make_tuple(3)};
200 std::vector<std::tuple<int>> v2 = {std::make_tuple(1), std::make_tuple(2)};
201
202 auto vh1 = SpyHash(v1);
203 auto vh2 = SpyHash(v2);
204 EXPECT_NE(vh1, vh2);
205 }
206
207 struct DummyDeleter {
208 template <typename T>
operator ()__anon611dec080111::DummyDeleter209 void operator() (T* ptr) {}
210 };
211
212 struct SmartPointerEq {
213 template <typename T, typename U>
operator ()__anon611dec080111::SmartPointerEq214 bool operator()(const T& t, const U& u) const {
215 return GetPtr(t) == GetPtr(u);
216 }
217
218 template <typename T>
GetPtr__anon611dec080111::SmartPointerEq219 static auto GetPtr(const T& t) -> decltype(&*t) {
220 return t ? &*t : nullptr;
221 }
222
GetPtr__anon611dec080111::SmartPointerEq223 static std::nullptr_t GetPtr(std::nullptr_t) { return nullptr; }
224 };
225
TEST(HashValueTest,SmartPointers)226 TEST(HashValueTest, SmartPointers) {
227 EXPECT_TRUE((is_hashable<std::unique_ptr<int>>::value));
228 EXPECT_TRUE((is_hashable<std::unique_ptr<int, DummyDeleter>>::value));
229 EXPECT_TRUE((is_hashable<std::shared_ptr<int>>::value));
230
231 int i, j;
232 std::unique_ptr<int, DummyDeleter> unique1(&i);
233 std::unique_ptr<int, DummyDeleter> unique2(&i);
234 std::unique_ptr<int, DummyDeleter> unique_other(&j);
235 std::unique_ptr<int, DummyDeleter> unique_null;
236
237 std::shared_ptr<int> shared1(&i, DummyDeleter());
238 std::shared_ptr<int> shared2(&i, DummyDeleter());
239 std::shared_ptr<int> shared_other(&j, DummyDeleter());
240 std::shared_ptr<int> shared_null;
241
242 // Sanity check of the Eq function.
243 ASSERT_TRUE(SmartPointerEq{}(unique1, shared1));
244 ASSERT_FALSE(SmartPointerEq{}(unique1, shared_other));
245 ASSERT_TRUE(SmartPointerEq{}(unique_null, nullptr));
246 ASSERT_FALSE(SmartPointerEq{}(shared2, nullptr));
247
248 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
249 std::forward_as_tuple(&i, nullptr, //
250 unique1, unique2, unique_null, //
251 absl::make_unique<int>(), //
252 shared1, shared2, shared_null, //
253 std::make_shared<int>()),
254 SmartPointerEq{}));
255 }
256
TEST(HashValueTest,FunctionPointer)257 TEST(HashValueTest, FunctionPointer) {
258 using Func = int (*)();
259 EXPECT_TRUE(is_hashable<Func>::value);
260
261 Func p1 = [] { return 2; }, p2 = [] { return 1; };
262 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
263 std::make_tuple(p1, p2, nullptr)));
264 }
265
266 struct WrapInTuple {
267 template <typename T>
operator ()__anon611dec080111::WrapInTuple268 std::tuple<int, T, size_t> operator()(const T& t) const {
269 return std::make_tuple(7, t, 0xdeadbeef);
270 }
271 };
272
FlatCord(absl::string_view sv)273 absl::Cord FlatCord(absl::string_view sv) {
274 absl::Cord c(sv);
275 c.Flatten();
276 return c;
277 }
278
FragmentedCord(absl::string_view sv)279 absl::Cord FragmentedCord(absl::string_view sv) {
280 if (sv.size() < 2) {
281 return absl::Cord(sv);
282 }
283 size_t halfway = sv.size() / 2;
284 std::vector<absl::string_view> parts = {sv.substr(0, halfway),
285 sv.substr(halfway)};
286 return absl::MakeFragmentedCord(parts);
287 }
288
TEST(HashValueTest,Strings)289 TEST(HashValueTest, Strings) {
290 EXPECT_TRUE((is_hashable<std::string>::value));
291
292 const std::string small = "foo";
293 const std::string dup = "foofoo";
294 const std::string large = std::string(2048, 'x'); // multiple of chunk size
295 const std::string huge = std::string(5000, 'a'); // not a multiple
296
297 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( //
298 std::string(), absl::string_view(), absl::Cord(), //
299 std::string(""), absl::string_view(""), absl::Cord(""), //
300 std::string(small), absl::string_view(small), absl::Cord(small), //
301 std::string(dup), absl::string_view(dup), absl::Cord(dup), //
302 std::string(large), absl::string_view(large), absl::Cord(large), //
303 std::string(huge), absl::string_view(huge), FlatCord(huge), //
304 FragmentedCord(huge))));
305
306 // Also check that nested types maintain the same hash.
307 const WrapInTuple t{};
308 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( //
309 t(std::string()), t(absl::string_view()), t(absl::Cord()), //
310 t(std::string("")), t(absl::string_view("")), t(absl::Cord("")), //
311 t(std::string(small)), t(absl::string_view(small)), //
312 t(absl::Cord(small)), //
313 t(std::string(dup)), t(absl::string_view(dup)), t(absl::Cord(dup)), //
314 t(std::string(large)), t(absl::string_view(large)), //
315 t(absl::Cord(large)), //
316 t(std::string(huge)), t(absl::string_view(huge)), //
317 t(FlatCord(huge)), t(FragmentedCord(huge)))));
318
319 // Make sure that hashing a `const char*` does not use its string-value.
320 EXPECT_NE(SpyHash(static_cast<const char*>("ABC")),
321 SpyHash(absl::string_view("ABC")));
322 }
323
TEST(HashValueTest,WString)324 TEST(HashValueTest, WString) {
325 EXPECT_TRUE((is_hashable<std::wstring>::value));
326
327 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
328 std::wstring(), std::wstring(L"ABC"), std::wstring(L"ABC"),
329 std::wstring(L"Some other different string"),
330 std::wstring(L"Iñtërnâtiônàlizætiøn"))));
331 }
332
TEST(HashValueTest,U16String)333 TEST(HashValueTest, U16String) {
334 EXPECT_TRUE((is_hashable<std::u16string>::value));
335
336 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
337 std::u16string(), std::u16string(u"ABC"), std::u16string(u"ABC"),
338 std::u16string(u"Some other different string"),
339 std::u16string(u"Iñtërnâtiônàlizætiøn"))));
340 }
341
TEST(HashValueTest,U32String)342 TEST(HashValueTest, U32String) {
343 EXPECT_TRUE((is_hashable<std::u32string>::value));
344
345 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
346 std::u32string(), std::u32string(U"ABC"), std::u32string(U"ABC"),
347 std::u32string(U"Some other different string"),
348 std::u32string(U"Iñtërnâtiônàlizætiøn"))));
349 }
350
TEST(HashValueTest,StdArray)351 TEST(HashValueTest, StdArray) {
352 EXPECT_TRUE((is_hashable<std::array<int, 3>>::value));
353
354 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
355 std::make_tuple(std::array<int, 3>{}, std::array<int, 3>{{0, 23, 42}})));
356 }
357
TEST(HashValueTest,StdBitset)358 TEST(HashValueTest, StdBitset) {
359 EXPECT_TRUE((is_hashable<std::bitset<257>>::value));
360
361 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
362 {std::bitset<2>("00"), std::bitset<2>("01"), std::bitset<2>("10"),
363 std::bitset<2>("11")}));
364 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
365 {std::bitset<5>("10101"), std::bitset<5>("10001"), std::bitset<5>()}));
366
367 constexpr int kNumBits = 256;
368 std::array<std::string, 6> bit_strings;
369 bit_strings.fill(std::string(kNumBits, '1'));
370 bit_strings[1][0] = '0';
371 bit_strings[2][1] = '0';
372 bit_strings[3][kNumBits / 3] = '0';
373 bit_strings[4][kNumBits - 2] = '0';
374 bit_strings[5][kNumBits - 1] = '0';
375 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
376 {std::bitset<kNumBits>(bit_strings[0].c_str()),
377 std::bitset<kNumBits>(bit_strings[1].c_str()),
378 std::bitset<kNumBits>(bit_strings[2].c_str()),
379 std::bitset<kNumBits>(bit_strings[3].c_str()),
380 std::bitset<kNumBits>(bit_strings[4].c_str()),
381 std::bitset<kNumBits>(bit_strings[5].c_str())}));
382 } // namespace
383
384 template <typename T>
385 class HashValueSequenceTest : public testing::Test {
386 };
387 TYPED_TEST_SUITE_P(HashValueSequenceTest);
388
TYPED_TEST_P(HashValueSequenceTest,BasicUsage)389 TYPED_TEST_P(HashValueSequenceTest, BasicUsage) {
390 EXPECT_TRUE((is_hashable<TypeParam>::value));
391
392 using ValueType = typename TypeParam::value_type;
393 auto a = static_cast<ValueType>(0);
394 auto b = static_cast<ValueType>(23);
395 auto c = static_cast<ValueType>(42);
396
397 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
398 std::make_tuple(TypeParam(), TypeParam{}, TypeParam{a, b, c},
399 TypeParam{a, b}, TypeParam{b, c})));
400 }
401
402 REGISTER_TYPED_TEST_CASE_P(HashValueSequenceTest, BasicUsage);
403 using IntSequenceTypes =
404 testing::Types<std::deque<int>, std::forward_list<int>, std::list<int>,
405 std::vector<int>, std::vector<bool>, std::set<int>,
406 std::multiset<int>>;
407 INSTANTIATE_TYPED_TEST_CASE_P(My, HashValueSequenceTest, IntSequenceTypes);
408
409 // Private type that only supports AbslHashValue to make sure our chosen hash
410 // implementation is recursive within absl::Hash.
411 // It uses std::abs() on the value to provide different bitwise representations
412 // of the same logical value.
413 struct Private {
414 int i;
415 template <typename H>
AbslHashValue(H h,Private p)416 friend H AbslHashValue(H h, Private p) {
417 return H::combine(std::move(h), std::abs(p.i));
418 }
419
operator ==(Private a,Private b)420 friend bool operator==(Private a, Private b) {
421 return std::abs(a.i) == std::abs(b.i);
422 }
423
operator <<(std::ostream & o,Private p)424 friend std::ostream& operator<<(std::ostream& o, Private p) {
425 return o << p.i;
426 }
427 };
428
429 // Test helper for combine_piecewise_buffer. It holds a string_view to the
430 // buffer-to-be-hashed. Its AbslHashValue specialization will split up its
431 // contents at the character offsets requested.
432 class PiecewiseHashTester {
433 public:
434 // Create a hash view of a buffer to be hashed contiguously.
PiecewiseHashTester(absl::string_view buf)435 explicit PiecewiseHashTester(absl::string_view buf)
436 : buf_(buf), piecewise_(false), split_locations_() {}
437
438 // Create a hash view of a buffer to be hashed piecewise, with breaks at the
439 // given locations.
PiecewiseHashTester(absl::string_view buf,std::set<size_t> split_locations)440 PiecewiseHashTester(absl::string_view buf, std::set<size_t> split_locations)
441 : buf_(buf),
442 piecewise_(true),
443 split_locations_(std::move(split_locations)) {}
444
445 template <typename H>
AbslHashValue(H h,const PiecewiseHashTester & p)446 friend H AbslHashValue(H h, const PiecewiseHashTester& p) {
447 if (!p.piecewise_) {
448 return H::combine_contiguous(std::move(h), p.buf_.data(), p.buf_.size());
449 }
450 absl::hash_internal::PiecewiseCombiner combiner;
451 if (p.split_locations_.empty()) {
452 h = combiner.add_buffer(std::move(h), p.buf_.data(), p.buf_.size());
453 return combiner.finalize(std::move(h));
454 }
455 size_t begin = 0;
456 for (size_t next : p.split_locations_) {
457 absl::string_view chunk = p.buf_.substr(begin, next - begin);
458 h = combiner.add_buffer(std::move(h), chunk.data(), chunk.size());
459 begin = next;
460 }
461 absl::string_view last_chunk = p.buf_.substr(begin);
462 if (!last_chunk.empty()) {
463 h = combiner.add_buffer(std::move(h), last_chunk.data(),
464 last_chunk.size());
465 }
466 return combiner.finalize(std::move(h));
467 }
468
469 private:
470 absl::string_view buf_;
471 bool piecewise_;
472 std::set<size_t> split_locations_;
473 };
474
475 // Dummy object that hashes as two distinct contiguous buffers, "foo" followed
476 // by "bar"
477 struct DummyFooBar {
478 template <typename H>
AbslHashValue(H h,const DummyFooBar &)479 friend H AbslHashValue(H h, const DummyFooBar&) {
480 const char* foo = "foo";
481 const char* bar = "bar";
482 h = H::combine_contiguous(std::move(h), foo, 3);
483 h = H::combine_contiguous(std::move(h), bar, 3);
484 return h;
485 }
486 };
487
TEST(HashValueTest,CombinePiecewiseBuffer)488 TEST(HashValueTest, CombinePiecewiseBuffer) {
489 absl::Hash<PiecewiseHashTester> hash;
490
491 // Check that hashing an empty buffer through the piecewise API works.
492 EXPECT_EQ(hash(PiecewiseHashTester("")), hash(PiecewiseHashTester("", {})));
493
494 // Similarly, small buffers should give consistent results
495 EXPECT_EQ(hash(PiecewiseHashTester("foobar")),
496 hash(PiecewiseHashTester("foobar", {})));
497 EXPECT_EQ(hash(PiecewiseHashTester("foobar")),
498 hash(PiecewiseHashTester("foobar", {3})));
499
500 // But hashing "foobar" in pieces gives a different answer than hashing "foo"
501 // contiguously, then "bar" contiguously.
502 EXPECT_NE(hash(PiecewiseHashTester("foobar", {3})),
503 absl::Hash<DummyFooBar>()(DummyFooBar{}));
504
505 // Test hashing a large buffer incrementally, broken up in several different
506 // ways. Arrange for breaks on and near the stride boundaries to look for
507 // off-by-one errors in the implementation.
508 //
509 // This test is run on a buffer that is a multiple of the stride size, and one
510 // that isn't.
511 for (size_t big_buffer_size : {1024 * 2 + 512, 1024 * 3}) {
512 SCOPED_TRACE(big_buffer_size);
513 std::string big_buffer;
514 for (int i = 0; i < big_buffer_size; ++i) {
515 // Arbitrary string
516 big_buffer.push_back(32 + (i * (i / 3)) % 64);
517 }
518 auto big_buffer_hash = hash(PiecewiseHashTester(big_buffer));
519
520 const int possible_breaks = 9;
521 size_t breaks[possible_breaks] = {1, 512, 1023, 1024, 1025,
522 1536, 2047, 2048, 2049};
523 for (unsigned test_mask = 0; test_mask < (1u << possible_breaks);
524 ++test_mask) {
525 SCOPED_TRACE(test_mask);
526 std::set<size_t> break_locations;
527 for (int j = 0; j < possible_breaks; ++j) {
528 if (test_mask & (1u << j)) {
529 break_locations.insert(breaks[j]);
530 }
531 }
532 EXPECT_EQ(
533 hash(PiecewiseHashTester(big_buffer, std::move(break_locations))),
534 big_buffer_hash);
535 }
536 }
537 }
538
TEST(HashValueTest,PrivateSanity)539 TEST(HashValueTest, PrivateSanity) {
540 // Sanity check that Private is working as the tests below expect it to work.
541 EXPECT_TRUE(is_hashable<Private>::value);
542 EXPECT_NE(SpyHash(Private{0}), SpyHash(Private{1}));
543 EXPECT_EQ(SpyHash(Private{1}), SpyHash(Private{1}));
544 }
545
TEST(HashValueTest,Optional)546 TEST(HashValueTest, Optional) {
547 EXPECT_TRUE(is_hashable<absl::optional<Private>>::value);
548
549 using O = absl::optional<Private>;
550 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
551 std::make_tuple(O{}, O{{1}}, O{{-1}}, O{{10}})));
552 }
553
TEST(HashValueTest,Variant)554 TEST(HashValueTest, Variant) {
555 using V = absl::variant<Private, std::string>;
556 EXPECT_TRUE(is_hashable<V>::value);
557
558 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
559 V(Private{1}), V(Private{-1}), V(Private{2}), V("ABC"), V("BCD"))));
560
561 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
562 struct S {};
563 EXPECT_FALSE(is_hashable<absl::variant<S>>::value);
564 #endif
565 }
566
TEST(HashValueTest,Maps)567 TEST(HashValueTest, Maps) {
568 EXPECT_TRUE((is_hashable<std::map<int, std::string>>::value));
569
570 using M = std::map<int, std::string>;
571 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
572 M{}, M{{0, "foo"}}, M{{1, "foo"}}, M{{0, "bar"}}, M{{1, "bar"}},
573 M{{0, "foo"}, {42, "bar"}}, M{{1, "foo"}, {42, "bar"}},
574 M{{1, "foo"}, {43, "bar"}}, M{{1, "foo"}, {43, "baz"}})));
575
576 using MM = std::multimap<int, std::string>;
577 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
578 MM{}, MM{{0, "foo"}}, MM{{1, "foo"}}, MM{{0, "bar"}}, MM{{1, "bar"}},
579 MM{{0, "foo"}, {0, "bar"}}, MM{{0, "bar"}, {0, "foo"}},
580 MM{{0, "foo"}, {42, "bar"}}, MM{{1, "foo"}, {42, "bar"}},
581 MM{{1, "foo"}, {1, "foo"}, {43, "bar"}}, MM{{1, "foo"}, {43, "baz"}})));
582 }
583
TEST(HashValueTest,ReferenceWrapper)584 TEST(HashValueTest, ReferenceWrapper) {
585 EXPECT_TRUE(is_hashable<std::reference_wrapper<Private>>::value);
586
587 Private p1{1}, p10{10};
588 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
589 p1, p10, std::ref(p1), std::ref(p10), std::cref(p1), std::cref(p10))));
590
591 EXPECT_TRUE(is_hashable<std::reference_wrapper<int>>::value);
592 int one = 1, ten = 10;
593 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
594 one, ten, std::ref(one), std::ref(ten), std::cref(one), std::cref(ten))));
595
596 EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
597 std::make_tuple(std::tuple<std::reference_wrapper<int>>(std::ref(one)),
598 std::tuple<std::reference_wrapper<int>>(std::ref(ten)),
599 std::tuple<int>(one), std::tuple<int>(ten))));
600 }
601
602 template <typename T, typename = void>
603 struct IsHashCallable : std::false_type {};
604
605 template <typename T>
606 struct IsHashCallable<T, absl::void_t<decltype(std::declval<absl::Hash<T>>()(
607 std::declval<const T&>()))>> : std::true_type {};
608
609 template <typename T, typename = void>
610 struct IsAggregateInitializable : std::false_type {};
611
612 template <typename T>
613 struct IsAggregateInitializable<T, absl::void_t<decltype(T{})>>
614 : std::true_type {};
615
TEST(IsHashableTest,ValidHash)616 TEST(IsHashableTest, ValidHash) {
617 EXPECT_TRUE((is_hashable<int>::value));
618 EXPECT_TRUE(std::is_default_constructible<absl::Hash<int>>::value);
619 EXPECT_TRUE(std::is_copy_constructible<absl::Hash<int>>::value);
620 EXPECT_TRUE(std::is_move_constructible<absl::Hash<int>>::value);
621 EXPECT_TRUE(absl::is_copy_assignable<absl::Hash<int>>::value);
622 EXPECT_TRUE(absl::is_move_assignable<absl::Hash<int>>::value);
623 EXPECT_TRUE(IsHashCallable<int>::value);
624 EXPECT_TRUE(IsAggregateInitializable<absl::Hash<int>>::value);
625 }
626
627 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
TEST(IsHashableTest,PoisonHash)628 TEST(IsHashableTest, PoisonHash) {
629 struct X {};
630 EXPECT_FALSE((is_hashable<X>::value));
631 EXPECT_FALSE(std::is_default_constructible<absl::Hash<X>>::value);
632 EXPECT_FALSE(std::is_copy_constructible<absl::Hash<X>>::value);
633 EXPECT_FALSE(std::is_move_constructible<absl::Hash<X>>::value);
634 EXPECT_FALSE(absl::is_copy_assignable<absl::Hash<X>>::value);
635 EXPECT_FALSE(absl::is_move_assignable<absl::Hash<X>>::value);
636 EXPECT_FALSE(IsHashCallable<X>::value);
637 #if !defined(__GNUC__) || __GNUC__ < 9
638 // This doesn't compile on GCC 9.
639 EXPECT_FALSE(IsAggregateInitializable<absl::Hash<X>>::value);
640 #endif
641 }
642 #endif // ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
643
644 // Hashable types
645 //
646 // These types exist simply to exercise various AbslHashValue behaviors, so
647 // they are named by what their AbslHashValue overload does.
648 struct NoOp {
649 template <typename HashCode>
AbslHashValue(HashCode h,NoOp n)650 friend HashCode AbslHashValue(HashCode h, NoOp n) {
651 return h;
652 }
653 };
654
655 struct EmptyCombine {
656 template <typename HashCode>
AbslHashValue(HashCode h,EmptyCombine e)657 friend HashCode AbslHashValue(HashCode h, EmptyCombine e) {
658 return HashCode::combine(std::move(h));
659 }
660 };
661
662 template <typename Int>
663 struct CombineIterative {
664 template <typename HashCode>
AbslHashValue(HashCode h,CombineIterative c)665 friend HashCode AbslHashValue(HashCode h, CombineIterative c) {
666 for (int i = 0; i < 5; ++i) {
667 h = HashCode::combine(std::move(h), Int(i));
668 }
669 return h;
670 }
671 };
672
673 template <typename Int>
674 struct CombineVariadic {
675 template <typename HashCode>
AbslHashValue(HashCode h,CombineVariadic c)676 friend HashCode AbslHashValue(HashCode h, CombineVariadic c) {
677 return HashCode::combine(std::move(h), Int(0), Int(1), Int(2), Int(3),
678 Int(4));
679 }
680 };
681 enum class InvokeTag {
682 kUniquelyRepresented,
683 kHashValue,
684 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
685 kLegacyHash,
686 #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
687 kStdHash,
688 kNone
689 };
690
691 template <InvokeTag T>
692 using InvokeTagConstant = std::integral_constant<InvokeTag, T>;
693
694 template <InvokeTag... Tags>
695 struct MinTag;
696
697 template <InvokeTag a, InvokeTag b, InvokeTag... Tags>
698 struct MinTag<a, b, Tags...> : MinTag<(a < b ? a : b), Tags...> {};
699
700 template <InvokeTag a>
701 struct MinTag<a> : InvokeTagConstant<a> {};
702
703 template <InvokeTag... Tags>
704 struct CustomHashType {
CustomHashType__anon611dec080111::CustomHashType705 explicit CustomHashType(size_t val) : value(val) {}
706 size_t value;
707 };
708
709 template <InvokeTag allowed, InvokeTag... tags>
710 struct EnableIfContained
711 : std::enable_if<absl::disjunction<
712 std::integral_constant<bool, allowed == tags>...>::value> {};
713
714 template <
715 typename H, InvokeTag... Tags,
716 typename = typename EnableIfContained<InvokeTag::kHashValue, Tags...>::type>
717 H AbslHashValue(H state, CustomHashType<Tags...> t) {
718 static_assert(MinTag<Tags...>::value == InvokeTag::kHashValue, "");
719 return H::combine(std::move(state),
720 t.value + static_cast<int>(InvokeTag::kHashValue));
721 }
722
723 } // namespace
724
725 namespace absl {
726 ABSL_NAMESPACE_BEGIN
727 namespace hash_internal {
728 template <InvokeTag... Tags>
729 struct is_uniquely_represented<
730 CustomHashType<Tags...>,
731 typename EnableIfContained<InvokeTag::kUniquelyRepresented, Tags...>::type>
732 : std::true_type {};
733 } // namespace hash_internal
734 ABSL_NAMESPACE_END
735 } // namespace absl
736
737 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
738 namespace ABSL_INTERNAL_LEGACY_HASH_NAMESPACE {
739 template <InvokeTag... Tags>
740 struct hash<CustomHashType<Tags...>> {
741 template <InvokeTag... TagsIn, typename = typename EnableIfContained<
742 InvokeTag::kLegacyHash, TagsIn...>::type>
operator ()ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash743 size_t operator()(CustomHashType<TagsIn...> t) const {
744 static_assert(MinTag<Tags...>::value == InvokeTag::kLegacyHash, "");
745 return t.value + static_cast<int>(InvokeTag::kLegacyHash);
746 }
747 };
748 } // namespace ABSL_INTERNAL_LEGACY_HASH_NAMESPACE
749 #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
750
751 namespace std {
752 template <InvokeTag... Tags> // NOLINT
753 struct hash<CustomHashType<Tags...>> {
754 template <InvokeTag... TagsIn, typename = typename EnableIfContained<
755 InvokeTag::kStdHash, TagsIn...>::type>
operator ()std::hash756 size_t operator()(CustomHashType<TagsIn...> t) const {
757 static_assert(MinTag<Tags...>::value == InvokeTag::kStdHash, "");
758 return t.value + static_cast<int>(InvokeTag::kStdHash);
759 }
760 };
761 } // namespace std
762
763 namespace {
764
765 template <typename... T>
TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>,T...)766 void TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>, T...) {
767 using type = CustomHashType<T::value...>;
768 SCOPED_TRACE(testing::PrintToString(std::vector<InvokeTag>{T::value...}));
769 EXPECT_TRUE(is_hashable<type>());
770 EXPECT_TRUE(is_hashable<const type>());
771 EXPECT_TRUE(is_hashable<const type&>());
772
773 const size_t offset = static_cast<int>(std::min({T::value...}));
774 EXPECT_EQ(SpyHash(type(7)), SpyHash(size_t{7 + offset}));
775 }
776
TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>)777 void TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>) {
778 #if ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
779 // is_hashable is false if we don't support any of the hooks.
780 using type = CustomHashType<>;
781 EXPECT_FALSE(is_hashable<type>());
782 EXPECT_FALSE(is_hashable<const type>());
783 EXPECT_FALSE(is_hashable<const type&>());
784 #endif // ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
785 }
786
787 template <InvokeTag Tag, typename... T>
TestCustomHashType(InvokeTagConstant<Tag> tag,T...t)788 void TestCustomHashType(InvokeTagConstant<Tag> tag, T... t) {
789 constexpr auto next = static_cast<InvokeTag>(static_cast<int>(Tag) + 1);
790 TestCustomHashType(InvokeTagConstant<next>(), tag, t...);
791 TestCustomHashType(InvokeTagConstant<next>(), t...);
792 }
793
TEST(HashTest,CustomHashType)794 TEST(HashTest, CustomHashType) {
795 TestCustomHashType(InvokeTagConstant<InvokeTag{}>());
796 }
797
TEST(HashTest,NoOpsAreEquivalent)798 TEST(HashTest, NoOpsAreEquivalent) {
799 EXPECT_EQ(Hash<NoOp>()({}), Hash<NoOp>()({}));
800 EXPECT_EQ(Hash<NoOp>()({}), Hash<EmptyCombine>()({}));
801 }
802
803 template <typename T>
804 class HashIntTest : public testing::Test {
805 };
806 TYPED_TEST_SUITE_P(HashIntTest);
807
TYPED_TEST_P(HashIntTest,BasicUsage)808 TYPED_TEST_P(HashIntTest, BasicUsage) {
809 EXPECT_NE(Hash<NoOp>()({}), Hash<TypeParam>()(0));
810 EXPECT_NE(Hash<NoOp>()({}),
811 Hash<TypeParam>()(std::numeric_limits<TypeParam>::max()));
812 if (std::numeric_limits<TypeParam>::min() != 0) {
813 EXPECT_NE(Hash<NoOp>()({}),
814 Hash<TypeParam>()(std::numeric_limits<TypeParam>::min()));
815 }
816
817 EXPECT_EQ(Hash<CombineIterative<TypeParam>>()({}),
818 Hash<CombineVariadic<TypeParam>>()({}));
819 }
820
821 REGISTER_TYPED_TEST_CASE_P(HashIntTest, BasicUsage);
822 using IntTypes = testing::Types<unsigned char, char, int, int32_t, int64_t,
823 uint32_t, uint64_t, size_t>;
824 INSTANTIATE_TYPED_TEST_CASE_P(My, HashIntTest, IntTypes);
825
826 struct StructWithPadding {
827 char c;
828 int i;
829
830 template <typename H>
AbslHashValue(H hash_state,const StructWithPadding & s)831 friend H AbslHashValue(H hash_state, const StructWithPadding& s) {
832 return H::combine(std::move(hash_state), s.c, s.i);
833 }
834 };
835
836 static_assert(sizeof(StructWithPadding) > sizeof(char) + sizeof(int),
837 "StructWithPadding doesn't have padding");
838 static_assert(std::is_standard_layout<StructWithPadding>::value, "");
839
840 // This check has to be disabled because libstdc++ doesn't support it.
841 // static_assert(std::is_trivially_constructible<StructWithPadding>::value, "");
842
843 template <typename T>
844 struct ArraySlice {
845 T* begin;
846 T* end;
847
848 template <typename H>
AbslHashValue(H hash_state,const ArraySlice & slice)849 friend H AbslHashValue(H hash_state, const ArraySlice& slice) {
850 for (auto t = slice.begin; t != slice.end; ++t) {
851 hash_state = H::combine(std::move(hash_state), *t);
852 }
853 return hash_state;
854 }
855 };
856
TEST(HashTest,HashNonUniquelyRepresentedType)857 TEST(HashTest, HashNonUniquelyRepresentedType) {
858 // Create equal StructWithPadding objects that are known to have non-equal
859 // padding bytes.
860 static const size_t kNumStructs = 10;
861 unsigned char buffer1[kNumStructs * sizeof(StructWithPadding)];
862 std::memset(buffer1, 0, sizeof(buffer1));
863 auto* s1 = reinterpret_cast<StructWithPadding*>(buffer1);
864
865 unsigned char buffer2[kNumStructs * sizeof(StructWithPadding)];
866 std::memset(buffer2, 255, sizeof(buffer2));
867 auto* s2 = reinterpret_cast<StructWithPadding*>(buffer2);
868 for (int i = 0; i < kNumStructs; ++i) {
869 SCOPED_TRACE(i);
870 s1[i].c = s2[i].c = '0' + i;
871 s1[i].i = s2[i].i = i;
872 ASSERT_FALSE(memcmp(buffer1 + i * sizeof(StructWithPadding),
873 buffer2 + i * sizeof(StructWithPadding),
874 sizeof(StructWithPadding)) == 0)
875 << "Bug in test code: objects do not have unequal"
876 << " object representations";
877 }
878
879 EXPECT_EQ(Hash<StructWithPadding>()(s1[0]), Hash<StructWithPadding>()(s2[0]));
880 EXPECT_EQ(Hash<ArraySlice<StructWithPadding>>()({s1, s1 + kNumStructs}),
881 Hash<ArraySlice<StructWithPadding>>()({s2, s2 + kNumStructs}));
882 }
883
TEST(HashTest,StandardHashContainerUsage)884 TEST(HashTest, StandardHashContainerUsage) {
885 std::unordered_map<int, std::string, Hash<int>> map = {{0, "foo"},
886 {42, "bar"}};
887
888 EXPECT_NE(map.find(0), map.end());
889 EXPECT_EQ(map.find(1), map.end());
890 EXPECT_NE(map.find(0u), map.end());
891 }
892
893 struct ConvertibleFromNoOp {
ConvertibleFromNoOp__anon611dec080411::ConvertibleFromNoOp894 ConvertibleFromNoOp(NoOp) {} // NOLINT(runtime/explicit)
895
896 template <typename H>
AbslHashValue(H hash_state,ConvertibleFromNoOp)897 friend H AbslHashValue(H hash_state, ConvertibleFromNoOp) {
898 return H::combine(std::move(hash_state), 1);
899 }
900 };
901
TEST(HashTest,HeterogeneousCall)902 TEST(HashTest, HeterogeneousCall) {
903 EXPECT_NE(Hash<ConvertibleFromNoOp>()(NoOp()),
904 Hash<NoOp>()(NoOp()));
905 }
906
TEST(IsUniquelyRepresentedTest,SanityTest)907 TEST(IsUniquelyRepresentedTest, SanityTest) {
908 using absl::hash_internal::is_uniquely_represented;
909
910 EXPECT_TRUE(is_uniquely_represented<unsigned char>::value);
911 EXPECT_TRUE(is_uniquely_represented<int>::value);
912 EXPECT_FALSE(is_uniquely_represented<bool>::value);
913 EXPECT_FALSE(is_uniquely_represented<int*>::value);
914 }
915
916 struct IntAndString {
917 int i;
918 std::string s;
919
920 template <typename H>
AbslHashValue(H hash_state,IntAndString int_and_string)921 friend H AbslHashValue(H hash_state, IntAndString int_and_string) {
922 return H::combine(std::move(hash_state), int_and_string.s,
923 int_and_string.i);
924 }
925 };
926
TEST(HashTest,SmallValueOn64ByteBoundary)927 TEST(HashTest, SmallValueOn64ByteBoundary) {
928 Hash<IntAndString>()(IntAndString{0, std::string(63, '0')});
929 }
930
931 struct TypeErased {
932 size_t n;
933
934 template <typename H>
AbslHashValue(H hash_state,const TypeErased & v)935 friend H AbslHashValue(H hash_state, const TypeErased& v) {
936 v.HashValue(absl::HashState::Create(&hash_state));
937 return hash_state;
938 }
939
HashValue__anon611dec080411::TypeErased940 void HashValue(absl::HashState state) const {
941 absl::HashState::combine(std::move(state), n);
942 }
943 };
944
TEST(HashTest,TypeErased)945 TEST(HashTest, TypeErased) {
946 EXPECT_TRUE((is_hashable<TypeErased>::value));
947 EXPECT_TRUE((is_hashable<std::pair<TypeErased, int>>::value));
948
949 EXPECT_EQ(SpyHash(TypeErased{7}), SpyHash(size_t{7}));
950 EXPECT_NE(SpyHash(TypeErased{7}), SpyHash(size_t{13}));
951
952 EXPECT_EQ(SpyHash(std::make_pair(TypeErased{7}, 17)),
953 SpyHash(std::make_pair(size_t{7}, 17)));
954 }
955
956 struct ValueWithBoolConversion {
operator bool__anon611dec080411::ValueWithBoolConversion957 operator bool() const { return false; }
958 int i;
959 };
960
961 } // namespace
962 namespace std {
963 template <>
964 struct hash<ValueWithBoolConversion> {
operator ()std::hash965 size_t operator()(ValueWithBoolConversion v) { return v.i; }
966 };
967 } // namespace std
968
969 namespace {
970
TEST(HashTest,DoesNotUseImplicitConversionsToBool)971 TEST(HashTest, DoesNotUseImplicitConversionsToBool) {
972 EXPECT_NE(absl::Hash<ValueWithBoolConversion>()(ValueWithBoolConversion{0}),
973 absl::Hash<ValueWithBoolConversion>()(ValueWithBoolConversion{1}));
974 }
975
976 } // namespace
977