-
Notifications
You must be signed in to change notification settings - Fork 49
/
way_test.go
146 lines (123 loc) · 3.08 KB
/
way_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
package annotate
import (
"context"
"encoding/xml"
"fmt"
"os"
"reflect"
"testing"
"github.com/paulmach/osm"
)
func TestWays(t *testing.T) {
ids := []osm.WayID{
6394949,
230391153,
}
for _, id := range ids {
o := loadTestdata(t, fmt.Sprintf("testdata/way_%d.osm", id))
ds := (&osm.OSM{Nodes: o.Nodes}).HistoryDatasource()
err := Ways(context.Background(), o.Ways, ds)
if err != nil {
t.Fatalf("compute error: %v", err)
}
filename := fmt.Sprintf("testdata/way_%d_expected.osm", id)
expected := loadTestdata(t, filename)
if !reflect.DeepEqual(o.Ways, expected.Ways) {
t.Errorf("expected way for id %d not equal", id)
filename := fmt.Sprintf("testdata/way_%d_got.osm", id)
t.Errorf("expected way not equal, file saved to %s", filename)
data, _ := xml.MarshalIndent(&osm.OSM{Ways: o.Ways}, "", " ")
err := os.WriteFile(filename, data, 0644)
if err != nil {
t.Fatalf("write error: %v", err)
}
}
}
}
func TestWays_childFilter(t *testing.T) {
nodes := osm.Nodes{
{ID: 1, Version: 1, Lat: 1, Lon: 1, Visible: true},
{ID: 1, Version: 2, Lat: 2, Lon: 2, Visible: true},
{ID: 1, Version: 3, Lat: 3, Lon: 3, Visible: true},
{ID: 2, Version: 1, Lat: 1, Lon: 1, Visible: true},
{ID: 2, Version: 2, Lat: 2, Lon: 2, Visible: true},
{ID: 2, Version: 3, Lat: 3, Lon: 3, Visible: true},
{ID: 3, Version: 1, Lat: 1, Lon: 1, Visible: true},
}
ways := osm.Ways{
{
ID: 1,
Version: 1,
Visible: true,
Nodes: osm.WayNodes{
{ID: 1, Version: 1},
{ID: 2, Version: 1}, // filter says no annotate
{ID: 3}, // annotate because not
},
},
}
ds := (&osm.OSM{Nodes: nodes}).HistoryDatasource()
err := Ways(
context.Background(),
ways,
ds,
Threshold(0),
ChildFilter(func(fid osm.FeatureID) bool {
return fid == osm.NodeID(1).FeatureID()
}),
)
if err != nil {
t.Fatalf("compute error: %v", err)
}
if ways[0].Nodes[0].Lat == 0 {
t.Errorf("should annotate first node")
}
if ways[0].Nodes[1].Lat != 0 {
t.Errorf("should not annotate second node")
}
if ways[0].Nodes[2].Lat == 0 {
t.Errorf("should annotate third node")
}
}
func BenchmarkWay(b *testing.B) {
o := loadTestdata(b, "testdata/way_6394949.osm")
ds := (&osm.OSM{Nodes: o.Nodes}).HistoryDatasource()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err := Ways(context.Background(), o.Ways, ds)
if err != nil {
b.Fatalf("compute error: %v", err)
}
}
}
func BenchmarkWays(b *testing.B) {
o := loadTestdata(b, "testdata/relation_2714790.osm")
ds := o.HistoryDatasource()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; {
for id, ways := range ds.Ways {
err := Ways(context.Background(), ways, ds)
if err != nil {
b.Fatalf("compute error for way %d: %v", id, err)
}
n++
if n >= b.N {
break
}
}
}
}
func loadTestdata(tb testing.TB, filename string) *osm.OSM {
data, err := os.ReadFile(filename)
if err != nil {
tb.Fatalf("unable to open file: %v", err)
}
o := &osm.OSM{}
err = xml.Unmarshal(data, o)
if err != nil {
tb.Fatalf("unable to unmarshal data: %v", err)
}
return o
}