forked from elodina/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary_decoder_positioning_test.go
170 lines (161 loc) · 3.75 KB
/
binary_decoder_positioning_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
package avro
import (
"encoding/hex"
"fmt"
"math/rand"
"testing"
"time"
)
//this tests whether the decoder is able to sequentially read values and keep track of his position normally
var primitives = []string{typeBoolean, typeInt, typeLong, typeFloat, typeDouble, typeBytes, typeString}
func TestPositioning(t *testing.T) {
bytes, types, expected := getTestData()
bd := NewBinaryDecoder(bytes)
for i := 0; i < len(types); i++ {
currentType := types[i]
currentExpected := expected[i]
switch currentType {
case typeBoolean:
{
value, _ := bd.ReadBoolean()
if value != currentExpected.(bool) {
t.Fatalf("Unexpected boolean: expected %v, actual %v\n", currentExpected, value)
}
}
case typeInt:
{
value, _ := bd.ReadInt()
if value != currentExpected.(int32) {
t.Fatalf("Unexpected int: expected %v, actual %v\n", currentExpected, value)
}
}
case typeLong:
{
value, _ := bd.ReadLong()
if value != currentExpected.(int64) {
t.Fatalf("Unexpected long: expected %v, actual %v\n", currentExpected, value)
}
}
case typeFloat:
{
value, _ := bd.ReadFloat()
if value != currentExpected.(float32) {
t.Fatalf("Unexpected float: expected %v, actual %v\n", currentExpected, value)
}
}
case typeDouble:
{
value, _ := bd.ReadDouble()
if value != currentExpected.(float64) {
t.Fatalf("Unexpected double: expected %v, actual %v\n", currentExpected, value)
}
}
case typeBytes:
{
position := bd.Tell()
value, err := bd.ReadBytes()
if err != nil {
t.Fatal(err)
}
for i := 0; i < len(value); i++ {
if value[i] != bytes[position+int64(i)+int64(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{value[i]}))
}
}
}
case typeString:
{
value, _ := bd.ReadString()
if value != currentExpected.(string) {
t.Fatalf("Unexpected string: expected %v, actual %v\n", currentExpected, value)
}
}
}
}
}
func getTestData() ([]byte, []string, []interface{}) {
rand.Seed(time.Now().Unix())
testSize := rand.Intn(10000) + 1
fmt.Printf("Testing positioning on %d sequential values\n", testSize)
var bytes []byte
var types []string
var expected []interface{}
for i := 0; i < testSize; i++ {
currentType := primitives[rand.Intn(len(primitives))]
types = append(types, currentType)
k, v := getRandomFromMap(currentType)
bytes = append(bytes, k...)
expected = append(expected, v)
}
return bytes, types, expected
}
func getRandomFromMap(mapType string) ([]byte, interface{}) {
i := 0
switch mapType {
case typeBoolean:
{
random := rand.Intn(len(goodBooleans))
for value, bytes := range goodBooleans {
if i == random {
return bytes, value
}
i++
}
}
case typeInt:
{
random := rand.Intn(len(goodInts))
for value, bytes := range goodInts {
if i == random {
return bytes, value
}
i++
}
}
case typeLong:
{
random := rand.Intn(len(goodLongs))
for value, bytes := range goodLongs {
if i == random {
return bytes, value
}
i++
}
}
case typeFloat:
{
random := rand.Intn(len(goodFloats))
for value, bytes := range goodFloats {
if i == random {
return bytes, value
}
i++
}
}
case typeDouble:
{
random := rand.Intn(len(goodDoubles))
for value, bytes := range goodDoubles {
if i == random {
return bytes, value
}
i++
}
}
case typeBytes:
{
return goodBytes[rand.Intn(len(goodBytes))], "1"
}
case typeString:
{
random := rand.Intn(len(goodStrings))
for value, bytes := range goodStrings {
if i == random {
return bytes, value
}
i++
}
}
}
panic("cant get random from map")
}