-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBytePatternsTest.cs
More file actions
94 lines (84 loc) · 1.73 KB
/
BytePatternsTest.cs
File metadata and controls
94 lines (84 loc) · 1.73 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
#if NETFRAMEWORK
using System;
using System.Linq;
using Force.Crc32.Tests.Crc32Implementations;
using NUnit.Framework;
namespace Force.Crc32.Tests
{
[TestFixture]
public class BytePatternsTest
{
[Test]
public void Crc32ForEmptySequenseIs0()
{
var actual = Crc32Algorithm.Compute(new byte[0]);
Assert.That(actual, Is.EqualTo(0));
}
// Pattern:
// xx
// xx xx
// xx xx xx
// ...
[Test]
public void RepeatedBytePatternTest()
{
foreach (var x in Enumerable.Range(0, 256))
{
foreach (int len in Enumerable.Range(1, 32))
{
var data = Enumerable.Repeat((byte)x, len).ToArray();
TestByteSequence(data);
}
}
}
// Pattern:
// xx
// xx 00
// 00 xx
// xx 00 00
// 00 xx 00
// 00 00 xx
// ...
// xx
// xx FF
// FF xx
// xx FF FF
// FF xx FF
// FF FF xx
// ...
[TestCase(0x00)]
[TestCase(0xFF)]
public void SlidingBytePatternTest(byte fillValue)
{
foreach (int len in Enumerable.Range(1, 32))
{
var data = Enumerable.Repeat(fillValue, len).ToArray();
foreach (var i in Enumerable.Range(0, len))
{
foreach (var x in Enumerable.Range(0, 256))
{
data[i] = (byte)x;
TestByteSequence(data);
}
data[i] = fillValue;
}
}
}
private void TestByteSequence(byte[] data)
{
var actual = Crc32Algorithm.Compute(data);
var expected = _referenceImplementation.Calculate(data);
if (expected != actual)
{
var message = string.Format(
"Test failed for {0}\nExpected: {1:x8}\nBut was: {2:x8}",
BitConverter.ToString(data),
expected,
actual);
Assert.Fail(message);
}
}
private readonly CrcCalculator _referenceImplementation = new System_Data_HashFunction_CRC();
}
}
#endif