From 442f9f0d8e2e313d8b193716865a9c76a2037115 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sun, 12 Jun 2016 05:55:35 -0700 Subject: [PATCH] point tests --- rtree_base.go | 5 ++--- rtree_test.go | 47 +++++++++++++++++++++++++++++++++++++--------- vendor/d1/rtree.go | 5 ++--- vendor/d2/rtree.go | 5 ++--- vendor/d3/rtree.go | 5 ++--- vendor/d4/rtree.go | 5 ++--- 6 files changed, 48 insertions(+), 24 deletions(-) diff --git a/rtree_base.go b/rtree_base.go index 3e603d5..9effb61 100644 --- a/rtree_base.go +++ b/rtree_base.go @@ -73,8 +73,7 @@ func init() { } type RTree struct { - root *Node ///< Root of tree - unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions + root *Node ///< Root of tree } /// Minimal bounding rectangle (n-dimensional) @@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) { } //_DEBUG var branch Branch branch.data = dataId - branch.child = nil for axis := 0; axis < NUMDIMS; axis++ { branch.rect.min[axis] = min[axis] branch.rect.max[axis] = max[axis] @@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) { // Remove element by swapping with the last element to prevent gaps in array node.branch[index] = node.branch[node.count-1] node.branch[node.count-1].data = nil + node.branch[node.count-1].child = nil node.count-- } diff --git a/rtree_test.go b/rtree_test.go index 775a829..d1700b4 100644 --- a/rtree_test.go +++ b/rtree_test.go @@ -52,14 +52,14 @@ func tRandPoint() *tPoint { } func TestRTree(t *testing.T) { tr := New() - + zeroPoint := &tRect{0, 0, 0, 0} tr.Insert(&tRect{10, 10, 10, 10, 20, 20, 20, 20}) tr.Insert(&tRect{10, 10, 10, 20, 20, 20}) tr.Insert(&tRect{10, 10, 20, 20}) tr.Insert(&tRect{10, 20}) - - if tr.Count() != 4 { - t.Fatalf("expecting %v, got %v", 4, tr.Count()) + tr.Insert(zeroPoint) + if tr.Count() != 5 { + t.Fatalf("expecting %v, got %v", 5, tr.Count()) } var count int @@ -68,6 +68,15 @@ func TestRTree(t *testing.T) { return true }) + if count != 3 { + t.Fatalf("expecting %v, got %v", 3, count) + } + tr.Remove(zeroPoint) + count = 0 + tr.Search(&tRect{0, 0, 0, 100, 100, 5}, func(item Item) bool { + count++ + return true + }) if count != 2 { t.Fatalf("expecting %v, got %v", 2, count) } @@ -100,8 +109,8 @@ func TestInsertDelete(t *testing.T) { if p < .23 || p > .27 { t.Fatalf("bad random range, expected between 0.24-0.26, got %v", p) } - for _, r := range r2arr { - tr.Remove(r) + for _, i := range rand.Perm(len(r2arr)) { + tr.Remove(r2arr[i]) } total := tr.Count() + count if total != n { @@ -110,7 +119,7 @@ func TestInsertDelete(t *testing.T) { } func TestPoints(t *testing.T) { rand.Seed(time.Now().UnixNano()) - n := 100000 + n := 25000 tr := New() var points []*tPoint for i := 0; i < n; i++ { @@ -130,11 +139,31 @@ func TestPoints(t *testing.T) { if count != n { t.Fatalf("expecting %v, got %v", n, count) } - for _, p := range points { - tr.Remove(p) + for _, i := range rand.Perm(len(points)) { + tr.Remove(points[i]) } total := tr.Count() + count if total != n { t.Fatalf("expected %v, got %v", n, total) } } +func BenchmarkInsert(t *testing.B) { + t.StopTimer() + rand.Seed(time.Now().UnixNano()) + tr := New() + var points []*tPoint + for i := 0; i < t.N; i++ { + points = append(points, tRandPoint()) + } + t.StartTimer() + for i := 0; i < t.N; i++ { + tr.Insert(points[i]) + } + t.StopTimer() + count := tr.Count() + if count != t.N { + t.Fatalf("expected %v, got %v", t.N, count) + } + + t.StartTimer() +} diff --git a/vendor/d1/rtree.go b/vendor/d1/rtree.go index 125ea56..d747c7d 100644 --- a/vendor/d1/rtree.go +++ b/vendor/d1/rtree.go @@ -73,8 +73,7 @@ func init() { } type RTree struct { - root *Node ///< Root of tree - unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions + root *Node ///< Root of tree } /// Minimal bounding rectangle (n-dimensional) @@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) { } //_DEBUG var branch Branch branch.data = dataId - branch.child = nil for axis := 0; axis < NUMDIMS; axis++ { branch.rect.min[axis] = min[axis] branch.rect.max[axis] = max[axis] @@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) { // Remove element by swapping with the last element to prevent gaps in array node.branch[index] = node.branch[node.count-1] node.branch[node.count-1].data = nil + node.branch[node.count-1].child = nil node.count-- } diff --git a/vendor/d2/rtree.go b/vendor/d2/rtree.go index 35ad4ce..2f6434e 100644 --- a/vendor/d2/rtree.go +++ b/vendor/d2/rtree.go @@ -73,8 +73,7 @@ func init() { } type RTree struct { - root *Node ///< Root of tree - unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions + root *Node ///< Root of tree } /// Minimal bounding rectangle (n-dimensional) @@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) { } //_DEBUG var branch Branch branch.data = dataId - branch.child = nil for axis := 0; axis < NUMDIMS; axis++ { branch.rect.min[axis] = min[axis] branch.rect.max[axis] = max[axis] @@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) { // Remove element by swapping with the last element to prevent gaps in array node.branch[index] = node.branch[node.count-1] node.branch[node.count-1].data = nil + node.branch[node.count-1].child = nil node.count-- } diff --git a/vendor/d3/rtree.go b/vendor/d3/rtree.go index 007de07..dd11b70 100644 --- a/vendor/d3/rtree.go +++ b/vendor/d3/rtree.go @@ -73,8 +73,7 @@ func init() { } type RTree struct { - root *Node ///< Root of tree - unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions + root *Node ///< Root of tree } /// Minimal bounding rectangle (n-dimensional) @@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) { } //_DEBUG var branch Branch branch.data = dataId - branch.child = nil for axis := 0; axis < NUMDIMS; axis++ { branch.rect.min[axis] = min[axis] branch.rect.max[axis] = max[axis] @@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) { // Remove element by swapping with the last element to prevent gaps in array node.branch[index] = node.branch[node.count-1] node.branch[node.count-1].data = nil + node.branch[node.count-1].child = nil node.count-- } diff --git a/vendor/d4/rtree.go b/vendor/d4/rtree.go index 3e2fd6a..5449b29 100644 --- a/vendor/d4/rtree.go +++ b/vendor/d4/rtree.go @@ -73,8 +73,7 @@ func init() { } type RTree struct { - root *Node ///< Root of tree - unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions + root *Node ///< Root of tree } /// Minimal bounding rectangle (n-dimensional) @@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) { } //_DEBUG var branch Branch branch.data = dataId - branch.child = nil for axis := 0; axis < NUMDIMS; axis++ { branch.rect.min[axis] = min[axis] branch.rect.max[axis] = max[axis] @@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) { // Remove element by swapping with the last element to prevent gaps in array node.branch[index] = node.branch[node.count-1] node.branch[node.count-1].data = nil + node.branch[node.count-1].child = nil node.count-- }