-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Crc32Tests.cs
145 lines (125 loc) · 4.43 KB
/
Crc32Tests.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
// 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 Crc32Tests : NonCryptoHashTestDriver
{
private static readonly byte[] s_emptyHashValue = new byte[4];
public Crc32Tests()
: 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;
}
}
}
protected static IEnumerable<TestCase> TestCaseDefinitions { get; } =
new[]
{
new TestCase(
"Empty",
"",
"00000000"),
new TestCase(
"One",
"01",
"1BDF05A5"),
new TestCase(
"Zero-Residue",
"00000000",
"1CDF4421"),
new TestCase(
"Zero-InverseResidue",
"FFFFFFFF",
"FFFFFFFF"),
new TestCase(
"Self-test 123456789",
"123456789"u8.ToArray(),
"2639F4CB"),
new TestCase(
"Self-test residue",
"3132333435363738392639F4CB",
"1CDF4421"),
new TestCase(
"Self-test inverse residue",
"313233343536373839D9C60B34",
"FFFFFFFF"),
new TestCase(
"The quick brown fox jumps over the lazy dog",
"The quick brown fox jumps over the lazy dog"u8.ToArray(),
"39A34F41"),
};
protected override NonCryptographicHashAlgorithm CreateInstance() => new Crc32();
protected override byte[] StaticOneShot(byte[] source) => Crc32.Hash(source);
protected override byte[] StaticOneShot(ReadOnlySpan<byte> source) => Crc32.Hash(source);
protected override int StaticOneShot(ReadOnlySpan<byte> source, Span<byte> destination) =>
Crc32.Hash(source, destination);
protected override bool TryStaticOneShot(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) =>
Crc32.TryHash(source, destination, out bytesWritten);
[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(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);
}
}
}