-
Notifications
You must be signed in to change notification settings - Fork 22
/
binary_serialization_test.go
172 lines (153 loc) · 4.15 KB
/
binary_serialization_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
package avro
import (
"bytes"
"math/rand"
"testing"
)
//this makes sure the given value remains the same after encoding and decoding
const testTimes = 1000
func TestNullSerialization(t *testing.T) {
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteNull(nil)
if decoded, err := NewBinaryDecoder(buf.Bytes()).ReadNull(); err != nil {
t.Fatalf("Error decoding null: %v", err)
} else {
if decoded != nil {
t.Fatalf("Unexpected value: expected %v, actual %v\n", nil, decoded)
}
}
}
func TestBooleanSerialization(t *testing.T) {
values := []bool{true, false}
for i := range values {
value := values[i]
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteBoolean(value)
if decoded, err := NewBinaryDecoder(buf.Bytes()).ReadBoolean(); err != nil {
t.Fatalf("Error decoding boolean: %v", err)
} else {
if decoded != value {
t.Fatalf("Unexpected value: expected %v, actual %v\n", value, decoded)
}
}
}
}
func BenchmarkBooleanSerialization(b *testing.B) {
buf := &bytes.Buffer{}
enc := NewBinaryEncoder(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
enc.WriteBoolean(true)
buf.Reset()
}
}
func TestIntSerialization(t *testing.T) {
testPrimitiveSerialization(t, func(i int) interface{} {
r := rand.Int31() / (int32(i) * int32(i))
if i%2 == 0 {
r = -r
}
return r
}, func(r interface{}) (interface{}, error) {
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteInt(r.(int32))
return NewBinaryDecoder(buf.Bytes()).ReadInt()
})
}
func TestLongSerialization(t *testing.T) {
testPrimitiveSerialization(t, func(i int) interface{} {
r := rand.Int63() / (int64(i) * int64(i))
if i%2 == 0 {
r = -r
}
return r
}, func(r interface{}) (interface{}, error) {
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteLong(r.(int64))
return NewBinaryDecoder(buf.Bytes()).ReadLong()
})
}
func TestFloatSerialization(t *testing.T) {
testPrimitiveSerialization(t, func(i int) interface{} {
r := rand.Float32() * float32(i)
if i%2 == 0 {
r = -r
}
return r
}, func(r interface{}) (interface{}, error) {
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteFloat(r.(float32))
return NewBinaryDecoder(buf.Bytes()).ReadFloat()
})
}
func TestDoubleSerialization(t *testing.T) {
testPrimitiveSerialization(t, func(i int) interface{} {
r := rand.Float64() * float64(i*10)
if i%2 == 0 {
r = -r
}
return r
}, func(r interface{}) (interface{}, error) {
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteDouble(r.(float64))
return NewBinaryDecoder(buf.Bytes()).ReadDouble()
})
}
func TestBytesSerialization(t *testing.T) {
for i := 1; i <= testTimes/10; i++ {
r := randomBytes(i) //randByteArray(i)
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteBytes(r)
if decoded, err := NewBinaryDecoder(buf.Bytes()).ReadBytes(); err != nil {
t.Fatalf("Error decoding: %v", err)
} else {
if !bytes.Equal(decoded, r) {
t.Fatalf("Unexpected value: expected %v, actual %v\n", r, decoded)
}
}
}
}
func TestStringSerialization(t *testing.T) {
testPrimitiveSerialization(t, func(i int) interface{} {
return randomString(i) //randString(i, letters)
}, func(r interface{}) (interface{}, error) {
buf := &bytes.Buffer{}
NewBinaryEncoder(buf).WriteString(r.(string))
return NewBinaryDecoder(buf.Bytes()).ReadString()
})
}
func BenchmarkStringSerialization_small(b *testing.B) {
benchStringSerialization(b, 10)
}
func BenchmarkStringSerialization_med(b *testing.B) {
benchStringSerialization(b, 70)
}
func BenchmarkStringSerialization_large(b *testing.B) {
benchStringSerialization(b, 2000)
}
func benchStringSerialization(b *testing.B, n int) {
s := "abcdefghijklmnop"
for len(s) < n {
s += s
}
s = s[:n]
buf := &bytes.Buffer{}
enc := NewBinaryEncoder(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
enc.WriteString(s)
buf.Reset()
}
}
func testPrimitiveSerialization(t *testing.T, random func(int) interface{}, serialize func(interface{}) (interface{}, error)) {
for i := 1; i <= testTimes; i++ {
r := random(i)
if decoded, err := serialize(r); err != nil {
t.Fatalf("Error decoding: %v", err)
} else {
if decoded != r {
t.Fatalf("Unexpected value: expected %v, actual %v\n", r, decoded)
}
}
}
}