-
Notifications
You must be signed in to change notification settings - Fork 24
/
world_test.go
329 lines (268 loc) · 7.49 KB
/
world_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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package donburi_test
import (
"testing"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
"github.com/yohamta/donburi/internal/storage"
)
type vec2f struct {
X float64
Y float64
}
type transformData struct {
Position vec2f
}
type velocityData struct {
Velocity vec2f
}
var (
transform = donburi.NewComponentType[transformData]()
velocity = donburi.NewComponentType[velocityData]()
tagA = donburi.NewTag()
tagB = donburi.NewTag()
)
func TestEntry(t *testing.T) {
world := donburi.NewWorld()
ent := world.Create(tagA, transform, velocity)
entry := world.Entry(ent)
if !entry.HasComponent(tagA) {
t.Fatalf("TagA should be in ent archetype")
}
}
func TestMutateComponent(t *testing.T) {
world := donburi.NewWorld()
a := world.Create(tagA, transform, velocity)
b := world.Create(tagB, transform, velocity)
c := world.Create(tagB, transform, velocity)
entryA := world.Entry(a)
donburi.Get[transformData](entryA, transform).Position.X = 10
donburi.Get[transformData](entryA, transform).Position.Y = 20
entryB := world.Entry(b)
donburi.Get[transformData](entryB, transform).Position.X = 30
donburi.Get[transformData](entryB, transform).Position.Y = 40
entryC := world.Entry(c)
tr := &transformData{Position: vec2f{40, 50}}
donburi.Add(entryC, transform, tr)
tests := []struct {
entry *donburi.Entry
expected *vec2f
}{
{entryA, &vec2f{10, 20}},
{entryB, &vec2f{30, 40}},
{entryC, &vec2f{40, 50}},
}
for _, tt := range tests {
tf := donburi.Get[transformData](tt.entry, transform)
if tf.Position.X != tt.expected.X {
t.Errorf("X should be %f, but %f", tt.expected.X, tf.Position.X)
}
if tf.Position.Y != tt.expected.Y {
t.Errorf("Y should be %f, but %f", tt.expected.Y, tf.Position.Y)
}
}
}
func TestArchetype(t *testing.T) {
world := donburi.NewWorld()
entity := world.Create(tagA, transform, velocity)
entry := world.Entry(entity)
if entry.HasComponent(tagA) == false {
t.Errorf("TagA should be in the archetype")
}
if entry.HasComponent(tagB) == true {
t.Errorf("TagB should not be in the archetype")
}
}
func TestAddComponent(t *testing.T) {
world := donburi.NewWorld()
entities := []storage.Entity{
world.Create(tagA, transform),
world.Create(tagA, transform),
world.Create(tagA, transform),
}
entry := world.Entry(entities[1])
archtype := entry.Archetype()
vd := &velocityData{Velocity: vec2f{10, 20}}
donburi.Add(entry, velocity, vd)
entry.AddComponent(tagB)
newArchtype := entry.Archetype()
if len(newArchtype.Layout().Components()) != 4 {
t.Errorf("New Archetype should have 4 components")
}
if len(archtype.Entities()) != 2 {
t.Errorf("Old archetype should have 2 entities")
}
if len(newArchtype.Entities()) != 1 {
t.Errorf("New archetype should have 1 entities")
}
}
func TestRemoveComponent(t *testing.T) {
world := donburi.NewWorld()
entities := []storage.Entity{
world.Create(tagA, transform, velocity),
world.Create(tagA, transform, velocity),
world.Create(tagA, transform, velocity),
}
entry := world.Entry(entities[1])
archtype := entry.Archetype()
entry.RemoveComponent(transform)
newArchtype := entry.Archetype()
if len(newArchtype.Layout().Components()) != 2 {
t.Errorf("Archetype should have 2 components")
}
if len(archtype.Entities()) != 2 {
t.Errorf("Old archetype should have 2 entities")
}
if len(newArchtype.Entities()) != 1 {
t.Errorf("New archetype should have 1 entities")
}
}
func TestDeleteEntity(t *testing.T) {
createWorldFunc := func() (donburi.World, []*donburi.Entry) {
world := donburi.NewWorld()
entities := []*donburi.Entry{
world.Entry(world.Create(tagA, transform, velocity)),
world.Entry(world.Create(tagA, transform, velocity)),
world.Entry(world.Create(tagA, transform, velocity)),
}
return world, entities
}
var tests = []struct {
DeleteIndex []int
expectedEntityCount int
}{
{[]int{0}, 2},
{[]int{0, 1, 2}, 0},
{[]int{2}, 2},
}
for _, tt := range tests {
world, entities := createWorldFunc()
for _, ent := range entities {
if !world.Valid(ent.Entity()) {
t.Errorf("Entity should be valid")
}
if !ent.Valid() {
t.Errorf("Entry should be valid")
}
}
for _, del := range tt.DeleteIndex {
world.Remove(entities[del].Entity())
if world.Valid(entities[del].Entity()) {
t.Errorf("Entity should be invalid")
}
if entities[del].Valid() {
t.Errorf("Entry should be invalid")
}
}
if world.Len() != tt.expectedEntityCount {
t.Errorf("World should have %d entities", tt.expectedEntityCount)
}
}
}
func TestArchetypeStorageExpands(t *testing.T) {
world := donburi.NewWorld()
dummy := donburi.NewTag()
entry := world.Entry(world.Create(dummy))
const N = 256
for i := 0; i < N; i++ {
tag := donburi.NewTag()
entry.AddComponent(tag)
}
q := donburi.NewQuery(filter.Contains(dummy))
e, ok := q.First(world)
if !ok {
t.Fatalf("Entity should be found")
}
if len(e.Archetype().Layout().Components()) != N+1 {
t.Fatalf("Archetype should have 301 components")
}
}
func TestRemoveAndCreateEntity(t *testing.T) {
world := donburi.NewWorld()
var calledCreate, calledRemove bool
if calledCreate || calledRemove {
t.Fatalf("OnCreate/OnRemove event should not have been called at this point")
}
world.OnCreate(func(world donburi.World, entity donburi.Entity) {
calledCreate = true
})
world.OnRemove(func(world donburi.World, entity donburi.Entity) {
calledRemove = true
})
entityA := world.Create(tagA)
if !calledCreate {
t.Fatalf("OnCreate event must have been called at this point")
}
world.Remove(entityA)
if !calledRemove {
t.Fatalf("OnCreate event must have been called at this point")
}
if world.Valid(entityA) {
t.Errorf("Entity should be invalid")
}
entityB := world.Create(tagA)
query := donburi.NewQuery(filter.Contains(tagA))
entry, ok := query.First(world)
if !ok {
t.Fatalf("Entity should be found")
}
if entry.Entity() != entityB {
t.Errorf("Entity should be %d, but %d", entityB, entry.Entity())
}
if !entry.HasComponent(tagA) {
t.Errorf("TagA should be in the archetype")
}
}
type archeTest struct {
name string
archetype *storage.Archetype
expectedCount int
}
func TestCreateEntityAndExtend(t *testing.T) {
testFunc := func(tests []archeTest) {
t.Helper()
for _, tt := range tests {
if len(tt.archetype.Entities()) != tt.expectedCount {
t.Errorf("%s archetype should have %d entities", tt.name, tt.expectedCount)
}
}
}
world := donburi.NewWorld()
entity := world.Create(velocity)
entry := world.Entry(entity)
oldArchtype := entry.Archetype()
testFunc([]archeTest{
{"old", oldArchtype, 1},
})
// Add new component
donburi.Add(entry, transform, &transformData{})
newArchtype := entry.Archetype()
testFunc([]archeTest{
{"old", oldArchtype, 0},
{"new", newArchtype, 1},
})
// Create another entity
anotherEntity := world.Create(velocity)
anotherEntry := world.Entry(anotherEntity)
testFunc([]archeTest{
{"old", oldArchtype, 1},
{"new", newArchtype, 1},
})
donburi.Add(anotherEntry, transform, &transformData{})
testFunc([]archeTest{
{"old", oldArchtype, 0},
{"new", newArchtype, 2},
})
}
func TestComponentDefaultVal(t *testing.T) {
type ComponentData struct {
Val int
}
defVal := ComponentData{Val: 10}
component := donburi.NewComponentType[ComponentData](defVal)
world := donburi.NewWorld()
entry := world.Entry(world.Create(component))
val := donburi.Get[ComponentData](entry, component)
if val.Val != defVal.Val {
t.Errorf("Default value should be %d, but %d", defVal.Val, val.Val)
}
}