-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelpers_test.go
152 lines (145 loc) · 3.37 KB
/
helpers_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
150
151
152
// Copyright (c) 2015-2024 The usbtmc developers. All rights reserved.
// Project site: https://github.com/gotmc/usbtmc
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package usbtmc
import "testing"
func TestNextBtag(t *testing.T) {
testCases := []struct {
bTag byte
nextbTag byte
}{
{0x01, 0x02},
{0xff, 0x01},
{255, 1},
{1, 2},
{10, 11},
{254, 255},
}
for _, testCase := range testCases {
nextbTag := nextbTag(testCase.bTag)
if nextbTag != testCase.nextbTag {
t.Errorf(
"nextbTag == %x, want %x for given bTag %x",
nextbTag, testCase.nextbTag, testCase.bTag)
}
}
}
func TestInvertingBtag(t *testing.T) {
testCases := []struct {
bTag byte
bTagInverse byte
}{
{0x00, 0xff},
{0x0f, 0xf0},
{0x55, 0xaa},
{0xaa, 0x55},
{0xf0, 0x0f},
{0xff, 0x00},
}
for _, testCase := range testCases {
bTagInverse := invertbTag(testCase.bTag)
if bTagInverse != testCase.bTagInverse {
t.Errorf(
"bTagInverse == %x, want %x for bTag %x",
bTagInverse, testCase.bTagInverse, testCase.bTag)
}
}
}
func TestEncodeBulkHeaderPrefix(t *testing.T) {
tests := []struct {
msgID msgID
bTag byte
headerPrefix [4]byte
}{
{devDepMsgOut, 2, [4]byte{0x01, 0x02, 0xfd, 0x00}},
{devDepMsgOut, 129, [4]byte{0x01, 0x81, 0x7e, 0x00}},
{devDepMsgOut, 255, [4]byte{0x01, 0xff, 0x00, 0x00}},
{devDepMsgOut, 1, [4]byte{0x01, 0x01, 0xfe, 0x00}},
{requestDevDepMsgIn, 4, [4]byte{0x02, 0x04, 0xfb, 0x00}},
{vendorSpecificOut, 4, [4]byte{0x7e, 0x04, 0xfb, 0x00}},
{requestVendorSpecificIn, 4, [4]byte{0x7f, 0x04, 0xfb, 0x00}},
}
for _, test := range tests {
headerPrefix := encodeBulkHeaderPrefix(test.bTag, test.msgID)
if headerPrefix != test.headerPrefix {
t.Errorf(
"headerPrefix == %x, want %x",
headerPrefix, test.headerPrefix)
}
}
}
func TestEncodeBulkOutHeader(t *testing.T) {
tests := []struct {
transferSize uint32
eom bool
bTag byte
desired [12]byte
}{
{
9,
true,
1,
[12]byte{0x01, 0x01, 0xfe, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
},
{
256,
false,
2,
[12]byte{0x01, 0x02, 0xfd, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
256,
true,
2,
[12]byte{0x01, 0x02, 0xfd, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
},
{
512,
true,
2,
[12]byte{0x01, 0x02, 0xfd, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
},
}
for _, test := range tests {
result := encodeBulkOutHeader(test.bTag, test.transferSize, test.eom)
if result != test.desired {
t.Errorf("BulkOutHeader == %x, want %x", result, test.desired)
}
}
}
func TestEncodeMsgInBulkOutHeader(t *testing.T) {
tests := []struct {
bTag byte
transferSize uint32
termCharEnabled bool
termChar byte
desired [12]byte
}{
{
1,
9,
true,
'\n',
[12]byte{0x02, 0x01, 0xfe, 0x00, 0x09, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x00, 0x00},
},
{
2,
512,
true,
'\n',
[12]byte{0x02, 0x02, 0xfd, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x0a, 0x00, 0x00},
},
}
for _, test := range tests {
result := encodeMsgInBulkOutHeader(
test.bTag,
test.transferSize,
test.termCharEnabled,
test.termChar,
)
if result != test.desired {
t.Errorf("BulkOutHeader == %x, want %x", result, test.desired)
}
}
}