-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriters_test.go
669 lines (588 loc) · 17.2 KB
/
writers_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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* Copyright 2020 Stephen Guo ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rtl
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"math/big"
"reflect"
"strings"
"testing"
"time"
)
func Unhex(str string) []byte {
b, err := hex.DecodeString(strings.Replace(str, " ", "", -1))
if err != nil {
panic(fmt.Sprintf("invalid hex string: %q", str))
}
return b
}
type encodeTest struct {
i1 int8
i2 int16
i3 int32
i4 int64
u1 uint8
u2 uint16
u3 uint32
u4 uint64
bytes [32]byte
}
func (t *encodeTest) Serialization(w io.Writer) error {
w.Write(ToBinaryBytes(t.i1))
w.Write(ToBinaryBytes(t.i2))
w.Write(ToBinaryBytes(t.i3))
w.Write(ToBinaryBytes(t.i4))
w.Write(ToBinaryBytes(t.u1))
w.Write(ToBinaryBytes(t.u2))
w.Write(ToBinaryBytes(t.u3))
w.Write(ToBinaryBytes(t.u4))
w.Write(t.bytes[:])
return nil
}
func (t *encodeTest) Deserialization(r io.Reader) (shouldBeNil bool, err error) {
bs := make([]byte, 32)
r.Read(bs[:1])
t.i1 = int8(BinaryToInt(bs[:1]))
r.Read(bs[:2])
t.i2 = int16(BinaryToInt(bs[:2]))
r.Read(bs[:4])
t.i3 = int32(BinaryToInt(bs[:4]))
r.Read(bs[:8])
t.i4 = int64(BinaryToInt(bs[:8]))
r.Read(bs[:1])
t.u1 = uint8(BinaryToUint(bs[:1]))
r.Read(bs[:2])
t.u2 = uint16(BinaryToUint(bs[:2]))
r.Read(bs[:4])
t.u3 = uint32(BinaryToUint(bs[:4]))
r.Read(bs[:8])
t.u4 = uint64(BinaryToUint(bs[:8]))
r.Read(bs[0:32])
copy(t.bytes[:], bs)
return false, nil
}
type namedByteType byte
type RawValue []byte
type simplestruct struct {
A uint
B string
}
type recstruct struct {
I uint
Child *recstruct
}
type tailRaw struct {
A uint
Tail []RawValue
}
type hasIgnoredField struct {
A uint
B uint
C uint
D uint
}
type param struct {
val interface{}
equaler func(a interface{}, b reflect.Value) bool
}
type mapstruct struct {
A map[string]int64
B map[int64]*string
}
type bigstruct struct {
I *big.Int
R *big.Rat
F *big.Float
}
type stringAndSlice struct {
A string
B []byte
}
type arrayAndSlice struct {
A [32]byte
B []byte
}
type timeObjects struct {
A time.Time
B *time.Time
}
type Timed time.Time
type BigInt big.Int
type BigRat big.Rat
type BigFloat big.Float
type convertStruct struct {
T Timed
I *BigInt
R *BigRat
F *BigFloat
}
var (
string1 = "string1"
string2 = "string2"
)
func timePointer() *time.Time {
n := time.Now()
return &n
}
var encTests = []param{
{val: float32(111.3)},
{val: float64(34343434.333)},
{val: &encodeTest{i1: 0x12, i2: 0x3456, i3: 0x567890ab, i4: -1, u1: 0xF1, u2: 0xFFF2, u3: 0xFFFFFFF3,
u4: 0xFFFFFFFFFFFFFFF4, bytes: [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1'}}},
{val: mapstruct{map[string]int64{"key1": 1, "key2": 2}, map[int64]*string{1: &string1, 2: &string2}}},
// booleans
{val: true},
{val: false},
// integers
{val: uint32(0)},
{val: uint32(127)},
{val: uint32(128)},
{val: uint32(256)},
{val: uint32(1024)},
{val: uint32(0xFFFFFF)},
{val: uint32(0xFFFFFFFF)},
{val: uint64(0xFFFFFFFF)},
{val: uint64(0xFFFFFFFFFF)},
{val: uint64(0xFFFFFFFFFFFF)},
{val: uint64(0xFFFFFFFFFFFFFF)},
{val: uint64(0xFFFFFFFFFFFFFFFF)},
{val: int8(0)},
{val: int8(127)},
{val: int8(-127)},
{val: int16(128)},
{val: int16(-128)},
{val: int32(256)},
{val: int32(-1024)},
{val: int32(0xFFFFFF)},
{val: int64(0xFFFFFFFF)},
{val: int64(-0xFFFFFFFF)},
{val: int64(0xFFFFFFFFFF)},
{val: int64(-0xFFFFFFFFFF)},
{val: int64(0xFFFFFFFFFFFF)},
{val: int64(-0xFFFFFFFFFFFF)},
{val: int64(0xFFFFFFFFFFFFFF)},
{val: int64(-0xFFFFFFFFFFFFFF)},
{val: int64(0x7FFFFFFFFFFFFFFF)},
{val: int64(-0x7FFFFFFFFFFFFFFF)},
// big integers (should match uint for small values)
{val: big.NewInt(0)},
{val: big.NewInt(1)},
{val: big.NewInt(127)},
{val: big.NewInt(128)},
{val: big.NewInt(256)},
{val: big.NewInt(1024)},
{val: big.NewInt(0xFFFFFF)},
{val: big.NewInt(0xFFFFFFFF)},
{val: big.NewInt(0xFFFFFFFFFF)},
{val: big.NewInt(0xFFFFFFFFFFFF)},
{val: big.NewInt(0xFFFFFFFFFFFFFF)},
{val: big.NewInt(0).SetBytes(Unhex("102030405060708090A0B0C0D0E0F2"))},
{val: big.NewInt(0).SetBytes(Unhex("0100020003000400050006000700080009000A000B000C000D000E01"))},
{val: big.NewInt(0).SetBytes(Unhex("010000000000000000000000000000000000000000000000000000000000000000"))},
{val: big.NewInt(0).Sub(big.NewInt(0), big.NewInt(0).SetBytes(Unhex("0100020003000400050006000700080009000A000B000C000D000E01")))},
// non-pointer big.Int
{val: *big.NewInt(0)},
{val: *big.NewInt(0xFFFFFF)},
// negative ints are not supported
{val: big.NewInt(-1)},
// byte slices, strings
{val: []byte{}},
{val: []byte{0x7E}},
{val: []byte{0x7F}},
{val: []byte{0x80}},
{val: []byte{1, 2, 3}},
{val: []namedByteType{1, 2, 3}},
{val: [...]namedByteType{1, 2, 3}},
{val: ""},
{val: "\x7E"},
{val: "\x7F"},
{val: "\x80"},
{val: "dog"},
{
val: "Lorem ipsum dolor sit amet, consectetur adipisicing eli",
},
{
val: "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
},
{
val: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mauris magna, suscipit sed vehicula non, iaculis faucibus tortor. Proin suscipit ultricies malesuada. Duis tortor elit, dictum quis tristique eu, ultrices at risus. Morbi a est imperdiet mi ullamcorper aliquet suscipit nec lorem. Aenean quis leo mollis, vulputate elit varius, consequat enim. Nulla ultrices turpis justo, et posuere urna consectetur nec. Proin non convallis metus. Donec tempor ipsum in mauris congue sollicitudin. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse convallis sem vel massa faucibus, eget lacinia lacus tempor. Nulla quis ultricies purus. Proin auctor rhoncus nibh condimentum mollis. Aliquam consequat enim at metus luctus, a eleifend purus egestas. Curabitur at nibh metus. Nam bibendum, neque at auctor tristique, lorem libero aliquet arcu, non interdum tellus lectus sit amet eros. Cras rhoncus, metus ac ornare cursus, dolor justo ultrices metus, at ullamcorper volutpat",
},
// slices
{val: []uint{}},
{val: []uint{1, 2, 3}},
{
// [ [], [[]], [ [], [[]] ] ]
val: []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}, []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}}},
},
{
val: []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo"},
},
{
val: []interface{}{uint64(1), uint64(0xFFFFFF), []interface{}{[]interface{}{uint64(4), uint64(5), uint64(5)}}, "abc"},
},
{
val: [][]string{
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
{"asdf", "qwer", "zxcv"},
},
},
// RawValue
{val: RawValue(Unhex("01"))},
{val: RawValue(Unhex("82FFFF"))},
{val: []RawValue{Unhex("01"), Unhex("02")}},
// structs
{val: simplestruct{}},
{val: simplestruct{A: 3, B: "foo"}},
{val: &recstruct{5, nil}},
{val: &recstruct{5, &recstruct{4, &recstruct{3, nil}}}},
{val: &tailRaw{A: 1, Tail: []RawValue{Unhex("02"), Unhex("03")}}},
{val: &tailRaw{A: 1, Tail: []RawValue{Unhex("02")}}},
{val: &tailRaw{A: 1, Tail: []RawValue{}}},
{val: &tailRaw{A: 1, Tail: nil}},
{val: &hasIgnoredField{A: 1, B: 0, C: 3, D: 0}},
// nil
{val: (*uint)(nil)},
{val: (*string)(nil)},
{val: (*[]byte)(nil)},
{val: (*[10]byte)(nil)},
{val: (*big.Int)(nil)},
{val: (*[]string)(nil)},
{val: (*[10]string)(nil)},
{val: (*[]interface{})(nil)},
{val: (*[]struct{ uint })(nil)},
{val: (*interface{})(nil)},
// bigs
{val: &bigstruct{I: big.NewInt(3234234543)}},
{val: &bigstruct{R: big.NewRat(1233423323, 3545)}},
{val: &bigstruct{F: big.NewFloat(239842.23354345456)}},
{val: &bigstruct{I: big.NewInt(3234234543), R: big.NewRat(1233423323, 3545)}},
{val: &bigstruct{I: big.NewInt(3234234543), F: big.NewFloat(239842.23354345456)}},
{val: &bigstruct{R: big.NewRat(1233423323, 3545), F: big.NewFloat(239842.23354345456)}},
{val: &bigstruct{I: big.NewInt(3234234543), R: big.NewRat(1233423323, 3545), F: big.NewFloat(239842.23354345456)}},
{val: &stringAndSlice{A: "name1", B: []byte("slice1slice1")}},
{val: &stringAndSlice{A: "name1", B: nil}},
{val: &arrayAndSlice{A: [32]byte{8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}, B: []byte("sssssssss")}},
{val: &arrayAndSlice{A: [32]byte{8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}, B: nil}},
{val: time.Now()},
{val: timePointer()},
{val: Timed(time.Now().Round(0))},
{val: (*BigInt)(big.NewInt(2223))},
{val: (*BigRat)(big.NewRat(333, 444))},
{val: (*BigFloat)(big.NewFloat(888.444))},
{val: (*BigInt)(nil)},
{val: (*BigRat)(nil)},
{val: (*BigFloat)(nil)},
{val: &convertStruct{
T: Timed(time.Now().Round(0)),
I: nil,
R: (*BigRat)(big.NewRat(88, 113)),
F: nil,
}},
{val: &timeObjects{A: time.Now(), B: timePointer()},
equaler: func(a interface{}, b reflect.Value) bool {
p := a.(*timeObjects)
if b.Kind() != reflect.Pointer || b.IsNil() {
return false
}
o := b.Interface().(*timeObjects)
return p.A.Equal(o.A) && o.B != nil && (*p.B).Equal(*o.B)
}},
{val: &timeObjects{A: time.Now()},
equaler: func(a interface{}, b reflect.Value) bool {
p := a.(*timeObjects)
if b.Kind() != reflect.Pointer || b.IsNil() {
return false
}
o := b.Interface().(*timeObjects)
if o.B != nil {
return false
}
return p.A.Equal(o.A)
}},
}
func TestEncode(t *testing.T) {
// decoder := new(EventDecoder)
for _, test := range encTests {
val := reflect.ValueOf(test.val)
bs, err := Marshal(test.val)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(bs)
typ := val.Type()
nv := reflect.New(typ)
// vr := NewValueReader(buf, 100)
nvv := nv.Elem()
// if err := valueReader(vr, nvv); err != nil {
// vr := NewValueReader(buf, 256)
if err := Decode(buf, nv.Interface()); err != nil {
t.Error(err)
}
// buf = bytes.NewBuffer(bs)
// if err := decoder.Decode(buf, nv.Interface()); err != nil {
// t.Fatal(err)
// }
fmt.Printf("%v: %#v\n\t%X\n%v: %#v\n", typ, test.val, bs, nvv.Type(), nvv)
equaler := _valueEqualer
if test.equaler != nil {
equaler = test.equaler
}
if equaler(test.val, nvv) {
t.Log(test.val, "check")
} else {
t.Error(test.val, "error")
}
}
}
func _valueEqualer(a interface{}, b reflect.Value) bool {
switch s := a.(type) {
case *time.Time:
if b.Kind() != reflect.Pointer {
return false
}
if s == nil && b.IsNil() {
return true
}
if b.IsNil() {
return false
}
return (*s).Equal(b.Elem().Interface().(time.Time))
case time.Time:
return s.Equal(b.Interface().(time.Time))
default:
if reflect.DeepEqual(a, b.Interface()) {
return true
} else {
return false
}
}
}
// string -> []int
func TestStringArray(t *testing.T) {
s := "this is a string"
i := make([]int, 0)
bs, err := Marshal(s)
if err != nil {
t.Fatalf("marshal string failed: %v", err)
}
err = Unmarshal(bs, &i)
if err != nil {
t.Fatalf("unmarshal string to []int failed: %v", err)
}
if len(s) != len(i) {
t.Fatalf("len(%s) != len(%d)", s, i)
}
sb := []byte(s)
for j := 0; j < len(s); j++ {
if int(sb[j]) != i[j] {
t.Fatalf("%d != %d", sb, i)
}
}
t.Logf("string (%s) -> %x -> []int %d", s, bs, i)
}
type version1 struct {
A uint
B uint
}
type version2 struct {
A uint
B uint
C string `rtlorder:"5"`
D []byte
}
type version3 struct {
A uint
B uint
C string `rtlorder:"5"`
E int `rtlorder:"3"`
F *big.Int `rtlorder:"4"`
G version1 `rtlorder:"6"`
}
type version4 struct {
C string `rtlorder:"5"`
E int `rtlorder:"3"`
F *big.Int `rtlorder:"4"`
G version2 `rtlorder:"6"`
}
type version5 struct {
A uint `rtlorder:"0"`
C string `rtlorder:"5"`
E int `rtlorder:"3"`
F *big.Int `rtlorder:"4"`
H version3 `rtlorder:"7"`
}
func TestVersion(t *testing.T) {
{
v1 := &version1{A: 87690, B: 12345}
buf := new(bytes.Buffer)
Encode(v1, buf)
bs1 := buf.Bytes()
v2 := new(version2)
if err := Decode(buf, v2); err != nil {
t.Error(err)
}
if v1.A == v2.A && v1.B == v2.B && v2.C == "" && v2.D == nil {
t.Log("version1 -> version2 check")
} else {
t.Errorf("version1 -> version2 failed, %+v -> %+v", v1, v2)
}
v3 := &version3{A: 22, B: 33, C: "ccc", E: 8888, F: big.NewInt(99999), G: version1{A: 12, B: 34}}
if err := Decode(bytes.NewReader(bs1), v3); err != nil {
t.Error(err)
}
if v3.A == v1.A && v3.B == v1.B && v3.C == "" && v3.E == 0 && v3.F == nil && v3.G.A == 0 && v3.G.B == 0 {
t.Log("version1 -> version3 check")
} else {
t.Errorf("version1 -> version3 failed, %+v -> %+v", v1, v3)
}
}
{
v2 := &version2{A: 222999, B: 333000, C: "version2", D: []byte("xxxxxxx")}
buf := new(bytes.Buffer)
Encode(v2, buf)
bs2 := buf.Bytes()
v3 := &version3{A: 22, B: 33, C: "ccc", E: 8888, F: big.NewInt(99999), G: version1{A: 12, B: 34}}
if err := Decode(bytes.NewReader(bs2), v3); err != nil {
t.Error(err)
}
if v3.A == v2.A && v3.B == v2.B && v3.C == v2.C && v3.E == 0 && v3.F == nil && v3.G.A == 0 && v3.G.B == 0 {
t.Log("version2 -> version3 check")
} else {
t.Errorf("version2 -> version3 failed, %+v -> %x -> %+v", v2, bs2, v3)
}
v1 := &version1{A: 87690, B: 12345}
if err := Decode(bytes.NewReader(bs2), v1); err != nil {
t.Error(err)
}
if v1.A == v2.A && v1.B == v2.B {
t.Log("version2 -> version1 check")
} else {
t.Errorf("version2 -> version1 failed, %+v -> %+v", v2, v1)
}
}
{
v3 := &version3{A: 22, B: 33, C: "ccc", E: 8888, F: big.NewInt(99999), G: version1{A: 12, B: 34}}
buf := new(bytes.Buffer)
Encode(v3, buf)
bs := buf.Bytes()
v1 := &version1{A: 87690, B: 12345}
if err := Decode(bytes.NewReader(bs), v1); err != nil {
t.Error(err)
}
if v1.A == v3.A && v1.B == v3.B {
t.Log("version3 -> version1 check")
} else {
t.Errorf("version3 -> version1 failed, %+v -> %+v", v3, v1)
}
v2 := &version2{A: 8802, B: 65623, C: "xxxx", D: []byte("yyyyyy")}
if err := Decode(bytes.NewReader(bs), v2); err != nil {
t.Error(err)
}
if v3.A == v2.A && v3.B == v2.B && v3.C == v2.C && v2.D == nil {
t.Log("version3 -> version2 check")
} else {
t.Errorf("version3 -> version2 failed, %+v -> %+v", v3, v2)
}
v4 := &version4{
C: "version4",
E: 99999,
F: big.NewInt(100000),
G: version2{A: 222999, B: 333000, C: "version2", D: []byte("xxxxxxx")},
}
if err := Decode(bytes.NewReader(bs), v4); err != nil {
t.Error(err)
}
if v4.C == v3.C && v4.E == v3.E && v4.F.Cmp(v3.F) == 0 && v4.G.A == v3.G.A && v4.G.B == v3.G.B && v4.G.C == "" && v4.G.D == nil {
t.Log("version3 -> version4 check")
} else {
t.Errorf("version3 -> version4 failed, %+v -> %+v", v3, v4)
}
}
{
v4 := &version4{
C: "version4",
E: 99999,
F: big.NewInt(100000),
G: version2{A: 222999, B: 333000, C: "version2", D: []byte("xxxxxxx")},
}
buf := new(bytes.Buffer)
Encode(v4, buf)
bs := buf.Bytes()
v3 := &version3{A: 22, B: 33, C: "ccc", E: 8888, F: big.NewInt(99999), G: version1{A: 12, B: 34}}
if err := Decode(bytes.NewReader(bs), v3); err != nil {
t.Error(err)
}
if v3.C == v4.C && v3.E == v4.E && v3.F.Cmp(v4.F) == 0 && v3.G.A == v4.G.A && v3.G.B == v4.G.B {
t.Log("version4 -> version3 check")
} else {
t.Errorf("version4 -> version3 failed, %+v -> %x -> %+v", v4, bs, v3)
}
v1 := &version1{A: 87690, B: 12345}
if err := Decode(bytes.NewReader(bs), v1); err != nil {
t.Error(err)
}
if v1.A == 0 && v1.B == 0 {
t.Log("version4 -> version1 check")
} else {
t.Errorf("version4 -> version1 failed, %+v -> %+v", v4, v1)
}
v5 := new(version5)
if err := Decode(bytes.NewReader(bs), v5); err != nil {
t.Fatalf("version4 -> version5 failed: %v", err)
}
if v5.A == 0 && v5.C == v4.C && v5.E == v4.E && v5.F.Cmp(v4.F) == 0 &&
v5.H.A == 0 && v5.H.B == 0 && v5.H.C == "" && v5.H.E == 0 && v5.H.F == nil && v5.H.G.A == 0 && v5.H.G.B == 0 {
t.Log("version4 -> version5 check")
} else {
t.Errorf("version4 -> version5 failed, %+v -> %x -> %+v", v4, bs, v5)
}
}
}