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
|
package coffee.liz.ecs
import kotlin.reflect.KClass
import kotlin.test.*
// Test systems for circular dependency test
class CircularSystemA : System {
override val dependencies: Set<KClass<out System>>
get() = setOf(CircularSystemB::class)
override fun update(world: World, deltaTime: Float) {}
}
class CircularSystemB : System {
override val dependencies: Set<KClass<out System>>
get() = setOf(CircularSystemA::class)
override fun update(world: World, deltaTime: Float) {}
}
class DAGWorldTest {
@Test
fun `can create entities with unique ids`() {
val world = DAGWorld(emptyList())
val entity1 = world.createEntity()
val entity2 = world.createEntity()
assertNotEquals(entity1.id, entity2.id)
}
@Test
fun `can destroy entity`() {
val world = DAGWorld(emptyList())
val entity = world.createEntity()
entity.add(Position(10f, 20f))
world.removeEntity(entity)
val results = world.query<Position>()
assertFalse(results.contains(entity))
}
@Test
fun `query returns entities with matching components`() {
val world = DAGWorld(emptyList())
val entity1 = world.createEntity().add(Position(10f, 20f))
val entity2 = world.createEntity().add(Position(30f, 40f))
val entity3 = world.createEntity().add(Velocity(1f, 2f))
world.update(0f) // Trigger cache update
val results = world.query<Position>()
assertEquals(2, results.size)
assertTrue(results.contains(entity1))
assertTrue(results.contains(entity2))
assertFalse(results.contains(entity3))
}
@Test
fun `query with multiple components returns intersection`() {
val world = DAGWorld(emptyList())
val entity1 = world.createEntity()
.add(Position(10f, 20f))
.add(Velocity(1f, 2f))
val entity2 = world.createEntity()
.add(Position(30f, 40f))
val entity3 = world.createEntity()
.add(Velocity(3f, 4f))
world.update(0f) // Trigger cache update
val results = world.query<Position, Velocity>()
assertEquals(1, results.size)
assertTrue(results.contains(entity1))
}
@Test
fun `systems execute in dependency order`() {
val executionOrder = mutableListOf<String>()
val systemA = object : System {
override fun update(world: World, deltaTime: Float) {
executionOrder.add("A")
}
}
val systemB = object : System {
override val dependencies = setOf(systemA::class)
override fun update(world: World, deltaTime: Float) {
executionOrder.add("B")
}
}
val systemC = object : System {
override val dependencies = setOf(systemB::class)
override fun update(world: World, deltaTime: Float) {
executionOrder.add("C")
}
}
val world = DAGWorld(listOf(systemC, systemA, systemB))
world.update(0f)
assertEquals(listOf("A", "B", "C"), executionOrder)
}
@Test
fun `systems with no dependencies can execute in any order`() {
val executionOrder = mutableListOf<String>()
val systemA = object : System {
override fun update(world: World, deltaTime: Float) {
executionOrder.add("A")
}
}
val systemB = object : System {
override fun update(world: World, deltaTime: Float) {
executionOrder.add("B")
}
}
val world = DAGWorld(listOf(systemA, systemB))
world.update(0f)
assertEquals(2, executionOrder.size)
assertTrue(executionOrder.contains("A"))
assertTrue(executionOrder.contains("B"))
}
@Test
fun `circular dependency throws exception`() {
val systemA = CircularSystemA()
val systemB = CircularSystemB()
assertFailsWith<IllegalArgumentException> {
DAGWorld(listOf(systemA, systemB))
}
}
@Test
fun `system receives correct deltaTime`() {
var receivedDelta = 0f
val system = object : System {
override fun update(world: World, deltaTime: Float) {
receivedDelta = deltaTime
}
}
val world = DAGWorld(listOf(system))
world.update(0.016f)
assertEquals(0.016f, receivedDelta)
}
@Test
fun `systems can query entities during update`() {
var queriedEntities: Set<Entity>? = null
val system = object : System {
override fun update(world: World, deltaTime: Float) {
queriedEntities = world.query<Position>()
}
}
val world = DAGWorld(listOf(system))
val entity = world.createEntity().add(Position(10f, 20f))
world.update(0f)
assertNotNull(queriedEntities)
assertEquals(1, queriedEntities!!.size)
assertTrue(queriedEntities!!.contains(entity))
}
@Test
fun `component cache updates after entity changes`() {
val world = DAGWorld(emptyList())
val entity = world.createEntity()
world.update(0f)
assertEquals(0, world.query<Position>().size)
entity.add(Position(10f, 20f))
world.update(0f)
assertEquals(1, world.query<Position>().size)
entity.remove<Position>()
world.update(0f)
assertEquals(0, world.query<Position>().size)
}
@Test
fun `diamond dependency resolves correctly`() {
val executionOrder = mutableListOf<String>()
// A
// / \
// B C
// \ /
// D
val systemA = object : System {
override fun update(world: World, deltaTime: Float) {
executionOrder.add("A")
}
}
val systemB = object : System {
override val dependencies = setOf(systemA::class)
override fun update(world: World, deltaTime: Float) {
executionOrder.add("B")
}
}
val systemC = object : System {
override val dependencies = setOf(systemA::class)
override fun update(world: World, deltaTime: Float) {
executionOrder.add("C")
}
}
val systemD = object : System {
override val dependencies = setOf(systemB::class, systemC::class)
override fun update(world: World, deltaTime: Float) {
executionOrder.add("D")
}
}
val world = DAGWorld(listOf(systemD, systemC, systemB, systemA))
world.update(0f)
// A must come first, D must come last, B and C in between
assertEquals("A", executionOrder.first())
assertEquals("D", executionOrder.last())
assertTrue(executionOrder.indexOf("B") < executionOrder.indexOf("D"))
assertTrue(executionOrder.indexOf("C") < executionOrder.indexOf("D"))
}
}
|