#include "evo/server/lifecycle/cellular_census_set.hpp" #include "fixtures/cellular_ecology.hpp" namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; using Kind = CellularCensusKind; using Scope = CellularCensusScope; std::size_t at(Kind kind) { return static_cast(kind); } std::vector inputs(const CellularLives& state, bool bud = false, bool frozen = false, bool move = false) { std::vector result; for (const auto& life : state.lives()) { CellularLifeIntent input{life.identity.life, {}}; if (life.account != LifeAccount{}) { input.intent.bud = bud && !frozen; input.intent.frozen = frozen; if (move && !frozen) input.intent.movement = {1000, 0}; } result.push_back(input); } return result; } Owner regional(std::uint64_t region) { auto state = opening(7, true, 1, false, region); checked(state->advance_tick({}), "regional census clock"); LifeAccount account{}; account.back() = static_cast(region); const auto id = region * 100; state = checked(state->birth_candidate({account, 1}, {id, id, id, id, {state->tick()}}), "census founder"); state = checked(state->migration_upgrade_candidate(), "census boundary"); state = checked(state->maturation_upgrade_candidate(), "census maturation"); return state; } void choose(Owner& state, std::uint64_t successor) { for (unsigned tick = 0; tick < 60'000; ++tick) { const auto cell = state->lives().front(); if (cell.mutation.offer) { const auto& offer = *cell.mutation.offer; const auto structural = std::ranges::find_if(offer.candidates, [](const auto& candidate) { return candidate.target == biology::Capability::membrane || candidate.target == biology::Capability::target_size; }); const CellularMutationSelection selection{ cell.identity.life, cell.mutation.event_index, cell.mutation.offer_tick, successor, structural == offer.candidates.end() ? std::uint16_t{0} : structural->operator_id}; checked(state->advance_tick_with_mutations(inputs(*state), std::span{&selection, 1}), "census plan selection"); if (structural != offer.candidates.end()) return; } else checked(state->advance_tick(inputs(*state)), "census earned offer"); } throw std::runtime_error("finite food did not earn census structural plan"); } CellularCensusTotals query(const CellularCensus& census, std::uint64_t genome) { return checked(census.query(genome, Scope::exact_genome), "plan census query"); } void verify(const CellularLives& state) { const auto before = state.snapshot(); const auto census = checked(CellularCensus::observe(state), "plan census observation"); require(census.boundary().projection_version == 2 && state.snapshot() == before, "wrong projection or changed owner"); Inventory body{}, reserve{}; std::uint64_t residents = 0, holders = 0, dead = 0; for (const auto& row : census.rows()) { for (std::size_t kind = 0; kind < cellular_census_kind_count; ++kind) { const auto& stock = row.totals.resident[kind]; residents += stock.count; holders += row.totals.living_plan_holders[kind]; body.energy.value += stock.body.energy.value; reserve.energy.value += stock.reserve.energy.value; for (std::size_t channel = 0; channel < body.matter.size(); ++channel) { body.matter[channel].value += stock.body.matter[channel].value; reserve.matter[channel].value += stock.reserve.matter[channel].value; } } dead += row.totals.retained_dead_plan_records; } Inventory actual_body{}, actual_reserve{}; for (const auto& life : state.lives()) { actual_body.energy.value += life.body.energy.value; actual_reserve.energy.value += life.reserve.energy.value; for (std::size_t channel = 0; channel < body.matter.size(); ++channel) { actual_body.matter[channel].value += life.body.matter[channel].value; actual_reserve.matter[channel].value += life.reserve.matter[channel].value; } } require(residents == state.lives().size() && holders == state.lives().size() && dead == state.corpses().size() && body == actual_body && reserve == actual_reserve, "plan references invented organisms/inventory or lost holder history"); require(std::ranges::equal( census.rows(), checked(CellularCensus::observe(*restore(state)), "restored census").rows()), "restore changed plan observation"); } void family_and_history() { auto state = regional(1); const auto old = checked(CellularCensus::observe(*state), "old projection"); require(old.boundary().projection_version == 1 && query(old, 100).living_plan_holders == std::array{}, "old projection silently gained plan measures"); state = checked(state->inheritance_upgrade_candidate(), "inherited census upgrade"); choose(state, 1'000'000); const auto unexpressed = checked(CellularCensus::observe(*state), "unexpressed plan"); const auto plan = query(unexpressed, 1'000'000); require(plan.living_plan_holders[at(Kind::active_controlled)] == 1 && plan.resident == std::array{} && checked(state->archive().living_carriers(1'000'000), "unborn archive carriers") == 0, "unexpressed retained plan was erased or counted as tissue"); const auto descendants = checked(unexpressed.query(100, Scope::descendants), "plan descendants"); require(descendants.resident[at(Kind::active_controlled)].count == 1 && descendants.living_plan_holders[at(Kind::active_controlled)] == 1, "ancestry query double-counted the parent's plan as another organism"); bool born = false; for (unsigned tick = 0; tick < 30'000 && !born; ++tick) { checked(state->advance_tick(inputs(*state, true)), "census funded child"); born = std::ranges::any_of(state->lives(), [](const auto& life) { return life.parent_life == 100; }); } require(born, "census family had no funded child"); verify(*state); choose(state, 500'000); // Sparse descendant IDs need not follow ancestry order. verify(*state); for (unsigned tick = 0; tick <= cellular_linger_ticks; ++tick) checked(state->advance_tick(inputs(*state, false, true)), "census suspended plan"); const auto suspended = checked(CellularCensus::observe(*state), "suspended plan observation"); require(query(suspended, 500'000).living_plan_holders[at(Kind::suspended_controlled)] == 1 && query(unexpressed, 1'000'000).living_plan_holders[at(Kind::active_controlled)] == 1, "logout erased a plan or rewrote detached history"); verify(*state); bool died = false; for (unsigned tick = 0; tick < 100'000 && !died; ++tick) { checked(state->advance_tick(inputs(*state, false, false, true)), "census ecological death"); died = std::ranges::any_of(state->corpses(), [](const auto& corpse) { return corpse.life_id == 100; }); } require(died, "census parent did not die after leaving finite food"); verify(*state); const auto dead = query(checked(CellularCensus::observe(*state), "dead plan census"), 500'000); require(dead.retained_dead_plan_records == 1 && dead.living_plan_holders == std::array{} && dead.resident == std::array{}, "dead plan history became current viability or stock"); } void scoped() { auto a = regional(1), b = regional(2); a = checked(a->inheritance_upgrade_candidate(), "scope A upgrade"); b = checked(b->inheritance_upgrade_candidate(), "scope B upgrade"); const auto ca = checked(CellularCensus::observe(*a), "scope A"); const auto cb = checked(CellularCensus::observe(*b), "scope B"); const auto& cut = ca.boundary(); CellularCensusSetBoundary boundary{cut.world, cut.frame, cut.tick, cut.owner_format, cut.rules, cut.quantum_microseconds, cut.projection_version}; const std::array required{1, 2}; const std::array observations{cb, ca}; const auto set = checked(CellularCensusSet::observe(boundary, required, observations), "scoped plans"); require(set.boundary().projection_version == 2 && checked(set.query(100, Scope::exact_genome), "scope founder A") == query(ca, 100) && checked(set.query(200, Scope::descendants), "scope founder B") == query(cb, 200), "scoped plan observation lost independent measures"); boundary.projection_version = 1; require(!CellularCensusSet::observe(boundary, required, observations), "mixed projection admitted"); } } // namespace int main() try { family_and_history(); scoped(); return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }