|
1 | 1 | package quadtree |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "math/rand" |
5 | 6 | "reflect" |
6 | 7 | "sort" |
7 | 8 | "testing" |
| 9 | + "time" |
8 | 10 |
|
9 | 11 | "github.com/paulmach/orb" |
10 | 12 | "github.com/paulmach/orb/planar" |
@@ -105,6 +107,42 @@ func TestQuadtreeRemoveAndAdd(t *testing.T) { |
105 | 107 |
|
106 | 108 | } |
107 | 109 |
|
| 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 | + |
108 | 146 | func TestQuadtreeFind(t *testing.T) { |
109 | 147 | points := orb.MultiPoint{} |
110 | 148 | dim := 17 |
|
0 commit comments