-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspatial_test.go
344 lines (272 loc) · 6.57 KB
/
spatial_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
package spatial
import (
"math"
"testing"
"time"
"github.com/dhconnelly/rtreego"
)
const (
itUserObject IndexableType = 1
itUserObject2 IndexableType = 2
testObjectID = "obj1"
)
var (
testBounds = MapBounds{
SouthWestLng: -10,
SouthWestLat: -10,
NorthEastLng: 10,
NorthEastLat: 10,
}
)
type (
object struct {
id string
rect *rtreego.Rect
objType IndexableType
}
)
func (o *object) ID() string {
return o.id
}
func (o *object) Type() IndexableType {
return o.objType
}
func (o *object) Bounds() *rtreego.Rect {
return o.rect
}
func (o *object) Ref() interface{} {
return nil
}
func newObject(oType IndexableType, id string, lat float64, lng float64) *object {
p := rtreego.Point{lng, lat}
rect, _ := rtreego.NewRect(p, []float64{0.1, 0.1})
return &object{id, rect, oType}
}
func newRectObject(oType IndexableType, id string, rect *rtreego.Rect) *object {
return &object{id, rect, oType}
}
func timeout(n time.Duration) <-chan time.Time {
t := time.NewTimer(n * time.Millisecond)
return t.C
}
func getUpdates(ch <-chan []Indexable) []Indexable {
for {
select {
case <-timeout(50):
return nil
case u := <-ch:
return u
}
}
}
func eq(a, b float64) bool {
tolerance := 0.0001
diff := math.Abs(a - b)
return diff < tolerance
}
func checkCoords(rect *rtreego.Rect, x float64, y float64) bool {
return eq(rect.PointCoord(0), x) && eq(rect.PointCoord(1), y)
}
func TestBoundsUpdate(t *testing.T) {
var updates []Indexable
var ok bool
srv := New(25, 50)
lst := srv.NewListener(100, 10*time.Millisecond)
defer lst.Stop()
lst.SetBounds(testBounds)
ch := lst.Updates()
// make sure there are no updates yet
updates = getUpdates(ch)
if updates != nil {
t.Errorf("unexpected update: %s", updates)
return
}
obj := newObject(itUserObject, testObjectID, 0, 0)
srv.Add(obj)
updates = getUpdates(ch)
if updates == nil {
t.Errorf("one update expected, got nil")
return
}
if len(updates) != 1 {
t.Errorf("one update expected, got %d", len(updates))
return
}
if obj, ok = updates[0].(*object); !ok {
t.Errorf("error asserting update type as object")
return
}
if !checkCoords(obj.rect, 0, 0) {
t.Errorf("expected coords %.3f/%.3f, got %.3f/%.3f",
0.0, 0.0,
obj.rect.PointCoord(0), obj.rect.PointCoord(1),
)
return
}
obj = newObject(itUserObject, testObjectID, 3, 3)
srv.Add(obj)
updates = getUpdates(ch)
if updates == nil {
t.Errorf("one update expected, got nil")
return
}
if len(updates) != 1 {
t.Errorf("one update expected, got %d", len(updates))
return
}
if obj, ok = updates[0].(*object); !ok {
t.Errorf("error asserting update type as object")
return
}
if !checkCoords(obj.rect, 3, 3) {
t.Errorf("expected coords %.3f/%.3f, got %.3f/%.3f",
3.0, 3.0,
obj.rect.PointCoord(0), obj.rect.PointCoord(1),
)
return
}
// move outside
obj = newObject(itUserObject, testObjectID, 12, 12)
srv.Add(obj)
updates = getUpdates(ch)
if updates == nil {
t.Errorf("zero updates expected, got nil")
return
}
if len(updates) != 0 {
t.Errorf("zero updates expected, got %d", len(updates))
return
}
}
func TestWatchID(t *testing.T) {
var updates []Indexable
var ok bool
srv := New(25, 50)
lst := srv.NewListener(100, 10*time.Millisecond)
defer lst.Stop()
lst.SetBounds(testBounds)
lst.SubscribeID(testObjectID)
ch := lst.Updates()
// make sure there are no updates yet
updates = getUpdates(ch)
if updates != nil {
t.Errorf("unexpected update: %s", updates)
return
}
obj := newObject(itUserObject, testObjectID, 0, 0)
srv.Add(obj)
updates = getUpdates(ch)
if updates == nil {
t.Errorf("one update expected, got nil")
return
}
if len(updates) != 1 {
t.Errorf("one update expected, got %d", len(updates))
return
}
if obj, ok = updates[0].(*object); !ok {
t.Errorf("error asserting update type as object")
return
}
if !checkCoords(obj.rect, 0, 0) {
t.Errorf("expected coords %.3f/%.3f, got %.3f/%.3f",
0.0, 0.0,
obj.rect.PointCoord(0), obj.rect.PointCoord(1),
)
return
}
obj = newObject(itUserObject, testObjectID, 3, 3)
srv.Add(obj)
updates = getUpdates(ch)
if updates == nil {
t.Errorf("one update expected, got nil")
return
}
if len(updates) != 1 {
t.Errorf("one update expected, got %d", len(updates))
return
}
if obj, ok = updates[0].(*object); !ok {
t.Errorf("error asserting update type as object")
return
}
if !checkCoords(obj.rect, 3, 3) {
t.Errorf("expected coords %.3f/%.3f, got %.3f/%.3f",
3.0, 3.0,
obj.rect.PointCoord(0), obj.rect.PointCoord(1),
)
return
}
// move outside
obj = newObject(itUserObject, testObjectID, 12, 12)
srv.Add(obj)
updates = getUpdates(ch)
if updates == nil {
t.Errorf("one update expected, got nil")
return
}
if len(updates) != 1 {
t.Errorf("one update expected, got %d", len(updates))
return
}
if obj, ok = updates[0].(*object); !ok {
t.Errorf("error asserting update type as object")
return
}
if !checkCoords(obj.rect, 12, 12) {
t.Errorf("expected coords %.3f/%.3f, got %.3f/%.3f",
12.0, 12.0,
obj.rect.PointCoord(0), obj.rect.PointCoord(1),
)
return
}
}
func TestFilters(t *testing.T) {
srv := New(25, 50)
lst := srv.NewListener(100, time.Second)
defer lst.Stop()
obj := newObject(itUserObject, testObjectID, 0, 0)
srv.Add(obj)
obj2 := newObject(itUserObject2, "obj2", 0.1, 0.1)
srv.Add(obj2)
p := rtreego.Point{-1.0, -1.0}
rect, _ := rtreego.NewRect(p, []float64{2.0, 2.0})
boxObj := newRectObject(itBoundingBox, "box1", rect)
srv.Add(boxObj)
p = rtreego.Point{-30.0, -30.0}
rect, _ = rtreego.NewRect(p, []float64{60.0, 60.0})
bounds := newBoundingBox(rect, lst)
srv.Add(bounds)
results := srv.findObjectsByBoundingBoxes([]*boundingBox{bounds})
if len(results) != 2 {
t.Errorf("expected exactly 2 objects, but %d were found", len(results))
return
}
for id := range results {
if id != obj.id && id != obj2.id {
t.Errorf("unexpected object with id \"%s\" found", id)
}
}
results = srv.findObjectsByBoundingBoxes(
[]*boundingBox{bounds},
FilterByTypes([]IndexableType{itUserObject}),
)
if len(results) != 1 {
t.Errorf("expected exactly 1 object, but %d were found", len(results))
return
}
if _, found := results[obj.id]; !found {
t.Errorf("object %s is expected to be in results", obj.id)
}
results = srv.findObjectsByBoundingBoxes(
[]*boundingBox{bounds},
FilterByTypes([]IndexableType{itUserObject2}),
)
if len(results) != 1 {
t.Errorf("expected exactly 1 object, but %d were found", len(results))
return
}
if _, found := results[obj2.id]; !found {
t.Errorf("object %s is expected to be in results", obj.id)
}
}