Files
aevolution/tests/cellular_region_step_tests.cpp
T
dingo 9984f78940
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 1m53s
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 (postgres) (push) Canceled after 50s
Foundation / checks (client) (push) Canceled after 2m59s
Foundation / checks (network) (push) Canceled after 2m22s
Close cellular starvation into corpse transfer
2026-10-10 21:55:07 -04:00

110 lines
5.0 KiB
C++

#include "evo/biology/cellular_region_step.hpp"
#include <array>
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
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<bool>(__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<CellularHabitat> 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}, {{{1}, {2}, {3}, {4}}}}, {{0}, {{{1}, {1}, {1}, {1}}}},
{{17}, {}}, budget()};
auto starved_budget = budget();
starved_budget.maintenance = {1'000};
starved_life.budget = starved_budget;
const std::array starved{starved_life};
const auto result = propose_cellular_region_step(4, {3'000, 2'000}, starved, *map);
CHECK(result);
CHECK(result->begin_tick == 4 && result->end_tick == 5);
CHECK(result->lives.empty() && result->starved.size() == 1);
CHECK(result->starved[0] == CellularCorpseCandidate{2, {0, 0}, {{0}, {{{2}, {3}, {4}, {5}}}}});
CHECK(result->heat_released.value == 17);
CHECK(result->patches.empty());
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: shared claims and starvation closure");
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
}