#include "evo/biology/cellular_step.hpp" #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 step line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) MetabolicBudget budget() { MetabolicBudget result{}; result.phenotype.movement_speed = 200; result.maintenance = {100}; result.movement = {1'000}; result.intake_limit = {{1'000}, {{{100}, {50}, {25}, {10}}}}; result.rate_microseconds = 1'000'000; return result; } std::unique_ptr habitat() { CellularHabitatConfig opening{ 7, 9, 11, 3'000, 2'000, {{{0, 0}, 51, {{1'000}, {{{20}, {10}, {5}, {2}}}}}}}; auto made = CellularHabitat::create(std::move(opening)); CHECK(made); return std::move(*made); } void maintenance_motion_and_intake_conserve_one_tick() { const Inventory reserve{{200}, {}}; const Inventory heat{}; const auto map = habitat(); const auto step = propose_cellular_step(12, {3'000, 2'000}, {0, 0}, 20, {{1'000, 0}, CellularPatchAddress{0, 0}}, reserve, heat, budget(), *map); if (!step) throw std::runtime_error("cellular step failed"); CHECK(step->begin_tick == 12 && step->end_tick == 13); CHECK(step->position == CellularPosition{10, 0}); CHECK(step->maintenance_cost.value == 5 && step->movement_cost.value == 50); CHECK(step->movement_status == CellularMovementStatus::moved); CHECK(step->heat_after.energy.value == 55); CHECK(step->reserve_after == Inventory{{195}, {{{5}, {2}, {1}, {0}}}}); if (step->intake_status != CellularIntakeStatus::acquired || !step->intake) throw std::runtime_error("cellular step intake missing"); const auto intake = step->intake.value_or(CellularPatchUptakeProposal{}); CHECK(intake.transfer.patch_after == Inventory{{950}, {{{15}, {8}, {4}, {2}}}}); CHECK(reserve.energy.value == 200 && heat.energy.value == 0); CHECK(map->patches()[0].stock == Inventory{{1'000}, {{{20}, {10}, {5}, {2}}}}); const auto& acquired = intake.transfer.acquired; CHECK(step->reserve_after.energy.value + step->heat_after.energy.value + intake.transfer.patch_after.energy.value == reserve.energy.value + heat.energy.value + map->patches()[0].stock.energy.value); for (std::size_t channel = 0; channel < acquired.matter.size(); ++channel) CHECK(step->reserve_after.matter[channel].value + intake.transfer.patch_after.matter[channel].value == reserve.matter[channel].value + map->patches()[0].stock.matter[channel].value); } void maintenance_precedes_motion_and_intake() { const auto map = habitat(); Inventory low_reserve{{54}, {}}; const auto step = propose_cellular_step(1, {3'000, 2'000}, {0, 0}, 20, {{1'000, 0}, CellularPatchAddress{0, 0}}, low_reserve, {}, budget(), *map); CHECK(step); CHECK(step->position == CellularPosition{0, 0}); CHECK(step->movement_cost.value == 0); CHECK(step->movement_status == CellularMovementStatus::insufficient_reserve); CHECK(step->maintenance_cost.value == 5 && step->heat_after.energy.value == 5); CHECK(step->intake_status == CellularIntakeStatus::acquired); CHECK(step->reserve_after.energy.value == 99); // Intake cannot fund this tick's move. low_reserve.energy.value = 4; const auto starved = propose_cellular_step(1, {3'000, 2'000}, {0, 0}, 20, {{}, CellularPatchAddress{0, 0}}, low_reserve, {}, budget(), *map); CHECK(!starved && starved.error() == CellularStepError::insufficient_maintenance); } void refusals_and_noncontact_are_explicit() { const auto map = habitat(); const auto missed = propose_cellular_step(4, {3'000, 2'000}, {1'500, 0}, 20, {{}, CellularPatchAddress{0, 0}}, {{100}, {}}, {}, budget(), *map); CHECK(missed && missed->intake_status == CellularIntakeStatus::out_of_contact); CHECK(missed->position == CellularPosition{1'500, 0}); CHECK(missed->reserve_after.energy.value == 95); CHECK(!missed->intake); const auto absent = propose_cellular_step(4, {3'000, 2'000}, {0, 0}, 20, {{}, CellularPatchAddress{1'000, 0}}, {{100}, {}}, {}, budget(), *map); CHECK(absent && absent->intake_status == CellularIntakeStatus::missing); CHECK(!propose_cellular_step(std::numeric_limits::max(), {3'000, 2'000}, {0, 0}, 20, {}, {{100}, {}}, {}, budget(), *map)); CHECK( !propose_cellular_step(0, {2'000, 2'000}, {0, 0}, 20, {}, {{100}, {}}, {}, budget(), *map)); auto full_heat = Inventory{{std::numeric_limits::max()}, {}}; CHECK(!propose_cellular_step(0, {3'000, 2'000}, {0, 0}, 20, {}, {{100}, {}}, full_heat, budget(), *map)); } } // namespace int main() { try { maintenance_motion_and_intake_conserve_one_tick(); maintenance_precedes_motion_and_intake(); refusals_and_noncontact_are_explicit(); std::puts("PASS cellular step: tick order, funded motion and bounded feeding"); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } }