forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_test.go
228 lines (181 loc) · 4.92 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
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 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 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
}