-
Notifications
You must be signed in to change notification settings - Fork 89
/
crc_test.go
106 lines (87 loc) · 2.34 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
package modbus
import (
"testing"
)
func TestCRC(t *testing.T) {
var c crc
var out []byte
// initialize the CRC object and make sure we get 0xffff as init value
c.init()
if c.crc != 0xffff {
t.Errorf("expected 0xffff, saw 0x%04x", c.crc)
}
out = c.value()
if len(out) != 2 {
t.Errorf("value() should have returned 2 bytes, got %v", len(out))
}
if out[0] != 0xff || out[1] != 0xff {
t.Errorf("expected {0xff, 0xff} got {0x%02x, 0x%02x}", out[0], out[1])
}
// add a few bytes, check the output
c.add([]byte{0x01, 0x02, 0x03, 0x04, 0x05})
if c.crc != 0xbb2a {
t.Errorf("expected 0xbb2a, saw 0x%04x", c.crc)
}
out = c.value()
if len(out) != 2 {
t.Errorf("value() should have returned 2 bytes, got %v", len(out))
}
if out[0] != 0x2a || out[1] != 0xbb {
t.Errorf("expected {0x2a, 0xbb} got {0x%02x, 0x%02x}", out[0], out[1])
}
// add one extra byte, test the output again
c.add([]byte{0x06})
if c.crc != 0xddba {
t.Errorf("expected 0xddba, saw 0x%04x", c.crc)
}
out = c.value()
if len(out) != 2 {
t.Errorf("value() should have returned 2 bytes, got %v", len(out))
}
if out[0] != 0xba || out[1] != 0xdd {
t.Errorf("expected {0xba, 0xdd} got {0x%02x, 0x%02x}", out[0], out[1])
}
// init the CRC once again: the output bytes should be back to 0xffff
c.init()
if c.crc != 0xffff {
t.Errorf("expected 0xffff, saw 0x%04x", c.crc)
}
out = c.value()
if len(out) != 2 {
t.Errorf("value() should have returned 2 bytes, got %v", len(out))
}
if out[0] != 0xff || out[1] != 0xff {
t.Errorf("expected {0xff, 0xff} got {0x%02x, 0x%02x}", out[0], out[1])
}
return
}
func TestCRCIsEqual(t *testing.T) {
var c crc
var out []byte
// initialize the CRC object and feed it a few bytes
c.init()
c.add([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06})
// make sure the register value is what it should be
if c.crc != 0xddba {
t.Errorf("expected 0xddba, saw 0x%04x", c.crc)
}
// positive test
if !c.isEqual(0xba, 0xdd) {
t.Error("isEqual() should have returned true")
}
// negative test
if c.isEqual(0xdd, 0xba) {
t.Error("isEqual() should have returned false")
}
// loopback test
out = c.value()
if !c.isEqual(out[0], out[1]) {
t.Error("isEqual() should have returned true")
}
// an empty payload should have a CRC of 0xffff
c.init()
if !c.isEqual(0xff, 0xff) {
t.Error("isEqual() should have returned true")
}
return
}