1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
49 math
.Float32frombits(0x1f202122),
50 math
.Float64frombits(0x232425262728292a),
52 math
.Float32frombits(0x2b2c2d2e),
53 math
.Float32frombits(0x2f303132),
56 math
.Float64frombits(0x333435363738393a),
57 math
.Float64frombits(0x3b3c3d3e3f404142),
60 [4]uint8{0x43, 0x44, 0x45, 0x46},
67 8, 9, 10, 11, 12, 13, 14, 15,
71 23, 24, 25, 26, 27, 28, 29, 30,
74 35, 36, 37, 38, 39, 40, 41, 42,
75 43, 44, 45, 46, 47, 48, 49, 50,
76 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
85 15, 14, 13, 12, 11, 10, 9, 8,
89 30, 29, 28, 27, 26, 25, 24, 23,
92 42, 41, 40, 39, 38, 37, 36, 35,
93 46, 45, 44, 43, 50, 49, 48, 47,
94 58, 57, 56, 55, 54, 53, 52, 51, 66, 65, 64, 63, 62, 61, 60, 59,
99 var src
= []byte{1, 2, 3, 4, 5, 6, 7, 8}
100 var res
= []int32{0x01020304, 0x05060708}
102 func checkResult(t
*testing
.T
, dir
string, order ByteOrder
, err error
, have
, want
interface{}) {
104 t
.Errorf("%v %v: %v", dir
, order
, err
)
107 if !reflect
.DeepEqual(have
, want
) {
108 t
.Errorf("%v %v:\n\thave %+v\n\twant %+v", dir
, order
, have
, want
)
112 func testRead(t
*testing
.T
, order ByteOrder
, b
[]byte, s1
interface{}) {
114 err
:= Read(bytes
.NewReader(b
), order
, &s2
)
115 checkResult(t
, "Read", order
, err
, s2
, s1
)
118 func testWrite(t
*testing
.T
, order ByteOrder
, b
[]byte, s1
interface{}) {
119 buf
:= new(bytes
.Buffer
)
120 err
:= Write(buf
, order
, s1
)
121 checkResult(t
, "Write", order
, err
, buf
.Bytes(), b
)
124 func TestLittleEndianRead(t
*testing
.T
) { testRead(t
, LittleEndian
, little
, s
) }
125 func TestLittleEndianWrite(t
*testing
.T
) { testWrite(t
, LittleEndian
, little
, s
) }
126 func TestLittleEndianPtrWrite(t
*testing
.T
) { testWrite(t
, LittleEndian
, little
, &s
) }
128 func TestBigEndianRead(t
*testing
.T
) { testRead(t
, BigEndian
, big
, s
) }
129 func TestBigEndianWrite(t
*testing
.T
) { testWrite(t
, BigEndian
, big
, s
) }
130 func TestBigEndianPtrWrite(t
*testing
.T
) { testWrite(t
, BigEndian
, big
, &s
) }
132 func TestReadSlice(t
*testing
.T
) {
133 slice
:= make([]int32, 2)
134 err
:= Read(bytes
.NewReader(src
), BigEndian
, slice
)
135 checkResult(t
, "ReadSlice", BigEndian
, err
, slice
, res
)
138 func TestWriteSlice(t
*testing
.T
) {
139 buf
:= new(bytes
.Buffer
)
140 err
:= Write(buf
, BigEndian
, res
)
141 checkResult(t
, "WriteSlice", BigEndian
, err
, buf
.Bytes(), src
)
144 // Addresses of arrays are easier to manipulate with reflection than are slices.
145 var intArrays
= []interface{}{
156 func TestSliceRoundTrip(t
*testing
.T
) {
157 buf
:= new(bytes
.Buffer
)
158 for _
, array
:= range intArrays
{
159 src
:= reflect
.ValueOf(array
).Elem()
161 switch src
.Index(0).Kind() {
162 case reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
:
165 for i
:= 0; i
< src
.Len(); i
++ {
167 src
.Index(i
).SetUint(uint64(i
* 0x07654321))
169 src
.Index(i
).SetInt(int64(i
* 0x07654321))
173 srcSlice
:= src
.Slice(0, src
.Len())
174 err
:= Write(buf
, BigEndian
, srcSlice
.Interface())
178 dst
:= reflect
.New(src
.Type()).Elem()
179 dstSlice
:= dst
.Slice(0, dst
.Len())
180 err
= Read(buf
, BigEndian
, dstSlice
.Interface())
184 if !reflect
.DeepEqual(src
.Interface(), dst
.Interface()) {
190 func TestWriteT(t
*testing
.T
) {
191 buf
:= new(bytes
.Buffer
)
193 if err
:= Write(buf
, BigEndian
, ts
); err
== nil {
194 t
.Errorf("WriteT: have err == nil, want non-nil")
197 tv
:= reflect
.Indirect(reflect
.ValueOf(ts
))
198 for i
, n
:= 0, tv
.NumField(); i
< n
; i
++ {
199 typ
:= tv
.Field(i
).Type().String()
201 typ
= "int" // the problem is int, not the [4]
203 if err
:= Write(buf
, BigEndian
, tv
.Field(i
).Interface()); err
== nil {
204 t
.Errorf("WriteT.%v: have err == nil, want non-nil", tv
.Field(i
).Type())
205 } else if !strings
.Contains(err
.Error(), typ
) {
206 t
.Errorf("WriteT: have err == %q, want it to mention %s", err
, typ
)
211 type BlankFields
struct {
223 type BlankFieldsProbe
struct {
235 func TestBlankFields(t
*testing
.T
) {
236 buf
:= new(bytes
.Buffer
)
237 b1
:= BlankFields
{A
: 1234567890, B
: 2.718281828, C
: 42}
238 if err
:= Write(buf
, LittleEndian
, &b1
); err
!= nil {
242 // zero values must have been written for blank fields
243 var p BlankFieldsProbe
244 if err
:= Read(buf
, LittleEndian
, &p
); err
!= nil {
248 // quick test: only check first value of slices
249 if p
.P0
!= 0 || p
.P1
[0] != 0 || p
.P2
[0] != 0 || p
.P3
.F
[0] != 0 {
250 t
.Errorf("non-zero values for originally blank fields: %#v", p
)
253 // write p and see if we can probe only some fields
254 if err
:= Write(buf
, LittleEndian
, &p
); err
!= nil {
258 // read should ignore blank fields in b2
260 if err
:= Read(buf
, LittleEndian
, &b2
); err
!= nil {
263 if b1
.A
!= b2
.A || b1
.B
!= b2
.B || b1
.C
!= b2
.C
{
264 t
.Errorf("%#v != %#v", b1
, b2
)
268 type byteSliceReader
struct {
272 func (br
*byteSliceReader
) Read(p
[]byte) (int, error
) {
273 n
:= copy(p
, br
.remain
)
274 br
.remain
= br
.remain
[n
:]
278 func BenchmarkReadSlice1000Int32s(b
*testing
.B
) {
279 bsr
:= &byteSliceReader
{}
280 slice
:= make([]int32, 1000)
281 buf
:= make([]byte, len(slice
)*4)
282 b
.SetBytes(int64(len(buf
)))
284 for i
:= 0; i
< b
.N
; i
++ {
286 Read(bsr
, BigEndian
, slice
)
290 func BenchmarkReadStruct(b
*testing
.B
) {
291 bsr
:= &byteSliceReader
{}
293 Write(&buf
, BigEndian
, &s
)
294 n
, _
:= dataSize(reflect
.ValueOf(s
))
298 for i
:= 0; i
< b
.N
; i
++ {
299 bsr
.remain
= buf
.Bytes()
300 Read(bsr
, BigEndian
, &t
)
303 if !reflect
.DeepEqual(s
, t
) {
308 func BenchmarkReadInts(b
*testing
.B
) {
310 bsr
:= &byteSliceReader
{}
311 var r io
.Reader
= bsr
312 b
.SetBytes(2 * (1 + 2 + 4 + 8))
314 for i
:= 0; i
< b
.N
; i
++ {
316 Read(r
, BigEndian
, &ls
.Int8
)
317 Read(r
, BigEndian
, &ls
.Int16
)
318 Read(r
, BigEndian
, &ls
.Int32
)
319 Read(r
, BigEndian
, &ls
.Int64
)
320 Read(r
, BigEndian
, &ls
.Uint8
)
321 Read(r
, BigEndian
, &ls
.Uint16
)
322 Read(r
, BigEndian
, &ls
.Uint32
)
323 Read(r
, BigEndian
, &ls
.Uint64
)
331 for i
:= range want
.Array
{
335 if !reflect
.DeepEqual(ls
, want
) {
340 func BenchmarkWriteInts(b
*testing
.B
) {
341 buf
:= new(bytes
.Buffer
)
342 var w io
.Writer
= buf
343 b
.SetBytes(2 * (1 + 2 + 4 + 8))
345 for i
:= 0; i
< b
.N
; i
++ {
347 Write(w
, BigEndian
, s
.Int8
)
348 Write(w
, BigEndian
, s
.Int16
)
349 Write(w
, BigEndian
, s
.Int32
)
350 Write(w
, BigEndian
, s
.Int64
)
351 Write(w
, BigEndian
, s
.Uint8
)
352 Write(w
, BigEndian
, s
.Uint16
)
353 Write(w
, BigEndian
, s
.Uint32
)
354 Write(w
, BigEndian
, s
.Uint64
)
357 if !bytes
.Equal(buf
.Bytes(), big
[:30]) {
358 b
.Fatalf("first half doesn't match: %x %x", buf
.Bytes(), big
[:30])
362 func BenchmarkWriteSlice1000Int32s(b
*testing
.B
) {
363 slice
:= make([]int32, 1000)
364 buf
:= new(bytes
.Buffer
)
365 var w io
.Writer
= buf
368 for i
:= 0; i
< b
.N
; i
++ {
370 Write(w
, BigEndian
, slice
)