OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [binary/] [binary_test.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package binary
6
 
7
import (
8
        "bytes"
9
        "io"
10
        "math"
11
        "reflect"
12
        "testing"
13
)
14
 
15
type Struct struct {
16
        Int8       int8
17
        Int16      int16
18
        Int32      int32
19
        Int64      int64
20
        Uint8      uint8
21
        Uint16     uint16
22
        Uint32     uint32
23
        Uint64     uint64
24
        Float32    float32
25
        Float64    float64
26
        Complex64  complex64
27
        Complex128 complex128
28
        Array      [4]uint8
29
}
30
 
31
type T struct {
32
        Int     int
33
        Uint    uint
34
        Uintptr uintptr
35
        Array   [4]int
36
}
37
 
38
var s = Struct{
39
        0x01,
40
        0x0203,
41
        0x04050607,
42
        0x08090a0b0c0d0e0f,
43
        0x10,
44
        0x1112,
45
        0x13141516,
46
        0x1718191a1b1c1d1e,
47
 
48
        math.Float32frombits(0x1f202122),
49
        math.Float64frombits(0x232425262728292a),
50
        complex(
51
                math.Float32frombits(0x2b2c2d2e),
52
                math.Float32frombits(0x2f303132),
53
        ),
54
        complex(
55
                math.Float64frombits(0x333435363738393a),
56
                math.Float64frombits(0x3b3c3d3e3f404142),
57
        ),
58
 
59
        [4]uint8{0x43, 0x44, 0x45, 0x46},
60
}
61
 
62
var big = []byte{
63
        1,
64
        2, 3,
65
        4, 5, 6, 7,
66
        8, 9, 10, 11, 12, 13, 14, 15,
67
        16,
68
        17, 18,
69
        19, 20, 21, 22,
70
        23, 24, 25, 26, 27, 28, 29, 30,
71
 
72
        31, 32, 33, 34,
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,
76
 
77
        67, 68, 69, 70,
78
}
79
 
80
var little = []byte{
81
        1,
82
        3, 2,
83
        7, 6, 5, 4,
84
        15, 14, 13, 12, 11, 10, 9, 8,
85
        16,
86
        18, 17,
87
        22, 21, 20, 19,
88
        30, 29, 28, 27, 26, 25, 24, 23,
89
 
90
        34, 33, 32, 31,
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,
94
 
95
        67, 68, 69, 70,
96
}
97
 
98
var src = []byte{1, 2, 3, 4, 5, 6, 7, 8}
99
var res = []int32{0x01020304, 0x05060708}
100
 
101
func checkResult(t *testing.T, dir string, order ByteOrder, err error, have, want interface{}) {
102
        if err != nil {
103
                t.Errorf("%v %v: %v", dir, order, err)
104
                return
105
        }
106
        if !reflect.DeepEqual(have, want) {
107
                t.Errorf("%v %v:\n\thave %+v\n\twant %+v", dir, order, have, want)
108
        }
109
}
110
 
111
func testRead(t *testing.T, order ByteOrder, b []byte, s1 interface{}) {
112
        var s2 Struct
113
        err := Read(bytes.NewBuffer(b), order, &s2)
114
        checkResult(t, "Read", order, err, s2, s1)
115
}
116
 
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)
121
}
122
 
123
func TestBigEndianRead(t *testing.T) { testRead(t, BigEndian, big, s) }
124
 
125
func TestLittleEndianRead(t *testing.T) { testRead(t, LittleEndian, little, s) }
126
 
127
func TestBigEndianWrite(t *testing.T) { testWrite(t, BigEndian, big, s) }
128
 
129
func TestLittleEndianWrite(t *testing.T) { testWrite(t, LittleEndian, little, s) }
130
 
131
func TestBigEndianPtrWrite(t *testing.T) { testWrite(t, BigEndian, big, &s) }
132
 
133
func TestLittleEndianPtrWrite(t *testing.T) { testWrite(t, LittleEndian, little, &s) }
134
 
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)
139
}
140
 
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)
145
}
146
 
147
func TestWriteT(t *testing.T) {
148
        buf := new(bytes.Buffer)
149
        ts := T{}
150
        err := Write(buf, BigEndian, ts)
151
        if err == nil {
152
                t.Errorf("WriteT: have nil, want non-nil")
153
        }
154
 
155
        tv := reflect.Indirect(reflect.ValueOf(ts))
156
        for i, n := 0, tv.NumField(); i < n; i++ {
157
                err = Write(buf, BigEndian, tv.Field(i).Interface())
158
                if err == nil {
159
                        t.Errorf("WriteT.%v: have nil, want non-nil", tv.Field(i).Type())
160
                }
161
        }
162
}
163
 
164
type byteSliceReader struct {
165
        remain []byte
166
}
167
 
168
func (br *byteSliceReader) Read(p []byte) (int, error) {
169
        n := copy(p, br.remain)
170
        br.remain = br.remain[n:]
171
        return n, nil
172
}
173
 
174
func BenchmarkReadSlice1000Int32s(b *testing.B) {
175
        bsr := &byteSliceReader{}
176
        slice := make([]int32, 1000)
177
        buf := make([]byte, len(slice)*4)
178
        b.SetBytes(int64(len(buf)))
179
        b.ResetTimer()
180
        for i := 0; i < b.N; i++ {
181
                bsr.remain = buf
182
                Read(bsr, BigEndian, slice)
183
        }
184
}
185
 
186
func BenchmarkReadStruct(b *testing.B) {
187
        bsr := &byteSliceReader{}
188
        var buf bytes.Buffer
189
        Write(&buf, BigEndian, &s)
190
        n := dataSize(reflect.ValueOf(s))
191
        b.SetBytes(int64(n))
192
        t := s
193
        b.ResetTimer()
194
        for i := 0; i < b.N; i++ {
195
                bsr.remain = buf.Bytes()
196
                Read(bsr, BigEndian, &t)
197
        }
198
        b.StopTimer()
199
        if !reflect.DeepEqual(s, t) {
200
                b.Fatal("no match")
201
        }
202
}
203
 
204
func BenchmarkReadInts(b *testing.B) {
205
        var ls Struct
206
        bsr := &byteSliceReader{}
207
        var r io.Reader = bsr
208
        b.SetBytes(2 * (1 + 2 + 4 + 8))
209
        b.ResetTimer()
210
        for i := 0; i < b.N; i++ {
211
                bsr.remain = big
212
                Read(r, BigEndian, &ls.Int8)
213
                Read(r, BigEndian, &ls.Int16)
214
                Read(r, BigEndian, &ls.Int32)
215
                Read(r, BigEndian, &ls.Int64)
216
                Read(r, BigEndian, &ls.Uint8)
217
                Read(r, BigEndian, &ls.Uint16)
218
                Read(r, BigEndian, &ls.Uint32)
219
                Read(r, BigEndian, &ls.Uint64)
220
        }
221
 
222
        want := s
223
        want.Float32 = 0
224
        want.Float64 = 0
225
        want.Complex64 = 0
226
        want.Complex128 = 0
227
        for i := range want.Array {
228
                want.Array[i] = 0
229
        }
230
        b.StopTimer()
231
        if !reflect.DeepEqual(ls, want) {
232
                panic("no match")
233
        }
234
}
235
 
236
func BenchmarkWriteInts(b *testing.B) {
237
        buf := new(bytes.Buffer)
238
        var w io.Writer = buf
239
        b.SetBytes(2 * (1 + 2 + 4 + 8))
240
        b.ResetTimer()
241
        for i := 0; i < b.N; i++ {
242
                buf.Reset()
243
                Write(w, BigEndian, s.Int8)
244
                Write(w, BigEndian, s.Int16)
245
                Write(w, BigEndian, s.Int32)
246
                Write(w, BigEndian, s.Int64)
247
                Write(w, BigEndian, s.Uint8)
248
                Write(w, BigEndian, s.Uint16)
249
                Write(w, BigEndian, s.Uint32)
250
                Write(w, BigEndian, s.Uint64)
251
        }
252
        b.StopTimer()
253
        if !bytes.Equal(buf.Bytes(), big[:30]) {
254
                b.Fatalf("first half doesn't match: %x %x", buf.Bytes(), big[:30])
255
        }
256
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.