summaryrefslogtreecommitdiff
path: root/honey/ecs/ecs.test.lua
blob: 5d0b89ea0d2bece2e302d696dee295ddbec4a859 (plain)
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
local testCount = 0
local failCount = 0

local function test(msg, f)
	testCount = testCount + 1
	local success, error = xpcall(f, debug.traceback)
	if success then
		print(msg .. "\t\t[OK]")
	else
		failCount = failCount + 1
		print(msg .. "\t\t[FAIL]")
		print(error)
	end
end


local ecs = require 'ecs'


--===== EntityDb tests =====--

local EntityDb = ecs.EntityDb


test("EntityDb.createEntity() always returns a new id", function()
	local db = EntityDb()

	local ids = {}
	for i=1,100 do
		local id = db:createEntity()
		assert(ids[id] == nil, "id was already returned!")
		ids[id] = true
	end
end)


test("EntityDb.queryComponent() gets all entities with a given component", function()
	local db = EntityDb()

	local ids = {}
	for i=1,100 do
		local id = db:createEntity()
		if i%2==0 then
			ids[id] = 5*i
			db:addComponent(id, "number", 5*i)
		end
	end

	local query = db:queryComponent("number")
	local count = 0
	for id, number in pairs(query) do
		count = count + 1
		assert(number == ids[id])
	end
	assert(count == 50)
end)


test("EntityDb.queryEntity() gets all components associated with an entity", function()
	local db = EntityDb()

	local entity
	for i=1,100 do
		local id = db:createEntity()
		if i%2 == 0 then db:addComponent(id, "number", 2) end
		if i%3 == 0 then db:addComponent(id, "string", "hello") end
		if i%5 == 0 then db:addComponent(id, "number2", 4) end
		if i%7 == 0 then db:addComponent(id, "string2", "world") end
		if i == 30 then entity=id end
	end

	local query = db:queryEntity(entity)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == 4)
	assert(query.string2 == nil)
end)


test("EntityDb.removeComponent() removes components correctly", function()
	local db = EntityDb()

	local id = db:createEntity()
	db:addComponent(id, "number", 2)
	db:addComponent(id, "string", "hello")
	db:addComponent(id, "number2", 4)
	db:addComponent(id, "string2", "world")

	local query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == 4)
	assert(query.string2 == "world")

	db:removeComponent(id, "string2")
	query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == 4)
	assert(query.string2 == nil)

	db:removeComponent(id, "number2")
	query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == nil)
	assert(query.string2 == nil)

	db:removeComponent(id, "string")
	query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == nil)
	assert(query.number2 == nil)
	assert(query.string2 == nil)

	db:removeComponent(id, "number")
	query = db:queryEntity(id)
	assert(query.number == nil)
	assert(query.string == nil)
	assert(query.number2 == nil)
	assert(query.string2 == nil)
end)


test("EntityDb.removeComponent() deletes component table when empty", function()
	local db = EntityDb()
	local id1 = db:createEntity()
	local id2 = db:createEntity()
	db:addComponent(id1, "number", 2)
	db:addComponent(id2, "number", 3)

	assert(db.components.number ~= nil)
	db:removeComponent(id1, "number")
	assert(db.components.number ~= nil)
	db:removeComponent(id2, "number")
	assert(db.components.number == nil)
end)


test("EntityDb.removeComponent() does nothing if the component is not present", function()
	local db = EntityDb()
	local id1 = db:createEntity()
	local id2 = db:createEntity()
	db:addComponent(id1, "number", 2)
	db:addComponent(id2, "number", 3)

	assert(db.components.number ~= nil)
	db:removeComponent(id1, "number")
	assert(db.components.number ~= nil)
	db:removeComponent(id1, "number")
	assert(db.components.number ~= nil)

	db:removeComponent(id2, "number")
	assert(db.components.number == nil)

end)


test("EntityDb.deleteEntity() correctly removes an entity", function()
	local db = EntityDb()
	local id1 = db:createEntity()
	local id2 = db:createEntity()
	db:addComponent(id1, "number", 2)
	db:addComponent(id2, "number", 3)

	local query = db:queryComponent("number")
	assert(query[id1] and query[id2])
	db:deleteEntity(id1)
	query = db:queryComponent("number")
	assert(query[id1] == nil)
	assert(query[id2] ~= nil)

	assert(false == pcall(function()
		local query = db:queryEntity(id1)
	end))
end)


--===== SystemDb tests =====--

local SystemDb = ecs.SystemDb

test("addSystems() correctly sorts systems", function()
	local sdb = SystemDb(nil)
	local str = ""

	local c = ecs.System("c", function(db, dt, params)
		str = str .. "c"
	end)

	local a = ecs.System("a", function(db, dt, params)
		str = str .. "a"
	end)

	local b = ecs.System("b", function(db, dt, params)
		str = str .. "b"
	end)

	b:addDependency(a)
	c:addDependency(b)

	sdb:addSystems({c, a, b}, nil)

	sdb:update(0)
	assert(str == "abc")
end)


test("addSystems() fails on dependency loop", function()
	local a = ecs.System("a", nil)
	local b = ecs.System("b", nil)
	local c = ecs.System("c", nil)

	a:addDependency(c)
	b:addDependency(a)
	c:addDependency(b)

	sdb = SystemDb(nil)
	local success = pcall(sdb,addSystems, sdb, {c, a, b}, nil)
	assert(success == false)
end)


test("addSystems() correctly inserts additional systems", function()
	local str = ""
	local sdb = SystemDb(nil)

	local c = ecs.System("c", function(db, dt, params)
		str = str .. "c"
	end)
	local a = ecs.System("a", function(db, dt, params)
		str = str .. "a"
	end)
	local b = ecs.System("b", function(db, dt, params)
		str = str .. "b"
	end)

	b:addDependency(a)
	c:addDependency(b)

	sdb:addSystems({c, a, b}, nil)

	local d = ecs.System("d", function(db, dt, params)
		str = str .. "d"
	end)
	local e = ecs.System("e", function(db, dt, params)
		str = str .. "e"
	end)

	d:addDependency(c)
	e:addDependency(d)

	sdb:addSystems({e, d}, nil)
	sdb:update(0)
	assert(str == "abcde")
end)


test("addSystems() correctly handles dangling dependencies", function()
	local str = ""
	local sdb = SystemDb(nil)

	local c = ecs.System("c", function(db, dt, params)
		str = str .. "c"
	end)
	local a = ecs.System("a", function(db, dt, params)
		str = str .. "a"
	end)
	local b = ecs.System("b", function(db, dt, params)
		str = str .. "b"
	end)

	b:addDependency(a)
	c:addDependency(b)

	local d = ecs.System("d", function(db, dt, params)
		str = str .. "d"
	end)
	local e = ecs.System("e", function(db, dt, params)
		str = str .. "e"
	end)

	d:addDependency(c)
	e:addDependency(d)

	sdb:addSystems({e, d}, nil)

	local success = pcall(sdb.update, sdb, nil)
	assert(success == false)

	sdb:addSystems({c, a, b}, nil)

	sdb:update(0)
	assert(str == "abcde")
end)




test("removeSystem() correctly handles things", function()
	local sdb = SystemDb(nil)
	local num = 1


	local two = ecs.System("x2", function(db, dt, p) num = num*2 end)	
	local three = ecs.System("x3", function(db, dt, p) num = num*3 end)	
	local five = ecs.System("x5", function(db, dt, p) num = num*5 end)	

	sdb:addSystems({two, three, five}, nil)

	sdb:update(0)
	assert(num == 30)

	num = 1

	sdb:removeSystems({three})
	sdb:update(1)
	assert(num == 10)
end)


print(string.format("ran %d tests, %d failed", testCount, failCount))