-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_test.go
134 lines (108 loc) · 2.49 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
package crc8
import (
"strings"
"testing"
)
var largeText = []byte(strings.Repeat("a", 500000))
var smallText = []byte(strings.Repeat("a", 5))
func AssetEqual(t *testing.T, expected uint8, actual uint8) {
if expected != actual {
t.Errorf("Expected 0x%02X got 0x%02X", expected, actual)
}
}
func TestCrc8EmptyVector(t *testing.T) {
AssetEqual(t, 0x00, Checksum(nil, 0x07, 0x00, 0x00))
}
func TestPrecalculatedNormalized(t *testing.T) {
input := []byte("abcdefgh")
t.Log("CRC8/CRC-8")
{
c := New(0x07, 0x00, 0x00)
AssetEqual(t, 0xCB, c.Checksum(input))
}
}
func TestGenericNormalized(t *testing.T) {
input := []byte("abcdefgh")
t.Log("CRC-8/CRC-8")
{
AssetEqual(t, 0xCB, Checksum(input, 0x07, 0x00, 0x00))
}
t.Log("CRC-8/SAE-J1850")
{
AssetEqual(t, 0xD7, Checksum(input, 0x1D, 0xFF, 0xFF))
}
t.Log("CRC-8/SAE-J1850-ZERO")
{
AssetEqual(t, 0x3E, Checksum(input, 0x1D, 0x00, 0x00))
}
t.Log("CRC-8/8H2F")
{
AssetEqual(t, 0x54, Checksum(input, 0x2F, 0xFF, 0xFF))
}
t.Log("CRC-8/CDMA2000")
{
AssetEqual(t, 0xF7, Checksum(input, 0x9B, 0xFF, 0x00))
}
//t.Log("CRC-8/DARC")
//{
//AssetEqual(t, 0x62, Checksum(input, 0x39, 0x00, 0x00, true, true))
//}
t.Log("CRC-8/DVB-S2")
{
AssetEqual(t, 0x62, Checksum(input, 0xD5, 0x00, 0x00))
}
//t.Log("CRC8/EBU")
//{
//AssetEqual(t, 0x41, Checksum(input, 0x1D, 0xFF, 0x00, true, true))
//}
t.Log("CRC-8/ICODE")
{
AssetEqual(t, 0x96, Checksum(input, 0x1D, 0XFD, 0x00))
}
t.Log("CRC-8/ITU")
{
AssetEqual(t, 0x9E, Checksum(input, 0x7, 0X00, 0x55))
}
//t.Log("CRC-8/MAXIM")
//{
//AssetEqual(t, 0x92, Checksum(input, 0x31, 0X00, 0x00, true, true))
//}
//t.Log("CRC-8/ROHC")
//{
//AssetEqual(t, 0x15, Checksum(input, 0x7, 0XFF, 0x0))
//}
//t.Log("CRC-8/ITU")
//{
//AssetEqual(t, 0x3D, Checksum(input, 0x9B, 0X00, 0x00))
//}
}
func BenchmarkPrecalculatedCrcSmall(b *testing.B) {
c := New(0x07, 0x00, 0x00)
b.ResetTimer()
b.SetBytes(int64(len(smallText)))
for n := 0; n < b.N; n++ {
c.Checksum(smallText)
}
}
func BenchmarkPrecalculatedCrcLarge(b *testing.B) {
c := New(0x07, 0x00, 0x00)
b.ResetTimer()
b.SetBytes(int64(len(largeText)))
for n := 0; n < b.N; n++ {
c.Checksum(largeText)
}
}
func BenchmarkCrcSmall(b *testing.B) {
b.ResetTimer()
b.SetBytes(int64(len(smallText)))
for n := 0; n < b.N; n++ {
Checksum(smallText, 0x07, 0x00, 0x00)
}
}
func BenchmarkCrcLarge(b *testing.B) {
b.ResetTimer()
b.SetBytes(int64(len(largeText)))
for n := 0; n < b.N; n++ {
Checksum(largeText, 0x07, 0x00, 0x00)
}
}