-
Notifications
You must be signed in to change notification settings - Fork 3
/
lq_test.go
242 lines (204 loc) · 6.34 KB
/
lq_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
package lq
import (
"fmt"
"log"
"testing"
)
type set[K comparable] map[K]struct{}
type idset set[int]
func (m idset) assertEmpty(t *testing.T) {
t.Helper()
if len(m) > 0 {
t.Errorf("got len(idset) = %d, want empty", len(m))
}
}
func (m idset) assertContains(t *testing.T, id int) {
t.Helper()
if _, ok := m[id]; !ok {
t.Errorf("idset doesn't not contain id=%d but should", id)
}
}
func (m idset) assertNotContains(t *testing.T, id int) {
t.Helper()
if _, ok := m[id]; ok {
t.Errorf("idset contains id=%d but shouldn't", id)
}
}
func (m idset) assertIsContained(t *testing.T, id int, contains bool) {
t.Helper()
if contains {
m.assertContains(t, id)
} else {
m.assertNotContains(t, id)
}
}
// storeID is a CallBackFunction that stores the entity ID into the set.
func (m idset) storeID(id int, sqDist float64) {
m[id] = struct{}{}
}
// printEntity is a CallBackFunction that prints the entity.
func printEntity(id int, sqDist float64) {
log.Printf("printAllEntities: id:%+v %f\n", id, sqDist)
}
func TestAddObjectToDatabase(t *testing.T) {
var tests = []struct {
orgx, orgy float64
szx, szy float64
divx, divy int
ptx, pty float64
name string
}{
{0, 0, 10, 10, 5, 5, 5, 5, "inside super brick"},
{0, 0, 10, 10, 5, 5, 0, 11, "outside super brick (top)"},
{0, 0, 10, 10, 5, 5, -1, 0, "outside super brick (left)"},
{0, 0, 10, 10, 5, 5, 11, 0, "outside super brick (right)"},
{0, 0, 10, 10, 5, 5, 0, -1, "outside super brick (bottom)"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := NewDB[int](tt.orgx, tt.orgy, tt.szx, tt.szy, tt.divx, tt.divy)
db.Attach(1, tt.ptx, tt.pty)
ids := make(idset)
db.ForEachObject(ids.storeID)
ids.assertContains(t, 1)
})
}
}
func TestRemoveObject(t *testing.T) {
var tests = []struct {
orgx, orgy float64
szx, szy float64
divx, divy int
ptx, pty float64
name string
}{
{0, 0, 10, 10, 5, 5, 5, 5, "inside super brick"},
{0, 0, 10, 10, 5, 5, 0, 11, "outside super brick (top)"},
{0, 0, 10, 10, 5, 5, -1, 0, "outside super brick (left)"},
{0, 0, 10, 10, 5, 5, 11, 0, "outside super brick (right)"},
{0, 0, 10, 10, 5, 5, 0, -1, "outside super brick (bottom)"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := NewDB[int](tt.orgx, tt.orgy, tt.szx, tt.szy, tt.divx, tt.divy)
p1 := db.Attach(1, tt.ptx, tt.pty)
p1.removeFromBin()
ids := make(idset)
db.ForEachObject(ids.storeID)
ids.assertNotContains(t, 1)
})
}
}
func TestRemoveAllObjects(t *testing.T) {
var tests = []struct {
orgx, orgy float64
szx, szy float64
divx, divy int
ptx, pty float64
name string
}{
{0, 0, 10, 10, 5, 5, 5, 5, "inside super brick"},
{0, 0, 10, 10, 5, 5, -1, -1, "outside super brick"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := NewDB[int](tt.orgx, tt.orgy, tt.szx, tt.szy, tt.divx, tt.divy)
db.Attach(1, tt.ptx, tt.pty)
db.Attach(2, tt.ptx, tt.pty)
db.DetachAll()
ids := make(idset)
db.ForEachObject(ids.storeID)
ids.assertEmpty(t)
})
}
}
func TestObjectLocality(t *testing.T) {
var tests = []struct {
p1x, p1y, p2x, p2y, p3x, p3y float64 // the 3 points in the db
cx, cy float64 // search circle center
cr float64 // search circle radius
r1, r2, r3 bool // expected result for p1, p2 and p3
}{
{1, 1, 1, 2, 1, 3, 1, 1, 0.1, true, false, false},
{1, 1, 1, 2, 1, 3, 1, 1, 1.1, true, true, false},
{1, 1, 1, 2, 1, 3, 1, 1, 2.1, true, true, true},
{1, 1, 1, 2, 1, 3, 1, 1, 10, true, true, true},
{1, 1, 1, 2, 1, 3, -1, -1, 1, false, false, false},
{1, 1, 1, 2, 1, 3, -1, -1, 3, true, false, false},
{0, 0, 5, 5, 10, 10, 0, 0, 1.5, true, false, false},
{0, 0, 5, 5, 10, 10, 1, 1, 1.5, true, false, false},
{0, 0, 5, 5, 10, 10, 1, 1, 1.5, true, false, false},
{0, 0, 5, 5, 10, 10, 4, 4, 1.5, false, true, false},
{0, 0, 5, 5, 10, 10, 5, 5, 1.5, false, true, false},
{0, 0, 5, 5, 10, 10, 6, 6, 1.5, false, true, false},
{0, 0, 5, 5, 10, 10, 9, 9, 1.5, false, false, true},
{0, 0, 5, 5, 10, 10, 10, 10, 1.5, false, false, true},
{0, 0, 5, 5, 10, 10, 11, 11, 1.5, false, false, true},
{1, 1, 1, 2, 1, 3, -1, -1, 0.1, false, false, false},
{1, 1, 1, 2, 1, 3, -11, -1, 0.1, false, false, false},
{1, 1, 1, 2, 1, 3, -11, -11, 0.1, false, false, false},
{1, 1, 1, 2, 1, 3, -1, -11, 0.1, false, false, false},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("locality test %d", i), func(t *testing.T) {
db := NewDB[int](0, 0, 10, 10, 5, 5)
db.Attach(1, tt.p1x, tt.p1y)
db.Attach(2, tt.p2x, tt.p2y)
db.Attach(3, tt.p3x, tt.p3y)
ids := make(idset)
db.ForEachWithinRadius(tt.cx, tt.cy, tt.cr, ids.storeID)
ids.assertIsContained(t, 1, tt.r1)
ids.assertIsContained(t, 2, tt.r2)
ids.assertIsContained(t, 3, tt.r3)
})
}
}
func TestBinRelinking(t *testing.T) {
for i := range []int{1, 2, 3} {
db := NewDB[int](0, 0, 10, 10, 5, 5)
p1 := db.Attach(1, 5, 5)
p2 := db.Attach(2, 5, 5)
p3 := db.Attach(3, 5, 5)
switch i {
case 1:
p1.removeFromBin()
case 2:
p2.removeFromBin()
case 3:
p3.removeFromBin()
}
ids := make(idset)
db.ForEachWithinRadius(5, 5, 1, ids.storeID)
ids.assertIsContained(t, 1, i != 1)
ids.assertIsContained(t, 2, i != 2)
ids.assertIsContained(t, 3, i != 3)
}
}
func TestNearestNeighbor(t *testing.T) {
var tests = []struct {
p1x, p1y, p2x, p2y, p3x, p3y float64 // the 3 points in the db
cx, cy float64 // search circle center
cr float64 // search circle radius
ignore int // ignored
want int // expected nearest object
wantFound bool // expected value of found
}{
{1, 1, 1, 2, 1, 3, 1, 1, 0.1, 1, 0, false},
{1, 1, 1, 2, 1, 3, 1, 1, 1.1, 1, 2, true},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("nearest test %d", i), func(t *testing.T) {
db := NewDB[int](0, 0, 10, 10, 5, 5)
db.Attach(1, tt.p1x, tt.p1y)
db.Attach(2, tt.p2x, tt.p2y)
db.Attach(3, tt.p3x, tt.p3y)
got, found := db.FindNearestInRadius(tt.cx, tt.cy, tt.cr, tt.ignore)
if found != tt.wantFound {
t.Errorf("found = %t, wantFound = %t", found, tt.wantFound)
}
if got != tt.want {
t.Errorf("got nearest neighbour = %v, want %v", got, tt.want)
}
})
}
}