-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
145 lines (138 loc) · 4.22 KB
/
reader_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
package pixi
import (
"bytes"
"reflect"
"testing"
)
func FuzzWriteReadMetadata(f *testing.F) {
f.Add("", "")
f.Add("a", "b")
f.Add("abcdefghijklnm", "opqrstuvwxyz")
f.Fuzz(func(t *testing.T, key string, val string) {
buf := new(bytes.Buffer)
err := WriteMetadata(buf, key, val)
if err != nil {
t.Fatal(err)
}
outKey, outVal, err := ReadMetadata(buf)
if err != nil {
t.Fatal(err)
}
if key != outKey || val != outVal {
t.Errorf("expected key %s, got %s, expected val %s, got %s", key, outKey, val, outVal)
}
})
}
func TestWriteReadDataSet(t *testing.T) {
testCases := []struct {
name string
data Summary
err error
}{
{
name: "contig",
data: Summary{
Metadata: map[string]string{},
Separated: false,
Compression: CompressionNone,
Dimensions: []Dimension{{Size: 4, TileSize: 4}, {Size: 4, TileSize: 2}, {Size: 3, TileSize: 3}},
Fields: []Field{{Name: "a", Type: FieldInt32}, {Name: "b", Type: FieldInt64}, {Name: "hello", Type: FieldInt16}},
TileBytes: []int64{100, 200},
TileOffsets: []int64{80, 160},
},
err: nil,
},
{
name: "with metadata",
data: Summary{
Metadata: map[string]string{"one": "two", "three": "four"},
Separated: false,
Compression: CompressionNone,
Dimensions: []Dimension{{Size: 4, TileSize: 4}, {Size: 4, TileSize: 2}, {Size: 3, TileSize: 3}},
Fields: []Field{{Name: "a", Type: FieldInt32}, {Name: "b", Type: FieldInt64}, {Name: "hello", Type: FieldInt16}},
TileBytes: []int64{100, 200},
TileOffsets: []int64{70, 30},
},
err: nil,
},
{
name: "no names",
data: Summary{
Metadata: map[string]string{},
Separated: false,
Compression: CompressionNone,
Dimensions: []Dimension{{Size: 4, TileSize: 4}, {Size: 4, TileSize: 2}, {Size: 3, TileSize: 3}},
Fields: []Field{{Type: FieldInt32}, {Type: FieldInt64}, {Type: FieldInt16}},
TileBytes: []int64{100, 200},
TileOffsets: []int64{100, 200},
},
err: nil,
},
{
name: "sep",
data: Summary{
Metadata: map[string]string{},
Separated: true,
Compression: CompressionFlate,
Dimensions: []Dimension{{Size: 4, TileSize: 2}, {Size: 4, TileSize: 2}},
Fields: []Field{{Name: "a", Type: FieldFloat64}, {Name: "hello", Type: FieldInt16}},
TileBytes: []int64{100, 200, 300, 400, 500, 600, 700, 800},
TileOffsets: []int64{100, 200, 300, 400, 500, 600, 700, 800},
},
err: nil,
},
{
name: "tile bytes err",
data: Summary{
Metadata: map[string]string{},
Separated: true,
Compression: CompressionFlate,
Dimensions: []Dimension{{Size: 4, TileSize: 2}, {Size: 4, TileSize: 2}},
Fields: []Field{{Name: "a", Type: FieldFloat64}, {Name: "hello", Type: FieldInt16}},
TileBytes: []int64{100, 200, 300, 400, 500, 600, 700},
TileOffsets: []int64{100, 200, 300, 400, 500, 600, 700, 800},
},
err: FormatError("TileBytes must have same number of tiles as data set for valid pixi files"),
},
{
name: "tile offsets err",
data: Summary{
Metadata: map[string]string{},
Separated: true,
Compression: CompressionFlate,
Dimensions: []Dimension{{Size: 4, TileSize: 2}, {Size: 4, TileSize: 2}},
Fields: []Field{{Name: "a", Type: FieldFloat64}, {Name: "hello", Type: FieldInt16}},
TileBytes: []int64{100, 200, 300, 400, 500, 600, 700, 800},
TileOffsets: []int64{100, 200, 300, 400, 500, 600, 700},
},
err: FormatError("TileOffsets must have same number of tiles as data set for valid pixi files"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf := NewBuffer(10)
err := WriteSummary(buf, tc.data)
if tc.err != nil {
if err == nil {
t.Fatalf("expected error %v but got none", tc.err)
}
return
}
if err != nil {
t.Fatal(err)
}
amtWritten := len(buf.Bytes())
if amtWritten != int(tc.data.DiskDataStart()) {
t.Errorf("expected data start to be %d, got %d", amtWritten, tc.data.DiskDataStart())
}
readBuf := NewBufferFrom(buf.Bytes())
ds, err := ReadSummary(readBuf)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tc.data, ds) {
t.Errorf("expected read dataset to be %v, got %v", tc.data, ds)
}
})
}
}