aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/gob/codec_test.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2022-10-18 13:29:10 -0700
committerGopher Robot <gobot@golang.org>2022-10-20 23:11:47 +0000
commit4c61e079c087052355c137ab8fcd9abf8728e50a (patch)
tree5eedc184afaf0cda7c82894405366de4b8a40718 /src/encoding/gob/codec_test.go
parent4725c71b735143a138b24f2b0e055c717d8d69ca (diff)
downloadgo-4c61e079c087052355c137ab8fcd9abf8728e50a.tar.xz
encoding/gob: support large slices in slice decode helpers
The slice decode helpers weren't aware of partially allocated slices. Also add large slice support for []byte. Change-Id: I5044587e917508887c7721f8059d364189831693 Reviewed-on: https://go-review.googlesource.com/c/go/+/443777 Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/encoding/gob/codec_test.go')
-rw-r--r--src/encoding/gob/codec_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/encoding/gob/codec_test.go b/src/encoding/gob/codec_test.go
index 649d75b7bb..54c356c464 100644
--- a/src/encoding/gob/codec_test.go
+++ b/src/encoding/gob/codec_test.go
@@ -1527,3 +1527,69 @@ func TestErrorInvalidTypeId(t *testing.T) {
}
}
}
+
+type LargeSliceByte struct {
+ S []byte
+}
+
+type LargeSliceInt8 struct {
+ S []int8
+}
+
+type StringPair struct {
+ A, B string
+}
+
+type LargeSliceStruct struct {
+ S []StringPair
+}
+
+func testEncodeDecode(t *testing.T, in, out any) {
+ t.Helper()
+ var b bytes.Buffer
+ err := NewEncoder(&b).Encode(in)
+ if err != nil {
+ t.Fatal("encode:", err)
+ }
+ err = NewDecoder(&b).Decode(out)
+ if err != nil {
+ t.Fatal("decode:", err)
+ }
+ if !reflect.DeepEqual(in, out) {
+ t.Errorf("output mismatch")
+ }
+}
+
+func TestLargeSlice(t *testing.T) {
+ t.Run("byte", func(t *testing.T) {
+ t.Parallel()
+ s := make([]byte, 10<<21)
+ for i := range s {
+ s[i] = byte(i)
+ }
+ st := &LargeSliceByte{S: s}
+ rt := &LargeSliceByte{}
+ testEncodeDecode(t, st, rt)
+ })
+ t.Run("int8", func(t *testing.T) {
+ t.Parallel()
+ s := make([]int8, 10<<21)
+ for i := range s {
+ s[i] = int8(i)
+ }
+ st := &LargeSliceInt8{S: s}
+ rt := &LargeSliceInt8{}
+ testEncodeDecode(t, st, rt)
+ })
+ t.Run("struct", func(t *testing.T) {
+ t.Parallel()
+ s := make([]StringPair, 1<<21)
+ for i := range s {
+ s[i].A = string(rune(i))
+ s[i].B = s[i].A
+ }
+ st := &LargeSliceStruct{S: s}
+ rt := &LargeSliceStruct{}
+ testEncodeDecode(t, st, rt)
+ })
+}