hash_test.cc 35 KB

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