diff options
author | Russ Cox <rsc@golang.org> | 2009-08-31 16:10:16 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-08-31 16:10:16 -0700 |
commit | 071fccbb3850d2d3390ce64ddc36db84c69ff94b (patch) | |
tree | f6c6f63c7f2f9a4557795f4cd16124caa58e7891 /src/pkg/debug/binary/binary_test.go | |
parent | 5a6f918c6c43dea5fb8c2aa16593b5f3f26341d9 (diff) | |
download | golang-071fccbb3850d2d3390ce64ddc36db84c69ff94b.tar.gz |
package debug/binary
R=austin
DELTA=320 (320 added, 0 deleted, 0 changed)
OCL=33983
CL=34143
Diffstat (limited to 'src/pkg/debug/binary/binary_test.go')
-rw-r--r-- | src/pkg/debug/binary/binary_test.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/pkg/debug/binary/binary_test.go b/src/pkg/debug/binary/binary_test.go new file mode 100644 index 000000000..7f0c226ce --- /dev/null +++ b/src/pkg/debug/binary/binary_test.go @@ -0,0 +1,87 @@ +// 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. + +package binary + +import ( + "bytes"; + "math"; + "reflect"; + "testing"; +) + +type Struct struct { + Int8 int8; + Int16 int16; + Int32 int32; + Int64 int64; + Uint8 uint8; + Uint16 uint16; + Uint32 uint32; + Uint64 uint64; + Float64 float64; + Array [4]uint8; +} + +var s = Struct { + 0x01, + 0x0203, + 0x04050607, + 0x08090a0b0c0d0e0f, + 0x10, + 0x1112, + 0x13141516, + 0x1718191a1b1c1d1e, + math.Float64frombits(0x1f20212223242526), + [4]uint8 { 0x27, 0x28, 0x29, 0x2a }, +} + +var big = []byte{ + 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, +} + +var little = []byte{ + 1, + 3, 2, + 7, 6, 5, 4, + 15, 14, 13, 12, 11, 10, 9, 8, + 16, + 18, 17, + 22, 21, 20, 19, + 30, 29, 28, 27, 26, 25, 24, 23, + 38, 37, 36, 35, 34, 33, 32, 31, + 39, 40, 41, 42, +} + +func TestRead(t *testing.T) { + var sl, sb Struct; + + err := Read(bytes.NewBuffer(big), BigEndian, &sb); + if err != nil { + t.Errorf("Read big-endian: %v", err); + goto little; + } + if !reflect.DeepEqual(sb, s) { + t.Errorf("Read big-endian:\n\thave %+v\n\twant %+v", sb, s); + } + +little: + err = Read(bytes.NewBuffer(little), LittleEndian, &sl); + if err != nil { + t.Errorf("Read little-endian: %v", err); + } + if !reflect.DeepEqual(sl, s) { + t.Errorf("Read big-endian:\n\thave %+v\n\twant %+v", sl, s); + } +} + |