-
Notifications
You must be signed in to change notification settings - Fork 22
/
binary_decoder_standalone_test.go
157 lines (141 loc) · 4.99 KB
/
binary_decoder_standalone_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
package avro
import (
"bytes"
"encoding/hex"
"testing"
)
func TestBool(t *testing.T) {
for value, buf := range goodBooleans {
if actual, _ := NewBinaryDecoder(buf).ReadBoolean(); actual != value {
t.Fatalf("Unexpected boolean: expected %v, actual %v\n", value, actual)
}
if actual, err := NewBinaryDecoderReader(bytes.NewReader(buf)).ReadBoolean(); err != nil {
t.Fatalf("Unexpected boolean io.Reader ERROR: expected nil, actual %v\n", err)
} else if actual != value {
t.Fatalf("Unexpected boolean io.Reader: expected %v, actual %v\n", value, actual)
}
}
for expected, invalid := range badBooleans {
if _, err := NewBinaryDecoder(invalid).ReadBoolean(); err != expected {
t.Fatalf("Unexpected error for boolean: expected %v, actual %v", expected, err)
}
if _, err := NewBinaryDecoderReader(bytes.NewReader(invalid)).ReadBoolean(); err != expected {
t.Fatalf("Unexpected error for boolean io.Reader: expected %v, actual %v", expected, err)
}
}
}
func TestInt(t *testing.T) {
for value, buf := range goodInts {
if actual, _ := NewBinaryDecoder(buf).ReadInt(); actual != value {
t.Fatalf("Unexpected int: expected %v, actual %v\n", value, actual)
}
actual, err := NewBinaryDecoderReader(bytes.NewReader(buf)).ReadInt()
if err != nil {
t.Fatalf("Unexpected int ERROR: expected nil, actual %v\n", err)
}
if actual != value {
t.Fatalf("Unexpected int: expected %v, actual %v\n", value, actual)
}
}
for _, input := range badInts {
if _, err := NewBinaryDecoder(input.buf).ReadInt(); err != input.err {
t.Fatalf("Bytes Decoder: Expected err %v, actual %v", input.err, err)
}
if _, err := NewBinaryDecoderReader(input.Reader()).ReadInt(); err != input.err {
t.Fatalf("io.Reader decoder: Expected err %v, actual %v", input.err, err)
}
}
}
func TestLong(t *testing.T) {
for value, buf := range goodLongs {
if actual, _ := NewBinaryDecoder(buf).ReadLong(); actual != value {
t.Fatalf("Unexpected long: expected %v, actual %v\n", value, actual)
}
actual, err := NewBinaryDecoderReader(bytes.NewReader(buf)).ReadLong()
if err != nil {
t.Fatalf("Unexpected long io.Reader ERROR: expected nil, actual %v\n", err)
}
if actual != value {
t.Fatalf("Unexpected long io.Reader: expected %v, actual %v\n", value, actual)
}
}
}
func TestFloat(t *testing.T) {
for value, bytes := range goodFloats {
for prefix, decoder := range bothDecoders(bytes) {
if actual, err := decoder.ReadFloat(); err != nil {
t.Fatalf("Unexpected float %s ERR: %v", prefix, err)
} else if actual != value {
t.Fatalf("Unexpected float %s: expected %v, actual %v\n", prefix, value, actual)
}
}
}
for _, input := range badFloats {
for prefix, decoder := range bothDecoders(input.buf) {
if _, err := decoder.ReadFloat(); err != input.err {
t.Fatalf("Unexpected float %s ERR: expected %v; actual %v", prefix, input.err, err)
}
}
}
}
func TestDouble(t *testing.T) {
for value, bytes := range goodDoubles {
for prefix, decoder := range bothDecoders(bytes) {
if actual, err := decoder.ReadDouble(); err != nil {
t.Fatalf("Unexpected double %s ERR: %v", prefix, err)
} else if actual != value {
t.Fatalf("Unexpected double %s: expected %v, actual %v\n", prefix, value, actual)
}
}
}
}
func TestBytes(t *testing.T) {
for _, buf := range goodBytes {
for prefix, decoder := range bothDecoders(buf) {
actual, err := decoder.ReadBytes()
if err != nil {
t.Fatalf("Unexpected err %s: %v", prefix, err)
}
for i := 0; i < len(actual); i++ {
if actual[i] != buf[i+1] {
t.Fatalf("Unexpected byte (%s) at index %d: expected 0x%v, actual 0x%v\n", prefix, i, hex.EncodeToString([]byte{buf[i+1]}), hex.EncodeToString([]byte{actual[i]}))
}
}
}
}
for _, pair := range badBytes {
expected := pair.err
arr := pair.buf
for prefix, decoder := range bothDecoders(arr) {
if _, err := decoder.ReadBytes(); err != expected {
t.Fatalf("Unexpected error for bytes %s: expected %v, actual %v", prefix, expected, err)
}
}
}
}
func TestString(t *testing.T) {
for value, buf := range goodStrings {
if actual, _ := NewBinaryDecoder(buf).ReadString(); actual != value {
t.Fatalf("Unexpected string bytes: expected %v, actual %v\n", value, actual)
}
if actual, _ := NewBinaryDecoderReader(bytes.NewReader(buf)).ReadString(); actual != value {
t.Fatalf("Unexpected string io.Reader: expected %v, actual %v\n", value, actual)
}
}
for _, pair := range badStrings {
expected := pair.err
arr := pair.buf
if _, err := NewBinaryDecoder(arr).ReadString(); err != expected {
t.Fatalf("Unexpected error for string []byte: expected %v, actual %v", expected, err)
}
if _, err := NewBinaryDecoderReader(pair.Reader()).ReadString(); err != expected {
t.Fatalf("Unexpected error for string io.Reader: expected %v, actual %v", expected, err)
}
}
}
func bothDecoders(input []byte) map[string]Decoder {
return map[string]Decoder{
"[]byte": NewBinaryDecoder(input),
"io.Reader": NewBinaryDecoderReader(bytes.NewReader(input)),
}
}