-
Notifications
You must be signed in to change notification settings - Fork 121
/
wire_test.go
260 lines (223 loc) · 5.78 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package ib
import (
"bufio"
"bytes"
"math"
"strconv"
"testing"
"time"
)
func makebuf() *bytes.Buffer {
return bytes.NewBuffer(make([]byte, 0, 1024))
}
func TestWriteString(t *testing.T) {
b := makebuf()
writeString(b, "foobar")
expected := "foobar\000"
if b.String() != expected {
t.Fatalf("writeString('foobar') = %s, want %s", b.String(), expected)
}
}
func TestWriteInt(t *testing.T) {
b := makebuf()
writeInt(b, int64(57))
expected := "57\000"
if b.String() != expected {
t.Fatalf("writeInt(57) = %s, want %s", b.String(), expected)
}
}
func TestWriteFloat(t *testing.T) {
f := 0.535
b := makebuf()
writeFloat(b, f)
expected := strconv.FormatFloat(f, 'g', 10, 64) + "\000"
if b.String() != expected {
t.Fatalf("writeFloat(%g) = %s, want %s", f, b.String(), expected)
}
}
func TestReadString(t *testing.T) {
x := "foobar"
b := makebuf()
writeString(b, x)
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readString(r)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x != y {
t.Fatalf("expected %s but got %s", x, y)
}
}
func TestReadInt(t *testing.T) {
x := int64(57)
b := makebuf()
writeInt(b, x)
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readInt(r)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x != y {
t.Fatalf("expected %d but got %d", x, y)
}
}
func TestReadEmptyStringReturnsMaxInt(t *testing.T) {
b := makebuf()
writeString(b, "")
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readInt(r)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if y != math.MaxInt64 {
t.Fatalf("expected maximum int64 but got %d", y)
}
}
func TestReadIntList(t *testing.T) {
b := makebuf()
writeString(b, "1|2|3")
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readIntList(r)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if len(y) != 3 {
t.Fatalf("expected 3 but got %d", len(y))
}
}
func TestWriteTimeUTC(t *testing.T) {
b := makebuf()
ts := time.Now()
writeTime(b, ts, timeWriteUTC)
expected := ts.UTC().Format("20060102 15:04:05") + " UTC\000"
if b.String() != expected {
t.Fatalf("writeTime(%s) = %s, want %s", ts, b.String(), expected)
}
}
func TestWriteTimeLocal(t *testing.T) {
b := makebuf()
ts := time.Now()
writeTime(b, ts, timeWriteLocalTime)
expected := ts.Format("20060102 15:04:05") + "\000"
if b.String() != expected {
t.Fatalf("writeTime(%s) = %s, want %s", ts, b.String(), expected)
}
}
func TestReadTimeAutoDetect(t *testing.T) {
if f, _ := detectTime("349834583"); f != timeReadEpoch {
t.Fatalf("failed to detect epoch")
}
if f, _ := detectTime("20140322 23:59:31"); f != timeReadLocalDateTime {
t.Fatalf("failed to detect local time")
}
if f, _ := detectTime("20140322 23:59:31 US"); f != timeReadLocalDateTime {
t.Fatalf("failed to detect local time when timezone present")
}
if f, _ := detectTime("20140322"); f != timeReadLocalDate {
t.Fatalf("failed to detect local date")
}
if f, _ := detectTime("15:33"); f != timeReadLocalTime {
t.Fatalf("failed to detect local hh:mm time")
}
if f, _ := detectTime("15:33:42"); f != timeReadLocalTime {
t.Fatalf("failed to detect local hh:mm:ss time")
}
if _, err := detectTime("2014/03/22"); err == nil {
t.Fatalf("failed to return error for unknown time format")
}
}
func TestReadTimeEpoch(t *testing.T) {
x := time.Now()
x = x.Add(time.Duration(-1 * x.Nanosecond()))
b := makebuf()
err := writeString(b, strconv.Itoa(int(x.Unix())))
if err != nil {
t.Fatalf("failed to write: %v", err)
}
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readTime(r, timeReadEpoch)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x != y {
t.Fatalf("expected %v but got %v", x, y)
}
}
func TestReadTimeLocalDateTime(t *testing.T) {
x := time.Now()
x = x.Add(time.Duration(-1 * x.Nanosecond()))
b := makebuf()
err := writeTime(b, x, timeWriteLocalTime)
if err != nil {
t.Fatalf("failed to write: %v", err)
}
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readTime(r, timeReadLocalDateTime)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x != y {
t.Fatalf("expected %v but got %v", x, y)
}
}
func TestReadTimeLocalDate(t *testing.T) {
x := time.Date(2014, 3, 22, 0, 0, 0, 0, time.Local)
b := makebuf()
err := writeString(b, x.Format("20060102"))
if err != nil {
t.Fatalf("failed to write: %v", err)
}
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readTime(r, timeReadLocalDate)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x != y {
t.Fatalf("expected %v but got %v", x, y)
}
}
func TestReadTimeLocalTimeShort(t *testing.T) {
x := time.Date(2014, 3, 22, 14, 4, 0, 0, time.Local)
b := makebuf()
err := writeString(b, x.Format("15:04"))
if err != nil {
t.Fatalf("failed to write: %v", err)
}
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readTime(r, timeReadLocalTime)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x.Hour() != y.Hour() || x.Minute() != y.Minute() {
t.Fatalf("expected %v but got %v", x, y)
}
}
func TestReadTimeLocalTimeLong(t *testing.T) {
x := time.Date(2014, 3, 22, 14, 4, 32, 0, time.Local)
b := makebuf()
err := writeString(b, x.Format("15:04:05"))
if err != nil {
t.Fatalf("failed to write: %v", err)
}
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readTime(r, timeReadLocalTime)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x.Hour() != y.Hour() || x.Minute() != y.Minute() || x.Second() != y.Second() {
t.Fatalf("expected %v but got %v", x, y)
}
}
func TestReadFloat(t *testing.T) {
x := 0.545
b := makebuf()
writeFloat(b, x)
r := bufio.NewReader(bytes.NewReader(b.Bytes()))
y, err := readFloat(r)
if err != nil {
t.Fatalf("failed to read: %v", err)
}
if x != y {
t.Fatalf("expected %v but got %v", x, y)
}
}