]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/gob: add Encode-Decode Int slices tests
authorAlberto Donizetti <alb.donizetti@gmail.com>
Mon, 13 Mar 2017 19:35:07 +0000 (20:35 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 13 Mar 2017 20:22:39 +0000 (20:22 +0000)
Tinkering with the gob package shows that is currently possible to
*completely destroy* Int slices encoding without triggering a single
test failure.

The various encInt{8,16,32,64}Slice methods are only called during the
execution of the GobMapInterfaceEncode test, which only encodes a few
slices of length exactly 1 and then just checks that the error
returned by Encode is nil (without trying to Decode back the data).

This patch adds a few tests for signed integer slices encoding.

Change-Id: Ifaaee2f32132873118b241f79aa8203e4ad31416
Reviewed-on: https://go-review.googlesource.com/38066
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/gob/encoder_test.go

index 9256848b50e3f945c0ab78fa01b39e4cafb93f10..a1ca252ccd11c2b0d83f2913d580cd42c8d6ae08 100644 (file)
@@ -55,6 +55,71 @@ func TestBasicEncoderDecoder(t *testing.T) {
        }
 }
 
+func TestEncodeIntSlice(t *testing.T) {
+
+       s8 := []int8{1, 5, 12, 22, 35, 51, 70, 92, 117}
+       s16 := []int16{145, 176, 210, 247, 287, 330, 376, 425, 477}
+       s32 := []int32{532, 590, 651, 715, 782, 852, 925, 1001, 1080}
+       s64 := []int64{1162, 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926}
+
+       t.Run("int8", func(t *testing.T) {
+               var sink bytes.Buffer
+               enc := NewEncoder(&sink)
+               enc.Encode(s8)
+
+               dec := NewDecoder(&sink)
+               res := make([]int8, 9)
+               dec.Decode(&res)
+
+               if !reflect.DeepEqual(s8, res) {
+                       t.Fatalf("EncodeIntSlice: expected %v, got %v", s8, res)
+               }
+       })
+
+       t.Run("int16", func(t *testing.T) {
+               var sink bytes.Buffer
+               enc := NewEncoder(&sink)
+               enc.Encode(s16)
+
+               dec := NewDecoder(&sink)
+               res := make([]int16, 9)
+               dec.Decode(&res)
+
+               if !reflect.DeepEqual(s16, res) {
+                       t.Fatalf("EncodeIntSlice: expected %v, got %v", s16, res)
+               }
+       })
+
+       t.Run("int32", func(t *testing.T) {
+               var sink bytes.Buffer
+               enc := NewEncoder(&sink)
+               enc.Encode(s32)
+
+               dec := NewDecoder(&sink)
+               res := make([]int32, 9)
+               dec.Decode(&res)
+
+               if !reflect.DeepEqual(s32, res) {
+                       t.Fatalf("EncodeIntSlice: expected %v, got %v", s32, res)
+               }
+       })
+
+       t.Run("int64", func(t *testing.T) {
+               var sink bytes.Buffer
+               enc := NewEncoder(&sink)
+               enc.Encode(s64)
+
+               dec := NewDecoder(&sink)
+               res := make([]int64, 9)
+               dec.Decode(&res)
+
+               if !reflect.DeepEqual(s64, res) {
+                       t.Fatalf("EncodeIntSlice: expected %v, got %v", s64, res)
+               }
+       })
+
+}
+
 type ET0 struct {
        A int
        B string