Skip to content

Commit f5b7123

Browse files
committed
add more removal tests
1 parent 13c95cc commit f5b7123

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

quadtree/quadtree_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package quadtree
22

33
import (
4+
"fmt"
45
"math/rand"
56
"reflect"
67
"sort"
78
"testing"
9+
"time"
810

911
"github.com/paulmach/orb"
1012
"github.com/paulmach/orb/planar"
@@ -105,6 +107,42 @@ func TestQuadtreeRemoveAndAdd(t *testing.T) {
105107

106108
}
107109

110+
func TestQuadtreeRemoveAndAddRandom(t *testing.T) {
111+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
112+
113+
bounds := orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{3000, 3000}}
114+
qt := New(bounds)
115+
points := make([]*PExtra, 0, 3000)
116+
const perRun = 300
117+
const runs = 10
118+
id := 0
119+
for i := 0; i < runs; i++ {
120+
121+
for i := 0; i < perRun; i++ {
122+
x := r.Int63n(30)
123+
y := r.Int63n(30)
124+
id++
125+
p := &PExtra{p: orb.Point{float64(x), float64(y)}, id: fmt.Sprintf("%d", id)}
126+
qt.Add(p)
127+
points = append(points, p)
128+
}
129+
for i := 0; i < perRun/2; i++ {
130+
k := r.Int() % len(points)
131+
remP := points[k]
132+
points = append(points[:k], points[k+1:]...)
133+
qt.Remove(remP, func(p orb.Pointer) bool {
134+
return p.(*PExtra).id == remP.id
135+
})
136+
}
137+
}
138+
139+
left := len(qt.InBound(nil, bounds))
140+
expected := runs * perRun / 2
141+
if left != expected {
142+
t.Error("WRONG: ", left, expected)
143+
}
144+
}
145+
108146
func TestQuadtreeFind(t *testing.T) {
109147
points := orb.MultiPoint{}
110148
dim := 17

0 commit comments

Comments
 (0)