-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_encoding_reader_test.go
More file actions
executable file
·74 lines (69 loc) · 1.52 KB
/
binary_encoding_reader_test.go
File metadata and controls
executable file
·74 lines (69 loc) · 1.52 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
package wax_test
import (
"bytes"
"reflect"
"testing"
"github.com/bearmini/wax"
)
func TestBinaryEncodingReaderVarintN(t *testing.T) {
testData := []struct {
Name string
Bytes []byte
Consumed []byte
Expected int64
}{
{
Name: "pattern 1",
Bytes: []byte{0x01},
Consumed: []byte{0x01},
Expected: 1,
},
{
Name: "pattern 2",
Bytes: []byte{0x7F},
Consumed: []byte{0x7F},
Expected: -1,
},
{
Name: "pattern 3",
Bytes: []byte{0xFE, 0x7F},
Consumed: []byte{0xFE, 0x7F},
Expected: -2,
},
{
Name: "pattern 4",
Bytes: []byte{0xFE, 0xFF, 0x7F},
Consumed: []byte{0xFE, 0xFF, 0x7F},
Expected: -2,
},
{
Name: "pattern 5",
Bytes: []byte{0x9B, 0xF1, 0x59},
Consumed: []byte{0x9B, 0xF1, 0x59},
Expected: -624485,
},
{
Name: "pattern 6",
Bytes: []byte{0x80, 0x88, 0x80, 0x80, 0x00},
Consumed: []byte{0x80, 0x88, 0x80, 0x80, 0x00},
Expected: 0x400,
},
}
for _, data := range testData {
data := data // capture
t.Run(data.Name, func(t *testing.T) {
//t.Parallel()
ber := wax.NewBinaryEncodingReader(bytes.NewReader(data.Bytes))
v, consumed, err := ber.ReadVarint()
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
if v != data.Expected {
t.Fatalf("\nExpected: %#16x (%d)\nActual: %#16x (%d)", data.Expected, data.Expected, v, v)
}
if !reflect.DeepEqual(data.Consumed, consumed) {
t.Fatalf("\nExpected: %+v\nActual: %+v", data.Consumed, consumed)
}
})
}
}