#include "evo/server/lifecycle/primitive_lives.hpp" #include #include #include #include #include namespace { using namespace evo::server; using evo::accounting::Inventory; void check(bool value, std::source_location at = std::source_location::current()) { if (!value) throw std::runtime_error("primitive lives line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) LifeRequest request(std::uint8_t account, std::uint64_t action = 1) { LifeRequest r{}; r.account.back() = account; r.action = action; return r; } LifeAllocation allocation(std::uint64_t id) { return {id, id + 100, id + 200, id + 300, {10}}; } Inventory price(std::uint64_t count) { const auto endowment = evo::biology::primitive_endowment(); CHECK(endowment); auto cost = endowment->body; cost.energy.value = endowment->reserve.energy.value + endowment->construction.value; cost.energy.value *= count; for (auto& channel : cost.matter) channel.value *= count; return cost; } NurseryConfig config(std::uint64_t funded = 2) { return {90, 1, 7, 17, price(funded), {{0, 0}, {20, 0}}}; } Inventory summed(std::span balances) { Inventory sum{}; for (const auto& b : balances) { sum.energy.value += b.energy.value; for (std::size_t c = 0; c < 4; ++c) sum.matter[c].value += b.matter[c].value; } return sum; } struct Cut { std::vector balances; std::vector lives; std::vector archive; auto operator<=>(const Cut&) const = default; }; Cut cut(const PrimitiveLives& nursery) { return {{nursery.balances().begin(), nursery.balances().end()}, {nursery.lives().begin(), nursery.lives().end()}, nursery.archive().encode()}; } void funded_bodies_and_equal_starts() { auto created = PrimitiveLives::create(config()); CHECK(created); auto& nursery = **created; const auto opening = nursery.totals(); CHECK(nursery.archive().recorded() == 0 && nursery.lives().empty()); const auto first = nursery.spawn(request(1), allocation(1)); // A large action number represents a returning account; it grants no power. const auto second = nursery.spawn(request(2, 999'999), allocation(2)); CHECK(first && second && first->site == (SpawnSite{0, 0}) && second->site == (SpawnSite{20, 0})); CHECK(first->growth == (evo::biology::GrowthState{1000, 0})); CHECK(first->growth == second->growth && first->source == 17 && second->source == 17); CHECK(nursery.balances()[first->body.value - 1] == nursery.balances()[second->body.value - 1]); CHECK(nursery.balances()[first->reserve.value - 1] == nursery.balances()[second->reserve.value - 1]); const auto a = nursery.archive().find(first->identity.genome); const auto b = nursery.archive().find(second->identity.genome); CHECK(a && b); CHECK(evo::biology::metabolic_budget(*a, first->growth, {}) == evo::biology::metabolic_budget(*b, second->growth, {})); CHECK(nursery.archive().organism(first->identity.organism)->born == evo::Tick{10}); CHECK(nursery.balances()[0] == Inventory{}); // Independently calculated body: structure 8e6 fg, membrane 2.88e6 fg. const auto& body = nursery.balances()[first->body.value - 1]; CHECK(body.energy.value == 0 && body.matter[0].value == 6'016'000); CHECK(body.matter[1].value == 1'344'000 && body.matter[2].value == 688'000); CHECK(body.matter[3].value == 2'832'000); CHECK(nursery.balances()[first->reserve.value - 1].energy.value == 7'824'000); CHECK(nursery.balances()[1].energy.value == 2ULL * 435'200); CHECK(summed(nursery.balances()) == opening && nursery.totals() == opening); // Initial reserve is energy-only; phosphorus still binds the first bud. const auto budget = evo::biology::metabolic_budget(*a, first->growth, {}); CHECK(budget); CHECK(nursery.balances()[first->reserve.value - 1].matter == Inventory{}.matter); for (const std::uint64_t share : {1000ULL, 500ULL, 333ULL}) { const auto needed = budget->reproduction_investment.matter[2].value; const auto rate = budget->intake_limit.matter[2].value * share / 1000; const auto seconds = needed / rate + (needed % rate != 0 ? 1 : 0); CHECK(seconds >= 300 && seconds <= 901); } } void failures_and_retries_spend_nothing() { auto made = PrimitiveLives::create(config()); CHECK(made); auto& nursery = **made; const auto first = nursery.spawn(request(1), allocation(1)); CHECK(first); const auto before = cut(nursery); CHECK(nursery.spawn(request(1), {}) == first); // Retry needs no fresh allocation. CHECK(!nursery.spawn(request(1, 2), allocation(2))); CHECK(!nursery.spawn(request(0), allocation(2))); CHECK(!nursery.spawn(request(2, 0), allocation(2))); CHECK(!nursery.spawn(request(2), {})); for (int field = 0; field < 4; ++field) { auto duplicate = allocation(2); if (field == 0) duplicate.life = 1; if (field == 1) duplicate.organism = 101; if (field == 2) duplicate.genome = 201; if (field == 3) duplicate.event = 301; const auto result = nursery.spawn(request(2), duplicate); CHECK(!result && result.error() == LifeError::identity_conflict); } CHECK(cut(nursery) == before); CHECK(nursery.spawn(request(2), allocation(2))); const auto full = cut(nursery); const auto refused = nursery.spawn(request(3), allocation(3)); CHECK(!refused && refused.error() == LifeError::capacity && cut(nursery) == full); CHECK(nursery.spawn(request(1), {}) == first); // Even when full and source empty. CHECK(cut(nursery) == full); } void every_channel_must_be_funded() { for (std::size_t channel = 0; channel < 5; ++channel) { auto c = config(1); if (channel == 0) --c.funding.energy.value; else --c.funding.matter[channel - 1].value; auto made = PrimitiveLives::create(c); CHECK(made); const auto before = cut(**made); const auto result = (*made)->spawn(request(1), allocation(1)); CHECK(!result && result.error() == LifeError::insufficient_funding); CHECK(cut(**made) == before); } auto made = PrimitiveLives::create(config(1)); CHECK(made && (*made)->spawn(request(1), allocation(1))); const auto before = cut(**made); CHECK(!(*made)->spawn(request(2), allocation(2)) && cut(**made) == before); } void invalid_sites_and_bounds() { auto c = config(); c.sites[1] = {19, 19}; // 20 um square founder bodies overlap on both axes. CHECK(!PrimitiveLives::create(c)); c.sites[1] = {0, 0}; CHECK(!PrimitiveLives::create(c)); c.sites = {{20, 0}, {0, 0}}; CHECK(!PrimitiveLives::create(c)); c.sites = {{std::numeric_limits::min(), 0}}; CHECK(!PrimitiveLives::create(c)); c.sites.clear(); CHECK(!PrimitiveLives::create(c)); c = config(); c.world = 0; CHECK(!PrimitiveLives::create(c)); c = config(); c.sites.resize(max_primitive_lives + 1); CHECK(!PrimitiveLives::create(c)); c = config(); c.funding = {}; auto empty = PrimitiveLives::create(c); CHECK(empty && !(*empty)->spawn(request(1), allocation(1))); // Huge valid opening stays exactly conserved, without summing channels into one mass. c.funding.energy.value = std::numeric_limits::max(); for (auto& m : c.funding.matter) m.value = std::numeric_limits::max(); auto huge = PrimitiveLives::create(c); CHECK(huge && (*huge)->spawn(request(1), allocation(1))); CHECK(summed((*huge)->balances()) == c.funding); } } // namespace int main() try { funded_bodies_and_equal_starts(); failures_and_retries_spend_nothing(); every_channel_must_be_funded(); invalid_sites_and_bounds(); std::puts("PASS primitive lives: funded bodies, equal starts, atomic refusals and retries"); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "%s\n", e.what()); return 1; }