forked from sigurn/crc16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc16_test.go
149 lines (115 loc) · 3 KB
/
crc16_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
package crc16
import (
"fmt"
"testing"
)
var testData = []byte("123456789")
func testSelectedCRC(params Params, t *testing.T) {
table := MakeTable(params)
if table == nil {
t.Errorf("Failed to create %q computer", params.Name)
}
crc := Checksum(testData, table)
if crc != table.params.Check {
t.Errorf("Invalid %q sample calculation, expected: %X, actual: %X", table.params.Name, table.params.Check, crc)
}
}
func TestCRC16_ARC(t *testing.T) {
testSelectedCRC(CRC16_ARC, t)
}
func TestCRC16_AUG_CCIT(t *testing.T) {
testSelectedCRC(CRC16_AUG_CCITT, t)
}
func TestCRC16_BUYPASS(t *testing.T) {
testSelectedCRC(CRC16_BUYPASS, t)
}
func TestCRC16_CCITT_FALSE(t *testing.T) {
testSelectedCRC(CRC16_CCITT_FALSE, t)
}
func TestCRC16_CDMA2000(t *testing.T) {
testSelectedCRC(CRC16_CDMA2000, t)
}
func TestCRC16_DDS_110(t *testing.T) {
testSelectedCRC(CRC16_DDS_110, t)
}
func TestCRC16_DECT_R(t *testing.T) {
testSelectedCRC(CRC16_DECT_R, t)
}
func TestCRC16_DECT_X(t *testing.T) {
testSelectedCRC(CRC16_DECT_X, t)
}
func TestCRC16_DNP(t *testing.T) {
testSelectedCRC(CRC16_DNP, t)
}
func TestCRC16_EN_13757(t *testing.T) {
testSelectedCRC(CRC16_EN_13757, t)
}
func TestCRC16_GENIBUS(t *testing.T) {
testSelectedCRC(CRC16_GENIBUS, t)
}
func TestCRC16_MAXIM(t *testing.T) {
testSelectedCRC(CRC16_MAXIM, t)
}
func TestCRC16_MCRF4XX(t *testing.T) {
testSelectedCRC(CRC16_MCRF4XX, t)
}
func TestCRC16_RIELLO(t *testing.T) {
testSelectedCRC(CRC16_RIELLO, t)
}
func TestCRC16_T10_DIF(t *testing.T) {
testSelectedCRC(CRC16_T10_DIF, t)
}
func TestCRC16_TELEDISK(t *testing.T) {
testSelectedCRC(CRC16_TELEDISK, t)
}
func TestCRC16_TMS37157(t *testing.T) {
testSelectedCRC(CRC16_TMS37157, t)
}
func TestCRC16_USB(t *testing.T) {
testSelectedCRC(CRC16_USB, t)
}
func TestCRC16_CRC_A(t *testing.T) {
testSelectedCRC(CRC16_CRC_A, t)
}
func TestCRC16_KERMIT(t *testing.T) {
testSelectedCRC(CRC16_KERMIT, t)
}
func TestCRC16_MODBUS(t *testing.T) {
testSelectedCRC(CRC16_MODBUS, t)
}
func TestCRC16_X_25(t *testing.T) {
testSelectedCRC(CRC16_X_25, t)
}
func TestCRC16_XMODEM(t *testing.T) {
testSelectedCRC(CRC16_XMODEM, t)
}
func TestHash(t *testing.T) {
tbl := MakeTable(CRC16_XMODEM)
h := New(tbl)
fmt.Fprint(h, "standard")
fmt.Fprint(h, " library hash interface")
sum1 := h.Sum16()
h.Reset()
fmt.Fprint(h, "standard library hash interface")
sum2 := h.Sum16()
if sum1 != sum2 {
t.Errorf("CRC16 checksums for chunked input %x should be equal %x", sum1, sum2)
}
var crc uint16 = 0xe698
if sum1 != crc {
t.Errorf("CRC16 for input should equal %x but was %x", crc, sum1)
}
if h.Size() != 2 {
t.Errorf("CRC16 checksum should have a size of 2 byte. But was %d", h.Size())
}
buf := make([]byte, 0, 10)
buf = h.Sum(buf)
expected := []byte{0xe6, 0x98}
if len(buf) != 2 || buf[0] != expected[0] || buf[1] != expected[1] {
t.Errorf("CRC16 checksum should append %v to slice, but was %v", expected, buf)
}
// for the 100% test coverage
if h.BlockSize() != 1 {
t.Errorf("Block size should return 1")
}
}