-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler_test.go
153 lines (122 loc) · 4.22 KB
/
assembler_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
package sacio
import (
"math"
"reflect"
"testing"
)
func TestAssembleString(t *testing.T) {
data := []byte{'H', 'e', 'l', 'l', 'o'}
expectedResult := K(data)
result := assembleString(data)
if result != expectedResult {
t.Errorf("Expected assembled string: %v, got: %v", expectedResult, result)
}
}
func TestAssembleBool(t *testing.T) {
// Test case: LSBFIRST, true
data := []byte{0x01, 0x00, 0x00, 0x00}
expectedResult := L(true)
result := assembleBool(data, LSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled bool for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: LSBFIRST, false
data = []byte{0x00, 0x00, 0x00, 0x00}
expectedResult = L(false)
result = assembleBool(data, LSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled bool for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST, true
data = []byte{0x00, 0x00, 0x00, 0x01}
expectedResult = L(true)
result = assembleBool(data, MSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled bool for MSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST, false
data = []byte{0x00, 0x00, 0x00, 0x00}
expectedResult = L(false)
result = assembleBool(data, MSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled bool for MSBFIRST: %v, got: %v", expectedResult, result)
}
}
func TestAssembleInt32(t *testing.T) {
// Test case: LSBFIRST
data := []byte{0x78, 0x56, 0x34, 0x12}
expectedResult := N(0x12345678)
result := assembleInt32(data, LSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled int32 for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST
data = []byte{0x12, 0x34, 0x56, 0x78}
expectedResult = N(0x12345678)
result = assembleInt32(data, MSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled int32 for MSBFIRST: %v, got: %v", expectedResult, result)
}
}
func TestAssembleFloat32(t *testing.T) {
// Test case: LSBFIRST
data := []byte{0x7B, 0x14, 0xAE, 0x47}
expectedResult := F(math.Float32frombits(0x47AE147B))
result := assembleFloat32(data, LSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled float32 for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST
data = []byte{0x47, 0xAE, 0x14, 0x7B}
expectedResult = F(math.Float32frombits(0x47AE147B))
result = assembleFloat32(data, MSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled float32 for MSBFIRST: %v, got: %v", expectedResult, result)
}
}
func TestAssembleEnum(t *testing.T) {
// Test case: LSBFIRST, matching enum
data := []byte{0x01, 0x00, 0x00, 0x00}
expectedResult := I("itime")
result := assembleEnum(data, LSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled enum for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST, matching enum
data = []byte{0x00, 0x00, 0x00, 0x01}
expectedResult = I("itime")
result = assembleEnum(data, MSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled enum for MSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: LSBFIRST, no matching enum
data = []byte{0x02, 0x00, 0x00, 0x00}
expectedResult = I("irlim")
result = assembleEnum(data, LSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled enum for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST, no matching enum
data = []byte{0x00, 0x00, 0x00, 0x02}
expectedResult = I("irlim")
result = assembleEnum(data, MSBFIRST)
if result != expectedResult {
t.Errorf("Expected assembled enum for MSBFIRST: %v, got: %v", expectedResult, result)
}
}
func TestDisassembleFloat32(t *testing.T) {
// Test case: LSBFIRST
data := F(3.14159)
expectedResult := []byte{0x40, 0x49, 0x0F, 0xD0}
result := disassembleFloat32(data, LSBFIRST)
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected disassembled float32 for LSBFIRST: %v, got: %v", expectedResult, result)
}
// Test case: MSBFIRST
data = F(3.14159)
expectedResult = []byte{0xD0, 0x0F, 0x49, 0x40}
result = disassembleFloat32(data, MSBFIRST)
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected disassembled float32 for MSBFIRST: %v, got: %v", expectedResult, result)
}
}