#include "evo/biology/cellular_region_step.hpp" #include #include #include #include #include #include namespace { using namespace evo::biology; using evo::accounting::Inventory; void check(bool value, std::source_location at = std::source_location::current()) { if (!value) throw std::runtime_error("cellular region step line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) MetabolicBudget budget() { MetabolicBudget value{}; value.rate_microseconds = 1'000'000; value.intake_limit = {{1'000}, {{{1'000}, {1'000}, {1'000}, {1'000}}}}; return value; } std::unique_ptr habitat() { CellularHabitatConfig opening{7, 9, 11, 3'000, 2'000, {{{0, 0}, 51, {{5}, {{{2}, {2}, {2}, {2}}}}}}}; auto made = CellularHabitat::create(std::move(opening)); CHECK(made); return std::move(*made); } void shared_patch_claims_are_identity_ordered_and_conserved() { const auto map = habitat(); const CellularLifeStepInput later{ 20, {0, 0}, 20, {{0, 0}, CellularPatchAddress{0, 0}}, {{100}, {}}, {}, budget()}; const CellularLifeStepInput earlier{ 10, {0, 0}, 20, {{0, 0}, CellularPatchAddress{0, 0}}, {{100}, {}}, {}, budget()}; const std::array input{later, earlier}; // Deliberately opposite the arbitration order. const auto candidate = propose_cellular_region_step(4, {3'000, 2'000}, input, *map); CHECK(candidate); CHECK(candidate->begin_tick == 4 && candidate->end_tick == 5); CHECK(candidate->lives.size() == 2); CHECK(candidate->lives[0].life_id == 10 && candidate->lives[1].life_id == 20); CHECK(candidate->lives[0].candidate.intake_status == CellularIntakeStatus::acquired); const auto first_intake = candidate->lives[0].candidate.intake.value_or(CellularPatchUptakeProposal{}); CHECK(candidate->lives[0].candidate.intake.has_value()); CHECK(first_intake.transfer.acquired == Inventory{{5}, {{{2}, {2}, {2}, {2}}}}); CHECK(candidate->lives[1].candidate.intake_status == CellularIntakeStatus::acquired); const auto second_intake = candidate->lives[1].candidate.intake.value_or(CellularPatchUptakeProposal{}); CHECK(candidate->lives[1].candidate.intake.has_value()); CHECK(second_intake.transfer.acquired == Inventory{}); CHECK(candidate->patches.size() == 1); CHECK(candidate->patches[0] == CellularPatchStockUpdate{{0, 0}, {}}); CHECK(map->patches()[0].stock == Inventory{{5}, {{{2}, {2}, {2}, {2}}}}); Inventory matter{}; for (const auto& life : candidate->lives) for (std::size_t channel = 0; channel < matter.matter.size(); ++channel) matter.matter[channel].value += life.candidate.reserve_after.matter[channel].value; for (std::size_t channel = 0; channel < matter.matter.size(); ++channel) CHECK(matter.matter[channel].value + candidate->patches[0].stock.matter[channel].value == map->patches()[0].stock.matter[channel].value); CHECK(candidate->lives[0].candidate.reserve_after.energy.value + candidate->lives[1].candidate.reserve_after.energy.value + candidate->patches[0].stock.energy.value == 100 + 100 + map->patches()[0].stock.energy.value); } void refusal_never_returns_partial_tick_state() { const auto map = habitat(); const std::array duplicate{CellularLifeStepInput{1, {0, 0}, 20, {}, {{100}, {}}, {}, budget()}, CellularLifeStepInput{1, {0, 0}, 20, {}, {{100}, {}}, {}, budget()}}; const auto repeated = propose_cellular_region_step(4, {3'000, 2'000}, duplicate, *map); CHECK(!repeated && repeated.error() == CellularRegionStepError::duplicate_life_id); auto starved_life = CellularLifeStepInput{2, {0, 0}, 20, {}, {{0}, {}}, {}, budget()}; auto starved_budget = budget(); starved_budget.maintenance = {1'000}; starved_life.budget = starved_budget; const std::array starved{starved_life}; const auto refused = propose_cellular_region_step(4, {3'000, 2'000}, starved, *map); CHECK(!refused && refused.error() == CellularRegionStepError::life_step); CHECK(map->patches()[0].stock == Inventory{{5}, {{{2}, {2}, {2}, {2}}}}); } } // namespace int main() { try { shared_patch_claims_are_identity_ordered_and_conserved(); refusal_never_returns_partial_tick_state(); std::puts("PASS cellular region step: deterministic shared-stock candidate"); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } }