-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
XxHash64Tests.f00d.cs
224 lines (196 loc) · 8.26 KB
/
XxHash64Tests.f00d.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
215
216
217
218
219
220
221
222
223
224
// 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 XxHash64Tests_Seeded_f00d : NonCryptoHashTestDriver
{
private long Seed = unchecked((long)0xF00D_F00D_F00D_F00D);
private static readonly byte[] s_emptyHashValue =
new byte[] { 0x01, 0x3F, 0x6A, 0x1B, 0xB3, 0x9C, 0x10, 0x87 };
public XxHash64Tests_Seeded_f00d()
: 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 ThirtyThreeBytes = "This string has 33 ASCII bytes...";
private const string ThirtyThreeBytes3 = ThirtyThreeBytes + ThirtyThreeBytes + ThirtyThreeBytes;
private const string DotNetNCHashing = ".NET now has non-crypto hashing";
private const string SixtyThreeBytes = "A sixty-three byte test input requires substantial forethought!";
private const string SixtyThreeBytes3 = SixtyThreeBytes + SixtyThreeBytes + SixtyThreeBytes;
private const string ThirtyTwoBytes = "This string has 32 ASCII bytes..";
private const string ThirtyTwoBytes3 = ThirtyTwoBytes + ThirtyTwoBytes + ThirtyTwoBytes;
private const string SixteenBytes = "0123456789ABCDEF";
private const string SixteenBytes3 = SixteenBytes + SixteenBytes + SixteenBytes;
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
new[]
{
// Same inputs as the main XxHash64 tests, but with the seed applied.
new TestCase(
"Nobody inspects the spammish repetition",
"Nobody inspects the spammish repetition"u8.ToArray(),
"76E6275980CF4E30"),
new TestCase(
"The quick brown fox jumps over the lazy dog",
"The quick brown fox jumps over the lazy dog"u8.ToArray(),
"5CC25b2B8248DF76"),
new TestCase(
"The quick brown fox jumps over the lazy dog.",
"The quick brown fox jumps over the lazy dog."u8.ToArray(),
"10E8D9E4DA841407"),
new TestCase(
"abc",
"abc"u8.ToArray(),
"AE9DA0E407940A85"),
new TestCase(
"123456",
"313233343536",
"99E249386C1EB47D"),
new TestCase(
"1234567890123456789012345678901234567890",
"31323334353637383930313233343536373839303132333435363738393031323334353637383930",
"0DA1BD86FAAA1BC8"),
new TestCase(
"12345678901234567890123456789012345678901",
"3132333435363738393031323334353637383930313233343536373839303132333435363738393031",
"913DFB6C43C01EB3"),
new TestCase(
"12345678901234567890123456789012345678901234",
"3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334",
"F15334D3BCFC5841"),
new TestCase(
DotNetNCHashing,
Encoding.ASCII.GetBytes(DotNetNCHashing),
"C5551996D9F3737F"),
new TestCase(
$"{ThirtyThreeBytes} (x3)",
Encoding.ASCII.GetBytes(ThirtyThreeBytes3),
"6BD086C0CC425D9F"),
new TestCase(
$"{SixtyThreeBytes} (x3)",
Encoding.ASCII.GetBytes(SixtyThreeBytes3),
"6F1C62EB48EA2FEC"),
// stripe size
new TestCase(
$"{ThirtyTwoBytes} (x3)",
Encoding.ASCII.GetBytes(ThirtyTwoBytes3),
"B358EB96B8E3E7AD"),
new TestCase(
$"{SixteenBytes} (x3)",
Encoding.ASCII.GetBytes(SixteenBytes3),
"C9B96062B49FEC42"),
};
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 xxHash64 reference implementation.
new LargeTestCase(
"EEEEE... (10GB)",
(byte)'E',
10L * 1024 * 1024 * 1024, // 10 GB
"CD7B3A954E199AE8"),
};
protected override NonCryptographicHashAlgorithm CreateInstance() => new XxHash64(Seed);
protected override byte[] StaticOneShot(byte[] source) => XxHash64.Hash(source, Seed);
protected override byte[] StaticOneShot(ReadOnlySpan<byte> source) => XxHash64.Hash(source, Seed);
protected override int StaticOneShot(ReadOnlySpan<byte> source, Span<byte> destination) =>
XxHash64.Hash(source, destination, Seed);
protected override bool TryStaticOneShot(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) =>
XxHash64.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 VerifyHashToUInt64(TestCase testCase)
{
var alg = new XxHash64(Seed);
alg.Append(testCase.Input);
AssertEqualHashNumber(testCase.OutputHex, alg.GetCurrentHashAsUInt64());
AssertEqualHashNumber(testCase.OutputHex, XxHash64.HashToUInt64(testCase.Input, Seed));
}
}
}