-
Notifications
You must be signed in to change notification settings - Fork 37
/
data_test.go
333 lines (319 loc) · 8.26 KB
/
data_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package can
import (
"fmt"
"testing"
"testing/quick"
"gotest.tools/v3/assert"
)
func TestData_Bit(t *testing.T) {
for i, tt := range []struct {
data Data
bits []struct {
i uint8
bit bool
}
}{
{
data: Data{0x01, 0x23},
bits: []struct {
i uint8
bit bool
}{
// nibble 1: 0x1
{bit: true, i: 0},
{bit: false, i: 1},
{bit: false, i: 2},
{bit: false, i: 3},
// nibble 2: 0x0
{bit: false, i: 4},
{bit: false, i: 5},
{bit: false, i: 6},
{bit: false, i: 7},
// nibble 3: 0x3
{bit: true, i: 8},
{bit: true, i: 9},
{bit: false, i: 10},
{bit: false, i: 11},
// nibble 4: 0x2
{bit: false, i: 12},
{bit: true, i: 13},
{bit: false, i: 14},
{bit: false, i: 15},
},
},
} {
i, tt := i, tt
t.Run("Get", func(t *testing.T) {
i, tt := i, tt
for j, ttBit := range tt.bits {
j, ttBit := j, ttBit
t.Run(fmt.Sprintf("tt=%v,bit=%v", i, j), func(t *testing.T) {
bit := tt.data.Bit(ttBit.i)
assert.Equal(t, ttBit.bit, bit)
})
}
})
t.Run("Set", func(t *testing.T) {
i, tt := i, tt
t.Run(fmt.Sprintf("data=%v", i), func(t *testing.T) {
var data Data
for _, tBit := range tt.bits {
data.SetBit(tBit.i, tBit.bit)
}
assert.DeepEqual(t, tt.data, data)
})
})
}
}
func TestData_Property_SetGetBit(t *testing.T) {
f := func(_ Data, _ uint8, bit bool) bool {
return bit
}
g := func(data Data, i uint8, bit bool) bool {
i %= 64
data.SetBit(i, bit)
return data.Bit(i)
}
assert.NilError(t, quick.CheckEqual(f, g, nil))
}
func TestData_LittleEndian(t *testing.T) {
for i, tt := range []struct {
data Data
signals []struct {
start uint8
length uint8
unsigned uint64
signed int64
}
}{
{
data: Data{0x80, 0x01},
signals: []struct {
start uint8
length uint8
unsigned uint64
signed int64
}{
{start: 7, length: 2, unsigned: 0x3, signed: -1},
},
},
{
data: Data{0x01, 0x02, 0x03},
signals: []struct {
start uint8
length uint8
unsigned uint64
signed int64
}{
{start: 0, length: 24, unsigned: 0x030201, signed: 197121},
},
},
{
data: Data{0x40, 0x23, 0x01, 0x12},
signals: []struct {
start uint8
length uint8
unsigned uint64
signed int64
}{
{start: 24, length: 8, unsigned: 0x12, signed: 18},
{start: 8, length: 8, unsigned: 0x23, signed: 35},
{start: 4, length: 16, unsigned: 0x1234, signed: 4660},
},
},
} {
i, tt := i, tt
t.Run(fmt.Sprintf("UnsignedBits:%v", i), func(t *testing.T) {
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("signal:%v", j), func(t *testing.T) {
actual := tt.data.UnsignedBitsLittleEndian(signal.start, signal.length)
assert.Equal(t, signal.unsigned, actual)
})
}
})
t.Run(fmt.Sprintf("SignedBits:%v", i), func(t *testing.T) {
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("signal:%v", j), func(t *testing.T) {
actual := tt.data.SignedBitsLittleEndian(signal.start, signal.length)
assert.Equal(t, signal.signed, actual)
})
}
})
t.Run(fmt.Sprintf("SetUnsignedBits:%v", i), func(t *testing.T) {
var data Data
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("data:%v", j), func(_ *testing.T) {
data.SetUnsignedBitsLittleEndian(signal.start, signal.length, signal.unsigned)
})
}
assert.DeepEqual(t, tt.data, data)
})
t.Run(fmt.Sprintf("SetSignedBits:%v", i), func(t *testing.T) {
var data Data
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("data:%v", j), func(_ *testing.T) {
data.SetSignedBitsLittleEndian(signal.start, signal.length, signal.signed)
})
}
assert.DeepEqual(t, tt.data, data)
})
}
}
func TestData_BigEndian(t *testing.T) {
for i, tt := range []struct {
data Data
signals []struct {
start uint8
length uint8
unsigned uint64
signed int64
}
}{
{
data: Data{0x3f, 0xf7, 0x0d, 0xc4, 0x0c, 0x93, 0xff, 0xff},
signals: []struct {
start uint8
length uint8
unsigned uint64
signed int64
}{
{start: 7, length: 3, unsigned: 0x1, signed: 1},
{start: 4, length: 1, unsigned: 0x1, signed: -1},
{start: 55, length: 16, unsigned: 0xffff, signed: -1},
{start: 39, length: 16, unsigned: 0xc93, signed: 3219},
{start: 23, length: 16, unsigned: 0xdc4, signed: 3524},
{start: 3, length: 12, unsigned: 0xff7, signed: -9},
},
},
{
data: Data{0x3f, 0xe4, 0x0e, 0xb6, 0x0c, 0xba, 0x00, 0x05},
signals: []struct {
start uint8
length uint8
unsigned uint64
signed int64
}{
{start: 7, length: 3, unsigned: 0x1, signed: 1},
{start: 4, length: 1, unsigned: 0x1, signed: -1},
{start: 55, length: 16, unsigned: 0x5, signed: 5},
{start: 39, length: 16, unsigned: 0xcba, signed: 3258},
{start: 23, length: 16, unsigned: 0xeb6, signed: 3766},
{start: 3, length: 12, unsigned: 0xfe4, signed: -28},
},
},
{
data: Data{0x30, 0x53, 0x23, 0xe5, 0x0e, 0x11, 0xff, 0xff},
signals: []struct {
start uint8
length uint8
unsigned uint64
signed int64
}{
{start: 7, length: 3, unsigned: 0x1, signed: 1},
{start: 4, length: 1, unsigned: 0x1, signed: -1},
{start: 55, length: 16, unsigned: 0xffff, signed: -1},
{start: 39, length: 16, unsigned: 0xe11, signed: 3601},
{start: 23, length: 16, unsigned: 0x23e5, signed: 9189},
{start: 3, length: 12, unsigned: 0x53, signed: 83},
},
},
} {
i, tt := i, tt
t.Run(fmt.Sprintf("UnsignedBits:%v", i), func(t *testing.T) {
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("signal:%v", j), func(t *testing.T) {
actual := tt.data.UnsignedBitsBigEndian(signal.start, signal.length)
assert.Equal(t, signal.unsigned, actual)
})
}
})
t.Run(fmt.Sprintf("SignedBits:%v", i), func(t *testing.T) {
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("signal:%v", j), func(t *testing.T) {
actual := tt.data.SignedBitsBigEndian(signal.start, signal.length)
assert.Equal(t, signal.signed, actual)
})
}
})
t.Run(fmt.Sprintf("SetUnsignedBits:%v", i), func(t *testing.T) {
var data Data
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("data:%v", j), func(_ *testing.T) {
data.SetUnsignedBitsBigEndian(signal.start, signal.length, signal.unsigned)
})
}
assert.DeepEqual(t, tt.data, data)
})
t.Run(fmt.Sprintf("SetSignedBits:%v", i), func(t *testing.T) {
var data Data
for j, signal := range tt.signals {
j, signal := j, signal
t.Run(fmt.Sprintf("data:%v", j), func(_ *testing.T) {
data.SetSignedBitsBigEndian(signal.start, signal.length, signal.signed)
})
}
assert.DeepEqual(t, tt.data, data)
})
}
}
func TestInvertEndian_Property_Idempotent(t *testing.T) {
for i := uint8(0); i < 64; i++ {
assert.Equal(t, i, invertEndian(invertEndian(i)))
}
}
func TestPackUnpackBigEndian(t *testing.T) {
f := func(data Data) Data {
return data
}
g := func(data Data) Data {
data.UnpackBigEndian(data.PackBigEndian())
return data
}
assert.NilError(t, quick.CheckEqual(f, g, nil))
}
func TestPackUnpackLittleEndian(t *testing.T) {
f := func(data Data) Data {
return data
}
g := func(data Data) Data {
data.UnpackLittleEndian(data.PackLittleEndian())
return data
}
assert.NilError(t, quick.CheckEqual(f, g, nil))
}
func TestData_CheckBitRange(t *testing.T) {
// example case that big-endian signals and little-endian signals use different indexing
assert.NilError(t, CheckBitRangeBigEndian(8, 55, 16))
assert.ErrorContains(t, CheckBitRangeLittleEndian(8, 55, 16), "bit range out of bounds")
}
func BenchmarkData_UnpackLittleEndian(b *testing.B) {
var data Data
for i := 0; i < b.N; i++ {
data.UnpackLittleEndian(0)
}
}
func BenchmarkData_UnpackBigEndian(b *testing.B) {
var data Data
for i := 0; i < b.N; i++ {
data.UnpackBigEndian(0)
}
}
func BenchmarkData_PackBigEndian(b *testing.B) {
var data Data
for i := 0; i < b.N; i++ {
_ = data.PackBigEndian()
}
}
func BenchmarkData_PackLittleEndian(b *testing.B) {
var data Data
for i := 0; i < b.N; i++ {
_ = data.PackLittleEndian()
}
}