-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_test.go
187 lines (150 loc) · 3.55 KB
/
crc_test.go
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
package crc16
import (
"strings"
"testing"
)
var largeText = []byte(strings.Repeat("a", 50000))
var smallText = []byte(strings.Repeat("a", 5))
func AssetEqual(t *testing.T, expected uint16, actual uint16) {
if expected != actual {
t.Errorf("Expected 0x%04X got 0x%04X", expected, actual)
}
}
func TestCrc16EmptyVector(t *testing.T) {
AssetEqual(t, 0xFFFF, Checksum(nil, 0x9AC1, 0xFFFF, 0x0000))
}
func TestPrecalculatedNormalized(t *testing.T) {
input := []byte("abcdefgh")
t.Log("CRC-16/CCITT-FALSE")
{
c := New(0x1021, 0xFFFF, 0x0000)
AssetEqual(t, 0x9AC1, c.Checksum(input))
}
}
func TestNormalized(t *testing.T) {
input := []byte("abcdefgh")
t.Log("CRC-16/CCITT-FALSE")
{
AssetEqual(t, 0x9AC1, Checksum(input, 0x1021, 0xFFFF, 0x0000))
}
/*
t.Log("CRC-16/ARC")
{
CRC-16/ARC 0xE8C1 0xBB3D 0x8005 0x0000 true true 0x0000
}
*/
t.Log("CRC-16/AUG-CCITT")
{
AssetEqual(t, 0x4AC6, Checksum(input, 0x1021, 0x1D0F, 0x0000))
}
t.Log("CRC-16/BUYPASS")
{
AssetEqual(t, 0x7D68, Checksum(input, 0x8005, 0x0000, 0x0000))
}
t.Log("CRC-16/CDMA2000")
{
AssetEqual(t, 0x2007, Checksum(input, 0xC867, 0xFFFF, 0x0000))
}
t.Log("CRC-16/DDS-110")
{
AssetEqual(t, 0x7388, Checksum(input, 0x8005, 0x800D, 0x0000))
}
t.Log("CRC-16/DECT-R")
{
AssetEqual(t, 0x73B6, Checksum(input, 0x0589, 0x0000, 0x0001))
}
t.Log("CRC-16/DECT-X")
{
AssetEqual(t, 0x73B7, Checksum(input, 0x0589, 0x0000, 0x0000))
}
//t.Log("CRC-16/DNP")
//{
// CRC-16/DNP 0xB350 0xEA82 0x3D65 0x0000 true true 0xFFFF
//}
t.Log("CRC-16/EN-13757")
{
AssetEqual(t, 0x4A7D, Checksum(input, 0x3D65, 0x0000, 0xFFFF))
}
t.Log("CRC-16/GENIBUS")
{
AssetEqual(t, 0x653E, Checksum(input, 0x1021, 0xFFFF, 0xFFFF))
}
//t.Log("CRC-16/MAXIM")
//{
// CRC-16/MAXIM 0x173E 0x44C2 0x8005 0x0000 true true 0xFFFF
//}
//t.Log("CRC-16/MCRF4XX")
//{
// CRC-16/MCRF4XX 0x7D08 0x6F91 0x1021 0xFFFF true true 0x0000
//}
//t.Log("CRC-16/RIELLO")
//{
// CRC-16/RIELLO 0xEB3B 0x63D0 0x1021 0xB2AA true true 0x0000
//}
t.Log("CRC-16/T10-DIF")
{
AssetEqual(t, 0xE9A3, Checksum(input, 0x8BB7, 0x0000, 0x0000))
}
t.Log("CRC-16/TELEDISK")
{
AssetEqual(t, 0x9F9D, Checksum(input, 0xA097, 0x0000, 0x0000))
}
//t.Log("CRC-16/TMS37157")
//{
// CRC-16/TMS37157 0xF7B8 0x26B1 0x1021 0x89EC true true 0x0000
//}
//t.Log("CRC-16/USB")
//{
// CRC-16/USB 0x5781 0xB4C8 0x8005 0xFFFF true true 0xFFFF
//}
//t.Log("CRC-A")
//{
// CRC-A 0x2371 0xBF05 0x1021 0xC6C6 true true 0x0000
//}
//t.Log("CRC-16/KERMIT")
//{
// CRC-16/KERMIT 0x728F 0x2189 0x1021 0x0000 true true 0x0000
//}
//t.Log("CRC-16/MODBUS")
//{
// CRC-16/MODBUS 0xA87E 0x4B37 0x8005 0xFFFF true true 0x0000
//}
//t.Log("CRC-16/X-25")
//{
// CRC-16/X-25 0x82F7 0x906E 0x1021 0xFFFF true true 0xFFFF
//}
t.Log("CRC-16/XMODEM")
{
AssetEqual(t, 0xABFF, Checksum(input, 0x1021, 0x0000, 0x0000))
}
}
func BenchmarkCrcSmall(b *testing.B) {
b.ResetTimer()
b.SetBytes(int64(len(smallText)))
for n := 0; n < b.N; n++ {
Checksum(smallText, 0x9AC1, 0xFFFF, 0x0000)
}
}
func BenchmarkCrcLarge(b *testing.B) {
b.ResetTimer()
b.SetBytes(int64(len(largeText)))
for n := 0; n < b.N; n++ {
Checksum(largeText, 0x9AC1, 0xFFFF, 0x0000)
}
}
func BenchmarkPrecalculatedCrcSmall(b *testing.B) {
c := New(0x1021, 0xFFFF, 0x0000)
b.ResetTimer()
b.SetBytes(int64(len(smallText)))
for n := 0; n < b.N; n++ {
c.Checksum(smallText)
}
}
func BenchmarkPrecalculatedCrcLarge(b *testing.B) {
c := New(0x1021, 0xFFFF, 0x0000)
b.ResetTimer()
b.SetBytes(int64(len(largeText)))
for n := 0; n < b.N; n++ {
c.Checksum(largeText)
}
}