forked from EngoEngine/ecs
/
entity_test.go
284 lines (227 loc) · 5.72 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
package ecs
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
type MySystemOneEntity struct {
e *BasicEntity
c1 *MyComponent1
}
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 = 5
}
}
func (*MySystemOne) Remove(e BasicEntity) {}
func (sys *MySystemOne) Add(e *BasicEntity, c1 *MyComponent1) {
sys.entities = append(sys.entities, MySystemOneEntity{e, c1})
}
type MySystemOneTwoEntity struct {
e *BasicEntity
c1 *MyComponent1
c2 *MyComponent2
}
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 {
if e.c1 == nil {
return
}
if e.c2 == nil {
return
}
}
}
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})
}
type MyEntity1 struct {
BasicEntity
MyComponent1
}
type MyEntity2 struct {
BasicEntity
MyComponent2
}
type MyEntity12 struct {
BasicEntity
MyComponent1
MyComponent2
}
type MyComponent1 struct {
A, B int
}
type MyComponent2 struct {
C, D int
}
// 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])
}
// TestBulkCreateBasics checks that bulk creating using NewBasics doesn't have a data race
func TestBulkCreateBasics(t *testing.T) {
var e1, e2 []BasicEntity
entityChan := make(chan []BasicEntity)
cb := func(out chan<- []BasicEntity) {
out <- NewBasics(1000)
}
go cb(entityChan)
go cb(entityChan)
e1 = <-entityChan
e2 = <-entityChan
assert.Len(t, e1, 1000)
assert.Len(t, e2, 1000)
}
func BenchmarkIdiomatic(b *testing.B) {
preload := func() {}
setup := func(w *World) {
sys12 := &MySystemOneTwo{}
w.AddSystem(sys12)
e1 := MyEntity1{}
e1.BasicEntity = NewBasic()
sys12.Add(&e1.BasicEntity, &e1.MyComponent1, nil)
}
Bench(b, preload, setup)
}
func BenchmarkIdiomaticDouble(b *testing.B) {
preload := func() {}
setup := func(w *World) {
sys12 := &MySystemOneTwo{}
w.AddSystem(sys12)
e12 := MyEntity12{}
e12.BasicEntity = NewBasic()
sys12.Add(&e12.BasicEntity, &e12.MyComponent1, &e12.MyComponent2)
}
Bench(b, preload, setup)
}
// 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)) {
w := &World{}
preload()
setup(w)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w.Update(1 / 120) // 120 fps
}
}
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)
}
}