forked from simonvetter/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_test.go
610 lines (531 loc) · 18.1 KB
/
encoding_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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package modbus
import (
"testing"
)
func TestUint16ToBytes(t *testing.T) {
var out []byte
out = uint16ToBytes(BIG_ENDIAN, 0x4321)
if len(out) != 2 {
t.Errorf("expected 2 bytes, got %v", len(out))
}
if out[0] != 0x43 || out[1] != 0x21 {
t.Errorf("expected {0x43, 0x21}, got {0x%02x, 0x%02x}", out[0], out[1])
}
out = uint16ToBytes(LITTLE_ENDIAN, 0x4321)
if len(out) != 2 {
t.Errorf("expected 2 bytes, got %v", len(out))
}
if out[0] != 0x21 || out[1] != 0x43 {
t.Errorf("expected {0x21, 0x43}, got {0x%02x, 0x%02x}", out[0], out[1])
}
return
}
func TestUint16sToBytes(t *testing.T) {
var out []byte
out = uint16sToBytes(BIG_ENDIAN, []uint16{0x4321, 0x8765, 0xcba9})
if len(out) != 6 {
t.Errorf("expected 6 bytes, got %v", len(out))
}
if out[0] != 0x43 || out[1] != 0x21 {
t.Errorf("expected {0x43, 0x21}, got {0x%02x, 0x%02x}", out[0], out[1])
}
if out[2] != 0x87 || out[3] != 0x65 {
t.Errorf("expected {0x87, 0x65}, got {0x%02x, 0x%02x}", out[0], out[1])
}
if out[4] != 0xcb || out[5] != 0xa9 {
t.Errorf("expected {0xcb, 0xa9}, got {0x%02x, 0x%02x}", out[0], out[1])
}
out = uint16sToBytes(LITTLE_ENDIAN, []uint16{0x4321, 0x8765, 0xcba9})
if len(out) != 6 {
t.Errorf("expected 6 bytes, got %v", len(out))
}
if out[0] != 0x21 || out[1] != 0x43 {
t.Errorf("expected {0x21, 0x43}, got {0x%02x, 0x%02x}", out[0], out[1])
}
if out[2] != 0x65 || out[3] != 0x87 {
t.Errorf("expected {0x65, 0x87}, got {0x%02x, 0x%02x}", out[0], out[1])
}
if out[4] != 0xa9 || out[5] != 0xcb {
t.Errorf("expected {0xa9, 0xcb}, got {0x%02x, 0x%02x}", out[0], out[1])
}
return
}
func TestBytesToUint16(t *testing.T) {
var result uint16
result = bytesToUint16(BIG_ENDIAN, []byte{0x43, 0x21})
if result != 0x4321 {
t.Errorf("expected 0x4321, got 0x%04x", result)
}
result = bytesToUint16(LITTLE_ENDIAN, []byte{0x43, 0x21})
if result != 0x2143 {
t.Errorf("expected 0x2143, got 0x%04x", result)
}
return
}
func TestBytesToUint16s(t *testing.T) {
var results []uint16
results = bytesToUint16s(BIG_ENDIAN, []byte{0x11, 0x22, 0x33, 0x44})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x1122 {
t.Errorf("expected 0x1122, got 0x%04x", results[0])
}
if results[1] != 0x3344 {
t.Errorf("expected 0x3344, got 0x%04x", results[1])
}
results = bytesToUint16s(LITTLE_ENDIAN, []byte{0x11, 0x22, 0x33, 0x44})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x2211 {
t.Errorf("expected 0x2211, got 0x%04x", results[0])
}
if results[1] != 0x4433 {
t.Errorf("expected 0x4433, got 0x%04x", results[1])
}
return
}
func TestUint32ToBytes(t *testing.T) {
var out []byte
out = uint32ToBytes(BIG_ENDIAN, HIGH_WORD_FIRST, 0x87654321)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0x87 || out[1] != 0x65 || // first word
out[2] != 0x43 || out[3] != 0x21 { // second word
t.Errorf("expected {0x87, 0x65, 0x43, 0x21}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
out = uint32ToBytes(BIG_ENDIAN, LOW_WORD_FIRST, 0x87654321)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0x43 || out[1] != 0x21 || out[2] != 0x87 || out[3] != 0x65 {
t.Errorf("expected {0x43, 0x21, 0x87, 0x65}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
out = uint32ToBytes(LITTLE_ENDIAN, LOW_WORD_FIRST, 0x87654321)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0x21 || out[1] != 0x43 || out[2] != 0x65 || out[3] != 0x87 {
t.Errorf("expected {0x21, 0x43, 0x65, 0x87}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
out = uint32ToBytes(LITTLE_ENDIAN, HIGH_WORD_FIRST, 0x87654321)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0x65 || out[1] != 0x87 || out[2] != 0x21 || out[3] != 0x43 {
t.Errorf("expected {0x65, 0x87, 0x21, 0x43}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
return
}
func TestBytesToUint32s(t *testing.T) {
var results []uint32
results = bytesToUint32s(BIG_ENDIAN, HIGH_WORD_FIRST, []byte{
0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x87654321 {
t.Errorf("expected 0x87654321, got 0x%08x", results[0])
}
if results[1] != 0x00112233 {
t.Errorf("expected 0x00112233, got 0x%08x", results[1])
}
results = bytesToUint32s(BIG_ENDIAN, LOW_WORD_FIRST, []byte{
0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x43218765 {
t.Errorf("expected 0x43218765, got 0x%08x", results[0])
}
if results[1] != 0x22330011 {
t.Errorf("expected 0x22330011, got 0x%08x", results[1])
}
results = bytesToUint32s(LITTLE_ENDIAN, LOW_WORD_FIRST, []byte{
0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x21436587 {
t.Errorf("expected 0x21436587, got 0x%08x", results[0])
}
if results[1] != 0x33221100 {
t.Errorf("expected 0x33221100, got 0x%08x", results[1])
}
results = bytesToUint32s(LITTLE_ENDIAN, HIGH_WORD_FIRST, []byte{
0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x65872143 {
t.Errorf("expected 0x65872143, got 0x%08x", results[0])
}
if results[1] != 0x11003322 {
t.Errorf("expected 0x11003322, got 0x%08x", results[1])
}
return
}
func TestFloat32ToBytes(t *testing.T) {
var out []byte
out = float32ToBytes(BIG_ENDIAN, HIGH_WORD_FIRST, 1.234)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0x3f || out[1] != 0x9d || out[2] != 0xf3 || out[3] != 0xb6 {
t.Errorf("expected {0x3f, 0x9d, 0xf3, 0xb6}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
out = float32ToBytes(BIG_ENDIAN, LOW_WORD_FIRST, 1.234)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0xf3 || out[1] != 0xb6 || out[2] != 0x3f || out[3] != 0x9d {
t.Errorf("expected {0xf3, 0xb6, 0x3f, 0x9d}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
out = float32ToBytes(LITTLE_ENDIAN, LOW_WORD_FIRST, 1.234)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0xb6 || out[1] != 0xf3 || out[2] != 0x9d || out[3] != 0x3f {
t.Errorf("expected {0xb6, 0xf3, 0x9d, 0x3f}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
out = float32ToBytes(LITTLE_ENDIAN, HIGH_WORD_FIRST, 1.234)
if len(out) != 4 {
t.Errorf("expected 4 bytes, got %v", len(out))
}
if out[0] != 0x9d || out[1] != 0x3f || out[2] != 0xb6 || out[3] != 0xf3 {
t.Errorf("expected {0x9d, 0x3f, 0xb6, 0xf3}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3])
}
return
}
func TestBytesToFloat32s(t *testing.T) {
var results []float32
results = bytesToFloat32s(BIG_ENDIAN, HIGH_WORD_FIRST, []byte{
0x3f, 0x9d, 0xf3, 0xb6,
0x40, 0x49, 0x0f, 0xdb,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.234 {
t.Errorf("expected 1.234, got %.04f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
results = bytesToFloat32s(BIG_ENDIAN, LOW_WORD_FIRST, []byte{
0xf3, 0xb6, 0x3f, 0x9d,
0x0f, 0xdb, 0x40, 0x49,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.234 {
t.Errorf("expected 1.234, got %.04f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
results = bytesToFloat32s(LITTLE_ENDIAN, LOW_WORD_FIRST, []byte{
0xb6, 0xf3, 0x9d, 0x3f,
0xdb, 0x0f, 0x49, 0x40,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.234 {
t.Errorf("expected 1.234, got %.04f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
results = bytesToFloat32s(LITTLE_ENDIAN, HIGH_WORD_FIRST, []byte{
0x9d, 0x3f, 0xb6, 0xf3,
0x49, 0x40, 0xdb, 0x0f,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.234 {
t.Errorf("expected 1.234, got %.04f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
return
}
func TestUint64ToBytes(t *testing.T) {
var out []byte
out = uint64ToBytes(BIG_ENDIAN, HIGH_WORD_FIRST, 0x0fedcba987654321)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0x0f || out[1] != 0xed || // 1st word
out[2] != 0xcb || out[3] != 0xa9 || // 2nd word
out[4] != 0x87 || out[5] != 0x65 || // 3rd word
out[6] != 0x43 || out[7] != 0x21 { // 4th word
t.Errorf("expected {0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
out = uint64ToBytes(BIG_ENDIAN, LOW_WORD_FIRST, 0x0fedcba987654321)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0x43 || out[1] != 0x21 || // 1st word
out[2] != 0x87 || out[3] != 0x65 || // 2nd word
out[4] != 0xcb || out[5] != 0xa9 || // 3rd word
out[6] != 0x0f || out[7] != 0xed { // 4th word
t.Errorf("expected {0x43, 0x21, 0x87, 0x65, 0xcb, 0xa9, 0x0f, 0xed}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
out = uint64ToBytes(LITTLE_ENDIAN, LOW_WORD_FIRST, 0x0fedcba987654321)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0x21 || out[1] != 0x43 || // 1st word
out[2] != 0x65 || out[3] != 0x87 || // 2nd word
out[4] != 0xa9 || out[5] != 0xcb || // 3rd word
out[6] != 0xed || out[7] != 0x0f { // 4th word
t.Errorf("expected {0x21, 0x43, 0x65, 0x87, 0xa9, 0xcb, 0xed, 0x0f}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
out = uint64ToBytes(LITTLE_ENDIAN, HIGH_WORD_FIRST, 0x0fedcba987654321)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0xed || out[1] != 0x0f || // 1st word
out[2] != 0xa9 || out[3] != 0xcb || // 2nd word
out[4] != 0x65 || out[5] != 0x87 || // 3rd word
out[6] != 0x21 || out[7] != 0x43 { // 4th word
t.Errorf("expected {0xed, 0x0f, 0xa9, 0xcb, 0x65, 0x87, 0x21, 0x43}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
return
}
func TestBytesToUint64s(t *testing.T) {
var results []uint64
results = bytesToUint64s(BIG_ENDIAN, HIGH_WORD_FIRST, []byte{
0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x0fedcba987654321 {
t.Errorf("expected 0x0fedcba987654321, got 0x%016x", results[0])
}
if results[1] != 0x0011223344556677 {
t.Errorf("expected 0x0011223344556677, got 0x%016x", results[1])
}
results = bytesToUint64s(BIG_ENDIAN, LOW_WORD_FIRST, []byte{
0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x43218765cba90fed {
t.Errorf("expected 0x43218765cba90fed, got 0x%016x", results[0])
}
if results[1] != 0x6677445522330011 {
t.Errorf("expected 0x6677445522330011, got 0x%016x", results[1])
}
results = bytesToUint64s(LITTLE_ENDIAN, LOW_WORD_FIRST, []byte{
0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0x21436587a9cbed0f {
t.Errorf("expected 0x21436587a9cbed0f, got 0x%016x", results[0])
}
if results[1] != 0x7766554433221100 {
t.Errorf("expected 0x7766554433221100, got 0x%016x", results[1])
}
results = bytesToUint64s(LITTLE_ENDIAN, HIGH_WORD_FIRST, []byte{
0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 0xed0fa9cb65872143 {
t.Errorf("expected 0xed0fa9cb65872143, got 0x%016x", results[0])
}
if results[1] != 0x1100332255447766 {
t.Errorf("expected 0x1100332255447766, got 0x%016x", results[1])
}
return
}
func TestFloat64ToBytes(t *testing.T) {
var out []byte
out = float64ToBytes(BIG_ENDIAN, HIGH_WORD_FIRST, 1.2345678)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0x3f || out[1] != 0xf3 || out[2] != 0xc0 || out[3] != 0xca ||
out[4] != 0x2a || out[5] != 0x5b || out[6] != 0x1d || out[7] != 0x5d {
t.Errorf("expected {0x3f, 0xf3, 0xc0, 0xca, 0x2a, 0x5b, 0x1d, 0x5d}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
out = float64ToBytes(BIG_ENDIAN, LOW_WORD_FIRST, 1.2345678)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0x1d || out[1] != 0x5d || out[2] != 0x2a || out[3] != 0x5b ||
out[4] != 0xc0 || out[5] != 0xca || out[6] != 0x3f || out[7] != 0xf3 {
t.Errorf("expected {0x1d, 0x5d, 0x2a, 0x5b, 0xc0, 0xca, 0x3f, 0xf3}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
out = float64ToBytes(LITTLE_ENDIAN, LOW_WORD_FIRST, 1.2345678)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0x5d || out[1] != 0x1d || out[2] != 0x5b || out[3] != 0x2a ||
out[4] != 0xca || out[5] != 0xc0 || out[6] != 0xf3 || out[7] != 0x3f {
t.Errorf("expected {0x5d, 0x1d, 0x5b, 0x2a, 0xca, 0xc0, 0xf3, 0x3f}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
out = float64ToBytes(LITTLE_ENDIAN, HIGH_WORD_FIRST, 1.2345678)
if len(out) != 8 {
t.Errorf("expected 8 bytes, got %v", len(out))
}
if out[0] != 0xf3 || out[1] != 0x3f || out[2] != 0xca || out[3] != 0xc0 ||
out[4] != 0x5b || out[5] != 0x2a || out[6] != 0x5d || out[7] != 0x1d {
t.Errorf("expected {0xf3, 0x3f, 0xca, 0xc0, 0x5b, 0x2a, 0x5d, 0x1d}, got {0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}",
out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7])
}
return
}
func TestBytesToFloat64s(t *testing.T) {
var results []float64
results = bytesToFloat64s(BIG_ENDIAN, HIGH_WORD_FIRST, []byte{
0x3f, 0xf3, 0xc0, 0xca, 0x2a, 0x5b, 0x1d, 0x5d,
0x40, 0x09, 0x21, 0xfb, 0x5f, 0xff, 0xe9, 0x5e,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.2345678 {
t.Errorf("expected 1.2345678, got %.08f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
results = bytesToFloat64s(BIG_ENDIAN, LOW_WORD_FIRST, []byte{
0x1d, 0x5d, 0x2a, 0x5b, 0xc0, 0xca, 0x3f, 0xf3,
0xe9, 0x5e, 0x5f, 0xff, 0x21, 0xfb, 0x40, 0x09,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.2345678 {
t.Errorf("expected 1.234, got %.08f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
results = bytesToFloat64s(LITTLE_ENDIAN, LOW_WORD_FIRST, []byte{
0x5d, 0x1d, 0x5b, 0x2a, 0xca, 0xc0, 0xf3, 0x3f,
0x5e, 0xe9, 0xff, 0x5f, 0xfb, 0x21, 0x09, 0x40,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.2345678 {
t.Errorf("expected 1.234, got %.08f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
results = bytesToFloat64s(LITTLE_ENDIAN, HIGH_WORD_FIRST, []byte{
0xf3, 0x3f, 0xca, 0xc0, 0x5b, 0x2a, 0x5d, 0x1d,
0x09, 0x40, 0xfb, 0x21, 0xff, 0x5f, 0x5e, 0xe9,
})
if len(results) != 2 {
t.Errorf("expected 2 values, got %v", len(results))
}
if results[0] != 1.2345678 {
t.Errorf("expected 1.234, got %.08f", results[0])
}
if results[1] != 3.14159274101 {
t.Errorf("expected 3.14159274101, got %.09f", results[1])
}
return
}
func TestDecodeBools(t *testing.T) {
var results []bool
results = decodeBools(1, []byte{0x01})
if len(results) != 1 {
t.Errorf("expected 1 value, got %v", len(results))
}
if results[0] != true {
t.Errorf("expected true, got false")
}
results = decodeBools(1, []byte{0x0f})
if len(results) != 1 {
t.Errorf("expected 1 value, got %v", len(results))
}
if results[0] != true {
t.Errorf("expected true, got false")
}
results = decodeBools(9, []byte{0x75, 0x03})
if len(results) != 9 {
t.Errorf("expected 9 values, got %v", len(results))
}
for i, b := range []bool{
true, false, true, false, // 0x05
true, true, true, false, // 0x07
true, } { // 0x01
if b != results[i] {
t.Errorf("expected %v at %v, got %v", b, i, results[i])
}
}
return
}
func TestEncodeBools(t *testing.T) {
var results []byte
results = encodeBools([]bool{false, true, false, true, })
if len(results) != 1 {
t.Errorf("expected 1 byte, got %v", len(results))
}
if results[0] != 0x0a {
t.Errorf("expected 0x0a, got 0x%02x", results[0])
}
results = encodeBools([]bool{true, false, true, })
if len(results) != 1 {
t.Errorf("expected 1 byte, got %v", len(results))
}
if results[0] != 0x05 {
t.Errorf("expected 0x05, got 0x%02x", results[0])
}
results = encodeBools([]bool{true, false, false, true, false, true, true, false,
true, true, true, false, true, true, true, false,
false, true})
if len(results) != 3 {
t.Errorf("expected 3 bytes, got %v", len(results))
}
if results[0] != 0x69 || results[1] != 0x77 || results[2] != 0x02 {
t.Errorf("expected {0x69, 0x77, 0x02}, got {0x%02x, 0x%02x, 0x%02x}",
results[0], results[1], results[2])
}
return
}