-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathImplementationTest.cs
More file actions
147 lines (131 loc) · 4.29 KB
/
ImplementationTest.cs
File metadata and controls
147 lines (131 loc) · 4.29 KB
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
using System;
#if NETCORE30
using System.Buffers.Binary;
#endif
using System.Linq;
using System.Text;
using NUnit.Framework;
#if !NETCORE
using E = Crc32.Crc32Algorithm;
#endif
namespace Force.Crc32.Tests
{
[TestFixture]
public class ImplementationTest
{
#if !NETCORE
[TestCase("Hello", 3)]
[TestCase("Nazdar", 0)]
[TestCase("Ahoj", 1)]
[TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 0)]
[TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 3)]
public void ResultConsistency(string text, int offset)
{
var bytes = Encoding.ASCII.GetBytes(text);
var crc1 = E.Compute(bytes.Skip(offset).ToArray());
var crc2 = Crc32Algorithm.Append(0, bytes, offset, bytes.Length - offset);
Assert.That(crc2, Is.EqualTo(crc1));
}
#endif
[Test]
public void ResultConsistency2()
{
Assert.That(Crc32Algorithm.Compute(new byte[] { 1 }), Is.EqualTo(2768625435));
Assert.That(Crc32Algorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(622876539));
}
#if !NETCORE
[Test]
public void ResultConsistencyAsHashAlgorithm()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var e = new E();
var c = new Crc32Algorithm();
var crc1 = BitConverter.ToInt32(e.ComputeHash(bytes), 0);
var crc2 = BitConverter.ToInt32(c.ComputeHash(bytes), 0);
Console.WriteLine(crc1.ToString("X8"));
Console.WriteLine(crc2.ToString("X8"));
Assert.That(crc1, Is.EqualTo(crc2));
}
#endif
#if NETCORE30
[Test]
public void ResultConsistencyAsHashAlgorithm_SpanVsArray()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var e = new Crc32Algorithm();
var c = new Crc32Algorithm();
var crc1 = BitConverter.ToInt32(e.ComputeHash(bytes), 0);
var dest = new byte[4];
Assert.That(c.TryComputeHash(bytes, dest, out var bytesWritten), Is.True);
Assert.That(bytesWritten, Is.EqualTo(4));
var crc2 = BinaryPrimitives.ReadInt32LittleEndian(dest);
Console.WriteLine(crc1.ToString("X8"));
Console.WriteLine(crc2.ToString("X8"));
Assert.That(crc1, Is.EqualTo(crc2));
}
#endif
[Test]
public void PartIsWhole()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var r1 = Crc32Algorithm.Append(0, bytes, 0, 15000);
var r2 = Crc32Algorithm.Append(r1, bytes, 15000, 15000);
var r3 = Crc32Algorithm.Append(0, bytes, 0, 30000);
Assert.That(r2, Is.EqualTo(r3));
}
[Test]
public void Result_Is_BigEndian()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var crc1 = Crc32Algorithm.Append(0, bytes, 0, bytes.Length);
var crc2Bytes = new Crc32Algorithm().ComputeHash(bytes);
if (BitConverter.IsLittleEndian) crc2Bytes = crc2Bytes.Reverse().ToArray();
var crc2 = BitConverter.ToUInt32(crc2Bytes, 0);
Assert.That(crc2, Is.EqualTo(crc1));
}
[Test]
public void Result_Is_LittleEndian_IF_Specified()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var crc1 = Crc32Algorithm.Append(0, bytes, 0, bytes.Length);
var crc2Bytes = new Crc32Algorithm(false).ComputeHash(bytes);
if (!BitConverter.IsLittleEndian) crc2Bytes = crc2Bytes.Reverse().ToArray();
var crc2 = BitConverter.ToUInt32(crc2Bytes, 0);
Assert.That(crc2, Is.EqualTo(crc1));
}
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
[TestCase(5)]
[TestCase(11)]
[TestCase(30)]
[TestCase(200)]
[TestCase(10000)]
public void Computation_With_Crc_End_Should_Be_Validated(int length)
{
var buf = new byte[length + 4];
var r = new Random();
r.NextBytes(buf);
Crc32Algorithm.ComputeAndWriteToEnd(buf);
Assert.That(Crc32Algorithm.IsValidWithCrcAtEnd(buf), Is.True);
buf[r.Next(buf.Length)] ^= 0x1;
Assert.That(Crc32Algorithm.IsValidWithCrcAtEnd(buf), Is.False);
// partial test
if (length > 2)
{
Crc32Algorithm.ComputeAndWriteToEnd(buf, 1, length - 2);
Assert.That(Crc32Algorithm.IsValidWithCrcAtEnd(buf, 1, length - 2 + 4), Is.True);
buf[1 + r.Next(buf.Length - 2)] ^= 0x1;
Assert.That(Crc32Algorithm.IsValidWithCrcAtEnd(buf, 1, length - 2 + 4), Is.False);
}
}
}
}