-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_test.go
65 lines (55 loc) · 1.67 KB
/
table_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
package crcutil
import (
"testing"
)
var ibmcrc16 = (&Poly[uint16]{Word: 0x8005, Width: 16}).ReversedForm()
// TestMakeTable is creating a lookup table multiple times,
// each time calculating the same example checksum.
// Between sub tests, the table cache state is verified.
func TestMakeTable(t *testing.T) {
cacheKey := tableCacheKey(ibmcrc16, &tableConf{
dataWidth: 8,
})
t.Run("verify cache key", func(t *testing.T) {
if cacheKey != "a001.r-0.8" {
t.Fatalf("cache key mismatch: %q", cacheKey)
}
})
t.Run("verify nil cache state", func(t *testing.T) {
if tableCache[cacheKey] != nil {
t.Fatal("tableCache entry not nil")
}
})
t.Run("with empty cache entry", checkMakeTable)
var tab []uint16
// now there must be an entry for ibmcrc16 in the cache
t.Run("verify initialized cache state", func(t *testing.T) {
u, ok := tableCache[cacheKey].([]uint16)
if !ok {
t.Fatal("tableCache entry missing")
}
tab = u
})
t.Run("with existing cache entry", checkMakeTable)
if update16(0xFFFF, tab, modExFrame) != 0x1241 {
t.Fatal("unexpected failure when using cached table directly")
}
}
var modExFrame = []byte{2, 7}
// CheckMakeTable creates a table for the CRC-16-IBM polynomial,
// then calculates a checksum over an example frame {2, 7}
// from the Modbus over serial line specification, and compares
// the result with the expected value.
func checkMakeTable(t *testing.T) {
tab := ibmcrc16.MakeTable()
crc := update16(0xFFFF, tab, modExFrame)
if crc != 0x1241 {
t.Errorf("crc mismatch: want 0x1241, got %#04x", crc)
}
}
func update16(crc uint16, tab []uint16, p []byte) uint16 {
for _, v := range p {
crc = tab[byte(crc)^v] ^ (crc >> 8)
}
return crc
}