-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_test.go
373 lines (304 loc) · 8.32 KB
/
encoder_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package goetf_test
import (
"bytes"
"maps"
"math"
"math/big"
"reflect"
"slices"
"strings"
"testing"
"github.com/nicolito128/goetf"
)
func TestEncodeSmallInteger(t *testing.T) {
var data uint8 = 255
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 97, 255}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
func TestEncodeInteger(t *testing.T) {
{
var data uint16 = math.MaxUint16
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 98, 0, 0, 255, 255}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
var data int16 = math.MaxInt16
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 98, 0, 0, 127, 255}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
var data int32 = math.MaxInt32
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 98, 127, 255, 255, 255}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
}
func TestEncodeBig(t *testing.T) {
{
var data int64 = 314159265359
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 110, 8, 0, 79, 246, 89, 37, 73, 0, 0, 0}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
data := big.NewInt(16777216)
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 111, 0, 0, 0, 4, 0, 0, 0, 0, 1}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
}
func TestEncodeFloat(t *testing.T) {
{
var data float64 = 3.14159265359
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 70, 64, 9, 33, 251, 84, 68, 46, 234}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
var data float32 = 340.28234663852885
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 70, 64, 117, 68, 132, 128, 0, 0, 0}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
}
func TestEncodeString(t *testing.T) {
{
data := "hello, etf world"
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 107, 0, 16, 104, 101, 108, 108, 111, 44, 32, 101, 116, 102, 32,
119, 111, 114, 108, 100}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
data := []byte{'p', 'h', 'o', 'n', 'e', ' ', 'n', 'u', 'm', 'b', 'e', 'e', 'r'}
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 77, 0, 0, 0, 13, 8, 112, 104, 111, 110, 101, 32, 110, 117, 109, 98, 101, 101, 114}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
var data strings.Builder
data.Grow(256)
for range 256 {
_, err := data.WriteString("n")
if err != nil {
t.Fatal(err)
}
}
got, err := goetf.Marshal(data.String())
if err != nil {
t.Fatal(err)
}
want := []byte{131, 107, 1, 0, 110}
for range 255 {
want = append(want, 110)
}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
}
func TestEncodeBool(t *testing.T) {
{
data := true
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 119, 4, 116, 114, 117, 101}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
data := false
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 119, 5, 102, 97, 108, 115, 101}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
}
func TestEncodeNil(t *testing.T) {
var data *int
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 119, 3, 110, 105, 108}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
func TestEncodeTuples(t *testing.T) {
{
data := []int{1, 2, 3, 4, 5}
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 104, 5, 98, 0, 0, 0, 1, 98, 0, 0, 0, 2, 98, 0, 0, 0, 3, 98, 0, 0, 0, 4, 98, 0, 0, 0, 5}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
{
data := [][]int{{1, 2}, {3, 4}, {5, 6}}
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 104, 3, 104, 2, 98, 0, 0, 0, 1, 98, 0, 0, 0, 2, 104, 2, 98, 0, 0, 0, 3, 98, 0, 0, 0, 4, 104, 2, 98, 0, 0, 0, 5, 98, 0, 0, 0, 6}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
}
func TestEncodeList(t *testing.T) {
data := [3]string{"a", "b", "c"}
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
want := []byte{131, 108, 0, 0, 0, 3, 119, 1, 97, 119, 1, 98, 119, 1, 99, 106}
if !slices.Equal(want, got) {
t.Errorf("encode error: want = %v got = %v", want, got)
}
}
func TestEncodeMap(t *testing.T) {
want := map[string]int{"a": 97, "b": 98, "c": 99}
got, err := goetf.Marshal(want)
if err != nil {
t.Fatal("marshal error:", err)
}
out := map[string]int{}
if err := goetf.Unmarshal(got, out); err != nil {
t.Fatal("unmarshal error:", err)
}
if !maps.Equal(out, want) {
t.Errorf("encode error: want = %v got = %v", want, out)
}
}
func TestEncodeStruct(t *testing.T) {
type Profile struct {
Active bool `etf:"active"`
LastLogin int64 `etf:"last_login"`
}
type User struct {
Name string `etf:"name"`
Age uint8 `etf:"age"`
P Profile `etf:"profile"`
}
data := User{"John", 37, Profile{true, 24000}}
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
var out User
if err := goetf.Unmarshal(got, &out); err != nil {
t.Fatal("unmarshal error:", err)
}
if !reflect.ValueOf(out).Equal(reflect.ValueOf(data)) {
t.Errorf("encode error: want = %v got = %v", data, out)
}
}
func TestEncodeStructWithNil(t *testing.T) {
type Account struct {
Username string `etf:"name"`
Status int `etf:"status"`
Items *[]int `etf:"items"`
Thing *string `etf:"thing"`
}
data := Account{"username", 15, nil, nil}
got, err := goetf.Marshal(data)
if err != nil {
t.Fatal("marshal error:", err)
}
var out Account
if err := goetf.Unmarshal(got, &out); err != nil {
t.Fatal("unmarshal error:", err)
}
want := Account{Username: "username", Status: 15, Items: nil, Thing: nil}
if want.Username != out.Username || want.Status != out.Status || out.Items != nil || out.Thing != nil {
t.Errorf("encode error: want = %v got = %v", want, out)
}
}
func TestEncodeOptions(t *testing.T) {
data := map[string]int{"1": 1, "2": 2}
buf := bytes.NewBuffer(make([]byte, 0))
eng := goetf.NewEncoder(buf, goetf.WithStringOverAtom(true))
if err := eng.Encode(data); err != nil {
t.Fatal("marshal error:", err)
}
got, err := eng.ReadAll()
if err != nil {
t.Fatal(err)
}
w1 := []byte{131, 116, 0, 0, 0, 2, 107, 0, 1, 49, 98, 0, 0, 0, 1, 107, 0, 1, 50, 98, 0, 0, 0, 2}
w2 := []byte{131, 116, 0, 0, 0, 2, 107, 0, 1, 50, 98, 0, 0, 0, 2, 107, 0, 1, 49, 98, 0, 0, 0, 1}
if !slices.Equal(got, w1) && !slices.Equal(got, w2) {
t.Errorf("encode ReadAll error: got = %v want = %v or %v", got, w1, w2)
}
want := map[string]int{}
if err := goetf.Unmarshal(got, want); err != nil {
t.Fatal("unmarshal error:", err)
}
if !maps.Equal(data, want) {
t.Errorf("encode error: want = %v got = %v", want, data)
}
}