Add tests

This commit is contained in:
Ukendio 2024-08-28 19:19:00 +02:00
parent 1033022a9a
commit 1f46e92505
3 changed files with 84 additions and 4 deletions

67
benches/insert.luau Normal file
View file

@ -0,0 +1,67 @@
--!optimize 2
--!native
local testkit = require("@testkit")
local BENCH, START = testkit.benchmark()
local function TITLE(title: string)
print()
print(testkit.color.white(title))
end
local jecs = require("@jecs")
type i53 = number
local N = 1000
do TITLE(testkit.color.white_underline("Jecs insert"))
local ecs = jecs.World.new()
local ctypes = {}
local A = ecs:component()
local B = ecs:component()
local C = ecs:component()
local D = ecs:component()
local E = ecs:component()
local F = ecs:component()
local G = ecs:component()
local H = ecs:component()
ecs:add(A, jecs.Tag)
ecs:add(B, jecs.Tag)
ecs:add(C, jecs.Tag)
ecs:add(D, jecs.Tag)
ecs:add(E, jecs.Tag)
ecs:add(F, jecs.Tag)
ecs:add(G, jecs.Tag)
ecs:add(H, jecs.Tag)
BENCH("insert 1 components on entity with 16 components", function()
local entities = {}
for i = 1, N do
entities[i] = ecs:entity()
end
for _, entity in entities do
ecs:add(entity, A)
ecs:add(entity, B)
ecs:add(entity, C)
ecs:add(entity, D)
ecs:add(entity, E)
ecs:add(entity, F)
ecs:add(entity, G)
ecs:add(entity, H)
end
for i = 1, N do
ecs:remove(entities[i], A)
end
for i = 1, START(N) do
local entity = entities[i]
ecs:add(entity, A)
end
end)
end

View file

@ -80,7 +80,7 @@ local ECS_ID_HAS_HOOKS = 0b0010
--local EcsIdExclusive = 0b0100 --local EcsIdExclusive = 0b0100
local ECS_ID_IS_TAG = 0b1000 local ECS_ID_IS_TAG = 0b1000
local TAG_OBJ = {} local NULL_ARRAY = table.freeze({})
local function FLAGS_ADD(is_pair: boolean): number local function FLAGS_ADD(is_pair: boolean): number
local flags = 0x0 local flags = 0x0
@ -201,12 +201,15 @@ local function archetype_move(entity_index: EntityIndex, to: Archetype,
local records = to.records local records = to.records
for i, column in src_columns do for i, column in src_columns do
if column == NULL_ARRAY then
continue
end
-- Retrieves the new column index from the source archetype's record from each component -- Retrieves the new column index from the source archetype's record from each component
-- We have to do this because the columns are tightly packed and indexes may not correspond to each other. -- We have to do this because the columns are tightly packed and indexes may not correspond to each other.
local tr = records[types[i]] local tr = records[types[i]]
-- Sometimes target column may not exist, e.g. when you remove a component. -- Sometimes target column may not exist, e.g. when you remove a component.
if tr and column ~= TAG_OBJ then if tr then
dst_columns[tr.column][dst_row] = column[src_row] dst_columns[tr.column][dst_row] = column[src_row]
end end
-- If the entity is the last row in the archetype then swapping it would be meaningless. -- If the entity is the last row in the archetype then swapping it would be meaningless.
@ -501,7 +504,7 @@ local function archetype_create(world: World, types: { i24 }, prev: Archetype?):
if bit32.band(idr.flags, ECS_ID_IS_TAG) == 0 then if bit32.band(idr.flags, ECS_ID_IS_TAG) == 0 then
columns[i] = {} columns[i] = {}
else else
columns[i] = TAG_OBJ columns[i] = NULL_ARRAY
end end
end end

View file

@ -211,6 +211,16 @@ TEST("world:add()", function()
end) end)
TEST("world:query()", function() TEST("world:query()", function()
do CASE "tag"
local world = jecs.World.new()
local A = world:component()
world:add(A, jecs.Tag)
local e = world:entity()
world:set(e, A, "test")
for id, a in world:query(A) do
CHECK(a == nil)
end
end
do CASE "query single component" do CASE "query single component"
do do
local world = jecs.World.new() local world = jecs.World.new()