-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree_test.go
More file actions
285 lines (237 loc) · 6.43 KB
/
quadtree_test.go
File metadata and controls
285 lines (237 loc) · 6.43 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package quadtree
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmptyTree(t *testing.T) {
qt := EmptyTree(0)
assert.Equal(t, uint(0), qt.Level)
qt = EmptyTree(qt.Level - 1)
assert.Equal(t, uint(0), qt.Level)
qt = EmptyTree(7)
treeCorrectness(t, qt)
}
func TestGrowToFit(t *testing.T) {
qt := EmptyTree(1)
qt = qt.GrowToFit(63, 63)
assert.Equal(t, uint(7), qt.Level)
treeCorrectness(t, qt)
}
func TestSetCellPanic(t *testing.T) {
qt := EmptyTree(1)
qt = qt.GrowToFit(3, 3)
assert.Panics(t, func() { qt.SetCell(8, 8, 1) })
}
func TestSetCell(t *testing.T) {
qt := EmptyTree(1)
for counter := 0; counter < 10; counter++ {
//x := dim(uint64(rand.Uint32())<<1 | uint64(rand.Uint32()))
//y := dim(uint64(rand.Uint32())<<1 | uint64(rand.Uint32()))
x := Dim((counter - 5) * 3)
y := Dim((counter - 5) * counter)
qt = qt.GrowToFit(x, y)
qt = qt.SetCell(x, y, 1)
assert.Equal(t, Dim(1), qt.Cell(x, y))
qt = qt.SetCell(x, y, 0)
assert.Equal(t, Dim(0), qt.Cell(x, y))
}
// check that not all cells get set
qt = qt.SetCell(1, 1, 1)
assert.Equal(t, Dim(0), qt.Cell(2, 2))
}
func TestCell(t *testing.T) {
qt := EmptyTree(1)
qt = qt.GrowToFit(55, 233)
assert.Equal(t, Dim(0), qt.Cell(55, 233))
qt = qt.SetCell(55, 233, 1)
assert.Equal(t, Dim(1), qt.Cell(55, 233))
treeCorrectness(t, qt)
}
func TestFindLifeCells(t *testing.T) {
qt := EmptyTree(1)
qt = qt.GrowToFit(55, 233)
qt = qt.SetCell(55, 232, 1)
qt = qt.SetCell(55, 233, 1)
qt.FindLifeCells(-(1 << (qt.Level - 1)), -(1 << (qt.Level - 1)), func(x, y Dim) { fmt.Println(x, y) })
}
func TestOneGen(t *testing.T) {
// dying overpopulation
var bitmask uint16 = 0xFFFF
assert.Equal(t, int64(0), oneGen(bitmask).Population)
// liveless
bitmask = 0x0000
assert.Equal(t, int64(0), oneGen(bitmask).Population)
// 3 live neighbours
// 0b0111 0000 0000
bitmask = 0x0700
assert.Equal(t, int64(1), oneGen(bitmask).Population)
// 2 live neighbours and self is live
// 0b0011 0010 0000
bitmask = 0x0320
assert.Equal(t, int64(1), oneGen(bitmask).Population)
// 1 live neighbours and self is live
// 0b0010 0010 0000
bitmask = 0x0220
assert.Equal(t, int64(0), oneGen(bitmask).Population)
// 3 live neighbours below
// 0b0000 0000 0111
bitmask = 0x0007
assert.Equal(t, int64(1), oneGen(bitmask).Population)
}
func TestCenteredSubnode(t *testing.T) {
qt := EmptyTree(3) //(-4,3)
qt.SetCell(1, 1, 1)
qt.SetCell(-1, -1, 1)
center := qt.centeredSubnode()
center = center.grow()
assert.Equal(t, qt, center)
}
func TestCenteredHorizontal(t *testing.T) {
w := EmptyTree(2)
e := EmptyTree(2)
w.SetCell(1, -1, 1)
e.SetCell(-2, 0, 1)
centerH := centeredHorizontal(w, e)
expect := backslashLevelOne()
assert.Equal(t, expect, centerH)
w = EmptyTree(2)
e = EmptyTree(2)
w.SetCell(1, -0, 1)
e.SetCell(-2, -1, 1)
centerH = centeredHorizontal(w, e)
expect = slashLevelOne()
assert.Equal(t, expect, centerH)
}
func TestCenteredVertical(t *testing.T) {
n := EmptyTree(2)
s := EmptyTree(2)
n.SetCell(-1, 1, 1)
s.SetCell(0, -2, 1)
centerV := centeredVertical(n, s)
expect := backslashLevelOne()
assert.Equal(t, expect, centerV)
n = EmptyTree(2)
s = EmptyTree(2)
n.SetCell(0, 1, 1)
s.SetCell(-1, -2, 1)
centerV = centeredVertical(n, s)
expect = slashLevelOne()
assert.Equal(t, expect, centerV)
}
func TestCenteredSubSubnode(t *testing.T) {
qt, _ := treeWithRandomPattern(1)
grown := qt.grow().grow()
centeredSubSubnode := grown.centeredSubSubnode()
assert.Equal(t, qt, centeredSubSubnode)
}
func TestSlowSimulation(t *testing.T) {
qt := EmptyTree(2)
// empty stays empty
emptyResult := qt.slowSimulation()
assert.Equal(t, EmptyTree(1), emptyResult)
// 1 | 1
// 0 | 1
qt = EmptyTree(2)
qt.SetCell(-1, -1, 1)
qt.SetCell(0, -1, 1)
qt.SetCell(0, 0, 1)
fullResult := qt.slowSimulation()
expect := EmptyTree(1)
expect.SetCell(0, 0, 1)
expect.SetCell(-1, 0, 1)
expect.SetCell(-1, -1, 1)
expect.SetCell(0, -1, 1)
assert.Equal(t, expect, fullResult)
// next genartion should be full as well
fullResult = fullResult.grow().slowSimulation()
assert.Equal(t, expect, fullResult)
// 1 | 1| 1| 1
// 1 | 1| 1| 1
// 1 | 1| 1| 1
// 1 | 1| 1| 1
qt = EmptyTree(2)
for x := Dim(-2); x < 2; x++ {
for y := Dim(-2); y < 2; y++ {
qt.SetCell(x, y, 1)
}
}
emptyResult2 := qt.slowSimulation()
assert.Equal(t, EmptyTree(1), emptyResult2)
}
// trivial case of empty tree
// more testing should happen on universe level
func TestNextGeneration(t *testing.T) {
qt := EmptyTree(4)
qt = qt.grow()
qtNext := qt.NextGeneration()
qtNext = qtNext.grow()
assert.Equal(t, qt, qtNext)
}
func TestString(t *testing.T) {
qt, _ := treeWithRandomPattern(3)
fmt.Sprint(qt)
}
/*
* Benchmarks
*/
var result Dim
func benchmarkAddAndReadCells(size Dim, b *testing.B) {
qt := EmptyTree(1)
qt = qt.GrowToFit(Dim(size), Dim(size))
//b.ResetTimer()
for n := 0; n < b.N; n++ {
qt.SetCell(2, 2, 1)
result = qt.Cell(2, 2)
}
}
func BenchmarkAddAndReadCells3(b *testing.B) { benchmarkAddAndReadCells(Dim(1)<<3, b) }
func BenchmarkAddAndReadCells16(b *testing.B) { benchmarkAddAndReadCells(Dim(1)<<16, b) }
func BenchmarkAddAndReadCells32(b *testing.B) { benchmarkAddAndReadCells(Dim(1)<<32, b) }
func benchmarkGrowToFit(size Dim, b *testing.B) {
for n := 0; n < b.N; n++ {
qt := EmptyTree(1)
qt = qt.GrowToFit(Dim(size), Dim(size))
}
}
func BenchmarkGrowToFit3(b *testing.B) { benchmarkGrowToFit(Dim(1)<<3, b) }
func BenchmarkGrowToFit8(b *testing.B) { benchmarkGrowToFit(Dim(1)<<8, b) }
func BenchmarkGrowToFit16(b *testing.B) { benchmarkGrowToFit(Dim(1)<<16, b) }
func BenchmarkGrowToFit32(b *testing.B) { benchmarkGrowToFit(Dim(1)<<32, b) }
/*
* Helper
*/
// treeCorrectness recursivly checks Level of each node and that leaf nodes have no childs
func treeCorrectness(t *testing.T, qt *Quadtree) {
if qt.Level == 0 {
for _, child := range qt.childs() {
assert.Nil(t, child, "Leafe nodes shouldn't have child nodes")
}
return
}
for _, child := range qt.childs() {
if child == nil {
continue
}
assert.Equal(t, qt.Level-1, child.Level)
treeCorrectness(t, child)
}
}
// slashLevelOne returns a level one tree with the following pattern
// 0 | 1
// 1 | 0
func slashLevelOne() (qt *Quadtree) {
qt = EmptyTree(1)
qt.SetCell(0, -1, 1)
qt.SetCell(-1, 0, 1)
return
}
// backslashLevelOne returns a level one tree with the following pattern
// 1 | 0
// 0 | 1
func backslashLevelOne() (qt *Quadtree) {
qt = EmptyTree(1)
qt.SetCell(0, 0, 1)
qt.SetCell(-1, -1, 1)
return
}