-
Notifications
You must be signed in to change notification settings - Fork 43
/
entity_test.go
624 lines (524 loc) · 14.3 KB
/
entity_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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package ecs
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
type MySystemOneEntity struct {
e *BasicEntity
c1 *MyComponent1
}
type MySystemOneable interface {
BasicFace
MyComponent1Face
}
type MySystemOne struct {
entities []MySystemOneEntity
}
func (*MySystemOne) Priority() int { return 0 }
func (*MySystemOne) New(*World) {}
func (sys *MySystemOne) Update(dt float32) {
for _, e := range sys.entities {
e.c1.A++
}
}
func (*MySystemOne) Remove(e BasicEntity) {}
func (sys *MySystemOne) Add(e *BasicEntity, c1 *MyComponent1) {
sys.entities = append(sys.entities, MySystemOneEntity{e, c1})
}
func (sys *MySystemOne) AddByInterface(o Identifier) {
obj := o.(MySystemOneable)
sys.Add(obj.GetBasicEntity(), obj.GetMyComponent1())
}
type MySystemOneTwoEntity struct {
e *BasicEntity
c1 *MyComponent1
c2 *MyComponent2
}
type MySystemOneTwoable interface {
BasicFace
MyComponent1Face
MyComponent2Face
}
type NotMySystemOneTwoable interface {
NotMyComponent12Face
}
type MySystemOneTwo struct {
entities []MySystemOneTwoEntity
}
func (*MySystemOneTwo) Priority() int { return 0 }
func (*MySystemOneTwo) New(*World) {}
func (sys *MySystemOneTwo) Update(dt float32) {
for _, e := range sys.entities {
e.c1.B++
e.c2.D++
}
}
func (sys *MySystemOneTwo) Remove(e BasicEntity) {
delete := -1
for index, entity := range sys.entities {
if entity.e.ID() == e.ID() {
delete = index
}
}
if delete >= 0 {
sys.entities = append(sys.entities[:delete], sys.entities[delete+1:]...)
}
}
func (sys *MySystemOneTwo) Add(e *BasicEntity, c1 *MyComponent1, c2 *MyComponent2) {
sys.entities = append(sys.entities, MySystemOneTwoEntity{e, c1, c2})
}
func (sys *MySystemOneTwo) AddByInterface(o Identifier) {
obj := o.(MySystemOneTwoable)
sys.Add(obj.GetBasicEntity(), obj.GetMyComponent1(), obj.GetMyComponent2())
}
type MySystemTwoEntity struct {
e *BasicEntity
c2 *MyComponent2
}
type MySystemTwoable interface {
BasicFace
MyComponent2Face
}
type NotMySystemTwoable interface {
NotMyComponent2Face
}
type MySystemTwo struct {
entities []MySystemTwoEntity
}
func (*MySystemTwo) Priority() int { return 0 }
func (*MySystemTwo) New(*World) {}
func (sys *MySystemTwo) Update(dt float32) {
for _, e := range sys.entities {
e.c2.C++
}
}
func (sys *MySystemTwo) Remove(e BasicEntity) {
delete := -1
for index, entity := range sys.entities {
if entity.e.ID() == e.ID() {
delete = index
}
}
if delete >= 0 {
sys.entities = append(sys.entities[:delete], sys.entities[delete+1:]...)
}
}
func (sys *MySystemTwo) Add(e *BasicEntity, c2 *MyComponent2) {
sys.entities = append(sys.entities, MySystemTwoEntity{e, c2})
}
func (sys *MySystemTwo) AddByInterface(o Identifier) {
obj := o.(MySystemTwoable)
sys.Add(obj.GetBasicEntity(), obj.GetMyComponent2())
}
type MyEntity1 struct {
BasicEntity
MyComponent1
}
type MyEntity2 struct {
BasicEntity
MyComponent2
}
type MyEntity12 struct {
BasicEntity
MyComponent1
MyComponent2
}
type MyComponent1 struct {
A, B int
}
type MyComponent1Face interface {
GetMyComponent1() *MyComponent1
}
func (c *MyComponent1) GetMyComponent1() *MyComponent1 {
return c
}
type MyComponent2 struct {
C, D int
}
type MyComponent2Face interface {
GetMyComponent2() *MyComponent2
}
func (c *MyComponent2) GetMyComponent2() *MyComponent2 {
return c
}
type NotMyComponent2 struct{}
type NotMyComponent2Face interface {
GetNotMyComponent2() *NotMyComponent2
}
func (n *NotMyComponent2) GetNotMyComponent2() *NotMyComponent2 {
return n
}
type NotMyComponent12 struct{}
type NotMyComponent12Face interface {
GetNotMyComponent12() *NotMyComponent12
}
func (n *NotMyComponent12) GetNotMyComponent12() *NotMyComponent12 {
return n
}
// TestCreateEntity ensures IDs which are created, are unique
func TestCreateEntity(t *testing.T) {
e1 := MyEntity1{}
e1.BasicEntity = NewBasic()
e2 := MyEntity1{}
e2.BasicEntity = NewBasic()
assert.NotEqual(t, e1.id, e2.id, "BasicEntity IDs should be unique")
}
// TestChangeableComponents ensures that Components which are being referenced, are changeable
func TestChangeableComponents(t *testing.T) {
w := &World{}
sys1 := &MySystemOne{}
w.AddSystem(sys1)
e1 := MyEntity1{}
e1.BasicEntity = NewBasic()
sys1.Add(&e1.BasicEntity, &e1.MyComponent1)
sys1.Update(0.125)
assert.NotZero(t, e1.MyComponent1.A, "MySystemOne should have been able to change the value of MyComponent1.A")
}
// TestDelete tests a commonly used method for removing an entity from the list of entities
func TestDelete(t *testing.T) {
const maxEntities = 10
for j := 1; j < maxEntities; j++ {
w := &World{}
sys12 := &MySystemOneTwo{}
w.AddSystem(sys12)
var entities []BasicEntity
// Add all of them
for i := 0; i < maxEntities; i++ {
e := MyEntity12{BasicEntity: NewBasic()}
sys12.Add(&e.BasicEntity, &e.MyComponent1, &e.MyComponent2)
entities = append(entities, e.BasicEntity) // in order to remove it without having a reference to e
}
before := len(sys12.entities)
// Attempt to remove j
sys12.Remove(entities[j])
assert.Len(t, sys12.entities, before-1, "MySystemOne should now have exactly one less Entity")
}
}
// TestIdentifierInterface makes sure that my entity can be stored as an Identifier interface
func TestIdentifierInterface(t *testing.T) {
e1 := MyEntity1{}
e1.BasicEntity = NewBasic()
var slice []Identifier = []Identifier{e1}
_, ok := slice[0].(MyEntity1)
assert.True(t, ok, "MyEntity1 should have been recoverable from the Identifier interface")
}
func TestSortableIdentifierSlice(t *testing.T) {
e1 := MyEntity1{}
e1.BasicEntity = NewBasic()
e2 := MyEntity1{}
e2.BasicEntity = NewBasic()
var entities IdentifierSlice = []Identifier{e2, e1}
sort.Sort(entities)
assert.ObjectsAreEqual(e1, entities[0])
assert.ObjectsAreEqual(e2, entities[1])
}
// TestSystemEntityFiltering checks that entities go into the right systems and the flags are obeyed
func TestSystemEntityFiltering(t *testing.T) {
w := &World{}
var sys1in *MySystemOneable
w.AddSystemInterface(&MySystemOne{}, sys1in, nil)
var sys2in *MySystemTwoable
var sys2out *NotMySystemTwoable
w.AddSystemInterface(&MySystemTwo{}, sys2in, sys2out)
var sys12in *MySystemOneTwoable
var sys12out *NotMySystemOneTwoable
w.AddSystemInterface(&MySystemOneTwo{}, sys12in, sys12out)
e1 := struct {
BasicEntity
*MyComponent1
}{
NewBasic(),
&MyComponent1{},
}
w.AddEntity(&e1)
e2 := struct {
BasicEntity
*MyComponent2
}{
NewBasic(),
&MyComponent2{},
}
w.AddEntity(&e2)
e12 := struct {
BasicEntity
*MyComponent1
*MyComponent2
}{
NewBasic(),
&MyComponent1{},
&MyComponent2{},
}
w.AddEntity(&e12)
e12x2 := struct {
BasicEntity
*MyComponent1
*MyComponent2
*NotMyComponent2
}{
NewBasic(),
&MyComponent1{},
&MyComponent2{},
&NotMyComponent2{},
}
w.AddEntity(&e12x2)
e12x12 := struct {
BasicEntity
*MyComponent1
*MyComponent2
*NotMyComponent12
}{
NewBasic(),
&MyComponent1{},
&MyComponent2{},
&NotMyComponent12{},
}
w.AddEntity(&e12x12)
e12x12x2 := struct {
BasicEntity
*MyComponent1
*MyComponent2
*NotMyComponent12
*NotMyComponent2
}{
NewBasic(),
&MyComponent1{},
&MyComponent2{},
&NotMyComponent12{},
&NotMyComponent2{},
}
w.AddEntity(&e12x12x2)
w.Update(0.125)
assert.Equal(t, 1, e1.A, "e1 was not updated by system 1")
assert.Equal(t, 0, e1.B, "e1 was updated by system 12")
assert.Equal(t, 1, e2.C, "e2 was not updated by system 2")
assert.Equal(t, 0, e2.D, "e2 was updated by system 12")
assert.Equal(t, 1, e12.A, "e12 was not updated by system 1")
assert.Equal(t, 1, e12.B, "e12 was not updated by system 12")
assert.Equal(t, 1, e12.C, "e12 was not updated by system 2")
assert.Equal(t, 1, e12.D, "e12 was not updated by system 12")
assert.Equal(t, 1, e12x2.A, "e12x2 was not updated by system 1")
assert.Equal(t, 1, e12x2.B, "e12x2 was not updated by system 12")
assert.Equal(t, 0, e12x2.C, "e12x2 was updated by system 2")
assert.Equal(t, 1, e12x2.D, "e12x2 was not updated by system 12")
assert.Equal(t, 1, e12x12.A, "e12x12 was not updated by system 1")
assert.Equal(t, 0, e12x12.B, "e12x12 was updated by system 12")
assert.Equal(t, 1, e12x12.C, "e12x12 was not updated by system 2")
assert.Equal(t, 0, e12x12.D, "e12x12 was updated by system 12")
assert.Equal(t, 1, e12x12x2.A, "e12x12x2 was not updated by system 1")
assert.Equal(t, 0, e12x12x2.B, "e12x12x2 was updated by system 12")
assert.Equal(t, 0, e12x12x2.C, "e12x12x2 was updated by system 2")
assert.Equal(t, 0, e12x12x2.D, "e12x12x2 was updated by system 12")
}
// TestParentChild tests parenting of BasicEntities
func TestParentChild(t *testing.T) {
parent := NewBasic()
children := NewBasics(3)
if len(parent.Children()) != 0 {
t.Errorf("Children did not initalize to a zero value")
}
if parent.Parent() != nil {
t.Errorf("Parent did not initalize as nil")
}
parent.AppendChild(&children[0])
parent.AppendChild(&children[1])
parent.AppendChild(&children[2])
if len(parent.Children()) != 3 {
t.Errorf("Failed to add all three children to parent")
}
for i := 0; i < 3; i++ {
if children[i].Parent() != &parent {
t.Errorf("Parent was not updated properly for children.")
}
}
}
// TestRemoveChild tests removing a child
func TestRemoveChild(t *testing.T) {
parent := NewBasic()
children := NewBasics(3)
parent.AppendChild(&children[0])
parent.AppendChild(&children[1])
parent.AppendChild(&children[2])
if len(parent.Children()) != 3 {
t.Errorf("Parent did not successfully add all three children.")
}
parent.RemoveChild(&children[1])
if len(parent.Children()) != 2 {
t.Errorf("Parent didd not successfully remove a child")
}
for i := 0; i < 2; i++ {
if children[1].ID() == parent.Children()[i].ID() {
t.Errorf("Found removed child in parent still")
}
}
}
func TestDescendents(t *testing.T) {
parent := NewBasic()
children := NewBasics(7)
parent.AppendChild(&children[0])
parent.AppendChild(&children[1])
parent.AppendChild(&children[2])
children[0].AppendChild(&children[3])
children[0].AppendChild(&children[4])
children[1].AppendChild(&children[5])
children[3].AppendChild(&children[6])
if len(parent.Descendents()) != 7 {
t.Errorf("Parent did not have all descendents.")
}
testMap := map[uint64]struct{}{}
testMap[children[0].ID()] = struct{}{}
testMap[children[1].ID()] = struct{}{}
testMap[children[2].ID()] = struct{}{}
testMap[children[3].ID()] = struct{}{}
testMap[children[4].ID()] = struct{}{}
testMap[children[5].ID()] = struct{}{}
testMap[children[6].ID()] = struct{}{}
for _, d := range parent.Descendents() {
delete(testMap, d.ID())
}
if len(testMap) != 0 {
t.Errorf("Expected children not found in parent. Did not find %v", testMap)
}
}
func BenchmarkIdiomatic(b *testing.B) {
preload := func() {}
setup := func(w *World) {
sys12 := &MySystemOneTwo{}
w.AddSystem(sys12)
e12 := MyEntity12{BasicEntity: NewBasic()}
sys12.Add(&e12.BasicEntity, &e12.MyComponent1, &e12.MyComponent2)
}
Bench(b, preload, setup, false, false, true)
}
func Benchmark10Systems10Entities(b *testing.B) {
BenchMultiples(b, 10, 10, false, false, true)
}
func Benchmark10Systems100Entities(b *testing.B) {
BenchMultiples(b, 10, 100, false, false, true)
}
func Benchmark10Systems1000Entities(b *testing.B) {
BenchMultiples(b, 10, 1000, false, false, true)
}
func Benchmark10Systems10000Entities(b *testing.B) {
BenchMultiples(b, 10, 10000, false, false, true)
}
func Benchmark100Systems10Entities(b *testing.B) {
BenchMultiples(b, 100, 10, false, false, true)
}
func Benchmark100Systems100Entities(b *testing.B) {
BenchMultiples(b, 100, 100, false, false, true)
}
func Benchmark100Systems1000Entities(b *testing.B) {
BenchMultiples(b, 100, 1000, false, false, true)
}
func Benchmark100Systems10000Entities(b *testing.B) {
BenchMultiples(b, 100, 10000, false, false, true)
}
func BenchmarkAddUpdate(b *testing.B) {
BenchMultiples(b, 100, 10000, true, false, true)
}
func BenchmarkAddInterfaceUpdate(b *testing.B) {
BenchMultiples(b, 100, 10000, true, true, false)
}
func BenchmarkRemoveUpdate(b *testing.B) {
BenchMultiples(b, 100, 10000, false, true, true)
}
func BenchmarkAddRemoveUpdate(b *testing.B) {
BenchMultiples(b, 100, 10000, true, true, true)
}
func BenchmarkAddInterfaceRemoveUpdate(b *testing.B) {
BenchMultiples(b, 100, 10000, true, true, false)
}
func BenchMultiples(b *testing.B, sysCount, entCount int, addDuringUpdate, removeDuringUpdate, addIdiomatic bool) {
preload := func() {}
setup := func(w *World) {
for i := 0; i < sysCount; i++ {
sys12 := &MySystemOneTwo{}
if addIdiomatic {
w.AddSystem(sys12)
} else {
var able *MySystemOneTwoable
var notable *NotMySystemOneTwoable
w.AddSystemInterface(sys12, able, notable)
}
}
for i := 0; i < entCount; i++ {
e12 := MyEntity12{BasicEntity: NewBasic()}
w.AddEntity(e12)
}
}
Bench(b, preload, setup, addDuringUpdate, removeDuringUpdate, addIdiomatic)
}
// Bench is a helper-function to easily benchmark one frame, given a preload / setup function
func Bench(b *testing.B, preload func(), setup func(w *World), addDuringUpdate, removeDuringUpdate, addIdiomatic bool) {
w := &World{}
preload()
setup(w)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w.Update(1 / 120) // 120 fps
e12 := MyEntity12{BasicEntity: NewBasic()}
if addDuringUpdate {
if addIdiomatic {
for _, system := range w.Systems() {
switch sys := system.(type) {
case *MySystemOneTwo:
sys.Add(e12.GetBasicEntity(), e12.GetMyComponent1(), e12.GetMyComponent2())
}
}
} else {
w.AddEntity(&e12)
}
}
if removeDuringUpdate {
w.RemoveEntity(e12.BasicEntity)
}
}
}
func BenchmarkNewBasic(b *testing.B) {
for i := 0; i < b.N; i++ {
NewBasic()
}
}
func BenchmarkNewBasics1(b *testing.B) {
for i := 0; i < b.N; i++ {
NewBasics(1)
}
}
func BenchmarkNewBasic10(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
NewBasic()
}
}
}
func BenchmarkNewBasics10(b *testing.B) {
for i := 0; i < b.N; i++ {
NewBasics(10)
}
}
func BenchmarkNewBasic100(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < 100; j++ {
NewBasic()
}
}
}
func BenchmarkNewBasics100(b *testing.B) {
for i := 0; i < b.N; i++ {
NewBasics(100)
}
}
func BenchmarkNewBasic1000(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
NewBasic()
}
}
}
func BenchmarkNewBasics1000(b *testing.B) {
for i := 0; i < b.N; i++ {
NewBasics(1000)
}
}