-
Notifications
You must be signed in to change notification settings - Fork 55
/
point_set_test.go
373 lines (301 loc) · 8.31 KB
/
point_set_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
package geo
import (
"math"
"testing"
)
func TestPointSetCentroid(t *testing.T) {
ps := &PointSet{}
ps.Push(&Point{0, 0}).
Push(&Point{1, 1.5}).
Push(&Point{2, 0})
centroid := ps.Centroid()
expectedCenter := &Point{1, 0.5}
if !centroid.Equals(expectedCenter) {
t.Errorf("should find centroid correctly, got %v", centroid)
}
}
func TestPointSetGeoCentroid(t *testing.T) {
ps := &PointSet{}
ps.Push(&Point{-188.1298828125, -33.97980872872456}).
Push(&Point{-186.1083984375, -38.54816542304658}).
Push(&Point{-194.8974609375, -46.10370875598026}).
Push(&Point{-192.1728515625, -47.8721439688873}).
Push(&Point{-179.7802734375, -37.30027528134431})
centroid := ps.GeoCentroid()
// NOTE: input of longitude is outside of the -180:180 ranage but output is within.
expectedCenter := &Point{172.08523311057562, -40.87523942007359}
if !centroid.Equals(expectedCenter) {
t.Errorf("should find centroid correctly, got %v", centroid)
}
}
func TestPointSetDistanceFrom(t *testing.T) {
ps := &PointSet{}
ps.Push(&Point{0, 0}).
Push(&Point{1, 1}).
Push(&Point{2, 2})
fromPoint := &Point{3, 2}
if distance, _ := ps.DistanceFrom(fromPoint); distance != 1 {
t.Errorf("distance incorrect, got %v", distance)
}
if _, index := ps.GeoDistanceFrom(fromPoint); index != 2 {
t.Errorf("incorrect closest index, got %v", index)
}
}
func TestPointSetGeoDistanceFrom(t *testing.T) {
ps := &PointSet{}
ps.Push(&Point{-122.42558918, 37.76159786}).
Push(&Point{-122.40206146, 37.77962363}).
Push(&Point{-122.41486043, 37.78138826})
fromPoint := &Point{-122.41941550000001, 37.7749295}
if distance, _ := ps.GeoDistanceFrom(fromPoint); math.Floor(distance) != 823 {
t.Errorf("geo distance incorrect, got %v", distance)
}
if _, index := ps.GeoDistanceFrom(fromPoint); index != 2 {
t.Errorf("incorrect closest index, got %v", index)
}
}
func TestNewPointSet(t *testing.T) {
ps := NewPointSet()
ps.Push(&Point{-122.42558918, 37.76159786}).
Push(&Point{-122.41486043, 37.78138826}).
Push(&Point{-122.40206146, 37.77962363})
if ps.Length() != 3 {
t.Errorf("should find correct length of new point set %v", ps.Length())
}
}
func TestNewPointSetPreallocate(t *testing.T) {
ps := NewPointSet()
ps.Push(&Point{-122.42558918, 37.76159786}).
Push(&Point{-122.41486043, 37.78138826}).
Push(&Point{-122.40206146, 37.77962363})
if ps.Length() != 3 {
t.Errorf("should find correct length of new point set %v", ps.Length())
}
if !ps.GetAt(0).Equals(&Point{-122.42558918, 37.76159786}) {
t.Errorf("should find correct first point of new point set %v", ps.GetAt(0))
}
if !ps.GetAt(2).Equals(&Point{-122.40206146, 37.77962363}) {
t.Errorf("should find correct first point of new point set %v", ps.GetAt(2))
}
}
func TestPathBound(t *testing.T) {
ps := NewPointSet()
ps.Push(NewPoint(0.5, .2))
ps.Push(NewPoint(-1, 0))
ps.Push(NewPoint(1, 10))
ps.Push(NewPoint(1, 8))
answer := NewBound(-1, 1, 0, 10)
if b := ps.Bound(); !b.Equals(answer) {
t.Errorf("bound, %v != %v", b, answer)
}
ps = NewPointSet()
if !ps.Bound().Empty() {
t.Error("expect empty point set to have empty bounds")
}
}
func TestPointSetSetAt(t *testing.T) {
ps := NewPointSet()
point := NewPoint(1, 2)
ps.Push(NewPoint(2, 3))
ps.SetAt(0, point)
if p := ps.GetAt(0); !p.Equals(point) {
t.Errorf("setAt expected %v == %v", p, point)
}
}
func TestPointSetSetAtPanicIndexOver(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expect setAt to panic if index out of range")
}
}()
ps := NewPointSet()
ps.Push(NewPoint(1, 2))
ps.SetAt(2, NewPoint(3, 4))
}
func TestPointSetSetAtPanicIndexUnder(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expect setAt to panic if index out of range")
}
}()
ps := NewPointSet()
ps.Push(NewPoint(1, 2))
ps.SetAt(-1, NewPoint(3, 4))
}
func TestPointSetGetAt(t *testing.T) {
ps := NewPointSet()
point := NewPoint(1, 2)
ps.Push(point)
if p := ps.GetAt(0); !p.Equals(point) {
t.Errorf("getAt expected %v == %v", p, point)
}
if p := ps.GetAt(10); p != nil {
t.Error("expect out of range getAt to be nil")
}
if p := ps.GetAt(-1); p != nil {
t.Error("expect negative index getAt to be nil")
}
if p := ps.GetAt(0).SetX(100); !p.Equals(ps.GetAt(0)) {
t.Error("expect getAt to return pointer to original value")
}
}
func TestPointSetInsertAt(t *testing.T) {
ps := NewPointSet()
point1 := NewPoint(1, 2)
point2 := NewPoint(3, 4)
ps.Push(point1)
ps.InsertAt(0, point2)
if p := ps.GetAt(0); !p.Equals(point2) {
t.Errorf("insertAt expected %v == %v", p, point2)
}
if p := ps.GetAt(1); !p.Equals(point1) {
t.Errorf("insertAt expected %v == %v", p, point1)
}
point3 := NewPoint(5, 6)
ps.InsertAt(2, point3)
if p := ps.GetAt(2); !p.Equals(point3) {
t.Errorf("insertAt expected %v == %v", p, point3)
}
}
func TestPointSetInsertAtPanicIndexOver(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expect insertAt to panic if index out of range")
}
}()
ps := NewPointSet()
ps.Push(NewPoint(1, 2))
ps.InsertAt(2, NewPoint(3, 4))
}
func TestPointSetInsertAtPanicIndexUnder(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expect insertAt to panic if index out of range")
}
}()
ps := NewPointSet()
ps.Push(NewPoint(1, 2))
ps.InsertAt(-1, NewPoint(3, 4))
}
func TestPointSetRemoveAt(t *testing.T) {
ps := NewPointSet()
point := NewPoint(1, 2)
ps.Push(point)
ps.RemoveAt(0)
if ps.Length() != 0 {
t.Error("expect removeAt to remove point")
}
}
func TestPointSetRemoveAtPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expect removeAt to panic if index out of range")
}
}()
ps := NewPointSet()
ps.Push(NewPoint(1, 2))
ps.RemoveAt(2)
}
func TestPointSetPush(t *testing.T) {
ps := NewPointSet()
ps.Push(NewPoint(1, 2))
if ps.Length() != 1 {
t.Errorf("push length 1 != %d", ps.Length())
}
answer := NewPoint(1, 2)
if a := ps.GetAt(0); !a.Equals(answer) {
t.Errorf("push first expecting %v == %v", a, answer)
}
}
func TestPointSetPop(t *testing.T) {
ps := NewPointSet()
if ps.Pop() != nil {
t.Error("expect empty pop to return nil")
}
ps.Push(NewPoint(1, 2))
answer := NewPoint(1, 2)
if a := ps.Pop(); !a.Equals(answer) {
t.Errorf("pop first expecting %v == %v", a, answer)
}
}
func TestPointSetEquals(t *testing.T) {
p1 := NewPointSet()
p1.Push(NewPoint(0.5, .2))
p1.Push(NewPoint(-1, 0))
p1.Push(NewPoint(1, 10))
p2 := NewPointSet()
p2.Push(NewPoint(0.5, .2))
p2.Push(NewPoint(-1, 0))
p2.Push(NewPoint(1, 10))
if !p1.Equals(p2) {
t.Error("equals paths should be equal")
}
p3 := p2.Clone().SetAt(1, NewPoint(0, 0))
if p1.Equals(p3) {
t.Error("equals paths should not be equal")
}
p2.Pop()
if p2.Equals(p1) {
t.Error("equals paths should not be equal")
}
}
func TestPointSetClone(t *testing.T) {
p1 := NewPointSet()
p1.Push(NewPoint(0, 0))
p1.Push(NewPoint(0.5, .2))
p1.Push(NewPoint(1, 0))
p2 := p1.Clone()
p2.Pop()
if p1.Length() == p2.Length() {
t.Errorf("clone length %d == %d", p1.Length(), p2.Length())
}
p2 = p1.Clone()
if p1 == p2 {
t.Error("clone should return different pointers")
}
if !p2.Equals(p1) {
t.Error("clone paths should be equal")
}
}
func TestPointSetToGeoJSON(t *testing.T) {
p := NewPointSet().
Push(NewPoint(1, 2))
f := p.ToGeoJSON()
if !f.Geometry.IsMultiPoint() {
t.Errorf("pointset, should be linestring geometry")
}
}
func TestPointSetToWKT(t *testing.T) {
ps := NewPointSet()
answer := "EMPTY"
if s := ps.ToWKT(); s != answer {
t.Errorf("pointset, string expected %s, got %s", answer, s)
}
ps.Push(NewPoint(1, 2))
answer = "MULTIPOINT(1 2)"
if s := ps.ToWKT(); s != answer {
t.Errorf("pointset, string expected %s, got %s", answer, s)
}
ps.Push(NewPoint(3, 4))
answer = "MULTIPOINT(1 2,3 4)"
if s := ps.ToWKT(); s != answer {
t.Errorf("pointset, string expected %s, got %s", answer, s)
}
}
func TestPointSetString(t *testing.T) {
ps := NewPointSet()
answer := "EMPTY"
if s := ps.String(); s != answer {
t.Errorf("pointset, string expected %s, got %s", answer, s)
}
ps.Push(NewPoint(1, 2))
answer = "MULTIPOINT(1 2)"
if s := ps.String(); s != answer {
t.Errorf("pointset, string expected %s, got %s", answer, s)
}
ps.Push(NewPoint(3, 4))
answer = "MULTIPOINT(1 2,3 4)"
if s := ps.String(); s != answer {
t.Errorf("pointset, string expected %s, got %s", answer, s)
}
}