forked from bost-h/go-packstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.go
More file actions
264 lines (237 loc) · 9.72 KB
/
Copy pathencoder_test.go
File metadata and controls
264 lines (237 loc) · 9.72 KB
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
261
262
263
264
package packstream
import (
"bytes"
"math"
"strconv"
"testing"
"time"
)
func TestNewEncoder(t *testing.T) {
b := new(bytes.Buffer)
if enc := NewEncoder(b); enc == nil {
t.Error("returned value should not be nil.")
} else if buf, ok := enc.wr.(*bytes.Buffer); !ok {
t.Error("encoder buffer is not valid, expected a bytes buffer.")
} else if buf != b {
t.Error("encoder buffer is not valid.")
}
}
func TestEncoder_Encode(t *testing.T) {
var b bytes.Buffer
enc := NewEncoder(&b)
for _, val := range validTestValues {
if err := enc.Encode(val.Decoded); err != nil {
t.Errorf("error while encoding value %v: %v", val.Decoded, err)
} else if b.Len() == 0 {
t.Errorf("invalid encoded value for %v, got empty buffer.", val.Decoded)
} else {
res := make([]byte, b.Len())
b.Read(res)
if !bytes.Equal(res, val.Encoded) {
t.Errorf("invalid encoded value for %v, got % #X, expected % #X", val.Decoded, res, val.Encoded)
}
}
}
}
func TestMarshal(t *testing.T) {
for _, val := range validTestValues {
if b, err := Marshal(val.Decoded); err != nil {
t.Errorf("error while encoding value %v: %v", val.Decoded, err)
} else if !bytes.Equal(b, val.Encoded) {
t.Errorf("invalid encoded value for %v, got % #X, expected % #X", val.Decoded, b, val.Encoded)
}
}
}
func TestMarshal_String(t *testing.T) {
s := maxInt4
longStr := string(make([]byte, s))
if b, err := Marshal(longStr); err != nil {
t.Errorf("error while encoding string of length %v: %v", s, err)
} else if len(b) != s+2 {
t.Errorf("error while encoding string of length %v: invalid buffer length got %v, expected %v", s, len(b), s+2)
} else if b[0] != mStringSize8 {
t.Errorf("error while encoding string of length %v: invalid marker, got %#X, expected %#X", s, b[0], mStringSize8)
}
s = math.MaxUint16
longStr = string(make([]byte, s))
if b, err := Marshal(longStr); err != nil {
t.Errorf("error while encoding string of length %v: %v", s, err)
} else if len(b) != s+3 {
t.Errorf("error while encoding string of length %v: invalid buffer length got %v, expected %v", s, len(b), s+3)
} else if b[0] != mStringSize16 {
t.Errorf("error while encoding string of length %v: invalid marker, got %#X, expected %#X", s, b[0], mStringSize16)
}
s = math.MaxUint16 + 1
longStr = string(make([]byte, s))
if b, err := Marshal(longStr); err != nil {
t.Errorf("error while encoding string of length %v: %v", s, err)
} else if len(b) != s+5 {
t.Errorf("error while encoding string of length %v: invalid buffer length got %v, expected %v", s, len(b), s+5)
} else if b[0] != mStringSize32 {
t.Errorf("error while encoding string of length %v: invalid marker, got %#X, expected %#X", s, b[0], mStringSize32)
}
}
func TestMarshal_List(t *testing.T) {
s := maxInt4
longList := make([]interface{}, s)
if b, err := Marshal(longList); err != nil {
t.Errorf("error while encoding list of length %v: %v", s, err)
} else if len(b) != s+2 {
t.Errorf("error while encoding list of length %v: invalid buffer length got %v, expected %v", s, len(b), s+2)
} else if b[0] != mListSize8 {
t.Errorf("error while encoding list of length %v: invalid marker, got %#X, expected %#X", s, b[0], mListSize8)
}
s = math.MaxUint16
longList = make([]interface{}, s)
if b, err := Marshal(longList); err != nil {
t.Errorf("error while encoding list of length %v: %v", s, err)
} else if len(b) != s+3 {
t.Errorf("error while encoding list of length %v: invalid buffer length got %v, expected %v", s, len(b), s+3)
} else if b[0] != mListSize16 {
t.Errorf("error while encoding list of length %v: invalid marker, got %#X, expected %#X", s, b[0], mListSize16)
}
s = math.MaxUint16 + 1
longList = make([]interface{}, s)
if b, err := Marshal(longList); err != nil {
t.Errorf("error while encoding list of length %v: %v", s, err)
} else if len(b) != s+5 {
t.Errorf("error while encoding list of length %v: invalid buffer length got %v, expected %v", s, len(b), s+5)
} else if b[0] != mListSize32 {
t.Errorf("error while encoding list of length %v: invalid marker, got %#X, expected %#X", s, b[0], mListSize16)
}
}
func TestMarshal_Map(t *testing.T) {
longMap := make(map[string]interface{})
s := maxInt4 - 1
for i := 0; i < s; i++ {
longMap[strconv.Itoa(i)] = i
}
if b, err := Marshal(longMap); err != nil {
t.Errorf("error while encoding map of length %v: %v", s, err)
} else if len(b) != 51 {
t.Errorf("error while encoding map of length %v: invalid buffer length got %v, expected %v", s, len(b), 51)
} else if b[0] != tinyMapSizes[s][0] {
t.Errorf("error while encoding map of length %v: invalid marker, got %#X, expected %#X", s, b[0], tinyMapSizes[s][0])
}
s = maxInt4
longMap[strconv.Itoa(s)] = s
if b, err := Marshal(longMap); err != nil {
t.Errorf("error while encoding map of length %v: %v", s, err)
} else if len(b) != 56 {
t.Errorf("error while encoding map of length %v: invalid buffer length got %v, expected %v", s, len(b), 56)
} else if b[0] != mMapSize8 {
t.Errorf("error while encoding map of length %v: invalid marker, got %#X, expected %#X", s, b[0], mMapSize8)
}
s = math.MaxUint16
for i := 0; i < s; i++ {
longMap[strconv.Itoa(i)] = i
}
if b, err := Marshal(longMap); err != nil {
t.Errorf("error while encoding map of length %v: %v", s, err)
} else if len(b) != 643986 {
t.Errorf("error while encoding map of length %v: invalid buffer length got %v, expected %v", s, len(b), 643986)
} else if b[0] != mMapSize16 {
t.Errorf("error while encoding map of length %v: invalid marker, got %#X, expected %#X", s, b[0], mMapSize16)
}
s = math.MaxUint16 + 1
longMap[strconv.Itoa(s)] = s
if b, err := Marshal(longMap); err != nil {
t.Errorf("error while encoding map of length %v: %v", s, err)
} else if len(b) != 643999 {
t.Errorf("error while encoding map of length %v: invalid buffer length got %v, expected %v", s, len(b), 643999)
} else if b[0] != mMapSize32 {
t.Errorf("error while encoding map of length %v: invalid marker, got %#X, expected %#X", s, b[0], mMapSize32)
}
}
func TestMarshal_Structure(t *testing.T) {
s := math.MaxUint16
longSt := Structure{Signature: 42, Fields: make([]interface{}, s)}
if b, err := Marshal(longSt); err != nil {
t.Errorf("error while encoding structure of length %v: %v", s, err)
} else if len(b) != s+4 {
t.Errorf("error while encoding structure of length %v: invalid buffer length got %v, expected %v", s, len(b), s+4)
} else if b[0] != mStructSize16 {
t.Errorf("error while encoding structure of length %v: invalid marker, got %#X, expected %#X", s, b[0], mStructSize16)
}
}
func TestMarshal_Bytes(t *testing.T) {
s := math.MaxUint16
longBytes := make([]byte, s)
if b, err := Marshal(longBytes); err != nil {
t.Errorf("error while encoding bytes of length %v: %v", s, err)
} else if len(b) != 65538 {
t.Errorf("error while encoding bytes of length %v: invalid buffer length got %v, expected %v", s, len(b), 65538)
} else if b[0] != mBytesSize16 {
t.Errorf("error while encoding bytes of length %v: invalid marker, got %#X, expected %#X", s, b[0], mBytesSize16)
}
longBytes = append(longBytes, 0)
s++
if b, err := Marshal(longBytes); err != nil {
t.Errorf("error while encoding bytes of length %v: %v", s, err)
} else if len(b) != 65541 {
t.Errorf("error while encoding bytes of length %v: invalid buffer length got %v, expected %v", s, len(b), 65541)
} else if b[0] != mBytesSize32 {
t.Errorf("error while encoding bytes of length %v: invalid marker, got %#X, expected %#X", s, b[0], mBytesSize32)
}
}
func TestMarshal_Float32(t *testing.T) {
res := []byte{mFloat64, 0x3F, 0xF1, 0x99, 0x99, 0xA0, 0x00, 0x00, 0x00}
if b, err := Marshal(float32(1.1)); err != nil {
t.Errorf("error while encoding float32: %v", err)
} else if !bytes.Equal(res, b) {
t.Errorf("error while encoding float32 got %v, expected %v", b, res)
}
}
func TestMarshal_Int64(t *testing.T) {
// Just check that these types are supported
res := []byte{mInt8, 0x80}
if b, err := Marshal(int(math.MinInt8)); err != nil {
t.Errorf("error while encoding int %v: %v", int(math.MinInt8), err)
} else if !bytes.Equal(res, b) {
t.Errorf("error while encoding int got %v, expected %v", b, res)
}
if b, err := Marshal(int8(math.MinInt8)); err != nil {
t.Errorf("error while encoding int8 %v: %v", int8(math.MinInt8), err)
} else if !bytes.Equal(res, b) {
t.Errorf("error while encoding int8 got %v, expected %v", b, res)
}
if b, err := Marshal(int16(math.MinInt8)); err != nil {
t.Errorf("error while encoding int16 %v: %v", int16(math.MinInt8), err)
} else if !bytes.Equal(res, b) {
t.Errorf("error while encoding int16 got %v, expected %v", b, res)
}
if b, err := Marshal(int32(math.MinInt8)); err != nil {
t.Errorf("error while encoding int32 %v: %v", int32(math.MinInt8), err)
} else if !bytes.Equal(res, b) {
t.Errorf("error while encoding int32 got %v, expected %v", b, res)
}
}
func TestMarshal_Marshaler(t *testing.T) {
vMarshaller := marshaller(42)
res := []byte{42}
if b, err := Marshal(&vMarshaller); err != nil {
t.Errorf("error while encoding marshaler %v: %v", vMarshaller, err)
} else if !bytes.Equal(res, b) {
t.Errorf("error while encoding marshaler got %v, expected %v", b, res)
}
}
func TestEncoder_Encode_Time(t *testing.T) {
var b bytes.Buffer
enc := NewEncoder(&b)
res := []byte{mInt64, 0x14, 0x25, 0x9C, 0x88, 0x0A, 0x59, 0xDE, 0x05}
tm := time.Date(2016, time.January, 02, 12, 42, 43, 5, time.FixedZone("UTC", 0))
if err := enc.Encode(&tm); err != nil {
t.Error(err)
} else if !bytes.Equal(res, b.Bytes()) {
t.Errorf("error while encoding time got %v, expected %v", b.Bytes(), res)
}
b.Reset()
res = []byte{0x0}
tm = time.Time{}
if err := enc.Encode(&tm); err != nil {
t.Error(err)
} else if !bytes.Equal(res, b.Bytes()) {
t.Errorf("error while encoding time got %v, expected %v", b.Bytes(), res)
}
b.Reset()
}