diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-05-02 14:43:35 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-05-02 14:43:35 +0000 |
commit | 34efdaf078b01a7387007c4e6bde6db86384c4b7 (patch) | |
tree | d503eaf41d085669d1481bb46ec038bc866fece6 /libgo/go/encoding/binary/binary_test.go | |
parent | f733cf303bcdc952c92b81dd62199a40a1f555ec (diff) | |
download | gcc-tarball-master.tar.gz |
gcc-7.1.0gcc-7.1.0
Diffstat (limited to 'libgo/go/encoding/binary/binary_test.go')
-rw-r--r-- | libgo/go/encoding/binary/binary_test.go | 80 |
1 files changed, 68 insertions, 12 deletions
diff --git a/libgo/go/encoding/binary/binary_test.go b/libgo/go/encoding/binary/binary_test.go index 7fd36fa4ef..fc7f2765ef 100644 --- a/libgo/go/encoding/binary/binary_test.go +++ b/libgo/go/encoding/binary/binary_test.go @@ -1,4 +1,4 @@ -// Copyright 2009 The Go Authors. All rights reserved. +// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -27,6 +27,8 @@ type Struct struct { Complex64 complex64 Complex128 complex128 Array [4]uint8 + Bool bool + BoolArray [4]bool } type T struct { @@ -58,6 +60,9 @@ var s = Struct{ ), [4]uint8{0x43, 0x44, 0x45, 0x46}, + + true, + [4]bool{true, false, true, false}, } var big = []byte{ @@ -76,6 +81,9 @@ var big = []byte{ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, + + 1, + 1, 0, 1, 0, } var little = []byte{ @@ -94,6 +102,9 @@ var little = []byte{ 58, 57, 56, 55, 54, 53, 52, 51, 66, 65, 64, 63, 62, 61, 60, 59, 67, 68, 69, 70, + + 1, + 1, 0, 1, 0, } var src = []byte{1, 2, 3, 4, 5, 6, 7, 8} @@ -141,6 +152,25 @@ func TestWriteSlice(t *testing.T) { checkResult(t, "WriteSlice", BigEndian, err, buf.Bytes(), src) } +func TestReadBool(t *testing.T) { + var res bool + var err error + err = Read(bytes.NewReader([]byte{0}), BigEndian, &res) + checkResult(t, "ReadBool", BigEndian, err, res, false) + res = false + err = Read(bytes.NewReader([]byte{1}), BigEndian, &res) + checkResult(t, "ReadBool", BigEndian, err, res, true) + res = false + err = Read(bytes.NewReader([]byte{2}), BigEndian, &res) + checkResult(t, "ReadBool", BigEndian, err, res, true) +} + +func TestReadBoolSlice(t *testing.T) { + slice := make([]bool, 4) + err := Read(bytes.NewReader([]byte{0, 1, 2, 255}), BigEndian, slice) + checkResult(t, "ReadBoolSlice", BigEndian, err, slice, []bool{false, true, true, true}) +} + // Addresses of arrays are easier to manipulate with reflection than are slices. var intArrays = []interface{}{ &[100]int8{}, @@ -266,7 +296,7 @@ func TestBlankFields(t *testing.T) { } // An attempt to read into a struct with an unexported field will -// panic. This is probably not the best choice, but at this point +// panic. This is probably not the best choice, but at this point // anything else would be an API change. type Unexported struct { @@ -339,6 +369,33 @@ func TestReadTruncated(t *testing.T) { } } +func testUint64SmallSliceLengthPanics() (panicked bool) { + defer func() { + panicked = recover() != nil + }() + b := [8]byte{1, 2, 3, 4, 5, 6, 7, 8} + LittleEndian.Uint64(b[:4]) + return false +} + +func testPutUint64SmallSliceLengthPanics() (panicked bool) { + defer func() { + panicked = recover() != nil + }() + b := [8]byte{} + LittleEndian.PutUint64(b[:4], 0x0102030405060708) + return false +} + +func TestEarlyBoundsChecks(t *testing.T) { + if testUint64SmallSliceLengthPanics() != true { + t.Errorf("binary.LittleEndian.Uint64 expected to panic for small slices, but didn't") + } + if testPutUint64SmallSliceLengthPanics() != true { + t.Errorf("binary.LittleEndian.PutUint64 expected to panic for small slices, but didn't") + } +} + type byteSliceReader struct { remain []byte } @@ -373,8 +430,8 @@ func BenchmarkReadStruct(b *testing.B) { Read(bsr, BigEndian, &t) } b.StopTimer() - if !reflect.DeepEqual(s, t) { - b.Fatal("no match") + if b.N > 0 && !reflect.DeepEqual(s, t) { + b.Fatalf("struct doesn't match:\ngot %v;\nwant %v", t, s) } } @@ -395,18 +452,17 @@ func BenchmarkReadInts(b *testing.B) { Read(r, BigEndian, &ls.Uint32) Read(r, BigEndian, &ls.Uint64) } - + b.StopTimer() want := s want.Float32 = 0 want.Float64 = 0 want.Complex64 = 0 want.Complex128 = 0 - for i := range want.Array { - want.Array[i] = 0 - } - b.StopTimer() - if !reflect.DeepEqual(ls, want) { - panic("no match") + want.Array = [4]uint8{0, 0, 0, 0} + want.Bool = false + want.BoolArray = [4]bool{false, false, false, false} + if b.N > 0 && !reflect.DeepEqual(ls, want) { + b.Fatalf("struct doesn't match:\ngot %v;\nwant %v", ls, want) } } @@ -427,7 +483,7 @@ func BenchmarkWriteInts(b *testing.B) { Write(w, BigEndian, s.Uint64) } b.StopTimer() - if !bytes.Equal(buf.Bytes(), big[:30]) { + if b.N > 0 && !bytes.Equal(buf.Bytes(), big[:30]) { b.Fatalf("first half doesn't match: %x %x", buf.Bytes(), big[:30]) } } |