-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquadtree_test.go
161 lines (136 loc) · 3.13 KB
/
quadtree_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
package aoi
import (
"fmt"
"github.com/stretchr/testify/assert"
"math/rand"
"sync"
"testing"
"time"
)
func Test_FindQuadrant(t *testing.T) {
aoi := NewQuadTree(0, 0, 100)
tree := aoi.(*QuadTree)
tree.cutNode()
tests := []struct {
x, y float64
want int
}{
{
x: 49.9, y: 49.9, want: leftUp,
},
{
x: 50, y: 50, want: rightDown,
},
{
x: 49.9, y: 50, want: leftDown,
},
{
x: 50, y: 49.9, want: rightUp,
},
}
for _, tt := range tests {
d := tree.findSonQuadrant(tt.x, tt.y)
assert.Equal(t, tt.want, d)
}
// 再次分割
tree.Child[rightUp].cutNode()
tests2 := []struct {
x, y float64
want int
}{
{
x: 74.9, y: 24.9, want: leftUp,
},
{
x: 75, y: 25, want: rightDown,
},
{
x: 74.9, y: 25, want: leftDown,
},
{
x: 75, y: 24.9, want: rightUp,
},
}
for _, tt := range tests2 {
d := tree.Child[rightUp].findSonQuadrant(tt.x, tt.y)
assert.Equal(t, tt.want, d)
}
}
func Test_NeedCut(t *testing.T) {
aoi := NewQuadTree(0, 0, 100)
tree := aoi.(*QuadTree)
tree.maxCap = 2 // 超过两人节点分裂
assert.Equal(t, false, tree.needCut())
tree.Add(60.9, 24.9, "player1")
assert.Equal(t, false, tree.needCut())
tree.Add(25, 25, "player2")
assert.Equal(t, true, tree.needCut())
}
func TestNode_Search(t *testing.T) {
aoi := NewQuadTree(0, 0, 100)
tree := aoi.(*QuadTree)
tree.maxCap = 2 // 超过两人节点分裂
tree.radius = 5
tree.Add(60.9, 24.9, "player1")
tree.Add(25, 25, "player2")
// 查询player1附近
entities := tree.Search(60.9, 24.9)
assert.Equal(t, 2, len(entities), "player1 player2")
// 当出现第三个玩家超过节点最大容量产生分裂
tree.Add(99, 24, "player3")
// 查询player1附近
entities = tree.Search(60.9, 24.9)
assert.Equal(t, 2, len(entities), "player1 player3")
// 添加第四个玩家
tree.Add(72, 23, "player4")
// 查询player1附近
entities = tree.Search(60.9, 24.9)
assert.Equal(t, 2, len(entities), "player1 player4")
// 查询player2附近
entities = tree.Search(25, 25)
assert.Equal(t, 1, len(entities), "player2")
// 添加第五个玩家
tree.Add(49.9, 49.9, "player5")
// 查询player2附近
entities = tree.Search(25, 25)
assert.Equal(t, 2, len(entities), "player2 player5")
// 移除player5
tree.Delete(49.9, 49.9, "player5")
// 查询player2附近
entities = tree.Search(25, 25)
assert.Equal(t, 1, len(entities), "player2")
}
func BenchmarkQuadtree(b *testing.B) {
var wg sync.WaitGroup
aoi := NewQuadTree(0, 0, 1024)
tree := aoi.(*QuadTree)
rand.Seed(time.Now().UnixNano())
for i := 0; i < b.N; i++ {
wg.Add(30000)
for j := 0; j < 10000; j++ {
go func() {
tree.Add(
float64(rand.Intn(10)*10+rand.Intn(10)),
float64(rand.Intn(10)*10+rand.Intn(10)),
fmt.Sprintf("player%d", rand.Intn(100)),
)
wg.Done()
}()
go func() {
tree.Delete(
float64(rand.Intn(10)*10+rand.Intn(10)),
float64(rand.Intn(10)*10+rand.Intn(10)),
fmt.Sprintf("player%d", rand.Intn(100)),
)
wg.Done()
}()
go func() {
tree.Search(
float64(rand.Intn(10)*10+rand.Intn(10)),
float64(rand.Intn(10)*10+rand.Intn(10)),
)
wg.Done()
}()
}
}
}