forked from Guazi-inc/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_decoder_standalone_test.go
92 lines (82 loc) · 2.59 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
package avro
import (
"encoding/hex"
"testing"
)
func TestBool(t *testing.T) {
for value, bytes := range goodBooleans {
if actual, _ := NewBinaryDecoder(bytes).ReadBoolean(); actual != value {
t.Fatalf("Unexpected boolean: 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)
}
}
}
func TestInt(t *testing.T) {
for value, bytes := range goodInts {
if actual, _ := NewBinaryDecoder(bytes).ReadInt(); actual != value {
t.Fatalf("Unexpected int: expected %v, actual %v\n", value, actual)
}
}
}
func TestLong(t *testing.T) {
for value, bytes := range goodLongs {
if actual, _ := NewBinaryDecoder(bytes).ReadLong(); actual != value {
t.Fatalf("Unexpected long: expected %v, actual %v\n", value, actual)
}
}
}
func TestFloat(t *testing.T) {
for value, bytes := range goodFloats {
if actual, _ := NewBinaryDecoder(bytes).ReadFloat(); actual != value {
t.Fatalf("Unexpected float: expected %v, actual %v\n", value, actual)
}
}
}
func TestDouble(t *testing.T) {
for value, bytes := range goodDoubles {
if actual, _ := NewBinaryDecoder(bytes).ReadDouble(); actual != value {
t.Fatalf("Unexpected double: expected %v, actual %v\n", value, actual)
}
}
}
func TestBytes(t *testing.T) {
for index := 0; index < len(goodBytes); index++ {
bytes := goodBytes[index]
actual, err := NewBinaryDecoder(bytes).ReadBytes()
if err != nil {
t.Fatal(err)
}
for i := 0; i < len(actual); i++ {
if actual[i] != bytes[i+1] {
t.Fatalf("Unexpected byte at index %d: expected 0x%v, actual 0x%v\n", i, hex.EncodeToString([]byte{bytes[i+1]}), hex.EncodeToString([]byte{actual[i]}))
}
}
}
for index := 0; index < len(badBytes); index++ {
pair := badBytes[index]
expected := pair[0].(error)
arr := pair[1].([]byte)
if _, err := NewBinaryDecoder(arr).ReadBytes(); err != expected {
t.Fatalf("Unexpected error for bytes: expected %v, actual %v", expected, err)
}
}
}
func TestString(t *testing.T) {
for value, bytes := range goodStrings {
if actual, _ := NewBinaryDecoder(bytes).ReadString(); actual != value {
t.Fatalf("Unexpected string: expected %v, actual %v\n", value, actual)
}
}
for index := 0; index < len(badStrings); index++ {
pair := badStrings[index]
expected := pair[0].(error)
arr := pair[1].([]byte)
if _, err := NewBinaryDecoder(arr).ReadString(); err != expected {
t.Fatalf("Unexpected error for string: expected %v, actual %v", expected, err)
}
}
}