-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodec_test.go
357 lines (297 loc) · 9.58 KB
/
codec_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
package y3
import (
"fmt"
"reflect"
"testing"
"github.com/yomorun/y3-codec-golang/internal/utils"
"github.com/stretchr/testify/assert"
)
func TestMarshalInt32(t *testing.T) {
testMarshalBasic(t, int32(456), func(v []byte) (interface{}, error) {
return ToInt32(v)
})
}
func TestMarshalUInt32(t *testing.T) {
testMarshalBasic(t, uint32(456), func(v []byte) (interface{}, error) {
return ToUInt32(v)
})
}
func TestMarshalInt64(t *testing.T) {
testMarshalBasic(t, int64(456), func(v []byte) (interface{}, error) {
return ToInt64(v)
})
}
func TestMarshalUInt64(t *testing.T) {
testMarshalBasic(t, uint64(456), func(v []byte) (interface{}, error) {
return ToUInt64(v)
})
}
func TestMarshalFloat32(t *testing.T) {
testMarshalBasic(t, float32(456), func(v []byte) (interface{}, error) {
return ToFloat32(v)
})
}
func TestMarshalFloat64(t *testing.T) {
testMarshalBasic(t, float64(23), func(v []byte) (interface{}, error) {
return ToFloat64(v)
})
}
func TestMarshalBool(t *testing.T) {
testMarshalBasic(t, true, func(v []byte) (interface{}, error) {
return ToBool(v)
})
}
func TestMarshalUTF8String(t *testing.T) {
testMarshalBasic(t, "yomo", func(v []byte) (interface{}, error) {
return ToUTF8String(v)
})
}
func TestMarshalEmptyUTF8String(t *testing.T) {
testMarshalBasic(t, "", func(v []byte) (interface{}, error) {
return ToUTF8String(v)
})
}
func TestMarshalInt32Slice(t *testing.T) {
testMarshalBasicSlice(t, []int32{123, 456}, func(v []byte) (interface{}, error) {
return ToInt32Slice(v)
})
}
func TestMarshalInt32SliceNil(t *testing.T) {
testMarshalBasicSlice(t, []int32{}, func(v []byte) (interface{}, error) {
return ToInt32Slice(v)
})
}
func TestMarshalUInt32Slice(t *testing.T) {
testMarshalBasicSlice(t, []uint32{11, 22}, func(v []byte) (interface{}, error) {
return ToUInt32Slice(v)
})
}
func TestMarshalUInt32SliceNil(t *testing.T) {
testMarshalBasicSlice(t, []uint32{}, func(v []byte) (interface{}, error) {
return ToUInt32Slice(v)
})
}
func TestMarshalInt64Slice(t *testing.T) {
testMarshalBasicSlice(t, []int64{-4097, 255}, func(v []byte) (interface{}, error) {
return ToInt64Slice(v)
})
}
func TestMarshalInt64SliceNil(t *testing.T) {
testMarshalBasicSlice(t, []int64{}, func(v []byte) (interface{}, error) {
return ToInt64Slice(v)
})
}
func TestMarshalUInt64Slice(t *testing.T) {
testMarshalBasicSlice(t, []uint64{0, 1}, func(v []byte) (interface{}, error) {
return ToUInt64Slice(v)
})
}
func TestMarshalUInt64SliceNil(t *testing.T) {
testMarshalBasicSlice(t, []uint64{}, func(v []byte) (interface{}, error) {
return ToUInt64Slice(v)
})
}
func TestMarshalFloat32Slice(t *testing.T) {
testMarshalBasicSlice(t, []float32{0.25, 0.375}, func(v []byte) (interface{}, error) {
return ToFloat32Slice(v)
})
}
func TestMarshalFloat32SliceNil(t *testing.T) {
testMarshalBasicSlice(t, []float32{}, func(v []byte) (interface{}, error) {
return ToFloat32Slice(v)
})
}
func TestMarshalFloat64Slice(t *testing.T) {
testMarshalBasicSlice(t, []float64{0.12, 0.45}, func(v []byte) (interface{}, error) {
return ToFloat64Slice(v)
})
}
func TestMarshalFloat64SliceNil(t *testing.T) {
testMarshalBasicSlice(t, []float64{}, func(v []byte) (interface{}, error) {
return ToFloat64Slice(v)
})
}
func TestMarshalBoolSlice(t *testing.T) {
testMarshalBasicSlice(t, []bool{true, false}, func(v []byte) (interface{}, error) {
return ToBoolSlice(v)
})
}
func TestMarshalBoolSliceNil(t *testing.T) {
testMarshalBasicSlice(t, []bool{}, func(v []byte) (interface{}, error) {
return ToBoolSlice(v)
})
}
func TestMarshalUTF8StringSlice(t *testing.T) {
testMarshalBasicSlice(t, []string{"a", "b"}, func(v []byte) (interface{}, error) {
return ToUTF8StringSlice(v)
})
}
func TestMarshalUTF8StringSliceNil(t *testing.T) {
testMarshalBasicSlice(t, []string{}, func(v []byte) (interface{}, error) {
return ToUTF8StringSlice(v)
})
}
func testMarshalBasicSlice(t *testing.T, expected interface{}, converter func(v []byte) (interface{}, error)) {
flag := false
input := expected
codec := NewCodec(0x10)
inputBuf, _ := codec.Marshal(input)
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
testDecoder(0x10, inputBuf, func(v []byte) (interface{}, error) {
flag = true
result, err := converter(v)
testPrintf("value=%v\n", result)
expectedValue := reflect.ValueOf(expected)
resultValue := reflect.ValueOf(result)
assert.Equal(t, expectedValue.Kind(), resultValue.Kind(), fmt.Sprintf("Kind mismatch %v: %v", expectedValue.Kind(), resultValue.Kind()))
assert.Equal(t, expectedValue.Len(), resultValue.Len(), fmt.Sprintf("Len is not equal %v: %v", expectedValue.Len(), resultValue.Len()))
for i := 0; i < expectedValue.Len(); i++ {
assert.Equal(t, expectedValue.Index(i).Interface(), resultValue.Index(i).Interface(),
fmt.Sprintf("Item values are not equal %v: %v",
expectedValue.Index(i).Interface(), resultValue.Index(i).Interface()))
}
return result, err
})
if !flag {
t.Errorf("Observable does not listen to values")
}
}
func testMarshalBasic(t *testing.T, expected interface{}, converter func(v []byte) (interface{}, error)) {
flag := false
input := expected
codec := NewCodec(0x10)
inputBuf, _ := codec.Marshal(input)
testPrintf("inputBuf=%# x\n", inputBuf)
testDecoder(0x10, inputBuf, func(v []byte) (interface{}, error) {
flag = true
value, err := converter(v)
testPrintf("value=%v\n", value)
if err != nil {
assert.Nil(t, err, ">>>>err is not nil>>>>")
}
// assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, expected, value, fmt.Sprintf("value does not match(%v): %v", expected, value))
return value, err
})
if !flag {
t.Errorf("Observable does not listen to values")
}
}
func TestMarshalObject(t *testing.T) {
flag := false
input := exampleData{
Name: "yomo",
Noise: float32(456),
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
}
codec := NewCodec(0x30)
inputBuf, _ := codec.Marshal(input)
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
testDecoder(0x12, inputBuf, func(v []byte) (interface{}, error) {
flag = true
testPrintf("v=%#x\n", v)
var mold thermometer
err := ToObject(v, &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
testPrintf("mold=%v\n", mold)
assert.Equal(t, float32(30), mold.Temperature, fmt.Sprintf("value does not match(%v): %v", float32(30), mold.Temperature))
assert.Equal(t, float32(40), mold.Humidity, fmt.Sprintf("value does not match(%v): %v", float32(40), mold.Humidity))
return mold, err
})
if !flag {
t.Errorf("Observable does not listen to values")
}
}
func TestMarshalObjectPointer(t *testing.T) {
flag := false
input := &exampleData{
Name: "yomo",
Noise: float32(456),
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
}
codec := NewCodec(0x30)
inputBuf, err := codec.Marshal(input)
assert.NoError(t, err)
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
testDecoder(0x30, inputBuf, func(v []byte) (interface{}, error) {
flag = true
testPrintf("v=%#x\n", v)
var mold exampleData
err := ToObject(v, &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
testPrintf("mold=%v\n", mold)
assert.Equal(t, input.Name, mold.Name)
assert.Equal(t, input.Noise, mold.Noise)
assert.Equal(t, input.Therm.Temperature, mold.Therm.Temperature)
assert.Equal(t, input.Therm.Humidity, mold.Therm.Humidity)
return mold, err
})
if !flag {
t.Error("The key 0x30 is not observed")
}
}
func TestMarshalObjectSlice(t *testing.T) {
flag := false
input := exampleSlice{
Therms: []thermometer{
{Temperature: float32(30), Humidity: float32(40)},
{Temperature: float32(50), Humidity: float32(60)},
},
}
codec := NewCodec(0x30)
inputBuf, _ := codec.Marshal(input)
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
testDecoder(0x12, inputBuf, func(v []byte) (interface{}, error) {
flag = true
testPrintf("v=%#x\n", v)
var mold []thermometer
err := ToObject(v, &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
testPrintf("mold=%v\n", mold)
assert.Equal(t, float32(30), mold[0].Temperature, fmt.Sprintf("value does not match(%v): %v", float32(30), mold[0].Temperature))
assert.Equal(t, float32(40), mold[0].Humidity, fmt.Sprintf("value does not match(%v): %v", float32(40), mold[0].Humidity))
assert.Equal(t, float32(50), mold[1].Temperature, fmt.Sprintf("value does not match(%v): %v", float32(50), mold[1].Temperature))
assert.Equal(t, float32(60), mold[1].Humidity, fmt.Sprintf("value does not match(%v): %v", float32(60), mold[1].Humidity))
return mold, err
})
if !flag {
t.Errorf("Observable does not listen to values")
}
}
func TestMarshalObjectSliceNil(t *testing.T) {
flag := false
input := exampleSliceNil{
Names: []string{},
}
codec := NewCodec(0x30)
inputBuf, _ := codec.Marshal(input)
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
testDecoder(0x10, inputBuf, func(v []byte) (interface{}, error) {
flag = true
testPrintf("v=%#x\n", v)
var mold []string
err := ToObject(v, &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
testPrintf("mold=%v\n", mold)
assert.Equal(t, 0, len(mold), fmt.Sprintf("value len does not match(%v): %v", 0, len(mold)))
return mold, err
})
if !flag {
t.Errorf("Observable does not listen to values")
}
}
type exampleData struct {
Name string `y3:"0x10"`
Noise float32 `y3:"0x11"`
Therm thermometer `y3:"0x12"`
}
type thermometer struct {
Temperature float32 `y3:"0x13"`
Humidity float32 `y3:"0x14"`
}
type exampleSlice struct {
Therms []thermometer `y3:"0x12"`
}
type exampleSliceNil struct {
Names []string `y3:"0x10"`
}