forked from gofinance/ib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_test.go
214 lines (163 loc) · 3.48 KB
/
wire_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package trade
import (
"bufio"
"bytes"
"reflect"
"strconv"
"testing"
"time"
)
type intRec struct {
I int64
}
type int64Rec struct {
L int64
}
type stringRec struct {
S string
}
type timeRec struct {
T time.Time
}
type floatRec struct {
F float64
}
type item struct {
S string
}
type sliceRec struct {
Items []item
}
type tagRec struct {
S string
I int64 `when:"S" cond:"is" value:""`
}
func makebuf() *bytes.Buffer {
return bytes.NewBuffer(make([]byte, 0, 1024))
}
func testEncode(t *testing.T, v interface{}, s string) {
b := makebuf()
if err := encode(b, reflect.ValueOf(v)); err != nil {
t.Fatal(err)
}
if b.String() != s+"\000" {
t.Fatalf("encode(%v) = %s, want %s", v, b.String(), s)
}
}
func TestEncodeInt(t *testing.T) {
v := &intRec{I: 57}
testEncode(t, v, "57")
}
func TestEncodeLong(t *testing.T) {
v := &int64Rec{L: 57}
testEncode(t, v, "57")
}
func TestEncodeString(t *testing.T) {
v := &stringRec{S: "foobar"}
testEncode(t, v, "foobar")
}
func TestEncodeTime(t *testing.T) {
ts := time.Now()
v := &timeRec{T: ts}
testEncode(t, v, ts.Format(ibTimeFormat))
}
func TestEncodeFloat(t *testing.T) {
f := 0.535
v := &floatRec{F: f}
testEncode(t, v, strconv.FormatFloat(f, 'g', 10, 64))
}
func TestEncodeEmptySlice(t *testing.T) {
v := &sliceRec{}
testEncode(t, v, "0")
}
func TestEncodeSlice(t *testing.T) {
v := &sliceRec{Items: []item{{"foo"}, {"bar"}}}
testEncode(t, v, "2\000foo\000bar")
}
func TestNotEncodeTag(t *testing.T) {
v := &tagRec{I: 10}
testEncode(t, v, "")
}
func TestEncodeTag(t *testing.T) {
v := &tagRec{S: "yes!", I: 10}
testEncode(t, v, "yes!\00010")
}
func testDecode(t *testing.T, src interface{}, dst interface{}) {
b := makebuf()
if err := encode(b, reflect.ValueOf(src)); err != nil {
t.Fatal(err)
}
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
if err := decode(r, reflect.ValueOf(dst)); err != nil {
t.Fatal(err)
}
}
func TestDecodeInt(t *testing.T) {
v1 := &intRec{I: 57}
v2 := &intRec{}
testDecode(t, v1, v2)
if *v1 != *v2 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestDecodeString(t *testing.T) {
v1 := &stringRec{S: "foobar"}
v2 := &stringRec{}
testDecode(t, v1, v2)
if *v1 != *v2 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestDecodeTime(t *testing.T) {
now := time.Now()
now = now.Add(time.Duration(-1 * now.Nanosecond()))
v1 := &timeRec{T: now}
v2 := &timeRec{}
testDecode(t, v1, v2)
if *v1 != *v2 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestDecodeFloat(t *testing.T) {
v1 := &floatRec{F: 0.545}
v2 := &floatRec{}
testDecode(t, v1, v2)
if *v1 != *v2 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestDecodeEmptySlice(t *testing.T) {
v1 := &sliceRec{}
v2 := &sliceRec{}
testDecode(t, v1, v2)
if len(v2.Items) != 0 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestDecodeSlice(t *testing.T) {
v1 := &sliceRec{Items: []item{{"foo"}, {"bar"}}}
v2 := &sliceRec{}
testDecode(t, v1, v2)
if len(v2.Items) != 2 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
if v2.Items[0] != v1.Items[0] || v2.Items[1] != v1.Items[1] {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestNotDecodeTag(t *testing.T) {
v1 := &tagRec{I: 10}
v2 := &tagRec{}
testDecode(t, v1, v2)
if v2.S != "" || v2.I != 0 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}
func TestDecodeTag(t *testing.T) {
v1 := &tagRec{S: "yes!", I: 10}
v2 := &tagRec{}
testDecode(t, v1, v2)
if *v1 != *v2 {
t.Fatalf("decode got %v, want %v", v2, v1)
}
}