-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathquery_test.go
220 lines (185 loc) · 5.24 KB
/
query_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
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
package donburi_test
import (
"testing"
"time"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
)
type orderableComponentTest struct {
time.Time
}
func (o orderableComponentTest) Order() int {
return int(time.Since(o.Time).Milliseconds())
}
var (
queryTagA = donburi.NewTag()
queryTagB = donburi.NewTag()
queryTagC = donburi.NewTag()
orderableTest = donburi.NewComponentType[orderableComponentTest]()
)
func TestQueryInQuery(t *testing.T) {
world := donburi.NewWorld()
world.Create(queryTagA)
world.Create(queryTagC)
world.Create(queryTagA, queryTagB)
world.Create(queryTagA, queryTagB, queryTagC)
query := donburi.NewQuery(filter.Contains(queryTagA, queryTagB))
count := 0
for entry := range query.Iter(world) {
count++
if entry.Archetype().Layout().HasComponent(queryTagA) == false {
t.Errorf("PlayerTag should be in ent archetype")
}
innerQuery := donburi.NewQuery(filter.Contains(queryTagA, queryTagB, queryTagC))
defer func() {
if r := recover(); r != nil {
t.Errorf("panic should not happen")
}
}()
for innerEntry := range innerQuery.Iter(world) {
if innerEntry.Archetype().Layout().HasComponent(queryTagA) == false {
t.Errorf("PlayerTag should be in ent archetype")
}
}
}
}
func TestQuery(t *testing.T) {
world := donburi.NewWorld()
world.Create(queryTagA)
world.Create(queryTagC)
world.Create(queryTagA, queryTagB)
query := donburi.NewQuery(filter.Contains(queryTagA))
count := 0
for entry := range query.Iter(world) {
count++
if entry.Archetype().Layout().HasComponent(queryTagA) == false {
t.Errorf("PlayerTag should be in ent archetype")
}
}
if count != 2 {
t.Errorf("counter should be 2, but got %d", count)
}
}
var _ donburi.IOrderable = orderableData{}
type orderableData struct {
Index int
}
func (o orderableData) Order() int {
return o.Index
}
var orderable = donburi.NewComponentType[orderableData]()
func TestOrderedQuery(t *testing.T) {
orderedEntitiesQuery := donburi.NewOrderedQuery[orderableData](
filter.Contains(orderable),
)
world := donburi.NewWorld()
for _, i := range []int{3, 1, 2} {
e := world.Create(orderable)
entr := world.Entry(e)
donburi.SetValue(entr, orderable, orderableData{i})
}
var i int
for e := range orderedEntitiesQuery.IterOrdered(world, orderable) {
o := orderable.GetValue(e)
i += 1
if o.Index != i {
t.Errorf("expected %d, but got %d", i, o.Index)
}
}
}
func BenchmarkQuery_EachOrdered(b *testing.B) {
world := donburi.NewWorld()
for i := 0; i < 30000; i++ {
e := world.Create(orderableTest)
entr := world.Entry(e)
donburi.SetValue(entr, orderableTest, orderableComponentTest{time.Now()})
}
query := donburi.NewQuery(filter.Contains(orderableTest))
orderedQuery := donburi.NewOrderedQuery[orderableComponentTest](filter.Contains(orderableTest))
countNormal := 0
countOrdered := 0
b.Run("Each", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for range query.Iter(world) {
countNormal++
}
}
})
b.Run("EachOrdered", func(b *testing.B) {
for i := 0; i < b.N; i++ {
orderedQuery.EachOrdered(world, orderableTest, func(entry *donburi.Entry) {
countOrdered++
})
}
})
}
func BenchmarkQuery_OnlyEachOrdered(b *testing.B) {
world := donburi.NewWorld()
for i := 0; i < 30000; i++ {
e := world.Create(orderableTest)
entr := world.Entry(e)
donburi.SetValue(entr, orderableTest, orderableComponentTest{time.Now()})
}
orderedQuery := donburi.NewOrderedQuery[orderableComponentTest](filter.Contains(orderableTest))
countOrdered := 0
b.Run("EachOrdered", func(b *testing.B) {
for i := 0; i < b.N; i++ {
orderedQuery.EachOrdered(world, orderableTest, func(entry *donburi.Entry) {
countOrdered++
})
}
})
}
func TestQueryMultipleComponent(t *testing.T) {
world := donburi.NewWorld()
world.Create(queryTagA)
world.Create(queryTagC)
world.Create(queryTagA, queryTagB)
query := donburi.NewQuery(filter.Contains(queryTagA, queryTagB))
count := query.Count(world)
if count != 1 {
t.Errorf("counter should be 1, but got %d", count)
}
}
func TestComplexQuery(t *testing.T) {
createWorldFunc := func() donburi.World {
world := donburi.NewWorld()
world.Create(queryTagA)
world.Create(queryTagC)
world.Create(queryTagA, queryTagB)
return world
}
var tests = []struct {
filter filter.LayoutFilter
expectedCount int
}{
{filter.Not(filter.Contains(queryTagA)), 1},
{filter.And(filter.Contains(queryTagA), filter.Not(filter.Contains(queryTagB))), 1},
{filter.Or(filter.Contains(queryTagA), filter.Contains(queryTagC)), 3},
}
for _, tt := range tests {
world := createWorldFunc()
query := donburi.NewQuery(tt.filter)
count := query.Count(world)
if count != tt.expectedCount {
t.Errorf("counter should be %d, but got %d", tt.expectedCount, count)
}
}
}
func TestFirstEntity(t *testing.T) {
world := donburi.NewWorld()
world.Create(queryTagA)
world.Create(queryTagC)
world.Create(queryTagA, queryTagB)
// find first entity withqueryTagC
query := donburi.NewQuery(filter.Contains(queryTagC))
entry, ok := query.First(world)
if entry == nil || ok == false {
t.Errorf("entry with queryTagC should not be nil")
}
entry.Remove()
entry, ok = query.First(world)
if entry != nil || ok {
t.Errorf("entry with queryTagC should be nil")
}
}