forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_test.go
430 lines (338 loc) · 9.15 KB
/
scanner_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package osmpbf
import (
"context"
"os"
"reflect"
"testing"
"time"
"github.com/paulmach/osm"
)
var (
Delaware = "../testdata/delaware-latest.osm.pbf"
)
func TestScanner(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 1)
defer scanner.Close()
if v := scanner.Scan(); !v {
t.Fatalf("should read first scan: %v", scanner.Err())
}
if n := scanner.Object().(*osm.Node); n.ID != 75385503 {
t.Fatalf("did not scan correctly, got %v", n)
}
if v := scanner.Scan(); !v {
t.Fatalf("should read second scan: %v", scanner.Err())
}
if n := scanner.Object().(*osm.Node); n.ID != 75390099 {
t.Fatalf("did not scan correctly, got %v", n)
}
}
func TestScanner_intermediateStart(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 1)
target := osm.NodeID(178592359) // first object in last partially scanned block
indexOfTarget := 0
for i := 0; i < 30000; i++ {
scanner.Scan()
if scanner.Object().(*osm.Node).ID == target {
indexOfTarget = i
}
}
// verifies the target is less than 1 block length from the end.
if 30000-indexOfTarget > 8000 {
t.Errorf("target is not near the end, index %v", indexOfTarget)
}
scanner.Close()
// move the file pointer past all the fully scanned bytes,
// to the start of the not-fully scanned block.
f.Seek(scanner.FullyScannedBytes(), 0)
scanner = New(context.Background(), f, 1)
scanner.Scan()
if id := scanner.Object().(*osm.Node).ID; id != target {
t.Errorf("incorrect first id, got %v", id)
}
scanner.Close()
}
func TestScanner_context(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(ctx, f, 1)
defer scanner.Close()
if v := scanner.Scan(); !v {
t.Fatalf("should read first scan: %v", scanner.Err())
}
if n := scanner.Object().(*osm.Node); n.ID != 75385503 {
t.Fatalf("did not scan correctly, got %v", n)
}
cancel()
if v := scanner.Scan(); v {
t.Fatalf("should be closed for second scan: %v", scanner.Err())
}
if v := scanner.Err(); v != ctx.Err() {
t.Errorf("incorrect error, got %v", v)
}
}
func TestScanner_Header(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 1)
header, err := scanner.Header()
if err != nil {
t.Fatalf("error reading header: %v", err)
}
expected := &osm.Bounds{
MinLat: 38.450430000000004,
MaxLat: 40.03221,
MinLon: -75.78974000000001,
MaxLon: -74.96121000000001,
}
if !reflect.DeepEqual(header.Bounds, expected) {
t.Errorf("incorrect bounds: %v", header.Bounds)
}
if !reflect.DeepEqual(header.RequiredFeatures, []string{"OsmSchema-V0.6", "DenseNodes"}) {
t.Errorf("incorrect required features: %v", header.RequiredFeatures)
}
if !reflect.DeepEqual(header.WritingProgram, "Osmium (http://wiki.openstreetmap.org/wiki/Osmium)") {
t.Errorf("incorrect writing program: %v", header.WritingProgram)
}
if !reflect.DeepEqual(header.ReplicationTimestamp, time.Date(2016, 8, 10, 19, 28, 3, 0, time.UTC)) {
t.Errorf("incorrect timestamp: %v", header.ReplicationTimestamp)
}
}
func TestScanner_Close(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 1)
if v := scanner.Scan(); !v {
t.Fatalf("should read first scan: %v", scanner.Err())
}
if n := scanner.Object().(*osm.Node); n.ID != 75385503 {
t.Fatalf("did not scan correctly, got %v", n)
}
scanner.Close()
if v := scanner.Scan(); v {
t.Fatalf("should be closed for second scan: %v", scanner.Err())
}
if v := scanner.Err(); v != osm.ErrScannerClosed {
t.Errorf("incorrect error, got %v", v)
}
}
func TestScanner_FullyScannedBytes(t *testing.T) {
t.Run("should be non-zero after reading whole file", func(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 1)
for i := 0; i < 30000; i++ {
scanner.Scan()
}
if v := scanner.FullyScannedBytes(); v != 214162 {
t.Errorf("incorrect scanned bytes: %v", v)
}
for scanner.Scan() {
// scan the whole thing
}
if v := scanner.FullyScannedBytes(); v != 7488871 {
t.Errorf("incorrect scanned bytes: %v", v)
}
})
t.Run("should be non-zero if cancel context", func(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 1)
for i := 0; i < 30000; i++ {
scanner.Scan()
}
if v := scanner.FullyScannedBytes(); v != 214162 {
t.Errorf("incorrect scanned bytes: %v", v)
}
for scanner.Scan() {
// scan the whole thing
}
if v := scanner.FullyScannedBytes(); v != 7488871 {
t.Errorf("incorrect scanned bytes: %v", v)
}
})
t.Run("should always be increasing", func(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
scanner := New(context.Background(), f, 2)
var previouslyScanned int64
for scanner.Scan() {
if v := scanner.FullyScannedBytes(); v < previouslyScanned {
t.Errorf("scanned bytes decreased: %v < %v", v, previouslyScanned)
}
previouslyScanned = scanner.FullyScannedBytes()
}
if v := scanner.FullyScannedBytes(); v != 7488871 {
t.Errorf("incorrect scanned bytes: %v", v)
}
})
t.Run("should always be increasing after restart", func(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatalf("unable to open file: %v", err)
}
defer f.Close()
_, err = f.Seek(214162, 0)
if err != nil {
t.Fatalf("seek failed: %v", err)
}
scanner := New(context.Background(), f, 2)
var previouslyScanned int64
for scanner.Scan() {
if v := scanner.FullyScannedBytes(); v < previouslyScanned {
t.Errorf("scanned bytes decreased: %v < %v", v, previouslyScanned)
}
previouslyScanned = scanner.FullyScannedBytes()
}
if v := scanner.FullyScannedBytes(); v != 7274709 {
t.Errorf("incorrect scanned bytes: %v", v)
}
})
}
func BenchmarkLondon(b *testing.B) {
f, err := os.Open(London)
if err != nil {
b.Fatalf("could not open file: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Seek(0, 0)
scanner := New(context.Background(), f, 4)
nodes, ways, relations := benchmarkScanner(b, scanner)
if nodes != 2729006 {
b.Errorf("wrong number of nodes, got %v", nodes)
}
if ways != 459055 {
b.Errorf("wrong number of ways, got %v", ways)
}
if relations != 12833 {
b.Errorf("wrong number of relations, got %v", relations)
}
scanner.Close()
}
}
func BenchmarkLondon_nodes(b *testing.B) {
f, err := os.Open(London)
if err != nil {
b.Fatalf("could not open file: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Seek(0, 0)
scanner := New(context.Background(), f, 4)
scanner.SkipWays = true
scanner.SkipRelations = true
nodes, ways, relations := benchmarkScanner(b, scanner)
if nodes != 2729006 {
b.Errorf("wrong number of nodes, got %v", nodes)
}
if ways != 0 {
b.Errorf("wrong number of ways, got %v", ways)
}
if relations != 0 {
b.Errorf("wrong number of relations, got %v", relations)
}
scanner.Close()
}
}
func BenchmarkLondon_ways(b *testing.B) {
f, err := os.Open(London)
if err != nil {
b.Fatalf("could not open file: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Seek(0, 0)
scanner := New(context.Background(), f, 4)
scanner.SkipNodes = true
scanner.SkipRelations = true
nodes, ways, relations := benchmarkScanner(b, scanner)
if nodes != 0 {
b.Errorf("wrong number of nodes, got %v", nodes)
}
if ways != 459055 {
b.Errorf("wrong number of ways, got %v", ways)
}
if relations != 0 {
b.Errorf("wrong number of relations, got %v", relations)
}
scanner.Close()
}
}
func BenchmarkLondon_relations(b *testing.B) {
f, err := os.Open(London)
if err != nil {
b.Fatalf("could not open file: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Seek(0, 0)
scanner := New(context.Background(), f, 4)
scanner.SkipNodes = true
scanner.SkipWays = true
nodes, ways, relations := benchmarkScanner(b, scanner)
if nodes != 0 {
b.Errorf("wrong number of nodes, got %v", nodes)
}
if ways != 0 {
b.Errorf("wrong number of ways, got %v", ways)
}
if relations != 12833 {
b.Errorf("wrong number of relations, got %v", relations)
}
scanner.Close()
}
}
func benchmarkScanner(b *testing.B, scanner osm.Scanner) (int, int, int) {
var (
nodes int
ways int
relations int
)
for scanner.Scan() {
switch scanner.Object().(type) {
case *osm.Node:
nodes++
case *osm.Way:
ways++
case *osm.Relation:
relations++
}
}
if err := scanner.Err(); err != nil {
b.Errorf("scanner returned error: %v", err)
}
return nodes, ways, relations
}