-
Notifications
You must be signed in to change notification settings - Fork 0
/
vararray_test.go
139 lines (107 loc) · 2.85 KB
/
vararray_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
package schemer
import (
"bytes"
"fmt"
"log"
"testing"
)
// make sure we can encode/decode binary schemas for EnumSchema
func TestDecodeVarLenArray1(t *testing.T) {
slice := []int{1, 2, 3, 4}
// setup an example schema
s, err := SchemaOf(slice)
if err != nil {
t.Error(err)
return
}
varArraySchema := s.(*VarArraySchema)
// encode i
b, err := varArraySchema.MarshalSchemer()
if err != nil {
t.Error(err)
return
}
// make sure we can successfully decode it
tmp, err := DecodeSchema(bytes.NewReader(b))
if err != nil {
t.Error("cannot encode binary encoded VarLenArraySchema")
return
}
decodedSchema := tmp.(*VarArraySchema)
// and then check the actual contents of the decoded schema
// to make sure it contains the correct values
if decodedSchema.Nullable() != varArraySchema.Nullable() {
t.Error("unexpected values when decoding binary EnumSchema")
return
}
}
func TestDecodeVarLenArray2(t *testing.T) {
// build up the schema programatically
varArraySchema := VarArraySchema{SchemaOptions: SchemaOptions{nullable: false}}
varArraySchema.Element = &(FloatSchema{Bits: 32})
var buf bytes.Buffer
var err error
floatslice := []float32{2.0, 3.1, 5.2, 7.3, 11.4, 13.5}
buf.Reset()
err = varArraySchema.Encode(&buf, floatslice)
if err != nil {
t.Error(err)
}
fmt.Println("fixed len float32 to fixed len float32")
r := bytes.NewReader(buf.Bytes())
var decodedFloats []float32 = make([]float32, 6)
err = varArraySchema.Decode(r, &decodedFloats)
if err != nil {
t.Error(err)
}
for i := 0; i < len(decodedFloats); i++ {
if floatslice[i] != decodedFloats[i] {
t.Error("unexpected value decoding floating point array")
}
}
}
func TestDecodeVarLenArray3(t *testing.T) {
// build up the schema programatically
/*
FixedArraySchema := VarArraySchema{IsNullable: false}
fixedLenArraySchema1 := VarArraySchema{IsNullable: false}
FloatSchema := FloatSchema{Bits: 32}
fixedLenArraySchema1.Element = FloatSchema
FixedArraySchema.Element = fixedLenArraySchema1
*/
var buf bytes.Buffer
var err error
floatSlice := [][]float32{
{0.5, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
}
FixedArraySchema, err := SchemaOf(floatSlice)
if err != nil {
t.Error(err)
}
buf.Reset()
err = FixedArraySchema.Encode(&buf, floatSlice)
if err != nil {
t.Error(err)
}
fmt.Println("fixed len float32 to fixed len float32")
r := bytes.NewReader(buf.Bytes())
log.Println("buf.Bytes()", buf.Len(), buf.Bytes())
decodedFloats := make([][]float32, 3)
for i := range decodedFloats {
decodedFloats[i] = make([]float32, 4)
}
err = FixedArraySchema.Decode(r, &decodedFloats)
if err != nil {
t.Error(err)
}
log.Println(decodedFloats)
for i := 0; i < 3; i++ {
for j := 0; j < 4; j++ {
if floatSlice[i][j] != decodedFloats[i][j] {
t.Error("unexpected value decoding 2-dimensional floating point array")
}
}
}
}