-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_test.go
192 lines (143 loc) · 3.03 KB
/
tree_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
package quadtree
import (
"testing"
)
func TestAdd(t *testing.T) {
t.Parallel()
q := New[int](100, 100, 4)
if q.Size() != 0 {
t.Fatal("non-empty")
}
q.Add(10, 10, 5, 5, 1)
q.Add(15, 20, 10, 10, 2)
q.Add(40, 10, 4, 4, 3)
q.Add(90, 90, 5, 8, 4)
if q.Size() != 4 {
t.Fatal("empty")
}
var (
val int
ok bool
)
if val, ok = q.Get(9, 9, 11, 11); !ok {
t.Fatal("got nothing at (9, 9, 11, 11)")
}
if val != 1 {
t.Fatalf("unexpected value: %d", val)
}
if _, ok = q.Get(101, 101, 111, 111); ok {
t.Fatal("got something at (101, 101, 111, 111)")
}
if val, ok = q.Get(39, 9, 42, 12); !ok {
t.Fatal("got nothing at (39, 9, 42, 12)")
}
if val != 3 {
t.Fatalf("invalid value: %d", val)
}
}
func TestDel(t *testing.T) {
t.Parallel()
q := New[int](100, 100, 4)
q.Add(5, 5, 2, 2, 1)
q.Add(10, 10, 3, 3, 2)
q.Add(11, 11, 4, 4, 3)
q.Del(12, 12)
if s := q.Size(); s != 2 {
t.Fatalf("unexpected len = %d", s)
}
}
func TestForEach(t *testing.T) {
t.Parallel()
q := New[int](100, 100, 4)
q.Add(5, 5, 2, 3, 1)
q.Add(10, 10, 5, 5, 2)
q.Add(11, 11, 3, 3, 3)
q.Add(25, 25, 10, 10, 100)
var sum int
q.ForEach(1, 1, 20, 20, func(_, _, _, _ float64, val int) {
sum += val
})
const sumExpect = 6
if sum != sumExpect {
t.Fatalf("unexpected sum = %d", sum)
}
}
func TestMove(t *testing.T) {
t.Parallel()
q := New[int](100, 100, 4)
q.Add(5, 5, 10, 10, 1)
var (
val int
ok bool
)
if val, ok = q.Get(4, 4, 6, 6); !ok || val != 1 {
t.Fatal("step 1 unexpected")
}
if q.Move(55, 55, 0, 0) {
t.Fatal("unexpected move success")
}
if !q.Move(5, 5, 50, 50) {
t.Fatal("cannot move")
}
if val, ok = q.Get(45, 45, 55, 55); !ok || val != 1 {
t.Fatal("step 2 unexpected")
}
}
func TestKNearest(t *testing.T) {
t.Parallel()
q := New[int](50, 50, 4)
q.Add(1, 1, 2, 2, 1)
q.Add(5, 1, 2, 2, 2)
q.Add(1, 5, 2, 2, 3)
q.Add(10, 10, 2, 2, 10)
q.Add(1, 10, 2, 2, 10)
var sum int
q.KNearest(3, 3, 5, 3, func(_, _, _, _ float64, val int) {
sum += val
})
const sumExpect = 6
if sum != sumExpect {
t.Fatalf("unexpected sum = %d", sum)
}
}
func BenchmarkTree(b *testing.B) {
const (
benchmarkSide = 1000.0
maxSize = 10.0
)
q := New[int](benchmarkSide, benchmarkSide, nodeTestDepth)
x, y := 1.0, 1.0
x2, y2 := 800.0, 800.0
b.ResetTimer()
b.Run("Add", func(b *testing.B) {
for n := 0; n < b.N; n++ {
q.Add(x, y, maxSize, maxSize, n)
}
})
b.Run("Get", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = q.Get(x, y, maxSize, maxSize)
}
})
b.Run("Move", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = q.Move(x, y, x2, y2)
x, y, x2, y2 = x2, y2, x, y
}
})
b.Run("ForEach", func(b *testing.B) {
for n := 0; n < b.N; n++ {
q.ForEach(x, y, maxSize, maxSize, func(_, _, _, _ float64, val int) {})
}
})
b.Run("KNearest", func(b *testing.B) {
for n := 0; n < b.N; n++ {
q.KNearest(x, y, maxSize, int(maxSize), func(_, _, _, _ float64, val int) {})
}
})
b.Run("Del", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = q.Del(x, y)
}
})
}