forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashBuilderTest.cpp
336 lines (289 loc) · 11.7 KB
/
HashBuilderTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//===- llvm/unittest/Support/HashBuilderTest.cpp - HashBuilder unit tests -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/HashBuilder.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/SHA1.h"
#include "llvm/Support/SHA256.h"
#include "gtest/gtest.h"
#include <list>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
// gtest utilities and macros rely on using a single type. So wrap both the
// hasher type and endianness.
template <typename _HasherT, llvm::support::endianness _Endianness>
struct HasherTAndEndianness {
using HasherT = _HasherT;
static constexpr llvm::support::endianness Endianness = _Endianness;
};
using HasherTAndEndiannessToTest =
::testing::Types<HasherTAndEndianness<llvm::MD5, llvm::support::big>,
HasherTAndEndianness<llvm::MD5, llvm::support::little>,
HasherTAndEndianness<llvm::MD5, llvm::support::native>,
HasherTAndEndianness<llvm::SHA1, llvm::support::big>,
HasherTAndEndianness<llvm::SHA1, llvm::support::little>,
HasherTAndEndianness<llvm::SHA1, llvm::support::native>,
HasherTAndEndianness<llvm::SHA256, llvm::support::big>,
HasherTAndEndianness<llvm::SHA256, llvm::support::little>,
HasherTAndEndianness<llvm::SHA256, llvm::support::native>>;
template <typename HasherT> class HashBuilderTest : public testing::Test {};
TYPED_TEST_SUITE(HashBuilderTest, HasherTAndEndiannessToTest, );
template <typename HasherTAndEndianness>
using HashBuilder = llvm::HashBuilder<typename HasherTAndEndianness::HasherT,
HasherTAndEndianness::Endianness>;
template <typename HasherTAndEndianness, typename... Ts>
static std::string hashWithBuilder(const Ts &...Args) {
return HashBuilder<HasherTAndEndianness>().add(Args...).final().str();
}
template <typename HasherTAndEndianness, typename... Ts>
static std::string hashRangeWithBuilder(const Ts &...Args) {
return HashBuilder<HasherTAndEndianness>().addRange(Args...).final().str();
}
// All the test infrastructure relies on the variadic helpers. Test them first.
TYPED_TEST(HashBuilderTest, VariadicHelpers) {
{
HashBuilder<TypeParam> HBuilder;
HBuilder.add(100);
HBuilder.add('c');
HBuilder.add("string");
EXPECT_EQ(HBuilder.final(), hashWithBuilder<TypeParam>(100, 'c', "string"));
}
{
HashBuilder<TypeParam> HBuilder;
std::vector<int> Vec{100, 101, 102};
HBuilder.addRange(Vec);
EXPECT_EQ(HBuilder.final(), hashRangeWithBuilder<TypeParam>(Vec));
}
{
HashBuilder<TypeParam> HBuilder;
std::vector<int> Vec{200, 201, 202};
HBuilder.addRange(Vec.begin(), Vec.end());
EXPECT_EQ(HBuilder.final(),
hashRangeWithBuilder<TypeParam>(Vec.begin(), Vec.end()));
}
}
TYPED_TEST(HashBuilderTest, AddRangeElements) {
HashBuilder<TypeParam> HBuilder;
int Values[] = {1, 2, 3};
HBuilder.addRangeElements(llvm::ArrayRef<int>(Values));
EXPECT_EQ(HBuilder.final(), hashWithBuilder<TypeParam>(1, 2, 3));
}
TYPED_TEST(HashBuilderTest, AddHashableData) {
using HE = TypeParam;
auto ByteSwapAndHashWithHasher = [](auto Data) {
using H = typename HE::HasherT;
constexpr auto E = HE::Endianness;
H Hasher;
auto SwappedData = llvm::support::endian::byte_swap(Data, E);
Hasher.update(llvm::makeArrayRef(
reinterpret_cast<const uint8_t *>(&SwappedData), sizeof(Data)));
return static_cast<std::string>(Hasher.final());
};
char C = 'c';
int32_t I = 0x12345678;
uint64_t UI64 = static_cast<uint64_t>(1) << 50;
enum TestEnumeration : uint16_t { TE_One = 1, TE_Two = 2 };
TestEnumeration Enum = TE_Two;
EXPECT_EQ(ByteSwapAndHashWithHasher(C), hashWithBuilder<HE>(C));
EXPECT_EQ(ByteSwapAndHashWithHasher(I), hashWithBuilder<HE>(I));
EXPECT_EQ(ByteSwapAndHashWithHasher(UI64), hashWithBuilder<HE>(UI64));
EXPECT_EQ(ByteSwapAndHashWithHasher(Enum), hashWithBuilder<HE>(Enum));
}
struct SimpleStruct {
char C;
int I;
};
template <typename HasherT, llvm::support::endianness Endianness>
void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
const SimpleStruct &Value) {
HBuilder.add(Value.C);
HBuilder.add(Value.I);
}
struct StructWithoutCopyOrMove {
int I;
StructWithoutCopyOrMove() = default;
StructWithoutCopyOrMove(const StructWithoutCopyOrMove &) = delete;
StructWithoutCopyOrMove &operator=(const StructWithoutCopyOrMove &) = delete;
template <typename HasherT, llvm::support::endianness Endianness>
friend void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
const StructWithoutCopyOrMove &Value) {
HBuilder.add(Value.I);
}
};
// The struct and associated tests are simplified to avoid failures caused by
// different alignments on different platforms.
struct /* __attribute__((packed)) */ StructWithFastHash {
int I;
// char C;
// If possible, we want to hash both `I` and `C` in a single `update`
// call for performance concerns.
template <typename HasherT, llvm::support::endianness Endianness>
friend void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
const StructWithFastHash &Value) {
if (Endianness == llvm::support::endian::system_endianness()) {
HBuilder.update(llvm::makeArrayRef(
reinterpret_cast<const uint8_t *>(&Value), sizeof(Value)));
} else {
// Rely on existing `add` methods to handle endianness.
HBuilder.add(Value.I);
// HBuilder.add(Value.C);
}
}
};
struct CustomContainer {
private:
size_t Size;
int Elements[100];
public:
CustomContainer(size_t Size) : Size(Size) {
for (size_t I = 0; I != Size; ++I)
Elements[I] = I;
}
template <typename HasherT, llvm::support::endianness Endianness>
friend void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
const CustomContainer &Value) {
if (Endianness == llvm::support::endian::system_endianness()) {
HBuilder.update(llvm::makeArrayRef(
reinterpret_cast<const uint8_t *>(&Value.Size),
sizeof(Value.Size) + Value.Size * sizeof(Value.Elements[0])));
} else {
HBuilder.addRange(&Value.Elements[0], &Value.Elements[0] + Value.Size);
}
}
};
TYPED_TEST(HashBuilderTest, HashUserDefinedStruct) {
using HE = TypeParam;
EXPECT_EQ(hashWithBuilder<HE>(SimpleStruct{'c', 123}),
hashWithBuilder<HE>('c', 123));
EXPECT_EQ(hashWithBuilder<HE>(StructWithoutCopyOrMove{1}),
hashWithBuilder<HE>(1));
EXPECT_EQ(hashWithBuilder<HE>(StructWithFastHash{123}),
hashWithBuilder<HE>(123));
EXPECT_EQ(hashWithBuilder<HE>(CustomContainer(3)),
hashWithBuilder<HE>(static_cast<size_t>(3), 0, 1, 2));
}
TYPED_TEST(HashBuilderTest, HashArrayRefHashableDataTypes) {
using HE = TypeParam;
int Values[] = {1, 20, 0x12345678};
llvm::ArrayRef<int> Array(Values);
EXPECT_NE(hashWithBuilder<HE>(Array), hashWithBuilder<HE>(1, 20, 0x12345678));
EXPECT_EQ(hashWithBuilder<HE>(Array),
hashRangeWithBuilder<HE>(Array.begin(), Array.end()));
EXPECT_EQ(
hashWithBuilder<HE>(Array),
hashRangeWithBuilder<HE>(Array.data(), Array.data() + Array.size()));
}
TYPED_TEST(HashBuilderTest, HashArrayRef) {
using HE = TypeParam;
int Values[] = {1, 2, 3};
llvm::ArrayRef<int> Array123(&Values[0], 3);
llvm::ArrayRef<int> Array12(&Values[0], 2);
llvm::ArrayRef<int> Array1(&Values[0], 1);
llvm::ArrayRef<int> Array23(&Values[1], 2);
llvm::ArrayRef<int> Array3(&Values[2], 1);
llvm::ArrayRef<int> ArrayEmpty(&Values[0], static_cast<size_t>(0));
auto Hash123andEmpty = hashWithBuilder<HE>(Array123, ArrayEmpty);
auto Hash12And3 = hashWithBuilder<HE>(Array12, Array3);
auto Hash1And23 = hashWithBuilder<HE>(Array1, Array23);
auto HashEmptyAnd123 = hashWithBuilder<HE>(ArrayEmpty, Array123);
EXPECT_NE(Hash123andEmpty, Hash12And3);
EXPECT_NE(Hash123andEmpty, Hash1And23);
EXPECT_NE(Hash123andEmpty, HashEmptyAnd123);
EXPECT_NE(Hash12And3, Hash1And23);
EXPECT_NE(Hash12And3, HashEmptyAnd123);
EXPECT_NE(Hash1And23, HashEmptyAnd123);
}
TYPED_TEST(HashBuilderTest, HashArrayRefNonHashableDataTypes) {
using HE = TypeParam;
SimpleStruct Values[] = {{'a', 100}, {'b', 200}};
llvm::ArrayRef<SimpleStruct> Array(Values);
EXPECT_NE(
hashWithBuilder<HE>(Array),
hashWithBuilder<HE>(SimpleStruct{'a', 100}, SimpleStruct{'b', 200}));
}
TYPED_TEST(HashBuilderTest, HashStringRef) {
using HE = TypeParam;
llvm::StringRef SEmpty("");
llvm::StringRef S1("1");
llvm::StringRef S12("12");
llvm::StringRef S123("123");
llvm::StringRef S23("23");
llvm::StringRef S3("3");
auto Hash123andEmpty = hashWithBuilder<HE>(S123, SEmpty);
auto Hash12And3 = hashWithBuilder<HE>(S12, S3);
auto Hash1And23 = hashWithBuilder<HE>(S1, S23);
auto HashEmptyAnd123 = hashWithBuilder<HE>(SEmpty, S123);
EXPECT_NE(Hash123andEmpty, Hash12And3);
EXPECT_NE(Hash123andEmpty, Hash1And23);
EXPECT_NE(Hash123andEmpty, HashEmptyAnd123);
EXPECT_NE(Hash12And3, Hash1And23);
EXPECT_NE(Hash12And3, HashEmptyAnd123);
EXPECT_NE(Hash1And23, HashEmptyAnd123);
}
TYPED_TEST(HashBuilderTest, HashStdString) {
using HE = TypeParam;
EXPECT_EQ(hashWithBuilder<HE>(std::string("123")),
hashWithBuilder<HE>(llvm::StringRef("123")));
}
TYPED_TEST(HashBuilderTest, HashStdPair) {
using HE = TypeParam;
EXPECT_EQ(hashWithBuilder<HE>(std::make_pair(1, "string")),
hashWithBuilder<HE>(1, "string"));
std::pair<StructWithoutCopyOrMove, std::string> Pair;
Pair.first.I = 1;
Pair.second = "string";
EXPECT_EQ(hashWithBuilder<HE>(Pair), hashWithBuilder<HE>(1, "string"));
}
TYPED_TEST(HashBuilderTest, HashStdTuple) {
using HE = TypeParam;
EXPECT_EQ(hashWithBuilder<HE>(std::make_tuple(1)), hashWithBuilder<HE>(1));
EXPECT_EQ(hashWithBuilder<HE>(std::make_tuple(2ULL)),
hashWithBuilder<HE>(2ULL));
EXPECT_EQ(hashWithBuilder<HE>(std::make_tuple("three")),
hashWithBuilder<HE>("three"));
EXPECT_EQ(hashWithBuilder<HE>(std::make_tuple(1, 2ULL)),
hashWithBuilder<HE>(1, 2ULL));
EXPECT_EQ(hashWithBuilder<HE>(std::make_tuple(1, 2ULL, "three")),
hashWithBuilder<HE>(1, 2ULL, "three"));
std::tuple<StructWithoutCopyOrMove, std::string> Tuple;
std::get<0>(Tuple).I = 1;
std::get<1>(Tuple) = "two";
EXPECT_EQ(hashWithBuilder<HE>(Tuple), hashWithBuilder<HE>(1, "two"));
}
TYPED_TEST(HashBuilderTest, HashRangeWithForwardIterator) {
using HE = TypeParam;
std::list<int> List;
List.push_back(1);
List.push_back(2);
List.push_back(3);
EXPECT_NE(hashRangeWithBuilder<HE>(List), hashWithBuilder<HE>(1, 2, 3));
}
TEST(CustomHasher, CustomHasher) {
struct SumHash {
explicit SumHash(uint8_t Seed1, uint8_t Seed2) : Hash(Seed1 + Seed2) {}
void update(llvm::ArrayRef<uint8_t> Data) {
for (uint8_t C : Data)
Hash += C;
}
uint8_t Hash;
};
{
llvm::HashBuilder<SumHash, llvm::support::endianness::little> HBuilder(0,
1);
EXPECT_EQ(HBuilder.add(0x02, 0x03, 0x400).getHasher().Hash, 0xa);
}
{
llvm::HashBuilder<SumHash, llvm::support::endianness::little> HBuilder(2,
3);
EXPECT_EQ(HBuilder.add("ab", 'c').getHasher().Hash,
static_cast<uint8_t>(/*seeds*/ 2 + 3 + /*range size*/ 2 +
/*characters*/ 'a' + 'b' + 'c'));
}
}