-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathImplementationCTest.cs
More file actions
131 lines (119 loc) · 3.85 KB
/
ImplementationCTest.cs
File metadata and controls
131 lines (119 loc) · 3.85 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
using System;
using System.Linq;
using System.Text;
using NUnit.Framework;
#if NETFRAMEWORK
using E = Crc32C.Crc32CAlgorithm;
#endif
namespace Force.Crc32.Tests
{
[TestFixture]
public class ImplementationCTest
{
[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);
#if NETFRAMEWORK
var crc1 = E.Compute(bytes.Skip(offset).ToArray());
var crc2 = Crc32CAlgorithm.Append(0, bytes, offset, bytes.Length - offset);
Assert.That(crc2, Is.EqualTo(crc1));
#elif NETCOREAPP3_0_OR_GREATER
if(Intrinsics.Crc32CAlgorithm.IsSupported)
{
var crc32C = new Ralph.Crc32C.Crc32C();
crc32C.Update(bytes, offset, bytes.Length - offset);
var crc3 = Intrinsics.Crc32CAlgorithm.Compute(bytes, offset, bytes.Length - offset);
Assert.That(crc3, Is.EqualTo(crc32C.GetIntValue()));
}
#endif
}
[Test]
public void ResultConsistency2()
{
Assert.That(Crc32CAlgorithm.Compute(new byte[] { 1 }), Is.EqualTo(0xA016D052));
Assert.That(Crc32CAlgorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(0xB219DB69));
}
#if NETCOREAPP3_0_OR_GREATER
[Test]
public void ResultConsistencyIntrinsics()
{
if(Intrinsics.Crc32CAlgorithm.IsSupported)
{
Assert.That(Intrinsics.Crc32CAlgorithm.Compute(new byte[] { 1 }), Is.EqualTo(0xA016D052));
Assert.That(Intrinsics.Crc32CAlgorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(0xB219DB69));
}
}
#endif
#if NETFRAMEWORK
[Test]
public void ResultConsistencyAsHashAlgorithm()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var e = new E();
// other implementation is little-endian
var c = new Crc32CAlgorithm(false);
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
[Test]
public void PartIsWhole()
{
var bytes = new byte[30000];
new Random().NextBytes(bytes);
var r1 = Crc32CAlgorithm.Append(0, bytes, 0, 15000);
var r2 = Crc32CAlgorithm.Append(r1, bytes, 15000, 15000);
var r3 = Crc32CAlgorithm.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 = Crc32CAlgorithm.Append(0, bytes, 0, bytes.Length);
var crc2Bytes = new Crc32CAlgorithm().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);
Crc32CAlgorithm.ComputeAndWriteToEnd(buf);
Assert.That(Crc32CAlgorithm.IsValidWithCrcAtEnd(buf), Is.True);
buf[r.Next(buf.Length)] ^= 0x1;
Assert.That(Crc32CAlgorithm.IsValidWithCrcAtEnd(buf), Is.False);
// partial test
if (length > 2)
{
Crc32CAlgorithm.ComputeAndWriteToEnd(buf, 1, length - 2);
Assert.That(Crc32CAlgorithm.IsValidWithCrcAtEnd(buf, 1, length - 2 + 4), Is.True);
buf[1 + r.Next(buf.Length - 2)] ^= 0x1;
Assert.That(Crc32CAlgorithm.IsValidWithCrcAtEnd(buf, 1, length - 2 + 4), Is.False);
}
}
}
}