-
Notifications
You must be signed in to change notification settings - Fork 2
/
commandresult_test.go
338 lines (267 loc) · 8.32 KB
/
commandresult_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
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2018 Canonical Ltd
//
// SPDX-License-Identifier: Apache-2.0
//
package gxds
import (
"bytes"
"encoding/binary"
"fmt"
"testing"
)
// Test NewBoolResult function.
func TestNewBoolResult(t *testing.T) {
var result bool
cr := NewBoolResult(nil, nil, 0, result)
if cr.Type != Bool {
t.Errorf("NewBoolResult: invalid Type: %v", cr.Type)
}
if result == true {
t.Errorf("NewBoolResult: invalid result: true")
}
result = true
cr = NewBoolResult(nil, nil, 0, result)
if cr.Type != Bool {
t.Errorf("NewBoolResult: invalid Type: %v #2", cr.Type)
}
if result == false {
t.Errorf("NewBoolResult: invalid result: false")
}
reading := cr.Reading("FakeDevice", "FakeDeviceObject")
fmt.Printf("bool reading: %v\n", reading)
}
// Test NewStringResult function.
func TestNewStringResult(t *testing.T) {
var result string
cr := NewStringResult(nil, nil, 0, result)
if cr.Type != String {
t.Errorf("NewStringResult: invalid Type: %v", cr.Type)
}
reading := cr.Reading("FakeDevice", "FakeDeviceObject")
fmt.Printf("string reading: %v\n", reading)
result = "this is a real string"
cr = NewStringResult(nil, nil, 0, result)
if cr.Type != String {
t.Errorf("NewStringResult: invalid Type: %v #2", cr.Type)
}
if result != cr.StringResult {
t.Errorf("NewStringResult: cr.StringResult: %s doesn't match result: %s")
}
reading = cr.Reading("FakeDevice", "FakeDeviceObject")
fmt.Printf("string reading #2: %v\n", reading)
}
// Test NewUint8Result function.
func TestNewUint8Result(t *testing.T) {
var result uint8
cr := NewUint8Result(nil, nil, 0, result)
if cr.Type != Uint8 {
t.Errorf("NewUint8Result: invalid Type: %v", cr.Type)
}
var res uint8
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint8Result: cr.Uint8Result: %d doesn't match result: %d", result, res)
}
reading := cr.Reading("FakeDevice", "FakeDeviceObject")
fmt.Printf("uint8 reading: %v\n", reading)
result = 42
cr = NewUint8Result(nil, nil, 0, result)
if cr.Type != Uint8 {
t.Errorf("NewUint8Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint8Result: cr.Uint8Result: %d doesn't match result: %d (#2)", result, res)
}
reading = cr.Reading("FakeDevice", "FakeDeviceObject")
fmt.Printf("uint8 reading #2: %v\n", reading)
}
// Test NewUint16Result function.
func TestNewUint16Result(t *testing.T) {
var result uint16
cr := NewUint16Result(nil, nil, 0, result)
if cr.Type != Uint16 {
t.Errorf("NewUint16Result: invalid Type: %v", cr.Type)
}
var res uint16
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint16Result: cr.Uint16Result: %d doesn't match result: %d", result, res)
}
result = 65535
cr = NewUint16Result(nil, nil, 0, result)
if cr.Type != Uint16 {
t.Errorf("NewUint16Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint16Result: cr.Uint16Result: %d doesn't match result: %d (#2)", result, res)
}
}
// Test NewUint32Result function.
func TestNewUint32Result(t *testing.T) {
var result uint32
cr := NewUint32Result(nil, nil, 0, result)
if cr.Type != Uint32 {
t.Errorf("NewUint32Result: invalid Type: %v", cr.Type)
}
var res uint32
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint32Result: cr.Uint32Result: %d doesn't match result: %d", result, res)
}
result = 4294967295
cr = NewUint32Result(nil, nil, 0, result)
if cr.Type != Uint32 {
t.Errorf("NewUint32Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint32Result: cr.Uint32Result: %d doesn't match result: %d (#2)", result, res)
}
}
// Test NewUint64Result function.
func TestNewUint64Result(t *testing.T) {
var result uint64
var origin int64 = 42
cr := NewUint64Result(nil, nil, origin, result)
if cr.Type != Uint64 {
t.Errorf("NewUint64Result: invalid Type: %v", cr.Type)
}
if cr.Origin != origin {
t.Errorf("NewUint64Result: invalid Origin: %d", cr.Origin)
}
var res uint64
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint64Result: cr.Uint64Result: %d doesn't match result: %d", result, res)
}
result = 18446744073709551615
cr = NewUint64Result(nil, nil, 0, result)
if cr.Type != Uint64 {
t.Errorf("NewUint64Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewUint64Result: cr.Uint64Result: %d doesn't match result: %d (#2)", result, res)
}
}
// Test NewInt8Result function.
func TestNewInt8Result(t *testing.T) {
var result int8 = -128
cr := NewInt8Result(nil, nil, 0, result)
if cr.Type != Int8 {
t.Errorf("NewInt8Result: invalid Type: %v", cr.Type)
}
var res int8
buf := bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt8Result: cr.Int8Result: %d doesn't match result: %d", result, res)
}
result = 127
cr = NewInt8Result(nil, nil, 0, result)
if cr.Type != Int8 {
t.Errorf("NewInt8Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt8Result: cr.Int8Result: %d doesn't match result: %d (#2)", result, res)
}
}
// Test NewInt16Result function.
func TestNewInt16Result(t *testing.T) {
var result int16 = -32768
cr := NewInt16Result(nil, nil, 0, result)
if cr.Type != Int16 {
t.Errorf("NewInt16Result: invalid Type: %v", cr.Type)
}
var res int16
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt16Result: cr.Int16Result: %d doesn't match result: %d", result, res)
}
result = 32767
cr = NewInt16Result(nil, nil, 0, result)
if cr.Type != Int16 {
t.Errorf("NewInt16Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt16Result: cr.Int16Result: %d doesn't match result: %d (#2)", result, res)
}
}
// Test NewInt32Result function.
func TestNewInt32Result(t *testing.T) {
var result int32 = -2147483648
cr := NewInt32Result(nil, nil, 0, result)
if cr.Type != Int32 {
t.Errorf("NewInt32Result: invalid Type: %v", cr.Type)
}
var res int32
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt32Result: cr.Int32Result: %d doesn't match result: %d", result, res)
}
result = 2147483647
cr = NewInt32Result(nil, nil, 0, result)
if cr.Type != Int32 {
t.Errorf("NewInt32Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt32Result: cr.Int32Result: %d doesn't match result: %d (#2)", result, res)
}
}
// Test NewInt64Result function.
func TestNewInt64Result(t *testing.T) {
var result int64 = -9223372036854775808
var origin int64 = 42
cr := NewInt64Result(nil, nil, origin, result)
if cr.Type != Int64 {
t.Errorf("NewInt64Result: invalid Type: %v", cr.Type)
}
if cr.Origin != origin {
t.Errorf("NewInt64Result: invalid Origin: %d", cr.Origin)
}
var res int64
buf := bytes.NewReader(cr.NumericResult)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt64Result: cr.Int64Result: %d doesn't match result: %d", result, res)
}
result = 9223372036854775807
cr = NewInt64Result(nil, nil, 0, result)
if cr.Type != Int64 {
t.Errorf("NewInt64Result: invalid Type: %v #3", cr.Type)
}
buf = bytes.NewReader(cr.NumericResult)
fmt.Printf("cr: %v\n", cr)
binary.Read(buf, binary.BigEndian, &res)
if result != res {
t.Errorf("NewInt64Result: cr.Int64Result: %d doesn't match result: %d (#2)", result, res)
}
}