-
Notifications
You must be signed in to change notification settings - Fork 1
/
canbus_test.go
117 lines (113 loc) · 2.26 KB
/
canbus_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
package nmea
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestParseCANID(t *testing.T) {
var testCases = []struct {
name string
canID uint32
expect CanBusHeader
}{
{
name: "ok, 0F001DA1",
canID: 251665825, // 0F001DA1
expect: CanBusHeader{
Priority: 3,
PGN: 196608, // 0x30000
Destination: 29, // 1D
Source: 161, // A1
},
},
{
name: "ok, 0F101DB5",
canID: 252714421, // 0F101DB5
expect: CanBusHeader{
Priority: 3,
PGN: 0x31000,
Destination: 29, // 1D
Source: 181, // B5
},
},
{
name: "ok, 0F101DA1",
canID: 252714401, // 0F101DA1
expect: CanBusHeader{
Priority: 3,
PGN: 0x31000,
Destination: 29, // 1D
Source: 161, // A1
},
},
{
name: "ok, 0F0007B8",
canID: 251660216, // 0F0007B8
expect: CanBusHeader{
Priority: 3,
PGN: 196608, // 0x30000
Destination: 7, // 07
Source: 184, // B8
},
},
{
name: "ok, 130323",
canID: 0x19FD1323,
expect: CanBusHeader{
PGN: 130323,
Priority: 6,
Source: 35,
Destination: 255,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
header := ParseCANID(tc.canID)
assert.Equal(t, tc.expect, header)
})
}
}
func TestCanBusHeader_Uint32(t *testing.T) {
var testCases = []struct {
name string
when CanBusHeader
expect uint32
}{
{
name: "ok, 59904 ISORequest broadcast from nulladdr",
when: CanBusHeader{
PGN: uint32(PGNISORequest), // ISO Request
Priority: 6,
Source: AddressNull,
Destination: AddressGlobal, // everyone/broadcast
},
expect: 0x18eafffe,
},
{
name: "ok, 130311",
when: CanBusHeader{
PGN: 130311, // 0x1FD07
Priority: 5,
Source: 23, // 0x17
Destination: 255, // 0xFF
},
expect: 0x15fdff17,
},
{
name: "ok, 130310",
when: CanBusHeader{
PGN: 130310,
Priority: 5,
Source: 23, // 0x17
Destination: 255, // 0xFF
},
expect: 0x15fdff17,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.when.Uint32()
assert.Equal(t, tc.expect, result)
})
}
}