From 4a2088e833bf472764f41caf57ad01cb4a4ed104 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 17 Aug 2025 21:53:22 +0700 Subject: [PATCH] wildcard queries and wildcard fuzz test --- develop/all.lua | 4 + develop/fuzzing/wildcard_fuzz.lua | 254 ++++++++++++++++++++++++++++++ develop/samples/relations.lua | 64 ++++++++ develop/testing/pairs_tests.lua | 3 +- evolved.lua | 211 +++++++++++++++++++------ 5 files changed, 488 insertions(+), 48 deletions(-) create mode 100644 develop/fuzzing/wildcard_fuzz.lua create mode 100644 develop/samples/relations.lua diff --git a/develop/all.lua b/develop/all.lua index 21f5475..c352fe2 100644 --- a/develop/all.lua +++ b/develop/all.lua @@ -1,6 +1,8 @@ require 'develop.example' require 'develop.untests' +require 'develop.samples.relations' + require 'develop.testing.name_tests' require 'develop.testing.pairs_tests' require 'develop.testing.requires_fragment_tests' @@ -23,3 +25,5 @@ print '----------------------------------------' basics.describe_fuzz 'develop.fuzzing.requires_fuzz' print '----------------------------------------' basics.describe_fuzz 'develop.fuzzing.unique_fuzz' +print '----------------------------------------' +basics.describe_fuzz 'develop.fuzzing.wildcard_fuzz' diff --git a/develop/fuzzing/wildcard_fuzz.lua b/develop/fuzzing/wildcard_fuzz.lua new file mode 100644 index 0000000..916afc2 --- /dev/null +++ b/develop/fuzzing/wildcard_fuzz.lua @@ -0,0 +1,254 @@ +local evo = require 'evolved' + +evo.debug_mode(true) + +--- +--- +--- +--- +--- + +local __table_unpack = (function() + ---@diagnostic disable-next-line: deprecated + return table.unpack or unpack +end)() + +--- +--- +--- +--- +--- + +local all_entity_list = {} ---@type evolved.entity[] +local all_fragment_list = {} ---@type evolved.fragment[] + +for i = 1, math.random(1, 5) do + local fragment_builder = evo.builder() + + if math.random(1, 3) == 1 then + fragment_builder:explicit() + end + + if math.random(1, 3) == 1 then + if math.random(1, 2) == 1 then + fragment_builder:destruction_policy(evo.DESTRUCTION_POLICY_DESTROY_ENTITY) + else + fragment_builder:destruction_policy(evo.DESTRUCTION_POLICY_REMOVE_FRAGMENT) + end + end + + all_fragment_list[i] = fragment_builder:spawn() +end + +for i = 1, math.random(50, 100) do + local entity_builder = evo.builder() + + for _ = 0, math.random(0, #all_fragment_list) do + if math.random(1, 2) == 1 then + local fragment = all_fragment_list[math.random(1, #all_fragment_list)] + entity_builder:set(fragment) + else + local primary = all_fragment_list[math.random(1, #all_fragment_list)] + local secondary = all_fragment_list[math.random(1, #all_fragment_list)] + entity_builder:set(evo.pair(primary, secondary)) + end + end + + all_entity_list[i] = entity_builder:spawn() +end + +--- +--- +--- +--- +--- + +for _ = 1, math.random(1, 100) do + local query_builder = evo.builder() + + local query_include_set = {} ---@type table + local query_include_list = {} ---@type evolved.entity[] + local query_include_count = 0 ---@type integer + + local query_exclude_set = {} ---@type table + local query_exclude_list = {} ---@type evolved.entity[] + local query_exclude_count = 0 ---@type integer + + for _ = 1, math.random(0, 2) do + if math.random(1, 2) == 1 then + local fragment = all_fragment_list[math.random(1, #all_fragment_list)] + + query_builder:include(fragment) + + if not query_include_set[fragment] then + query_include_count = query_include_count + 1 + query_include_set[fragment] = query_include_count + query_include_list[query_include_count] = fragment + end + else + local primary = all_fragment_list[math.random(1, #all_fragment_list)] + local secondary = all_fragment_list[math.random(1, #all_fragment_list)] + + if math.random(1, 3) == 1 then + primary = evo.ANY + end + + if math.random(1, 3) == 1 then + secondary = evo.ANY + end + + local pair = evo.pair(primary, secondary) + + query_builder:include(pair) + + if not query_include_set[pair] then + query_include_count = query_include_count + 1 + query_include_set[pair] = query_include_count + query_include_list[query_include_count] = pair + end + end + end + + for _ = 1, math.random(0, 2) do + if math.random(1, 2) == 1 then + local fragment = all_fragment_list[math.random(1, #all_fragment_list)] + + query_builder:exclude(fragment) + + if not query_exclude_set[fragment] then + query_exclude_count = query_exclude_count + 1 + query_exclude_set[fragment] = query_exclude_count + query_exclude_list[query_exclude_count] = fragment + end + else + local primary = all_fragment_list[math.random(1, #all_fragment_list)] + local secondary = all_fragment_list[math.random(1, #all_fragment_list)] + + if math.random(1, 3) == 1 then + primary = evo.ANY + end + + if math.random(1, 3) == 1 then + secondary = evo.ANY + end + + local pair = evo.pair(primary, secondary) + + query_builder:exclude(pair) + + if not query_exclude_set[pair] then + query_exclude_count = query_exclude_count + 1 + query_exclude_set[pair] = query_exclude_count + query_exclude_list[query_exclude_count] = pair + end + end + end + + local query_entity_set = {} ---@type table + local query_entity_count = 0 ---@type integer + + do + local query = query_builder:spawn() + + for chunk, entity_list, entity_count in evo.execute(query) do + if not chunk:has(evo.INTERNAL) then + for i = 1, entity_count do + local entity = entity_list[i] + assert(not query_entity_set[entity]) + query_entity_count = query_entity_count + 1 + query_entity_set[entity] = query_entity_count + end + end + end + + if query_entity_set[query] then + query_entity_set[query] = nil + query_entity_count = query_entity_count - 1 + end + + evo.destroy(query) + end + + do + local expected_entity_count = 0 + + for _, entity in ipairs(all_entity_list) do + local is_entity_expected = + not evo.empty(entity) and + evo.has_all(entity, __table_unpack(query_include_list)) and + not evo.has_any(entity, __table_unpack(query_exclude_list)) + + for fragment in evo.each(entity) do + if evo.has(fragment, evo.EXPLICIT) then + local is_fragment_included = + query_include_set[fragment] ~= nil or + query_include_set[evo.pair(fragment, evo.ANY)] ~= nil + + if not is_fragment_included then + is_entity_expected = false + break + end + end + end + + if is_entity_expected then + assert(query_entity_set[entity]) + expected_entity_count = expected_entity_count + 1 + else + assert(not query_entity_set[entity]) + end + end + + for _, entity in ipairs(all_fragment_list) do + local is_entity_expected = + not evo.empty(entity) and + evo.has_all(entity, __table_unpack(query_include_list)) and + not evo.has_any(entity, __table_unpack(query_exclude_list)) + + for fragment in evo.each(entity) do + if evo.has(fragment, evo.EXPLICIT) then + is_entity_expected = is_entity_expected and + (query_include_set[fragment] ~= nil) or + (evo.is_pair(fragment) and query_include_set[evo.pair(fragment, evo.ANY)] ~= nil) + end + end + + if is_entity_expected then + assert(query_entity_set[entity]) + expected_entity_count = expected_entity_count + 1 + else + assert(not query_entity_set[entity]) + end + end + + assert(query_entity_count == expected_entity_count) + end +end + +--- +--- +--- +--- +--- + +if math.random(1, 2) == 1 then + evo.collect_garbage() +end + +if math.random(1, 2) == 1 then + evo.destroy(__table_unpack(all_entity_list)) + if math.random(1, 2) == 1 then + evo.collect_garbage() + end + evo.destroy(__table_unpack(all_fragment_list)) +else + evo.destroy(__table_unpack(all_fragment_list)) + if math.random(1, 2) == 1 then + evo.collect_garbage() + end + evo.destroy(__table_unpack(all_entity_list)) +end + +if math.random(1, 2) == 1 then + evo.collect_garbage() +end diff --git a/develop/samples/relations.lua b/develop/samples/relations.lua new file mode 100644 index 0000000..7e98174 --- /dev/null +++ b/develop/samples/relations.lua @@ -0,0 +1,64 @@ +---@diagnostic disable: unused-local + +local evo = require 'evolved' + +evo.debug_mode(true) + +local fragments = { + planet = evo.builder():name('planet'):tag():spawn(), + spaceship = evo.builder():name('spaceship'):tag():spawn(), +} + +local relations = { + docked_to = evo.builder():name('docked_to'):tag():explicit():spawn(), +} + +local planets = { + mars = evo.builder():name('Mars'):set(fragments.planet):spawn(), + venus = evo.builder():name('Venus'):set(fragments.planet):spawn(), +} + +local spaceships = { + falcon = evo.builder() + :name('Millennium Falcon') + :set(fragments.spaceship) + :set(evo.pair(relations.docked_to, planets.mars)) + :spawn(), + enterprise = evo.builder() + :name('USS Enterprise') + :set(fragments.spaceship) + :set(evo.pair(relations.docked_to, planets.venus)) + :spawn(), +} + +local queries = { + all_docked_spaceships = evo.builder() + :include(fragments.spaceship) + :include(evo.pair(relations.docked_to, evo.ANY)) + :spawn(), + docked_spaceships_to_mars = evo.builder() + :include(fragments.spaceship) + :include(evo.pair(relations.docked_to, planets.mars)) + :spawn(), + +} + +print '-= | All Docked Spaceships | =-' + +for chunk, entity_list, entity_count in evo.execute(queries.all_docked_spaceships) do + for i = 1, entity_count do + local entity = entity_list[i] + local planet = evo.secondary(entity, relations.docked_to) + print(string.format('%s is docked to %s', evo.name(entity), evo.name(planet))) + end +end + +print '-= | Docked Spaceships to Mars | =-' + +for chunk, entity_list, entity_count in evo.execute(queries.docked_spaceships_to_mars) do + for i = 1, entity_count do + local entity = entity_list[i] + local planet = evo.secondary(entity, relations.docked_to) + print(string.format('%s is docked to %s', evo.name(entity), evo.name(planet))) + end +end diff --git a/develop/testing/pairs_tests.lua b/develop/testing/pairs_tests.lua index bc04c9e..75d2f47 100644 --- a/develop/testing/pairs_tests.lua +++ b/develop/testing/pairs_tests.lua @@ -1444,4 +1444,5 @@ end -- TODO: -- Add errors on modifying pairs --- Add wildcard query tests +-- add exclusive trait +-- add is_pair/is_wildcard diff --git a/evolved.lua b/evolved.lua index ec688bb..a59d3a4 100644 --- a/evolved.lua +++ b/evolved.lua @@ -1149,7 +1149,7 @@ function __iterator_fns.__each_iterator(each_state) end ---@type evolved.execute_iterator -function __iterator_fns.__execute_iterator(execute_state) +function __iterator_fns.__execute_major_iterator(execute_state) if not execute_state then return end local structural_changes = execute_state[1] @@ -1178,6 +1178,16 @@ function __iterator_fns.__execute_iterator(execute_state) (not chunk_child.__has_explicit_major) and (not exclude_set or not exclude_set[chunk_child_fragment]) + if is_chunk_child_matched and exclude_set and chunk_child.__has_pair_major then + local chunk_child_primary_index, chunk_child_secondary_index = + __evolved_unpack(chunk_child_fragment) + + is_chunk_child_matched = + not exclude_set[__WILDCARD_PAIR] and + not exclude_set[__primary_wildcard(chunk_child_secondary_index)] and + not exclude_set[__secondary_wildcard(chunk_child_primary_index)] + end + if is_chunk_child_matched then chunk_stack_size = chunk_stack_size + 1 chunk_stack[chunk_stack_size] = chunk_child @@ -1197,6 +1207,37 @@ function __iterator_fns.__execute_iterator(execute_state) __release_table(__table_pool_tag.execute_state, execute_state, true) end +---@type evolved.execute_iterator +function __iterator_fns.__execute_minor_iterator(execute_state) + if not execute_state then return end + + local structural_changes = execute_state[1] + local chunk_stack = execute_state[2] + local chunk_stack_size = execute_state[3] + + if structural_changes ~= __structural_changes then + __error_fmt('structural changes are prohibited during iteration') + end + + while chunk_stack_size > 0 do + local chunk = chunk_stack[chunk_stack_size] + + chunk_stack[chunk_stack_size] = nil + chunk_stack_size = chunk_stack_size - 1 + + local chunk_entity_list = chunk.__entity_list + local chunk_entity_count = chunk.__entity_count + + if chunk_entity_count > 0 then + execute_state[3] = chunk_stack_size + return chunk, chunk_entity_list, chunk_entity_count + end + end + + __release_table(__table_pool_tag.chunk_list, chunk_stack, true) + __release_table(__table_pool_tag.execute_state, execute_state, true) +end + ---@type evolved.primaries_iterator function __iterator_fns.__primaries_iterator(primaries_state) if not primaries_state then return end @@ -5910,14 +5951,14 @@ end function __evolved_execute(query) if __is_pair(query) then -- pairs cannot be used as queries, nothing to execute - return __iterator_fns.__execute_iterator + return __iterator_fns.__execute_major_iterator end local query_index = query % 2 ^ 20 if __freelist_ids[query_index] ~= query then -- this query is not alive, nothing to execute - return __iterator_fns.__execute_iterator + return __iterator_fns.__execute_major_iterator end ---@type evolved.chunk[] @@ -5935,72 +5976,148 @@ function __evolved_execute(query) local query_exclude_count = query_excludes and query_excludes.__item_count or 0 --[[@as integer]] if query_include_count > 0 then - local major_fragment = query_include_list[query_include_count] + local query_major_fragment = query_include_list[query_include_count] - local major_chunks = __major_chunks[major_fragment] - local major_chunk_list = major_chunks and major_chunks.__item_list --[=[@as evolved.chunk[]]=] - local major_chunk_count = major_chunks and major_chunks.__item_count or 0 --[[@as integer]] + if __is_wildcard(query_major_fragment) then + local minor_chunks = __minor_chunks[query_major_fragment] + local minor_chunk_list = minor_chunks and minor_chunks.__item_list --[=[@as evolved.chunk[]]=] + local minor_chunk_count = minor_chunks and minor_chunks.__item_count or 0 --[[@as integer]] - for major_chunk_index = 1, major_chunk_count do - local major_chunk = major_chunk_list[major_chunk_index] + for minor_chunk_index = 1, minor_chunk_count do + local minor_chunk = minor_chunk_list[minor_chunk_index] - local is_major_chunk_matched = - (query_include_count == 1 or __chunk_has_all_fragment_list( - major_chunk, query_include_list, query_include_count - 1)) and - (query_exclude_count == 0 or not __chunk_has_any_fragment_list( - major_chunk, query_exclude_list, query_exclude_count)) + local is_minor_chunk_matched = true - if is_major_chunk_matched and major_chunk.__has_explicit_minors then - local major_chunk_fragment_list = major_chunk.__fragment_list - local major_chunk_fragment_count = major_chunk.__fragment_count + if is_minor_chunk_matched and minor_chunk.__entity_count == 0 then + is_minor_chunk_matched = false + end - for major_chunk_fragment_index = 1, major_chunk_fragment_count - 1 do - local major_chunk_fragment = major_chunk_fragment_list[major_chunk_fragment_index] + if is_minor_chunk_matched and query_include_count > 1 then + is_minor_chunk_matched = __chunk_has_all_fragment_list( + minor_chunk, query_include_list, query_include_count - 1) + end - if not query_include_set[major_chunk_fragment] and __evolved_has(major_chunk_fragment, __EXPLICIT) then - is_major_chunk_matched = false - break + if is_minor_chunk_matched and query_exclude_count > 0 then + is_minor_chunk_matched = not __chunk_has_any_fragment_list( + minor_chunk, query_exclude_list, query_exclude_count) + end + + if is_minor_chunk_matched and minor_chunk.__has_explicit_fragments then + local minor_chunk_fragment_list = minor_chunk.__fragment_list + local minor_chunk_fragment_count = minor_chunk.__fragment_count + + for minor_chunk_fragment_index = 1, minor_chunk_fragment_count do + local minor_chunk_fragment = minor_chunk_fragment_list[minor_chunk_fragment_index] + + local is_minor_chunk_fragment_included = + query_include_set[minor_chunk_fragment] or + query_include_set[__secondary_wildcard(__evolved_unpack(minor_chunk_fragment))] + + if not is_minor_chunk_fragment_included and __evolved_has(minor_chunk_fragment, __EXPLICIT) then + is_minor_chunk_matched = false + break + end end end + + if is_minor_chunk_matched then + chunk_stack_size = chunk_stack_size + 1 + chunk_stack[chunk_stack_size] = minor_chunk + end end - if is_major_chunk_matched then - chunk_stack_size = chunk_stack_size + 1 - chunk_stack[chunk_stack_size] = major_chunk - end - end - elseif query_exclude_count > 0 then - for root_fragment, root_chunk in __lua_next, __root_chunks do - local is_root_chunk_matched = - not root_chunk.__has_explicit_major and - not query_exclude_set[root_fragment] + ---@type evolved.execute_state + local execute_state = __acquire_table(__table_pool_tag.execute_state) - if is_root_chunk_matched then - chunk_stack_size = chunk_stack_size + 1 - chunk_stack[chunk_stack_size] = root_chunk + execute_state[1] = __structural_changes + execute_state[2] = chunk_stack + execute_state[3] = chunk_stack_size + execute_state[4] = query_exclude_set + + return __iterator_fns.__execute_minor_iterator, execute_state + else + local major_chunks = __major_chunks[query_major_fragment] + local major_chunk_list = major_chunks and major_chunks.__item_list --[=[@as evolved.chunk[]]=] + local major_chunk_count = major_chunks and major_chunks.__item_count or 0 --[[@as integer]] + + for major_chunk_index = 1, major_chunk_count do + local major_chunk = major_chunk_list[major_chunk_index] + + local is_major_chunk_matched = true + + if is_major_chunk_matched and query_include_count > 1 then + is_major_chunk_matched = __chunk_has_all_fragment_list( + major_chunk, query_include_list, query_include_count - 1) + end + + if is_major_chunk_matched and query_exclude_count > 0 then + is_major_chunk_matched = not __chunk_has_any_fragment_list( + major_chunk, query_exclude_list, query_exclude_count) + end + + if is_major_chunk_matched and major_chunk.__has_explicit_minors then + local major_chunk_minor_list = major_chunk.__fragment_list + local major_chunk_minor_count = major_chunk.__fragment_count - 1 + + for major_chunk_fragment_index = 1, major_chunk_minor_count do + local major_chunk_minor = major_chunk_minor_list[major_chunk_fragment_index] + + local is_major_chunk_minor_included = + query_include_set[major_chunk_minor] or + query_include_set[__secondary_wildcard(__evolved_unpack(major_chunk_minor))] + + if not is_major_chunk_minor_included and __evolved_has(major_chunk_minor, __EXPLICIT) then + is_major_chunk_matched = false + break + end + end + end + + if is_major_chunk_matched then + chunk_stack_size = chunk_stack_size + 1 + chunk_stack[chunk_stack_size] = major_chunk + end end + + ---@type evolved.execute_state + local execute_state = __acquire_table(__table_pool_tag.execute_state) + + execute_state[1] = __structural_changes + execute_state[2] = chunk_stack + execute_state[3] = chunk_stack_size + execute_state[4] = query_exclude_set + + return __iterator_fns.__execute_major_iterator, execute_state end else for _, root_chunk in __lua_next, __root_chunks do - local is_root_chunk_matched = - not root_chunk.__has_explicit_major + local is_root_chunk_matched = true + + if is_root_chunk_matched and root_chunk.__has_explicit_fragments then + is_root_chunk_matched = false + end + + if is_root_chunk_matched and query_exclude_count > 0 then + is_root_chunk_matched = not __chunk_has_any_fragment_list( + root_chunk, query_exclude_list, query_exclude_count) + end if is_root_chunk_matched then chunk_stack_size = chunk_stack_size + 1 chunk_stack[chunk_stack_size] = root_chunk end end + + ---@type evolved.execute_state + local execute_state = __acquire_table(__table_pool_tag.execute_state) + + execute_state[1] = __structural_changes + execute_state[2] = chunk_stack + execute_state[3] = chunk_stack_size + execute_state[4] = query_exclude_set + + return __iterator_fns.__execute_major_iterator, execute_state end - - ---@type evolved.execute_state - local execute_state = __acquire_table(__table_pool_tag.execute_state) - - execute_state[1] = __structural_changes - execute_state[2] = chunk_stack - execute_state[3] = chunk_stack_size - execute_state[4] = query_exclude_set - - return __iterator_fns.__execute_iterator, execute_state end ---@param ... evolved.system systems