-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathorder_test.go
239 lines (203 loc) · 6.01 KB
/
order_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
package annotate
import (
"context"
"testing"
"github.com/paulmach/osm"
)
func TestChildFirstOrdering(t *testing.T) {
relations := osm.Relations{
{ID: 8, Members: osm.Members{{Type: osm.TypeNode, Ref: 12}}},
{ID: 10, Members: osm.Members{
{Type: osm.TypeWay, Ref: 8},
{Type: osm.TypeRelation, Ref: 8},
}},
{ID: 12, Members: osm.Members{{Type: osm.TypeRelation, Ref: 10}}},
{ID: 14, Members: osm.Members{{Type: osm.TypeNode, Ref: 12}}},
}
ordering := NewChildFirstOrdering(
context.Background(),
relations.IDs(),
(&osm.OSM{Relations: relations}).HistoryDatasource())
ids := make([]osm.RelationID, 0, len(relations))
for ordering.Next() {
ids = append(ids, ordering.RelationID())
}
if len(ids) != len(relations) {
t.Errorf("wrong number of ids, %v != %v", len(ids), len(relations))
}
aheadOf := [][2]osm.RelationID{
{8, 10}, // 8 ahead of, or before 10
{10, 12}, // 10 before 12
}
for i, p := range aheadOf {
if indexOf(ids, p[0]) > indexOf(ids, p[1]) {
t.Errorf("incorrect ordering, test %v", i)
t.Logf("ids: %v", ids)
}
}
if err := ordering.Err(); err != nil {
t.Errorf("unexpected error, got %v", err)
}
}
func TestChildFirstOrdering_cycle(t *testing.T) {
relations := osm.Relations{
{ID: 1, Version: 1, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 2},
{Type: osm.TypeRelation, Ref: 3},
}},
{ID: 1, Version: 2, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 2},
{Type: osm.TypeRelation, Ref: 3},
{Type: osm.TypeRelation, Ref: 5},
}},
{ID: 2, Version: 1, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 4},
{Type: osm.TypeRelation, Ref: 1},
}},
{ID: 2, Version: 2, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 6},
}},
{ID: 3, Members: osm.Members{{Type: osm.TypeWay, Ref: 8}}},
{ID: 4, Members: osm.Members{{Type: osm.TypeWay, Ref: 8}}},
{ID: 5, Members: osm.Members{{Type: osm.TypeWay, Ref: 8}}},
{ID: 6, Members: osm.Members{{Type: osm.TypeWay, Ref: 8}}},
// self cycle
{ID: 9, Members: osm.Members{{Type: osm.TypeRelation, Ref: 9}}},
}
ds := (&osm.OSM{Relations: relations}).HistoryDatasource()
ordering := NewChildFirstOrdering(context.Background(), relations.IDs(), ds)
ids := make([]osm.RelationID, 0, len(relations))
for ordering.Next() {
ids = append(ids, ordering.RelationID())
}
if len(ids) != len(ds.Relations) {
t.Errorf("wrong number of ids, %v != %v", len(ids), len(relations))
}
aheadOf := [][2]osm.RelationID{
{3, 1}, // 3 ahead of, or before 1
{4, 2}, // 4 before 2
{6, 2},
{5, 1},
}
for i, p := range aheadOf {
if indexOf(ids, p[0]) > indexOf(ids, p[1]) {
t.Errorf("incorrect ordering, test %v", i)
t.Logf("ids: %v", ids)
}
}
if err := ordering.Err(); err != nil {
t.Errorf("unexpected error, got %v", err)
}
}
func TestChildFirstOrdering_Cancel(t *testing.T) {
relations := osm.Relations{
{ID: 8, Members: osm.Members{{Type: osm.TypeNode, Ref: 12}}},
{ID: 10, Members: osm.Members{{Type: osm.TypeRelation, Ref: 8}}},
{ID: 12, Members: osm.Members{{Type: osm.TypeRelation, Ref: 10}}},
}
ctx, done := context.WithCancel(context.Background())
ordering := NewChildFirstOrdering(
ctx,
relations.IDs(),
(&osm.OSM{Relations: relations}).HistoryDatasource())
ordering.Next()
ordering.Next()
done()
if ordering.Next() {
t.Errorf("expect scan after cancel to be false")
}
if err := ordering.Err(); err != context.Canceled {
t.Errorf("incorrect error, got %v", err)
}
}
func TestChildFirstOrdering_Close(t *testing.T) {
relations := osm.Relations{
{ID: 8, Members: osm.Members{{Type: osm.TypeNode, Ref: 12}}},
{ID: 10, Members: osm.Members{{Type: osm.TypeRelation, Ref: 8}}},
{ID: 12, Members: osm.Members{{Type: osm.TypeRelation, Ref: 10}}},
}
ordering := NewChildFirstOrdering(
context.Background(),
relations.IDs(),
(&osm.OSM{Relations: relations}).HistoryDatasource())
ordering.Next()
ordering.Next()
ordering.Close()
if ordering.Next() {
t.Errorf("expect scan after cancel to be false")
}
if err := ordering.Err(); err != context.Canceled {
t.Errorf("incorrect error, got %v", err)
}
}
func TestChildFirstOrdering_Walk(t *testing.T) {
relations := osm.Relations{
{ID: 2, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 4},
}},
{ID: 4, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 6},
}},
{ID: 6, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 10},
}},
{ID: 8, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 6},
}},
{ID: 10, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 8},
}},
// circular relation of self.
{ID: 16, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 16},
}},
}
ordering := &ChildFirstOrdering{
ctx: context.Background(),
ds: (&osm.OSM{Relations: relations}).HistoryDatasource(),
visited: make(map[osm.RelationID]struct{}, len(relations)),
out: make(chan osm.RelationID, 10+len(relations)),
}
// start at all parts of cycle
// basically should not infinite loop
path := make([]osm.RelationID, 0, 100)
for _, r := range relations {
err := ordering.walk(r.ID, path)
if err != nil {
t.Errorf("should process cycle without problem: %v", err)
}
}
}
func TestChildFirstOrdering_missingRelation(t *testing.T) {
relations := osm.Relations{
{ID: 2, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 3},
}},
{ID: 4, Members: osm.Members{
{Type: osm.TypeRelation, Ref: 2},
}},
}
ordering := &ChildFirstOrdering{
ctx: context.Background(),
ds: (&osm.OSM{Relations: relations}).HistoryDatasource(),
visited: make(map[osm.RelationID]struct{}, len(relations)),
out: make(chan osm.RelationID, 10+len(relations)),
}
// start at all parts of cycle
// basically should not infinite loop
path := make([]osm.RelationID, 0, 100)
for _, r := range relations {
err := ordering.walk(r.ID, path)
if err != nil {
t.Errorf("should process cycle without problem: %v", err)
}
}
}
func indexOf(s []osm.RelationID, id osm.RelationID) int {
for i, sid := range s {
if sid == id {
return i
}
}
panic("id not found")
}