@@ -2,16 +2,13 @@ package protobuf
22
33import (
44 "encoding/hex"
5+ "math"
56 "reflect"
67 "testing"
78
89 "github.com/stretchr/testify/require"
910)
1011
11- type ArrayTest0 struct {
12- A []int
13- }
14-
1512type ArrayTest1 struct {
1613 A []int64
1714}
@@ -20,48 +17,52 @@ type ArrayTest2 struct {
2017 A []int32
2118}
2219
23- type ArrayTest3 struct {
24- A int
25- }
26-
2720func TestArray (t * testing.T ) {
28- // largest int32 is 2147483647
29- var large int = 3147483647
21+ var maxInt32 int32 = math . MaxInt32
22+ var maxInt64 int64 = math . MaxInt64 / 2
3023
31- a0 := ArrayTest0 {[]int {1 , 1 , large }}
3224 a1 := ArrayTest1 {[]int64 {1 , 1 , 1 }}
3325 a2 := ArrayTest2 {[]int32 {1 , 1 , 1 }}
34- a3 := ArrayTest3 {1 }
26+ a3 := ArrayTest2 {[]int32 {1 , 1 , maxInt32 }}
27+ a4 := ArrayTest1 {[]int64 {1 , 1 , maxInt64 }}
28+
29+ buf1 , err := Encode (& a1 )
30+ require .NoError (t , err )
31+ buf2 , err := Encode (& a2 )
32+ require .NoError (t , err )
33+ buf3 , err := Encode (& a3 )
34+ require .NoError (t , err )
35+ buf4 , err := Encode (& a4 )
36+ require .NoError (t , err )
3537
36- buf0 , _ := Encode (& a0 )
37- buf1 , _ := Encode (& a1 )
38- buf2 , _ := Encode (& a2 )
39- buf3 , _ := Encode (& a3 )
40-
41- t .Log (hex .Dump (buf0 ))
4238 t .Log (hex .Dump (buf1 ))
4339 t .Log (hex .Dump (buf2 ))
4440 t .Log (hex .Dump (buf3 ))
41+ t .Log (hex .Dump (buf4 ))
4542
46- b0 := ArrayTest0 {}
4743 b1 := ArrayTest1 {}
4844 b2 := ArrayTest2 {}
49- b3 := ArrayTest3 {}
50-
51- Decode (buf0 , & b0 )
52- t .Log (b0 , reflect .TypeOf (b0 ))
45+ b3 := ArrayTest2 {}
46+ b4 := ArrayTest1 {}
5347
54- Decode (buf1 , & b1 )
48+ err = Decode (buf1 , & b1 )
49+ require .NoError (t , err )
5550 t .Log (b1 , reflect .TypeOf (b1 ))
5651
57- Decode (buf2 , & b2 )
52+ err = Decode (buf2 , & b2 )
53+ require .NoError (t , err )
5854 t .Log (b2 , reflect .TypeOf (b2 ))
5955
60- Decode (buf3 , & b3 )
56+ err = Decode (buf3 , & b3 )
57+ require .NoError (t , err )
6158 t .Log (b3 , reflect .TypeOf (b3 ))
6259
63- require .Equal (t , a0 , b0 )
60+ err = Decode (buf4 , & b4 )
61+ require .NoError (t , err )
62+ t .Log (b4 , reflect .TypeOf (b4 ))
63+
6464 require .Equal (t , a1 , b1 )
6565 require .Equal (t , a2 , b2 )
6666 require .Equal (t , a3 , b3 )
67+ require .Equal (t , a4 , b4 )
6768}
0 commit comments