-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
XxHash32Tests.007.cs
214 lines (186 loc) · 7.41 KB
/
XxHash32Tests.007.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace System.IO.Hashing.Tests
{
public class XxHash32Tests_Seeded_007 : NonCryptoHashTestDriver
{
private int Seed = 0x007_007_00;
private static readonly byte[] s_emptyHashValue = new byte[] { 0xC4, 0xD2, 0x6D, 0x9A };
public XxHash32Tests_Seeded_007()
: base(s_emptyHashValue)
{
}
public static IEnumerable<object[]> TestCases
{
get
{
object[] arr = new object[1];
foreach (TestCase testCase in TestCaseDefinitions)
{
arr[0] = testCase;
yield return arr;
}
}
}
private const string DotNetHashesThis = ".NET Hashes This!";
private const string DotNetHashesThis3 = DotNetHashesThis + DotNetHashesThis + DotNetHashesThis;
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
private const string DotNetNCHashing3 = DotNetNCHashing + DotNetNCHashing + DotNetNCHashing;
private const string SixteenBytes = ".NET Hashes This";
private const string SixteenBytes3 = SixteenBytes + SixteenBytes + SixteenBytes;
private const string EightBytes = "Hashing!";
private const string EightBytes3 = EightBytes + EightBytes + EightBytes;
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
new[]
{
// Same inputs as the main XxHash32 tests, but with the seed applied.
new TestCase(
"Nobody inspects the spammish repetition",
"Nobody inspects the spammish repetition"u8.ToArray(),
"21C69E3A"),
new TestCase(
"The quick brown fox jumps over the lazy dog",
"The quick brown fox jumps over the lazy dog"u8.ToArray(),
"558D024f"),
new TestCase(
"The quick brown fox jumps over the lazy dog.",
"The quick brown fox jumps over the lazy dog."u8.ToArray(),
"E7D2C0A7"),
new TestCase(
"abc",
"abc"u8.ToArray(),
"D46070BB"),
new TestCase(
"123456",
"313233343536",
"4256C97B"),
new TestCase(
"12345678901234567890",
"3132333435363738393031323334353637383930",
"A57B8FEE"),
new TestCase(
"123456789012345678901",
"313233343536373839303132333435363738393031",
"6789AD70"),
new TestCase(
$"{DotNetHashesThis} (x3)",
Encoding.ASCII.GetBytes(DotNetHashesThis3),
"C5231E05"),
new TestCase(
$"{DotNetNCHashing} (x3)",
Encoding.ASCII.GetBytes(DotNetNCHashing3),
"CABC8ABD"),
// stripe size
new TestCase(
$"{SixteenBytes} (x3)",
Encoding.ASCII.GetBytes(SixteenBytes3),
"AD98EBD3"),
new TestCase(
$"{EightBytes} (x3)",
Encoding.ASCII.GetBytes(EightBytes3),
"FC23CD03"),
};
public static IEnumerable<object[]> LargeTestCases
{
get
{
object[] arr = new object[1];
foreach (LargeTestCase testCase in LargeTestCaseDefinitions)
{
arr[0] = testCase;
yield return arr;
}
}
}
protected static IEnumerable<LargeTestCase> LargeTestCaseDefinitions { get; } =
new[]
{
// Manually run against the xxHash32 reference implementation.
new LargeTestCase(
"EEEEE... (10GB)",
(byte)'E',
10L * 1024 * 1024 * 1024, // 10 GB
"1C44F650"),
};
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash32(Seed);
protected override byte[] StaticOneShot(byte[] source) => XxHash32.Hash(source, Seed);
protected override byte[] StaticOneShot(ReadOnlySpan<byte> source) => XxHash32.Hash(source, Seed);
protected override int StaticOneShot(ReadOnlySpan<byte> source, Span<byte> destination) =>
XxHash32.Hash(source, destination, Seed);
protected override bool TryStaticOneShot(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) =>
XxHash32.TryHash(source, destination, out bytesWritten, Seed);
[Theory]
[MemberData(nameof(TestCases))]
public void InstanceAppendAllocate(TestCase testCase)
{
InstanceAppendAllocateDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void InstanceAppendAllocateAndReset(TestCase testCase)
{
InstanceAppendAllocateAndResetDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void InstanceMultiAppendGetCurrentHash(TestCase testCase)
{
InstanceMultiAppendGetCurrentHashDriver(testCase);
}
[Theory]
[MemberData(nameof(LargeTestCases))]
[OuterLoop]
public void InstanceMultiAppendLargeInput(LargeTestCase testCase)
{
InstanceMultiAppendLargeInputDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void InstanceVerifyEmptyState(TestCase testCase)
{
InstanceVerifyEmptyStateDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void InstanceVerifyResetState(TestCase testCase)
{
InstanceVerifyResetStateDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void StaticVerifyOneShotArray(TestCase testCase)
{
StaticVerifyOneShotArrayDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void StaticVerifyOneShotSpanToArray(TestCase testCase)
{
StaticVerifyOneShotSpanToArrayDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void StaticVerifyOneShotSpanToSpan(TestCase testCase)
{
StaticVerifyOneShotSpanToSpanDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void StaticVerifyTryOneShot(TestCase testCase)
{
StaticVerifyTryOneShotDriver(testCase);
}
[Theory]
[MemberData(nameof(TestCases))]
public void VerifyHashToUInt32(TestCase testCase)
{
var alg = new XxHash32(Seed);
alg.Append(testCase.Input);
AssertEqualHashNumber(testCase.OutputHex, alg.GetCurrentHashAsUInt32());
AssertEqualHashNumber(testCase.OutputHex, XxHash32.HashToUInt32(testCase.Input, Seed));
}
}
}