-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhexabus_test.go
176 lines (152 loc) · 4.53 KB
/
hexabus_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
package hexabus
import "testing"
import "bytes"
type payload struct {
data interface{}
}
var data_types = map[byte]payload{
DTYPE_BOOL: payload{
true,
},
DTYPE_UINT8: payload{
uint8(109),
},
DTYPE_UINT32: payload{
uint32(32434353),
},
DTYPE_DATETIME: payload{
DateTime{17, 2, 15, 6, 3, 2014, 4},
},
DTYPE_FLOAT: payload{
float32(10.102930),
},
DTYPE_128STRING: payload{
"this is a hexabus packet test",
},
DTYPE_TIMESTAMP: payload{
Timestamp{899992},
},
DTYPE_16BYTES: payload{
make_byte_slice(16),
},
DTYPE_66BYTES: payload{
make_byte_slice(65),
},
}
var error_t = []byte{HXB_ERR_SUCCESS, HXB_ERR_UNKNOWNEID, HXB_ERR_WRITEREADONLY, HXB_ERR_CRCFAILED, HXB_ERR_DATATYPE, HXB_ERR_INVALID_VALUE}
var eids = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
func make_byte_slice(size int) []byte {
byte_slice := make([]byte, size)
for i := 0; i < size; i++ {
byte_slice[i] = byte(i)
}
return byte_slice
}
func Test_ErrorPacket(t *testing.T) {
for _, v := range error_t {
p_error := ErrorPacket{FLAG_NONE, v}
packet := p_error.Encode()
p0_error := ErrorPacket{}
p0_error.Decode(packet)
if p0_error != p_error {
t.Errorf("ErrorPacket with error Type %x did not match while testing: \n Encode: %+v \n Decode: %+v \n", v, p_error, p0_error)
} else {
t.Logf("ErrorPacket with Err type %x passed test", v)
t.Logf("Send :%+v", p_error)
t.Logf("Receive :%+v", p0_error)
t.Logf("RAW :%x", packet)
t.Logf("")
}
}
}
func Test_InfoPacket(t *testing.T) {
eid_c := 0
for k, v := range data_types {
p_info := InfoPacket{FLAG_NONE, eids[eid_c], k, v.data}
eid_c++
packet, err := p_info.Encode()
if err != nil {
t.Errorf("%s", err)
}
p0_info := InfoPacket{}
err = p0_info.Decode(packet)
if err != nil {
t.Errorf("%s", err)
}
if k != DTYPE_16BYTES && k != DTYPE_66BYTES {
if p_info != p0_info {
t.Errorf("InfoPacket with Data type %d did not match while testing: \n Encode: %+v \n Decode: %+v \n Data Length: %d \n RAW: %x \n", p0_info.Dtype, p_info, p0_info, len(packet[11:len(packet)-2]), packet)
} else {
t.Logf("InfoPacket with Data type %d passed test", k)
t.Logf("Send :%+v", p_info)
t.Logf("Receive :%+v", p0_info)
t.Logf("RAW :%x", packet)
t.Logf("")
}
} else if k == DTYPE_16BYTES || k == DTYPE_66BYTES {
if bytes.Equal(p_info.Data.([]byte), p0_info.Data.([]byte)) == false {
t.Errorf("InfoPacket with Data type %d did not match while testing: \n Encode: %+v \n Decode: %+v \n", p0_info.Dtype, p_info, p0_info)
} else {
t.Logf("InfoPacket with Data type %d passed test", k)
t.Logf("Send :%+v", p_info)
t.Logf("Receive :%+v", p0_info)
t.Logf("RAW :%x", packet)
t.Logf("")
}
}
}
}
func Test_QueryPacket(t *testing.T) {
for _, v := range eids {
p_query := QueryPacket{FLAG_NONE, v}
packet := p_query.Encode()
p0_query := QueryPacket{}
p0_query.Decode(packet)
if p0_query != p_query {
t.Errorf("QueryPacket with EID %d did not match while testing: \n Encode: %+v \n Decode: %+v \n", v, p_query, p0_query)
} else {
t.Logf("QuerryPacket with EID type %d passed test", v)
t.Logf("Send :%+v", p_query)
t.Logf("Receive :%+v", p0_query)
t.Logf("RAW :%x", packet)
t.Logf("")
}
}
}
func Test_WritePacket(t *testing.T) {
eid_c := 0
for k, v := range data_types {
p_write := WritePacket{FLAG_NONE, eids[eid_c], k, v.data}
eid_c++
packet, err := p_write.Encode()
if err != nil {
t.Errorf("%s", err)
}
p0_write := WritePacket{}
err = p0_write.Decode(packet)
if err != nil {
t.Errorf("%s", err)
}
if k != DTYPE_16BYTES && k != DTYPE_66BYTES {
if p_write != p0_write {
t.Errorf("WritePacket with Data type %d did not match while testing: \n Encode: %+v \n Decode: %+v \n Data Length: %d \n RAW: %x \n", p0_write.Dtype, p_write, p0_write, len(packet[11:len(packet)-2]), packet)
} else {
t.Logf("WritePacket with Data type %d passed test", k)
t.Logf("Send :%+v", p_write)
t.Logf("Receive :%+v", p0_write)
t.Logf("RAW :%x", packet)
t.Logf("")
}
} else if k == DTYPE_16BYTES || k == DTYPE_66BYTES {
if bytes.Equal(p_write.Data.([]byte), p0_write.Data.([]byte)) == false {
t.Errorf("WritePacket with Data type %d did not match while testing: \n Encode: %+v \n Decode: %+v \n", p0_write.Dtype, p_write, p0_write)
} else {
t.Logf("WritePacket with Data type %d passed test", k)
t.Logf("Send :%+v", p_write)
t.Logf("Receive :%+v", p0_write)
t.Logf("RAW :%x", packet)
t.Logf("")
}
}
}
}