-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathfeature_test.go
249 lines (214 loc) · 4.54 KB
/
feature_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
package osm
import (
"math/rand"
"reflect"
"testing"
)
func TestFeatureID_ids(t *testing.T) {
id := NodeID(1).FeatureID()
oid := id.ObjectID(10)
if v := oid.Type(); v != TypeNode {
t.Errorf("incorrect type: %v", v)
}
if v := oid.Ref(); v != 1 {
t.Errorf("incorrect id: %v", v)
}
if v := oid.Version(); v != 10 {
t.Errorf("incorrect version: %v", v)
}
eid := id.ElementID(10)
if v := eid.Type(); v != TypeNode {
t.Errorf("incorrect type: %v", v)
}
if v := eid.Ref(); v != 1 {
t.Errorf("incorrect id: %v", v)
}
if v := eid.Version(); v != 10 {
t.Errorf("incorrect version: %v", v)
}
if v := NodeID(1).FeatureID().NodeID(); v != 1 {
t.Errorf("incorrect id: %v", v)
}
if v := WayID(1).FeatureID().WayID(); v != 1 {
t.Errorf("incorrect id: %v", v)
}
if v := RelationID(1).FeatureID().RelationID(); v != 1 {
t.Errorf("incorrect id: %v", v)
}
t.Run("not a node", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic?")
}
}()
id := WayID(1).FeatureID()
id.NodeID()
})
t.Run("not a way", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic?")
}
}()
id := NodeID(1).FeatureID()
id.WayID()
})
t.Run("not a relation", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic?")
}
}()
id := WayID(1).FeatureID()
id.RelationID()
})
t.Run("should not panic if invalid type", func(t *testing.T) {
var id FeatureID
if v := id.Type(); v != "" {
t.Errorf("should return empty string for invalid type: %v", v)
}
})
}
func TestFeature_String(t *testing.T) {
cases := []struct {
name string
id FeatureID
expected string
}{
{
name: "node",
id: NodeID(1).FeatureID(),
expected: "node/1",
},
{
name: "way",
id: WayID(3).FeatureID(),
expected: "way/3",
},
{
name: "relation",
id: RelationID(1000).FeatureID(),
expected: "relation/1000",
},
{
name: "unknown",
id: 0,
expected: "unknown/0",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if v := tc.id.String(); v != tc.expected {
t.Errorf("incorrect string: %v", v)
}
})
}
}
func TestParseFeatureID(t *testing.T) {
cases := []struct {
name string
id FeatureID
}{
{
name: "node",
id: NodeID(0).FeatureID(),
},
{
name: "way",
id: WayID(10).FeatureID(),
},
{
name: "relation",
id: RelationID(100).FeatureID(),
},
{
name: "changeset",
id: RelationID(1000).FeatureID(),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
id, err := ParseFeatureID(tc.id.String())
if err != nil {
t.Errorf("parse error: %v", err)
}
if id != tc.id {
t.Errorf("incorrect id: %v != %v", id, tc.id)
}
})
}
// errors
if _, err := ParseFeatureID("123"); err == nil {
t.Errorf("should return error if only one part")
}
if _, err := ParseFeatureID("node/abc"); err == nil {
t.Errorf("should return error if id not a number")
}
if _, err := ParseFeatureID("lake/1"); err == nil {
t.Errorf("should return error if not a valid type")
}
}
func TestFeatureIDs_Counts(t *testing.T) {
ids := FeatureIDs{
RelationID(1).FeatureID(),
NodeID(1).FeatureID(),
WayID(2).FeatureID(),
WayID(1).FeatureID(),
RelationID(1).FeatureID(),
WayID(1).FeatureID(),
}
n, w, r := ids.Counts()
if n != 1 {
t.Errorf("incorrect nodes: %v", n)
}
if w != 3 {
t.Errorf("incorrect nodes: %v", w)
}
if r != 2 {
t.Errorf("incorrect nodes: %v", r)
}
}
func TestFeatureIDs_Sort(t *testing.T) {
ids := FeatureIDs{
RelationID(1).FeatureID(),
NodeID(1).FeatureID(),
WayID(2).FeatureID(),
WayID(1).FeatureID(),
}
expected := FeatureIDs{
NodeID(1).FeatureID(),
WayID(1).FeatureID(),
WayID(2).FeatureID(),
RelationID(1).FeatureID(),
}
ids.Sort()
if !reflect.DeepEqual(ids, expected) {
t.Errorf("not sorted correctly")
for i := range ids {
t.Logf("%d: %v", i, ids[i])
}
}
}
func BenchmarkFeatureIDs_Sort(b *testing.B) {
rand.Seed(1024)
tests := make([]FeatureIDs, b.N)
for i := range tests {
ids := make(FeatureIDs, 10000)
for j := range ids {
switch rand.Intn(4) {
case 0:
ids[j] = NodeID(rand.Int63n(int64(len(ids) / 10))).FeatureID()
case 1:
ids[j] = WayID(rand.Int63n(int64(len(ids) / 10))).FeatureID()
case 2:
ids[j] = RelationID(rand.Int63n(int64(len(ids) / 10))).FeatureID()
}
}
tests[i] = ids
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
tests[n].Sort()
}
}