-
Notifications
You must be signed in to change notification settings - Fork 8
/
kdtree_test.go
129 lines (113 loc) · 3.22 KB
/
kdtree_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
package bkdtree
import (
"testing"
)
func TestKdIntersectSome(t *testing.T) {
numDims := 3
maxVal := uint64(1000)
size := 1000
leafCap := 50
intraCap := 4
points := NewRandPoints(numDims, maxVal, size)
kdt := NewKdTree(points, numDims, leafCap, intraCap)
lowPoint := points[0]
highPoint := points[0]
visitor := &IntersectCollector{lowPoint, highPoint, make([]Point, 0, 1)}
kdt.Intersect(visitor)
//fmt.Printf("%v\n", visitor.points)
if len(visitor.Points) <= 0 {
t.Errorf("found 0 matchs, however some expected")
}
for _, point := range visitor.Points {
isInside := point.Inside(lowPoint, highPoint)
if !isInside {
t.Errorf("point %v is ouside of range", point)
}
}
}
func TestKdIntersectAll(t *testing.T) {
numDims := 3
maxVal := uint64(1000)
size := 1000
leafCap := 50
intraCap := 4
points := NewRandPoints(numDims, maxVal, size)
kdt := NewKdTree(points, numDims, leafCap, intraCap)
lowPoint := Point{[]uint64{0, 0, 0}, 0}
highPoint := Point{[]uint64{maxVal, maxVal, maxVal}, 0}
visitor := &IntersectCollector{lowPoint, highPoint, make([]Point, 0, size)}
kdt.Intersect(visitor)
if len(visitor.Points) != size {
t.Errorf("found %v matchs, however %v expected", len(visitor.Points), size)
}
}
func TestKdIntersect(t *testing.T) {
numDims := 3
maxVal := uint64(1000)
size := 100000
leafCap := 50
intraCap := 4
points := NewRandPoints(numDims, maxVal, size)
kdt := NewKdTree(points, numDims, leafCap, intraCap)
lowPoint := Point{[]uint64{20, 30, 40}, 0}
highPoint := Point{[]uint64{maxVal, maxVal, maxVal}, 0}
visitor := &IntersectCollector{lowPoint, highPoint, make([]Point, 0)}
kdt.Intersect(visitor)
//fmt.Printf("%v\n", visitor.points)
for _, point := range visitor.Points {
isInside := point.Inside(lowPoint, highPoint)
if !isInside {
t.Errorf("point %v is ouside of range", point)
}
}
}
func TestKdInsert(t *testing.T) {
numDims := 3
maxVal := uint64(1000)
size := 1000
leafCap := 50
intraCap := 4
points := NewRandPoints(numDims, maxVal, size)
kdt := NewKdTree(points, numDims, leafCap, intraCap)
newPoint := Point{[]uint64{40, 30, 20}, maxVal} //use unique userData
kdt.Insert(newPoint)
lowPoint := newPoint
highPoint := newPoint
visitor := &IntersectCollector{lowPoint, highPoint, make([]Point, 0)}
kdt.Intersect(visitor)
//fmt.Printf("%v\n", visitor.points)
if len(visitor.Points) <= 0 {
t.Errorf("found 0 matchs, however some expected")
}
numMatchs := 0
for _, point := range visitor.Points {
isInside := point.Inside(lowPoint, highPoint)
if !isInside {
t.Errorf("point %v is ouside of range", point)
}
if point.UserData == newPoint.UserData {
numMatchs++
}
}
if numMatchs != 1 {
t.Errorf("found %v matchs, however 1 expected", numMatchs)
}
}
func TestKdErase(t *testing.T) {
numDims := 3
maxVal := uint64(1000)
size := 1000
leafCap := 50
intraCap := 4
points := NewRandPoints(numDims, maxVal, size)
kdt := NewKdTree(points, numDims, leafCap, intraCap)
kdt.Erase(points[0])
lowPoint := points[0]
highPoint := points[0]
visitor := &IntersectCollector{lowPoint, highPoint, make([]Point, 0)}
kdt.Intersect(visitor)
//fmt.Printf("%v\n", visitor.points)
if len(visitor.Points) != 0 {
t.Errorf("found %v matchs, however 0 expected", len(visitor.Points))
}
}