forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_header_internal_test.go
311 lines (299 loc) · 8.77 KB
/
binary_header_internal_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package gpkg
import (
"encoding/binary"
"errors"
"reflect"
"strconv"
"testing"
)
func fmt8Bit(n byte) string {
val := []byte("0b00000000")
s := strconv.FormatUint(uint64(n), 2)
offset := len(val) - len(s)
copy(val[offset:], []byte(s))
return string(val)
}
func TestHeaderFlag(t *testing.T) {
tcheader := func(hf headerFlags) string { return hf.String() + " " + fmt8Bit(byte(hf)) }
type tcase struct {
header headerFlags
IsEmpty bool
IsStandard bool
Envelope envelopeType
Endian binary.ByteOrder
}
fn := func(tc tcase) (string, func(*testing.T)) {
return tcheader(tc.header), func(t *testing.T) {
t.Parallel()
if tc.header.IsEmpty() != tc.IsEmpty {
t.Errorf("is empty, expected %v got %v", tc.IsEmpty, tc.header.IsEmpty())
}
if tc.header.IsStandard() != tc.IsStandard {
t.Errorf("is standard, expected %v got %v", tc.IsStandard, tc.header.IsStandard())
}
if tc.header.Endian() != tc.Endian {
t.Errorf("byte order, expected %v got %v", tc.Endian, tc.header.Endian())
}
if tc.header.Envelope() != tc.Envelope {
t.Errorf("envelope type, expected %v got %v", tc.Envelope, tc.header.Envelope())
}
// The following two tests are just for coverage. :P The bottom two functions are of course
// will always pass if the above one passes. The one that really needs to be tested for is
// the NumberOfElements, but that is tested with binaryheader and is really just a table lookup.
if tc.header.Envelope().String() != tc.Envelope.String() {
t.Errorf("envelope type name, expected %v got %v", tc.Envelope, tc.header.Envelope())
}
if tc.header.Envelope().NumberOfElements() != tc.Envelope.NumberOfElements() {
t.Errorf("envelope type name, expected %v got %v", tc.Envelope.NumberOfElements(), tc.header.Envelope().NumberOfElements())
}
}
}
tests := [...]tcase{
{
header: 0x00, // 00 0 0 000 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeNone,
Endian: binary.BigEndian,
},
{
header: 0x01, // 00 0 0 000 1
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeNone,
Endian: binary.LittleEndian,
},
{
header: 0x02, // 00 0 0 001 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeXY,
Endian: binary.BigEndian,
},
{
header: 0x03, // 00 0 0 001 1
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeXY,
Endian: binary.LittleEndian,
},
{
header: 0x04, // 00 0 0 010 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeXYZ,
Endian: binary.BigEndian,
},
// 0x05 is the same as 0x04 but LittleEndian
{
header: 0x06, // 00 0 0 011 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeXYM,
Endian: binary.BigEndian,
},
// 0x07 is the same as 0x06 but Little Endian
{
header: 0x08, // 00 0 0 100 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeXYZM,
Endian: binary.BigEndian,
},
// 0x09 is the same as 0x08 but little Endian
{
header: 0x0A, // 00 0 0 101 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeInvalid,
Endian: binary.BigEndian,
},
{
header: 0x0B, // 00 0 0 101 1
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeInvalid,
Endian: binary.LittleEndian,
},
{
header: 0x0C, // 00 0 0 110 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeInvalid,
Endian: binary.BigEndian,
},
// 0x0D is the same as 0x0C but little Endian
{
header: 0x0E, // 00 0 0 111 0
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeInvalid,
Endian: binary.BigEndian,
},
{
header: 0x0F, // 00 0 0 111 1
IsStandard: true,
IsEmpty: false,
Envelope: EnvelopeTypeInvalid,
Endian: binary.LittleEndian,
},
{
header: 0x10, // 00 0 1 000 0
IsStandard: true,
IsEmpty: true,
Envelope: EnvelopeTypeNone,
Endian: binary.BigEndian,
},
// 0x11-0x1F are the various iterations of 0x02-0x0F but with IsEmpty bit set to true
{
header: 0x20, // 00 1 0 000 0
IsStandard: false,
IsEmpty: false,
Envelope: EnvelopeTypeNone,
Endian: binary.BigEndian,
},
// 0x21-0x2F are the various iterations of 0x02-0x0F but with IsExtention bit set to true
// No need to test the high bits 0x30-0xFF as the are reserved.
}
for _, tc := range tests {
t.Run(fn(tc))
}
}
func TestBinaryHeader(t *testing.T) {
type tcase struct {
bytes []byte
version uint8
flags headerFlags // This will not be tested as it's already being tested elsewhere.
srsid int32
envelopetype envelopeType
envelope []float64
size int
empty bool
standard bool
err error
}
fn := func(tc tcase) func(*testing.T) {
return func(t *testing.T) {
var bh *BinaryHeader
var err error
if tc.bytes != nil {
bh, err = NewBinaryHeader(tc.bytes)
}
if tc.err != nil {
if err == nil {
t.Errorf("error, expected %v got nil", tc.err)
return
}
if tc.err.Error() != err.Error() {
t.Errorf("error, expected %v got %v", tc.err.Error(), err.Error())
}
return
}
if err != nil {
t.Errorf("error, expected nil got %v", err.Error())
return
}
if bh.Version() != tc.version {
t.Errorf("version, expected %v got %v", tc.version, bh.Version())
}
if bh.SRSId() != tc.srsid {
t.Errorf("SRS Id, expected %v got %v", tc.srsid, bh.SRSId())
}
if bh.EnvelopeType() != tc.envelopetype {
t.Errorf("envelope type, expected %v got %v", tc.envelopetype, bh.EnvelopeType())
}
if !reflect.DeepEqual(bh.Envelope(), tc.envelope) {
t.Errorf("envelope, expected %v got %v", tc.envelope, bh.Envelope())
}
if !reflect.DeepEqual(bh.Magic(), Magic) {
t.Errorf("magic, expected %v got %v", Magic, bh.Magic())
}
if bh.IsGeometryEmpty() != tc.empty {
t.Errorf("empty geometry, expected %v got %v", tc.empty, bh.IsGeometryEmpty())
}
if bh.IsStandardGeometry() != tc.standard {
t.Errorf("standard geometry, expected %v got %v", tc.standard, bh.IsStandardGeometry())
}
if bh.Size() != tc.size {
t.Errorf("header size, expected %v got %v", tc.size, bh.Size())
}
}
}
tests := map[string]tcase{
"nil": {
empty: true,
standard: true,
envelopetype: EnvelopeTypeInvalid,
},
"zero bytes": {
bytes: []byte{},
err: errors.New("not enough bytes to decode header"),
},
"bad magic": {
bytes: []byte{
0x50, 0x47, // Magic number
0x00, // Version
0x03, // Flags -- LittleEndian, XY
0xE6, 0x10, 0x00, 0x00, // srs_id
0xE5, 0x6D, 0xFA, 0xB6, 0x67, 0xB6, 0x37, 0x40, // MinX
0xC1, 0xAB, 0xB0, 0xD0, 0xB9, 0xCB, 0x37, 0x40, // MaxX
0x2C, 0xC9, 0xBC, 0xE5, 0xD6, 0xF2, 0x42, 0x40, // MinY
0x20, 0xC2, 0x2E, 0x86, 0xB8, 0xF8, 0x42, 0x40, // MaxY
},
err: errors.New("invalid magic number"),
},
"invalid envelope XY given for XYZM": {
bytes: []byte{
0x47, 0x50, // Magic number
0x00, // Version
0x09, // Flags -- LittleEndian, XY
0xE6, 0x10, 0x00, 0x00, // srs_id
0xE5, 0x6D, 0xFA, 0xB6, 0x67, 0xB6, 0x37, 0x40, // MinX
0xC1, 0xAB, 0xB0, 0xD0, 0xB9, 0xCB, 0x37, 0x40, // MaxX
0x2C, 0xC9, 0xBC, 0xE5, 0xD6, 0xF2, 0x42, 0x40, // MinY
0x20, 0xC2, 0x2E, 0x86, 0xB8, 0xF8, 0x42, 0x40, // MaxY
},
err: errors.New("not enough bytes to decode header"),
},
"invalid envelope type": {
bytes: []byte{
0x47, 0x50, // Magic number
0x00, // Version
0x0B, // Flags -- LittleEndian, XY
0xE6, 0x10, 0x00, 0x00, // srs_id
0xE5, 0x6D, 0xFA, 0xB6, 0x67, 0xB6, 0x37, 0x40, // MinX
0xC1, 0xAB, 0xB0, 0xD0, 0xB9, 0xCB, 0x37, 0x40, // MaxX
0x2C, 0xC9, 0xBC, 0xE5, 0xD6, 0xF2, 0x42, 0x40, // MinY
0x20, 0xC2, 0x2E, 0x86, 0xB8, 0xF8, 0x42, 0x40, // MaxY
},
err: errors.New("invalid envelope type"),
},
"4326 XY": {
bytes: []byte{
0x47, 0x50, // Magic number
0x00, // Version
0x03, // Flags -- LittleEndian, XY
0xE6, 0x10, 0x00, 0x00, // srs_id
0xE5, 0x6D, 0xFA, 0xB6, 0x67, 0xB6, 0x37, 0x40, // MinX
0xC1, 0xAB, 0xB0, 0xD0, 0xB9, 0xCB, 0x37, 0x40, // MaxX
0x2C, 0xC9, 0xBC, 0xE5, 0xD6, 0xF2, 0x42, 0x40, // MinY
0x20, 0xC2, 0x2E, 0x86, 0xB8, 0xF8, 0x42, 0x40, // MaxY
},
version: 0,
flags: headerFlags(0x03),
srsid: 4326,
envelopetype: EnvelopeTypeXY,
envelope: []float64{
23.712520061626396, 23.79580406487708,
37.89718314855631, 37.94313123019333,
},
size: 40,
empty: false,
standard: true,
err: nil,
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}