Files
aevolution/tests/cellular_lives_tests.cpp
T
dingo 04cdc1e9c7
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (network) (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 41s
Foundation / checks (client) (push) Canceled after 42s
Foundation / checks (ecs) (push) Canceled after 38s
Retain cellular death provenance and growth
2026-10-10 22:25:06 -04:00

109 lines
4.7 KiB
C++

#include "evo/server/lifecycle/cellular_lives.hpp"
#include <array>
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
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("cellular lives line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
Inventory founder_price(std::uint64_t count) {
const auto endowment = evo::biology::primitive_endowment();
CHECK(endowment);
auto funding = endowment->body;
funding.energy.value = endowment->reserve.energy.value + endowment->construction.value;
funding.energy.value *= count;
for (auto& channel : funding.matter)
channel.value *= count;
return funding;
}
void owner_advances_and_closes_death_atomically() {
auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}});
CHECK(primitive);
LifeRequest request{};
request.account.back() = 1;
request.action = 1;
CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}}));
request.account.back() = 2;
CHECK((*primitive)->spawn(request, {2, 102, 202, 302, {1}}));
evo::biology::CellularHabitatConfig habitat_config{
90, 1, 7, 3'000, 2'000, {{{0, 0}, 29, {{10}, {{{10}, {10}, {10}, {10}}}}}}};
auto habitat = evo::biology::CellularHabitat::create(std::move(habitat_config));
CHECK(habitat);
auto created = CellularLives::create(
**primitive, std::move(*habitat), {{3'000, 2'000}, 1, 293'150});
CHECK(created);
auto& owner = **created;
CHECK(owner.world() == 90 && owner.region() == 1 && owner.frame() == 7);
CHECK(owner.lives().size() == 2 && owner.tick() == 1);
CHECK(owner.current_total() == owner.opening_total());
const std::array invalid{CellularLifeIntent{1, {}}, CellularLifeIntent{1, {}}};
const auto refused = owner.advance_tick(invalid);
CHECK(!refused && owner.tick() == 1 && owner.lives().size() == 2);
const auto first_life_id = owner.lives()[0].identity.life;
const auto second_life_id = owner.lives()[1].identity.life;
const std::array invalid_motion{
CellularLifeIntent{first_life_id, {}},
CellularLifeIntent{second_life_id, {{1'001, 0}, std::nullopt}}};
const auto partial = owner.advance_tick(invalid_motion);
CHECK(!partial && partial.error() == CellularLivesError::region_step && owner.tick() == 1 &&
owner.lives().size() == 2);
const std::array feed{CellularLifeIntent{first_life_id, {{0, 0}, {{0, 0}}}},
CellularLifeIntent{second_life_id, {{0, 0}, {{0, 0}}}}};
const auto fed = owner.advance_tick(feed);
CHECK(fed && owner.tick() == 2 && owner.lives().size() == 2);
CHECK(fed->lives.size() == 2 &&
fed->lives[0].candidate.intake_status == evo::biology::CellularIntakeStatus::acquired);
CHECK(owner.habitat().patches()[0].stock == (Inventory{{0}, {}}));
CHECK(owner.current_total() == owner.opening_total());
std::size_t steps{};
while (!owner.lives().empty() && steps < 2'000) {
std::vector<CellularLifeIntent> idle;
idle.reserve(owner.lives().size());
for (const auto& life : owner.lives())
idle.push_back({life.identity.life, {}});
const auto advanced = owner.advance_tick(idle);
CHECK(advanced && owner.current_total() == owner.opening_total());
++steps;
}
CHECK(owner.lives().empty() && owner.corpses().size() == 2 && steps < 2'000);
CHECK(owner.corpses()[0].organism == 101 && owner.corpses()[0].died_tick <= owner.tick());
CHECK(owner.corpses()[1].organism == 102 && owner.corpses()[1].died_tick <= owner.tick());
CHECK(owner.corpses()[0].life_id == 1 && owner.corpses()[0].source == 17 &&
owner.corpses()[0].growth == (evo::biology::GrowthState{1000, 0}) &&
owner.corpses()[0].extent_um == 20);
const auto lineage = owner.archive().organism(101);
CHECK(lineage && !lineage->alive && lineage->died.value == owner.corpses()[0].died_tick);
const auto second_lineage = owner.archive().organism(102);
CHECK(second_lineage && !second_lineage->alive &&
second_lineage->died.value == owner.corpses()[1].died_tick);
CHECK(owner.heat().energy.value != 0);
CHECK(owner.current_total() == owner.opening_total());
}
} // namespace
int main() {
try {
owner_advances_and_closes_death_atomically();
std::puts("PASS cellular lives: feeding, tick ownership and terminal closure");
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
}