Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/HashingTest.cpp ----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hashing.h unit tests. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Hashing.h" |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 15 | #include "llvm/Support/DataTypes.h" |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 17 | #include <deque> |
| 18 | #include <list> |
| 19 | #include <map> |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | // Helper for test code to print hash codes. |
| 25 | void PrintTo(const hash_code &code, std::ostream *os) { |
| 26 | *os << static_cast<size_t>(code); |
| 27 | } |
| 28 | |
| 29 | // Fake an object that is recognized as hashable data to test super large |
| 30 | // objects. |
| 31 | struct LargeTestInteger { uint64_t arr[8]; }; |
| 32 | |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 33 | struct NonPOD { |
| 34 | uint64_t x, y; |
| 35 | NonPOD(uint64_t x, uint64_t y) : x(x), y(y) {} |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 36 | friend hash_code hash_value(const NonPOD &obj) { |
| 37 | return hash_combine(obj.x, obj.y); |
| 38 | } |
| 39 | }; |
| 40 | |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 41 | namespace hashing { |
| 42 | namespace detail { |
Benjamin Kramer | c1dafe8 | 2014-03-07 14:42:25 +0000 | [diff] [blame] | 43 | template <> struct is_hashable_data<LargeTestInteger> : std::true_type {}; |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 44 | } // namespace detail |
| 45 | } // namespace hashing |
| 46 | |
| 47 | } // namespace llvm |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 48 | |
| 49 | using namespace llvm; |
| 50 | |
| 51 | namespace { |
| 52 | |
Chandler Carruth | d4d8b2a | 2012-03-07 09:32:32 +0000 | [diff] [blame] | 53 | enum TestEnumeration { |
| 54 | TE_Foo = 42, |
| 55 | TE_Bar = 43 |
| 56 | }; |
Chandler Carruth | 1c14489 | 2012-03-02 10:56:40 +0000 | [diff] [blame] | 57 | |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 58 | TEST(HashingTest, HashValueBasicTest) { |
| 59 | int x = 42, y = 43, c = 'x'; |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 60 | void *p = nullptr; |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 61 | uint64_t i = 71; |
| 62 | const unsigned ci = 71; |
| 63 | volatile int vi = 71; |
| 64 | const volatile int cvi = 71; |
| 65 | uintptr_t addr = reinterpret_cast<uintptr_t>(&y); |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 66 | EXPECT_EQ(hash_value(42), hash_value(x)); |
Chandler Carruth | d4d8b2a | 2012-03-07 09:32:32 +0000 | [diff] [blame] | 67 | EXPECT_EQ(hash_value(42), hash_value(TE_Foo)); |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 68 | EXPECT_NE(hash_value(42), hash_value(y)); |
Chandler Carruth | d4d8b2a | 2012-03-07 09:32:32 +0000 | [diff] [blame] | 69 | EXPECT_NE(hash_value(42), hash_value(TE_Bar)); |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 70 | EXPECT_NE(hash_value(42), hash_value(p)); |
| 71 | EXPECT_EQ(hash_value(71), hash_value(i)); |
| 72 | EXPECT_EQ(hash_value(71), hash_value(ci)); |
| 73 | EXPECT_EQ(hash_value(71), hash_value(vi)); |
| 74 | EXPECT_EQ(hash_value(71), hash_value(cvi)); |
| 75 | EXPECT_EQ(hash_value(c), hash_value('x')); |
| 76 | EXPECT_EQ(hash_value('4'), hash_value('0' + 4)); |
| 77 | EXPECT_EQ(hash_value(addr), hash_value(&y)); |
Chandler Carruth | 21d60d5 | 2012-03-04 10:23:11 +0000 | [diff] [blame] | 78 | } |
Chandler Carruth | c7384cf | 2012-03-02 08:32:29 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | 21d60d5 | 2012-03-04 10:23:11 +0000 | [diff] [blame] | 80 | TEST(HashingTest, HashValueStdPair) { |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 81 | EXPECT_EQ(hash_combine(42, 43), hash_value(std::make_pair(42, 43))); |
| 82 | EXPECT_NE(hash_combine(43, 42), hash_value(std::make_pair(42, 43))); |
| 83 | EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42ull, 43ull))); |
| 84 | EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42, 43ull))); |
| 85 | EXPECT_NE(hash_combine(42, 43), hash_value(std::make_pair(42ull, 43))); |
Chandler Carruth | 4d628e2 | 2012-03-02 09:26:36 +0000 | [diff] [blame] | 86 | |
| 87 | // Note that pairs are implicitly flattened to a direct sequence of data and |
| 88 | // hashed efficiently as a consequence. |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 89 | EXPECT_EQ(hash_combine(42, 43, 44), |
| 90 | hash_value(std::make_pair(42, std::make_pair(43, 44)))); |
| 91 | EXPECT_EQ(hash_value(std::make_pair(42, std::make_pair(43, 44))), |
| 92 | hash_value(std::make_pair(std::make_pair(42, 43), 44))); |
Chandler Carruth | 1c14489 | 2012-03-02 10:56:40 +0000 | [diff] [blame] | 93 | |
| 94 | // Ensure that pairs which have padding bytes *inside* them don't get treated |
| 95 | // this way. |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 96 | EXPECT_EQ(hash_combine('0', hash_combine(1ull, '2')), |
| 97 | hash_value(std::make_pair('0', std::make_pair(1ull, '2')))); |
Chandler Carruth | 1c14489 | 2012-03-02 10:56:40 +0000 | [diff] [blame] | 98 | |
| 99 | // Ensure that non-POD pairs don't explode the traits used. |
| 100 | NonPOD obj1(1, 2), obj2(3, 4), obj3(5, 6); |
Francois Pichet | 5fa6f5b | 2012-03-03 09:39:54 +0000 | [diff] [blame] | 101 | EXPECT_EQ(hash_combine(obj1, hash_combine(obj2, obj3)), |
| 102 | hash_value(std::make_pair(obj1, std::make_pair(obj2, obj3)))); |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chandler Carruth | 9406da6 | 2012-03-04 10:23:15 +0000 | [diff] [blame] | 105 | TEST(HashingTest, HashValueStdString) { |
| 106 | std::string s = "Hello World!"; |
| 107 | EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size()), hash_value(s)); |
| 108 | EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size() - 1), |
| 109 | hash_value(s.substr(0, s.size() - 1))); |
| 110 | EXPECT_EQ(hash_combine_range(s.c_str() + 1, s.c_str() + s.size() - 1), |
| 111 | hash_value(s.substr(1, s.size() - 2))); |
| 112 | |
| 113 | std::wstring ws = L"Hello Wide World!"; |
| 114 | EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size()), |
| 115 | hash_value(ws)); |
| 116 | EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size() - 1), |
| 117 | hash_value(ws.substr(0, ws.size() - 1))); |
| 118 | EXPECT_EQ(hash_combine_range(ws.c_str() + 1, ws.c_str() + ws.size() - 1), |
| 119 | hash_value(ws.substr(1, ws.size() - 2))); |
| 120 | } |
| 121 | |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 122 | template <typename T, size_t N> T *begin(T (&arr)[N]) { return arr; } |
| 123 | template <typename T, size_t N> T *end(T (&arr)[N]) { return arr + N; } |
| 124 | |
| 125 | // Provide a dummy, hashable type designed for easy verification: its hash is |
| 126 | // the same as its value. |
| 127 | struct HashableDummy { size_t value; }; |
| 128 | hash_code hash_value(HashableDummy dummy) { return dummy.value; } |
| 129 | |
| 130 | TEST(HashingTest, HashCombineRangeBasicTest) { |
| 131 | // Leave this uninitialized in the hope that valgrind will catch bad reads. |
| 132 | int dummy; |
| 133 | hash_code dummy_hash = hash_combine_range(&dummy, &dummy); |
Chandler Carruth | 4166989 | 2012-03-02 00:48:38 +0000 | [diff] [blame] | 134 | EXPECT_NE(hash_code(0), dummy_hash); |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 135 | |
| 136 | const int arr1[] = { 1, 2, 3 }; |
| 137 | hash_code arr1_hash = hash_combine_range(begin(arr1), end(arr1)); |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 138 | EXPECT_NE(dummy_hash, arr1_hash); |
| 139 | EXPECT_EQ(arr1_hash, hash_combine_range(begin(arr1), end(arr1))); |
| 140 | |
| 141 | const std::vector<int> vec(begin(arr1), end(arr1)); |
| 142 | EXPECT_EQ(arr1_hash, hash_combine_range(vec.begin(), vec.end())); |
| 143 | |
| 144 | const std::list<int> list(begin(arr1), end(arr1)); |
| 145 | EXPECT_EQ(arr1_hash, hash_combine_range(list.begin(), list.end())); |
| 146 | |
| 147 | const std::deque<int> deque(begin(arr1), end(arr1)); |
| 148 | EXPECT_EQ(arr1_hash, hash_combine_range(deque.begin(), deque.end())); |
| 149 | |
| 150 | const int arr2[] = { 3, 2, 1 }; |
| 151 | hash_code arr2_hash = hash_combine_range(begin(arr2), end(arr2)); |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 152 | EXPECT_NE(dummy_hash, arr2_hash); |
| 153 | EXPECT_NE(arr1_hash, arr2_hash); |
| 154 | |
| 155 | const int arr3[] = { 1, 1, 2, 3 }; |
| 156 | hash_code arr3_hash = hash_combine_range(begin(arr3), end(arr3)); |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 157 | EXPECT_NE(dummy_hash, arr3_hash); |
| 158 | EXPECT_NE(arr1_hash, arr3_hash); |
| 159 | |
| 160 | const int arr4[] = { 1, 2, 3, 3 }; |
| 161 | hash_code arr4_hash = hash_combine_range(begin(arr4), end(arr4)); |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 162 | EXPECT_NE(dummy_hash, arr4_hash); |
| 163 | EXPECT_NE(arr1_hash, arr4_hash); |
| 164 | |
| 165 | const size_t arr5[] = { 1, 2, 3 }; |
| 166 | const HashableDummy d_arr5[] = { {1}, {2}, {3} }; |
| 167 | hash_code arr5_hash = hash_combine_range(begin(arr5), end(arr5)); |
| 168 | hash_code d_arr5_hash = hash_combine_range(begin(d_arr5), end(d_arr5)); |
| 169 | EXPECT_EQ(arr5_hash, d_arr5_hash); |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 172 | TEST(HashingTest, HashCombineRangeLengthDiff) { |
| 173 | // Test that as only the length varies, we compute different hash codes for |
| 174 | // sequences. |
| 175 | std::map<size_t, size_t> code_to_size; |
| 176 | std::vector<char> all_one_c(256, '\xff'); |
| 177 | for (unsigned Idx = 1, Size = all_one_c.size(); Idx < Size; ++Idx) { |
| 178 | hash_code code = hash_combine_range(&all_one_c[0], &all_one_c[0] + Idx); |
| 179 | std::map<size_t, size_t>::iterator |
| 180 | I = code_to_size.insert(std::make_pair(code, Idx)).first; |
| 181 | EXPECT_EQ(Idx, I->second); |
| 182 | } |
| 183 | code_to_size.clear(); |
| 184 | std::vector<char> all_zero_c(256, '\0'); |
| 185 | for (unsigned Idx = 1, Size = all_zero_c.size(); Idx < Size; ++Idx) { |
| 186 | hash_code code = hash_combine_range(&all_zero_c[0], &all_zero_c[0] + Idx); |
| 187 | std::map<size_t, size_t>::iterator |
| 188 | I = code_to_size.insert(std::make_pair(code, Idx)).first; |
| 189 | EXPECT_EQ(Idx, I->second); |
| 190 | } |
| 191 | code_to_size.clear(); |
| 192 | std::vector<unsigned> all_one_int(512, -1); |
| 193 | for (unsigned Idx = 1, Size = all_one_int.size(); Idx < Size; ++Idx) { |
| 194 | hash_code code = hash_combine_range(&all_one_int[0], &all_one_int[0] + Idx); |
| 195 | std::map<size_t, size_t>::iterator |
| 196 | I = code_to_size.insert(std::make_pair(code, Idx)).first; |
| 197 | EXPECT_EQ(Idx, I->second); |
| 198 | } |
| 199 | code_to_size.clear(); |
| 200 | std::vector<unsigned> all_zero_int(512, 0); |
| 201 | for (unsigned Idx = 1, Size = all_zero_int.size(); Idx < Size; ++Idx) { |
| 202 | hash_code code = hash_combine_range(&all_zero_int[0], &all_zero_int[0] + Idx); |
| 203 | std::map<size_t, size_t>::iterator |
| 204 | I = code_to_size.insert(std::make_pair(code, Idx)).first; |
| 205 | EXPECT_EQ(Idx, I->second); |
| 206 | } |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 209 | TEST(HashingTest, HashCombineRangeGoldenTest) { |
| 210 | struct { const char *s; uint64_t hash; } golden_data[] = { |
Tim Northover | e25423d | 2018-08-31 19:24:37 +0000 | [diff] [blame] | 211 | #if SIZE_MAX == UINT64_MAX || SIZE_MAX == UINT32_MAX |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 212 | { "a", 0xaeb6f9d5517c61f8ULL }, |
| 213 | { "ab", 0x7ab1edb96be496b4ULL }, |
| 214 | { "abc", 0xe38e60bf19c71a3fULL }, |
| 215 | { "abcde", 0xd24461a66de97f6eULL }, |
| 216 | { "abcdefgh", 0x4ef872ec411dec9dULL }, |
| 217 | { "abcdefghijklm", 0xe8a865539f4eadfeULL }, |
| 218 | { "abcdefghijklmnopqrstu", 0x261cdf85faaf4e79ULL }, |
| 219 | { "abcdefghijklmnopqrstuvwxyzabcdef", 0x43ba70e4198e3b2aULL }, |
| 220 | { "abcdefghijklmnopqrstuvwxyzabcdef" |
| 221 | "abcdefghijklmnopqrstuvwxyzghijkl" |
| 222 | "abcdefghijklmnopqrstuvwxyzmnopqr" |
| 223 | "abcdefghijklmnopqrstuvwxyzstuvwx" |
| 224 | "abcdefghijklmnopqrstuvwxyzyzabcd", 0xdcd57fb2afdf72beULL }, |
| 225 | { "a", 0xaeb6f9d5517c61f8ULL }, |
| 226 | { "aa", 0xf2b3b69a9736a1ebULL }, |
| 227 | { "aaa", 0xf752eb6f07b1cafeULL }, |
| 228 | { "aaaaa", 0x812bd21e1236954cULL }, |
| 229 | { "aaaaaaaa", 0xff07a2cff08ac587ULL }, |
| 230 | { "aaaaaaaaaaaaa", 0x84ac949d54d704ecULL }, |
| 231 | { "aaaaaaaaaaaaaaaaaaaaa", 0xcb2c8fb6be8f5648ULL }, |
| 232 | { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0xcc40ab7f164091b6ULL }, |
| 233 | { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 234 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 235 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 236 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 237 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0xc58e174c1e78ffe9ULL }, |
| 238 | { "z", 0x1ba160d7e8f8785cULL }, |
| 239 | { "zz", 0x2c5c03172f1285d7ULL }, |
| 240 | { "zzz", 0x9d2c4f4b507a2ac3ULL }, |
| 241 | { "zzzzz", 0x0f03b9031735693aULL }, |
| 242 | { "zzzzzzzz", 0xe674147c8582c08eULL }, |
| 243 | { "zzzzzzzzzzzzz", 0x3162d9fa6938db83ULL }, |
| 244 | { "zzzzzzzzzzzzzzzzzzzzz", 0x37b9a549e013620cULL }, |
| 245 | { "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 0x8921470aff885016ULL }, |
| 246 | { "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 247 | "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 248 | "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 249 | "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 250 | "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 0xf60fdcd9beb08441ULL }, |
| 251 | { "a", 0xaeb6f9d5517c61f8ULL }, |
| 252 | { "ab", 0x7ab1edb96be496b4ULL }, |
| 253 | { "aba", 0x3edb049950884d0aULL }, |
| 254 | { "ababa", 0x8f2de9e73a97714bULL }, |
| 255 | { "abababab", 0xee14a29ddf0ce54cULL }, |
| 256 | { "ababababababa", 0x38b3ddaada2d52b4ULL }, |
| 257 | { "ababababababababababa", 0xd3665364219f2b85ULL }, |
Chandler Carruth | c3f9918 | 2012-03-02 10:01:29 +0000 | [diff] [blame] | 258 | { "abababababababababababababababab", 0xa75cd6afbf1bc972ULL }, |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 259 | { "abababababababababababababababab" |
| 260 | "abababababababababababababababab" |
| 261 | "abababababababababababababababab" |
| 262 | "abababababababababababababababab" |
| 263 | "abababababababababababababababab", 0x840192d129f7a22bULL } |
Chandler Carruth | 9731294 | 2012-03-01 23:06:19 +0000 | [diff] [blame] | 264 | #else |
| 265 | #error This test only supports 64-bit and 32-bit systems. |
| 266 | #endif |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 267 | }; |
| 268 | for (unsigned i = 0; i < sizeof(golden_data)/sizeof(*golden_data); ++i) { |
| 269 | StringRef str = golden_data[i].s; |
| 270 | hash_code hash = hash_combine_range(str.begin(), str.end()); |
Chandler Carruth | 5a491ca | 2012-03-01 23:20:45 +0000 | [diff] [blame] | 271 | #if 0 // Enable this to generate paste-able text for the above structure. |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 272 | std::string member_str = "\"" + str.str() + "\","; |
Chandler Carruth | 9731294 | 2012-03-01 23:06:19 +0000 | [diff] [blame] | 273 | fprintf(stderr, " { %-35s 0x%016llxULL },\n", |
| 274 | member_str.c_str(), static_cast<uint64_t>(hash)); |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 275 | #endif |
| 276 | EXPECT_EQ(static_cast<size_t>(golden_data[i].hash), |
| 277 | static_cast<size_t>(hash)); |
| 278 | } |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 281 | TEST(HashingTest, HashCombineBasicTest) { |
| 282 | // Hashing a sequence of homogenous types matches range hashing. |
| 283 | const int i1 = 42, i2 = 43, i3 = 123, i4 = 999, i5 = 0, i6 = 79; |
| 284 | const int arr1[] = { i1, i2, i3, i4, i5, i6 }; |
| 285 | EXPECT_EQ(hash_combine_range(arr1, arr1 + 1), hash_combine(i1)); |
| 286 | EXPECT_EQ(hash_combine_range(arr1, arr1 + 2), hash_combine(i1, i2)); |
| 287 | EXPECT_EQ(hash_combine_range(arr1, arr1 + 3), hash_combine(i1, i2, i3)); |
| 288 | EXPECT_EQ(hash_combine_range(arr1, arr1 + 4), hash_combine(i1, i2, i3, i4)); |
| 289 | EXPECT_EQ(hash_combine_range(arr1, arr1 + 5), |
| 290 | hash_combine(i1, i2, i3, i4, i5)); |
| 291 | EXPECT_EQ(hash_combine_range(arr1, arr1 + 6), |
| 292 | hash_combine(i1, i2, i3, i4, i5, i6)); |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 293 | |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 294 | // Hashing a sequence of heterogeneous types which *happen* to all produce the |
Chandler Carruth | 0b66c6f | 2012-03-01 18:55:25 +0000 | [diff] [blame] | 295 | // same data for hashing produces the same as a range-based hash of the |
| 296 | // fundamental values. |
| 297 | const size_t s1 = 1024, s2 = 8888, s3 = 9000000; |
| 298 | const HashableDummy d1 = { 1024 }, d2 = { 8888 }, d3 = { 9000000 }; |
| 299 | const size_t arr2[] = { s1, s2, s3 }; |
| 300 | EXPECT_EQ(hash_combine_range(begin(arr2), end(arr2)), |
| 301 | hash_combine(s1, s2, s3)); |
| 302 | EXPECT_EQ(hash_combine(s1, s2, s3), hash_combine(s1, s2, d3)); |
| 303 | EXPECT_EQ(hash_combine(s1, s2, s3), hash_combine(s1, d2, s3)); |
| 304 | EXPECT_EQ(hash_combine(s1, s2, s3), hash_combine(d1, s2, s3)); |
| 305 | EXPECT_EQ(hash_combine(s1, s2, s3), hash_combine(d1, d2, s3)); |
| 306 | EXPECT_EQ(hash_combine(s1, s2, s3), hash_combine(d1, d2, d3)); |
| 307 | |
| 308 | // Permuting values causes hashes to change. |
| 309 | EXPECT_NE(hash_combine(i1, i1, i1), hash_combine(i1, i1, i2)); |
| 310 | EXPECT_NE(hash_combine(i1, i1, i1), hash_combine(i1, i2, i1)); |
| 311 | EXPECT_NE(hash_combine(i1, i1, i1), hash_combine(i2, i1, i1)); |
| 312 | EXPECT_NE(hash_combine(i1, i1, i1), hash_combine(i2, i2, i1)); |
| 313 | EXPECT_NE(hash_combine(i1, i1, i1), hash_combine(i2, i2, i2)); |
| 314 | EXPECT_NE(hash_combine(i2, i1, i1), hash_combine(i1, i1, i2)); |
| 315 | EXPECT_NE(hash_combine(i1, i1, i2), hash_combine(i1, i2, i1)); |
| 316 | EXPECT_NE(hash_combine(i1, i2, i1), hash_combine(i2, i1, i1)); |
| 317 | |
| 318 | // Changing type w/o changing value causes hashes to change. |
| 319 | EXPECT_NE(hash_combine(i1, i2, i3), hash_combine((char)i1, i2, i3)); |
| 320 | EXPECT_NE(hash_combine(i1, i2, i3), hash_combine(i1, (char)i2, i3)); |
| 321 | EXPECT_NE(hash_combine(i1, i2, i3), hash_combine(i1, i2, (char)i3)); |
| 322 | |
| 323 | // This is array of uint64, but it should have the exact same byte pattern as |
| 324 | // an array of LargeTestIntegers. |
| 325 | const uint64_t bigarr[] = { |
| 326 | 0xaaaaaaaaababababULL, 0xacacacacbcbcbcbcULL, 0xccddeeffeeddccbbULL, |
| 327 | 0xdeadbeafdeadbeefULL, 0xfefefefededededeULL, 0xafafafafededededULL, |
| 328 | 0xffffeeeeddddccccULL, 0xaaaacbcbffffababULL, |
| 329 | 0xaaaaaaaaababababULL, 0xacacacacbcbcbcbcULL, 0xccddeeffeeddccbbULL, |
| 330 | 0xdeadbeafdeadbeefULL, 0xfefefefededededeULL, 0xafafafafededededULL, |
| 331 | 0xffffeeeeddddccccULL, 0xaaaacbcbffffababULL, |
| 332 | 0xaaaaaaaaababababULL, 0xacacacacbcbcbcbcULL, 0xccddeeffeeddccbbULL, |
| 333 | 0xdeadbeafdeadbeefULL, 0xfefefefededededeULL, 0xafafafafededededULL, |
| 334 | 0xffffeeeeddddccccULL, 0xaaaacbcbffffababULL |
| 335 | }; |
| 336 | // Hash a preposterously large integer, both aligned with the buffer and |
| 337 | // misaligned. |
| 338 | const LargeTestInteger li = { { |
| 339 | 0xaaaaaaaaababababULL, 0xacacacacbcbcbcbcULL, 0xccddeeffeeddccbbULL, |
| 340 | 0xdeadbeafdeadbeefULL, 0xfefefefededededeULL, 0xafafafafededededULL, |
| 341 | 0xffffeeeeddddccccULL, 0xaaaacbcbffffababULL |
| 342 | } }; |
| 343 | // Rotate the storage from 'li'. |
| 344 | const LargeTestInteger l2 = { { |
| 345 | 0xacacacacbcbcbcbcULL, 0xccddeeffeeddccbbULL, 0xdeadbeafdeadbeefULL, |
| 346 | 0xfefefefededededeULL, 0xafafafafededededULL, 0xffffeeeeddddccccULL, |
| 347 | 0xaaaacbcbffffababULL, 0xaaaaaaaaababababULL |
| 348 | } }; |
| 349 | const LargeTestInteger l3 = { { |
| 350 | 0xccddeeffeeddccbbULL, 0xdeadbeafdeadbeefULL, 0xfefefefededededeULL, |
| 351 | 0xafafafafededededULL, 0xffffeeeeddddccccULL, 0xaaaacbcbffffababULL, |
| 352 | 0xaaaaaaaaababababULL, 0xacacacacbcbcbcbcULL |
| 353 | } }; |
| 354 | EXPECT_EQ(hash_combine_range(begin(bigarr), end(bigarr)), |
| 355 | hash_combine(li, li, li)); |
| 356 | EXPECT_EQ(hash_combine_range(bigarr, bigarr + 9), |
| 357 | hash_combine(bigarr[0], l2)); |
| 358 | EXPECT_EQ(hash_combine_range(bigarr, bigarr + 10), |
| 359 | hash_combine(bigarr[0], bigarr[1], l3)); |
| 360 | EXPECT_EQ(hash_combine_range(bigarr, bigarr + 17), |
| 361 | hash_combine(li, bigarr[0], l2)); |
| 362 | EXPECT_EQ(hash_combine_range(bigarr, bigarr + 18), |
| 363 | hash_combine(li, bigarr[0], bigarr[1], l3)); |
| 364 | EXPECT_EQ(hash_combine_range(bigarr, bigarr + 18), |
| 365 | hash_combine(bigarr[0], l2, bigarr[9], l3)); |
| 366 | EXPECT_EQ(hash_combine_range(bigarr, bigarr + 20), |
| 367 | hash_combine(bigarr[0], l2, bigarr[9], l3, bigarr[18], bigarr[19])); |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Duncan P. N. Exon Smith | a9a0776 | 2015-02-09 23:21:05 +0000 | [diff] [blame] | 370 | TEST(HashingTest, HashCombineArgs18) { |
| 371 | // This tests that we can pass in up to 18 args. |
| 372 | #define CHECK_SAME(...) \ |
| 373 | EXPECT_EQ(hash_combine(__VA_ARGS__), hash_combine(__VA_ARGS__)) |
| 374 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18); |
| 375 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17); |
| 376 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); |
| 377 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); |
| 378 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); |
| 379 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); |
| 380 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); |
| 381 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 382 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 383 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 384 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7, 8); |
| 385 | CHECK_SAME(1, 2, 3, 4, 5, 6, 7); |
| 386 | CHECK_SAME(1, 2, 3, 4, 5, 6); |
| 387 | CHECK_SAME(1, 2, 3, 4, 5); |
| 388 | CHECK_SAME(1, 2, 3, 4); |
| 389 | CHECK_SAME(1, 2, 3); |
| 390 | CHECK_SAME(1, 2); |
| 391 | CHECK_SAME(1); |
| 392 | #undef CHECK_SAME |
| 393 | } |
| 394 | |
Talin | 1a4b19e | 2012-02-18 21:00:49 +0000 | [diff] [blame] | 395 | } |