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.
48 math.Float32frombits(0x1f202122),
49 math.Float64frombits(0x232425262728292a),
51 math.Float32frombits(0x2b2c2d2e),
52 math.Float32frombits(0x2f303132),
55 math.Float64frombits(0x333435363738393a),
56 math.Float64frombits(0x3b3c3d3e3f404142),
59 [4]uint8{0x43, 0x44, 0x45, 0x46},
66 8, 9, 10, 11, 12, 13, 14, 15,
70 23, 24, 25, 26, 27, 28, 29, 30,
73 35, 36, 37, 38, 39, 40, 41, 42,
74 43, 44, 45, 46, 47, 48, 49, 50,
75 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
84 15, 14, 13, 12, 11, 10, 9, 8,
88 30, 29, 28, 27, 26, 25, 24, 23,
91 42, 41, 40, 39, 38, 37, 36, 35,
92 46, 45, 44, 43, 50, 49, 48, 47,
93 58, 57, 56, 55, 54, 53, 52, 51, 66, 65, 64, 63, 62, 61, 60, 59,
98 var src = []byte{1, 2, 3, 4, 5, 6, 7, 8}
99 var res = []int32{0x01020304, 0x05060708}
101 func checkResult(t *testing.T, dir string, order, err os.Error, have, want interface{}) {
103 t.Errorf("%v %v: %v", dir, order, err)
106 if !reflect.DeepEqual(have, want) {
107 t.Errorf("%v %v:\n\thave %+v\n\twant %+v", dir, order, have, want)
111 func testRead(t *testing.T, order ByteOrder, b []byte, s1 interface{}) {
113 err := Read(bytes.NewBuffer(b), order, &s2)
114 checkResult(t, "Read", order, err, s2, s1)
117 func testWrite(t *testing.T, order ByteOrder, b []byte, s1 interface{}) {
118 buf := new(bytes.Buffer)
119 err := Write(buf, order, s1)
120 checkResult(t, "Write", order, err, buf.Bytes(), b)
123 func TestBigEndianRead(t *testing.T) { testRead(t, BigEndian, big, s) }
125 func TestLittleEndianRead(t *testing.T) { testRead(t, LittleEndian, little, s) }
127 func TestBigEndianWrite(t *testing.T) { testWrite(t, BigEndian, big, s) }
129 func TestLittleEndianWrite(t *testing.T) { testWrite(t, LittleEndian, little, s) }
131 func TestBigEndianPtrWrite(t *testing.T) { testWrite(t, BigEndian, big, &s) }
133 func TestLittleEndianPtrWrite(t *testing.T) { testWrite(t, LittleEndian, little, &s) }
135 func TestReadSlice(t *testing.T) {
136 slice := make([]int32, 2)
137 err := Read(bytes.NewBuffer(src), BigEndian, slice)
138 checkResult(t, "ReadSlice", BigEndian, err, slice, res)
141 func TestWriteSlice(t *testing.T) {
142 buf := new(bytes.Buffer)
143 err := Write(buf, BigEndian, res)
144 checkResult(t, "WriteSlice", BigEndian, err, buf.Bytes(), src)
147 func TestWriteT(t *testing.T) {
148 buf := new(bytes.Buffer)
150 err := Write(buf, BigEndian, ts)
152 t.Errorf("WriteT: have nil, want non-nil")
155 tv := reflect.Indirect(reflect.NewValue(ts)).(*reflect.StructValue)
156 for i, n := 0, tv.NumField(); i < n; i++ {
157 err = Write(buf, BigEndian, tv.Field(i).Interface())
159 t.Errorf("WriteT.%v: have nil, want non-nil", tv.Field(i).Type())