-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
210 lines (205 loc) · 6.58 KB
/
writer_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
package msgpack
import (
"bytes"
"math"
"strings"
"testing"
"time"
)
func TestWriterWrite(t *testing.T) {
tests := []struct {
write func(*Writer) error
data []byte
}{
// nil
{
write: func(w *Writer) error { return w.WriteNil() },
data: []byte{tagNil},
},
// bool
{
write: func(w *Writer) error { return w.WriteBool(false) },
data: []byte{tagFalse},
},
{
write: func(w *Writer) error { return w.WriteBool(true) },
data: []byte{tagTrue},
},
// int
{
write: func(w *Writer) error { return w.WriteInt64(7) },
data: []byte{posFixintTag(7)},
},
{
write: func(w *Writer) error { return w.WriteInt64(-7) },
data: []byte{negFixintTag(-7)},
},
{
write: func(w *Writer) error { return w.WriteInt64(math.MinInt8) },
data: []byte{tagInt8, 0x80},
},
{
write: func(w *Writer) error { return w.WriteInt64(math.MinInt16) },
data: []byte{tagInt16, 0x80, 0x0},
},
{
write: func(w *Writer) error { return w.WriteInt64(math.MinInt32) },
data: []byte{tagInt32, 0x80, 0x0, 0x0, 0x0},
},
{
write: func(w *Writer) error { return w.WriteInt64(math.MinInt64) },
data: []byte{tagInt64, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
},
// uint
{
write: func(w *Writer) error { return w.WriteUint64(7) },
data: []byte{posFixintTag(7)},
},
{
write: func(w *Writer) error { return w.WriteUint64(math.MaxUint8) },
data: []byte{tagUint8, 0xff},
},
{
write: func(w *Writer) error { return w.WriteUint64(math.MaxUint16) },
data: []byte{tagUint16, 0xff, 0xff},
},
{
write: func(w *Writer) error { return w.WriteUint64(math.MaxUint32) },
data: []byte{tagUint32, 0xff, 0xff, 0xff, 0xff},
},
{
write: func(w *Writer) error { return w.WriteUint64(math.MaxUint64) },
data: []byte{tagUint64, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
},
// float
{
write: func(w *Writer) error { return w.WriteFloat32(3.141592) },
data: []byte{tagFloat32, 0x40, 0x49, 0x0f, 0xd8},
},
{
write: func(w *Writer) error { return w.WriteFloat64(3.141592) },
data: []byte{tagFloat64, 0x40, 0x09, 0x21, 0xfa, 0xfc, 0x8b, 0x00, 0x7a},
},
// bytes
{
write: func(w *Writer) error { return w.WriteBytes(bytes.Repeat([]byte{'0'}, 1)) },
data: append([]byte{tagBin8, 0x1}, bytes.Repeat([]byte{'0'}, 1)...),
},
{
write: func(w *Writer) error { return w.WriteBytes(bytes.Repeat([]byte{'0'}, math.MaxUint8+1)) },
data: append([]byte{tagBin16, 0x1, 0x0}, bytes.Repeat([]byte{'0'}, math.MaxUint8+1)...),
},
{
write: func(w *Writer) error { return w.WriteBytes(bytes.Repeat([]byte{'0'}, math.MaxUint16+1)) },
data: append([]byte{tagBin32, 0x0, 0x1, 0x0, 0x0}, bytes.Repeat([]byte{'0'}, math.MaxUint16+1)...),
},
// string
{
write: func(w *Writer) error { return w.WriteString(strings.Repeat("0", 7)) },
data: append([]byte{fixstrTag(7)}, bytes.Repeat([]byte{'0'}, 7)...),
},
{
write: func(w *Writer) error { return w.WriteString(strings.Repeat("0", 32)) },
data: append([]byte{tagStr8, 0x20}, bytes.Repeat([]byte{'0'}, 32)...),
},
{
write: func(w *Writer) error { return w.WriteString(strings.Repeat("0", math.MaxUint8+1)) },
data: append([]byte{tagStr16, 0x1, 0x0}, bytes.Repeat([]byte{'0'}, math.MaxUint8+1)...),
},
{
write: func(w *Writer) error { return w.WriteString(strings.Repeat("0", math.MaxUint16+1)) },
data: append([]byte{tagStr32, 0x0, 0x01, 0x0, 0x0}, bytes.Repeat([]byte{'0'}, math.MaxUint16+1)...),
},
// array
{
write: func(w *Writer) error { return w.WriteArrayHeader(7) },
data: []byte{fixarrayTag(7)},
},
{
write: func(w *Writer) error { return w.WriteArrayHeader(math.MaxUint16) },
data: []byte{tagArray16, 0xff, 0xff},
},
{
write: func(w *Writer) error { return w.WriteArrayHeader(math.MaxUint32) },
data: []byte{tagArray32, 0xff, 0xff, 0xff, 0xff},
},
// map
{
write: func(w *Writer) error { return w.WriteMapHeader(7) },
data: []byte{fixmapTag(7)},
},
{
write: func(w *Writer) error { return w.WriteMapHeader(math.MaxUint16) },
data: []byte{tagMap16, 0xff, 0xff},
},
{
write: func(w *Writer) error { return w.WriteMapHeader(math.MaxUint32) },
data: []byte{tagMap32, 0xff, 0xff, 0xff, 0xff},
},
// raw
{
write: func(w *Writer) error { return w.WriteRaw(Raw{0x01, 0x02, 0x03, 0x04, 0x05}) },
data: []byte{0x01, 0x02, 0x03, 0x04, 0x05},
},
// ext
{
write: func(w *Writer) error { return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, 1))) },
data: append([]byte{tagFixExt1, 0xd}, bytes.Repeat([]byte{'0'}, 1)...),
},
{
write: func(w *Writer) error { return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, 2))) },
data: append([]byte{tagFixExt2, 0xd}, bytes.Repeat([]byte{'0'}, 2)...),
},
{
write: func(w *Writer) error { return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, 4))) },
data: append([]byte{tagFixExt4, 0xd}, bytes.Repeat([]byte{'0'}, 4)...),
},
{
write: func(w *Writer) error { return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, 8))) },
data: append([]byte{tagFixExt8, 0xd}, bytes.Repeat([]byte{'0'}, 8)...),
},
{
write: func(w *Writer) error { return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, 16))) },
data: append([]byte{tagFixExt16, 0xd}, bytes.Repeat([]byte{'0'}, 16)...),
},
{
write: func(w *Writer) error {
return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, 7)))
},
data: append([]byte{tagExt8, 0x7, 0xd}, bytes.Repeat([]byte{'0'}, 7)...),
},
{
write: func(w *Writer) error {
return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, math.MaxUint8+1)))
},
data: append([]byte{tagExt16, 0x1, 0x0, 0xd}, bytes.Repeat([]byte{'0'}, math.MaxUint8+1)...),
},
{
write: func(w *Writer) error {
return w.WriteExt(0xd, bytesMarshaler(bytes.Repeat([]byte{'0'}, math.MaxUint16+1)))
},
data: append([]byte{tagExt32, 0x0, 0x1, 0x0, 0x0, 0xd}, bytes.Repeat([]byte{'0'}, math.MaxUint16+1)...),
},
// time
{
write: func(w *Writer) error {
return w.WriteTime(time.Date(2017, time.September, 26, 13, 14, 15, 0, time.UTC))
},
data: []byte{tagExt8, 12, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0xca, 0x52, 0xa7},
},
}
var buf bytes.Buffer
for _, test := range tests {
buf.Reset()
err := test.write(NewWriter(&buf))
if err != nil {
t.Errorf("unexpected write error: %v", err)
} else if !bytes.Equal(buf.Bytes(), test.data) {
t.Errorf("unexpected data: %x", buf.Bytes())
}
}
}
type bytesMarshaler []byte
func (m bytesMarshaler) MarshalBinary() ([]byte, error) {
return []byte(m), nil
}