Add exact periodic neighborhood queries and measured scan baseline

This commit is contained in:
dingo
2026-09-19 19:21:45 -04:00
parent be4382472c
commit 9b19509673
15 changed files with 442 additions and 7 deletions
+95 -1
View File
@@ -1,4 +1,5 @@
#include "evo/simulation/scheduler.hpp"
#include "evo/simulation/spatial.hpp"
#include "evo/simulation/world.hpp"
#include "evo/telemetry/sinks.hpp"
@@ -220,6 +221,97 @@ void telemetry_is_host_owned() {
CHECK(log.str() == "{\"level\":\"error\",\"event\":\"invalid_snapshot\"}\n");
}
std::vector<evo::EntityId> spatial_reference(const evo::SimulationWorld& world,
evo::Position2 center, evo::Millimetres radius) {
std::vector<evo::EntityId> result;
for (const auto& entity : world.entities()) {
auto nearest = std::numeric_limits<std::uint64_t>::max();
// Independent oracle: enumerate nine translated images, no minimum-image formula.
for (const auto x_shift : {-1024, 0, 1024}) {
for (const auto y_shift : {-1024, 0, 1024}) {
const auto dx = static_cast<std::int64_t>(entity.position.x.value) + x_shift -
static_cast<std::int64_t>(center.x.value);
const auto dy = static_cast<std::int64_t>(entity.position.y.value) + y_shift -
static_cast<std::int64_t>(center.y.value);
nearest = std::min(nearest, static_cast<std::uint64_t>(dx * dx + dy * dy));
}
}
if (nearest <= static_cast<std::uint64_t>(radius.value) * radius.value) {
result.push_back(entity.id);
}
}
return result;
}
void spatial_boundaries() {
evo::SimulationWorld world{1};
const std::array positions{evo::Position2{},
evo::Position2{},
evo::Position2{{1023}, {0}},
evo::Position2{{0}, {1023}},
evo::Position2{{1023}, {1023}},
evo::Position2{{3}, {4}},
evo::Position2{{512}, {512}}};
for (const auto position : positions) {
CHECK(world.create_entity(position));
}
const auto expect = [&](std::uint32_t radius, const std::vector<evo::EntityId>& ids) {
const auto found = evo::query_neighbors(world, {}, evo::Millimetres{radius});
CHECK(found && *found == ids);
};
expect(0, {{1}, {2}});
expect(1, {{1}, {2}, {3}, {4}});
expect(2, {{1}, {2}, {3}, {4}, {5}});
expect(5, {{1}, {2}, {3}, {4}, {5}, {6}});
expect(724, {{1}, {2}, {3}, {4}, {5}, {6}});
expect(725, {{1}, {2}, {3}, {4}, {5}, {6}, {7}});
expect(std::numeric_limits<std::uint32_t>::max(), {{1}, {2}, {3}, {4}, {5}, {6}, {7}});
for (const auto center : {evo::Position2{{1024}, {}}, evo::Position2{{}, {1024}}}) {
const auto invalid = evo::query_neighbors(world, center, evo::Millimetres{1});
CHECK(!invalid && invalid.error() == evo::Error::invalid_position);
CHECK(!evo::query_neighbors(evo::SimulationWorld{0}, center, {}));
}
const auto empty = evo::query_neighbors(evo::SimulationWorld{0}, {}, {});
CHECK(empty && empty->empty());
}
void spatial_properties() {
for (std::uint64_t seed = 0; seed < 32; ++seed) {
evo::SimulationWorld world{seed};
evo::Random random{seed};
const auto coordinate = [&] {
return evo::Millimetres{static_cast<std::uint32_t>(random.next() % 1024)};
};
for (int index = 0; index < 127; ++index) {
CHECK(world.create_entity({coordinate(), coordinate()}));
}
for (int phase = 0; phase < 3; ++phase) {
const auto before = world.serialize();
for (int query = 0; query < 24; ++query) {
const evo::Position2 center{coordinate(), coordinate()};
const evo::Millimetres radius{static_cast<std::uint32_t>(random.next() % 800)};
const auto actual = evo::query_neighbors(world, center, radius);
CHECK(actual && *actual == spatial_reference(world, center, radius));
CHECK(std::ranges::is_sorted(*actual));
}
CHECK(world.serialize() == before);
if (phase == 0) {
const std::array commands{evo::Command{1, evo::RemoveEntity{{1}}},
evo::Command{2, evo::CreateEntity{}}};
CHECK(evo::advance(world, evo::Tick{1}, commands));
} else {
auto restored = evo::SimulationWorld::restore(before);
CHECK(restored);
world = std::move(*restored);
}
}
}
const auto full = fixture(1, evo::max_entities);
const auto all = evo::query_neighbors(full, {}, {});
CHECK(all && all->size() == evo::max_entities);
CHECK(all->front().value == 1 && all->back().value == evo::max_entities);
}
void scheduled_order_and_recovery() {
for (std::uint64_t seed = 0; seed < 32; ++seed) {
auto expected = fixture(seed, 2);
@@ -340,7 +432,9 @@ int main(int argc, char**) try {
Test{"canonical empty state", wire_layout_and_empty_state},
Test{"host telemetry", telemetry_is_host_owned},
Test{"scheduled order and recovery", scheduled_order_and_recovery},
Test{"atomic scheduler rejection", scheduled_rejection}};
Test{"atomic scheduler rejection", scheduled_rejection},
Test{"spatial boundaries", spatial_boundaries},
Test{"spatial oracle properties", spatial_properties}};
int failures = 0;
for (const auto& test : tests) {
try {