OSDN Git Service

Remove the types float and complex.
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / binary / binary_test.go
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.
4
5 package binary
6
7 import (
8         "os"
9         "bytes"
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, err os.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.NewValue(ts)).(*reflect.StructValue)
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 }