Births validated each parent against the progressively modified candidate, so a child allocated earlier in the same batch was already active and could fund a further birth. Two generations could appear in one tick, which ADR-0028 forbids. The contract was already correct; the implementation did not enforce it. Fix parent eligibility on entry to the reproduction phase, after terminal facts and feeding, and check every intent before applying any birth. A rejected cascade leaves the whole tick unchanged, as before. The regression test admits one intent whose parent is created by the preceding intent and requires unborn_actor with balances, progress, receipts and slot states untouched. It fails against the unguarded implementation and passes with the guard. The same pair succeeds across two ticks and records the grandchild ancestry. Native Release, clang-tidy analysis, ASan and format checks were rerun and pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
474 lines
26 KiB
C++
474 lines
26 KiB
C++
#include "evo/core/random.hpp"
|
|
#include "evo/processes/feeding.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
namespace {
|
|
using namespace evo::processes;
|
|
using namespace evo::accounting;
|
|
void check(bool condition, std::source_location location = std::source_location::current()) {
|
|
if (!condition) {
|
|
throw std::runtime_error("feeding line " + std::to_string(location.line()));
|
|
}
|
|
}
|
|
#define CHECK(expression) check(static_cast<bool>(expression))
|
|
Inventory bundle(std::uint64_t value) {
|
|
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
|
|
}
|
|
ProducerConfig producer() {
|
|
return {{{10}, 1}, {2, 1}, 1000, {1}, bundle(1).matter};
|
|
}
|
|
std::vector<Inventory> copy(const FeedingCommunity& community) {
|
|
return {community.balances().begin(), community.balances().end()};
|
|
}
|
|
Inventory sum(const FeedingCommunity& community) {
|
|
Inventory total{};
|
|
for (const auto& inventory : community.balances()) {
|
|
CHECK(inventory.energy.value <=
|
|
std::numeric_limits<std::uint64_t>::max() - total.energy.value);
|
|
total.energy.value += inventory.energy.value;
|
|
for (std::size_t index = 0; index < 4; ++index) {
|
|
CHECK(inventory.matter[index].value <=
|
|
std::numeric_limits<std::uint64_t>::max() - total.matter[index].value);
|
|
total.matter[index].value += inventory.matter[index].value;
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
void capture_feeding_and_terminal() {
|
|
const std::array configs{ConsumerConfig{{}, bundle(100), 500},
|
|
ConsumerConfig{{}, bundle(100), 1000}};
|
|
auto community = FeedingCommunity::create(producer(), {100}, bundle(20).matter, configs);
|
|
CHECK(community);
|
|
const auto total = sum(*community);
|
|
// Tick 1 produces 2 material units and retains 8 energy after growth.
|
|
const std::array meal{FeedIntent{1, 2, 1, {{8}, bundle(2).matter}}};
|
|
const auto result = community->advance(evo::Tick{1}, meal);
|
|
CHECK(result && result->size() == 1 && result->front().target_depleted);
|
|
CHECK(result->front().assimilated == (Inventory{{4}, bundle(1).matter}));
|
|
CHECK(community->balances()[3].energy.value == 6);
|
|
CHECK(community->balances().back().matter == bundle(1).matter);
|
|
CHECK(community->active(1) && !*community->active(1));
|
|
const auto progress = community->progress();
|
|
const auto source = community->balances()[0];
|
|
CHECK(community->advance(evo::Tick{2}));
|
|
CHECK(community->balances()[0] == source);
|
|
CHECK(community->progress().capture_remainder == progress.capture_remainder);
|
|
const std::array predation{FeedIntent{1, 3, 2, {{4}, bundle(1).matter}}};
|
|
CHECK(community->advance(evo::Tick{3}, predation));
|
|
CHECK(!*community->active(2));
|
|
const auto before = copy(*community);
|
|
const std::array dead_action{FeedIntent{1, 2, 3, bundle(1)}};
|
|
const auto denied = community->advance(evo::Tick{4}, dead_action);
|
|
CHECK(!denied && denied.error() == FeedingError::inactive_actor);
|
|
CHECK(copy(*community) == before && community->progress().tick.value == 3);
|
|
CHECK(sum(*community) == total);
|
|
}
|
|
void validation_and_rollback() {
|
|
const std::array configs{ConsumerConfig{bundle(10), bundle(10), 1000},
|
|
ConsumerConfig{bundle(10), bundle(10), 1000}};
|
|
auto community = FeedingCommunity::create(producer(), {100}, bundle(20).matter, configs);
|
|
CHECK(community);
|
|
const auto before = copy(*community);
|
|
const auto progress = community->progress();
|
|
const auto reject = [&](std::span<const FeedIntent> inputs) {
|
|
CHECK(!community->advance(evo::Tick{1}, inputs));
|
|
CHECK(copy(*community) == before && community->progress() == progress);
|
|
CHECK(*community->active(1) && *community->active(2) && *community->active(3));
|
|
};
|
|
std::array inputs{FeedIntent{1, 2, 1, bundle(2)}, FeedIntent{2, 3, 1, bundle(1)}};
|
|
reject(inputs); // First takes all producer matter; second cannot also take it.
|
|
inputs[1].sequence = 1;
|
|
reject(inputs);
|
|
inputs[1].sequence = 0;
|
|
reject(inputs);
|
|
inputs[1] = {2, 2, 1, bundle(1)};
|
|
reject(inputs); // Cannot split intake cap.
|
|
for (const auto target : {0U, 2U, 999U}) {
|
|
const std::array invalid{FeedIntent{1, 2, target, bundle(1)}};
|
|
reject(invalid);
|
|
}
|
|
const std::array excessive{FeedIntent{1, 2, 3, bundle(11)}};
|
|
reject(excessive);
|
|
const std::array zero{FeedIntent{1, 2, 3, {}}};
|
|
reject(zero);
|
|
std::vector<FeedIntent> many(max_consumers + 1, FeedIntent{1, 2, 3, bundle(1)});
|
|
reject(many);
|
|
CHECK(!community->advance(evo::Tick{0}));
|
|
CHECK(!community->advance(evo::Tick{2}));
|
|
CHECK(!community->active(0));
|
|
CHECK(!FeedingCommunity::create(producer(), {}, {}, {}));
|
|
auto bad = configs;
|
|
bad[0].assimilation_per_thousand = 1001;
|
|
CHECK(!FeedingCommunity::create(producer(), {}, {}, bad));
|
|
std::vector<ConsumerConfig> too_many(max_consumers + 1, configs[0]);
|
|
CHECK(!FeedingCommunity::create(producer(), {}, {}, too_many));
|
|
too_many.pop_back();
|
|
CHECK(FeedingCommunity::create(producer(), {}, {}, too_many));
|
|
}
|
|
void corpse_preservation_and_rollback() {
|
|
const std::array configs{ConsumerConfig{bundle(5), bundle(10), 1000}};
|
|
auto community = FeedingCommunity::create(producer(), {100}, bundle(20).matter, configs);
|
|
CHECK(community);
|
|
CHECK(!community->corpse(0) && !community->corpse(1));
|
|
CHECK(community->advance(evo::Tick{1}));
|
|
const auto before = copy(*community);
|
|
const auto progress = community->progress();
|
|
const std::array deaths{TerminalEvent{1}, TerminalEvent{2}};
|
|
const std::array bad_feed{FeedIntent{1, 2, 1, bundle(1)}};
|
|
CHECK(!community->advance(evo::Tick{2}, bad_feed, deaths));
|
|
CHECK(copy(*community) == before && community->progress() == progress);
|
|
CHECK(*community->active(1) && *community->active(2));
|
|
for (const auto actor : {0U, 3U}) {
|
|
const std::array bad{TerminalEvent{actor}};
|
|
CHECK(!community->advance(evo::Tick{2}, {}, bad));
|
|
}
|
|
const std::array duplicate{TerminalEvent{1}, TerminalEvent{1}};
|
|
CHECK(!community->advance(evo::Tick{2}, {}, duplicate));
|
|
const std::array excess{TerminalEvent{1}, TerminalEvent{2}, TerminalEvent{1}};
|
|
CHECK(!community->advance(evo::Tick{2}, {}, excess));
|
|
CHECK(community->advance(evo::Tick{2}, {}, deaths));
|
|
CHECK(copy(*community) == before); // Death relabels ownership without losing inventory.
|
|
CHECK(community->corpse(1) && *community->corpse(1) == before[2]);
|
|
CHECK(community->corpse(2) && *community->corpse(2) == bundle(5));
|
|
CHECK(community->last_decomposed() == Inventory{});
|
|
const auto dead_progress = community->progress();
|
|
CHECK(!community->advance(evo::Tick{3}, {}, deaths));
|
|
CHECK(community->progress() == dead_progress && copy(*community) == before);
|
|
CHECK(community->advance(evo::Tick{3}));
|
|
CHECK(copy(*community) == before && !*community->active(1) && !*community->active(2));
|
|
}
|
|
void decomposition_reference() {
|
|
for (std::uint64_t seed = 0; seed < 16; ++seed) {
|
|
evo::Random random{seed};
|
|
const auto opening = 1 + random.next() % 100;
|
|
const auto cap = 1 + random.next() % 7;
|
|
const std::array configs{ConsumerConfig{bundle(opening), {}, 0},
|
|
ConsumerConfig{bundle(opening), {}, 0}};
|
|
auto community = FeedingCommunity::create(producer(), {}, {}, configs, {bundle(cap)});
|
|
CHECK(community);
|
|
auto replay = *community;
|
|
const auto total = sum(*community);
|
|
const std::array deaths{TerminalEvent{2}, TerminalEvent{3}};
|
|
const std::array reversed{TerminalEvent{3}, TerminalEvent{2}};
|
|
std::uint64_t previous{};
|
|
for (std::uint64_t tick = 1; tick <= 256; ++tick) {
|
|
CHECK(community->advance(evo::Tick{tick}, {},
|
|
tick == 1 ? std::span{deaths}
|
|
: std::span<const TerminalEvent>{}));
|
|
CHECK(
|
|
replay.advance(evo::Tick{tick}, {},
|
|
tick == 1 ? std::span{reversed} : std::span<const TerminalEvent>{}));
|
|
const auto drained = std::min(tick * cap, 2 * opening);
|
|
const auto first = opening - std::min(opening, drained);
|
|
const auto second = 2 * opening - drained - first;
|
|
CHECK(*community->corpse(2) == bundle(first));
|
|
CHECK(*community->corpse(3) == bundle(second));
|
|
CHECK(community->last_decomposed() == bundle(drained - previous));
|
|
CHECK(community->balances()[1].matter == bundle(drained).matter);
|
|
CHECK(community->balances()[3].energy.value == drained);
|
|
CHECK(sum(*community) == total && copy(*community) == copy(replay));
|
|
CHECK(community->progress() == replay.progress());
|
|
CHECK(!*community->active(2) && !*community->active(3));
|
|
previous = drained;
|
|
}
|
|
}
|
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
|
const std::array configs{ConsumerConfig{bundle(maximum), {}, 0}};
|
|
auto extreme = FeedingCommunity::create(producer(), {}, {}, configs, {bundle(maximum)});
|
|
CHECK(extreme);
|
|
const std::array death{TerminalEvent{2}};
|
|
CHECK(extreme->advance(evo::Tick{1}, {}, death));
|
|
CHECK(*extreme->corpse(2) == Inventory{} && extreme->last_decomposed() == bundle(maximum));
|
|
CHECK(sum(*extreme) == bundle(maximum));
|
|
}
|
|
void nutrient_return_and_waste() {
|
|
const std::array configs{ConsumerConfig{bundle(1), bundle(10), 0}};
|
|
auto community = FeedingCommunity::create(producer(), {100}, {}, configs, {bundle(1)});
|
|
CHECK(community);
|
|
const auto total = sum(*community);
|
|
const std::array death{TerminalEvent{2}};
|
|
CHECK(community->advance(evo::Tick{1}, {}, death));
|
|
CHECK(community->balances()[2].matter == Inventory{}.matter);
|
|
CHECK(community->balances()[1].matter == bundle(1).matter);
|
|
CHECK(community->advance(evo::Tick{2}));
|
|
CHECK(community->balances()[2].matter == bundle(1).matter);
|
|
CHECK(community->balances()[1].matter == Inventory{}.matter);
|
|
CHECK(sum(*community) == total && community->last_decomposed() == Inventory{});
|
|
|
|
const std::array feeder{ConsumerConfig{{}, bundle(10), 0}};
|
|
auto waste = FeedingCommunity::create(producer(), {100}, bundle(1).matter, feeder, {bundle(1)});
|
|
CHECK(waste);
|
|
const std::array meal{FeedIntent{1, 2, 1, bundle(1)}};
|
|
CHECK(waste->advance(evo::Tick{1}, meal));
|
|
CHECK(waste->balances().back() == Inventory{});
|
|
CHECK(waste->balances()[1].matter == bundle(1).matter);
|
|
CHECK(waste->last_decomposed() == (Inventory{{}, bundle(1).matter}));
|
|
const auto before = copy(*waste);
|
|
const auto diagnostic = waste->last_decomposed();
|
|
const std::array invalid{FeedIntent{1, 2, 1, bundle(100)}};
|
|
CHECK(!waste->advance(evo::Tick{2}, invalid));
|
|
CHECK(copy(*waste) == before && waste->last_decomposed() == diagnostic);
|
|
|
|
const std::array competing{ConsumerConfig{bundle(1), {}, 0}, feeder[0]};
|
|
auto shared =
|
|
FeedingCommunity::create(producer(), {100}, bundle(1).matter, competing, {bundle(1)});
|
|
CHECK(shared);
|
|
const std::array feeding{FeedIntent{1, 3, 1, bundle(1)}};
|
|
CHECK(shared->advance(evo::Tick{1}, feeding, death));
|
|
CHECK(*shared->corpse(2) == Inventory{});
|
|
CHECK(shared->last_decomposed() == bundle(1));
|
|
CHECK(shared->balances().back().matter == bundle(1).matter); // Corpse used the shared cap.
|
|
|
|
const Inventory mixed{{0}, {Femtograms{1}, Femtograms{0}, Femtograms{2}, Femtograms{3}}};
|
|
const std::array funded{ConsumerConfig{bundle(5), {}, 0}};
|
|
auto channels = FeedingCommunity::create(producer(), {}, {}, funded, {mixed});
|
|
CHECK(channels && channels->advance(evo::Tick{1}, {}, death));
|
|
CHECK(channels->last_decomposed() == mixed);
|
|
CHECK(*channels->corpse(2) ==
|
|
(Inventory{{5}, {Femtograms{4}, Femtograms{5}, Femtograms{3}, Femtograms{2}}}));
|
|
}
|
|
void ordered_replay() {
|
|
for (std::uint64_t seed = 0; seed < 32; ++seed) {
|
|
evo::Random random{seed};
|
|
const std::array configs{ConsumerConfig{bundle(100), bundle(2), 750},
|
|
ConsumerConfig{bundle(100), bundle(2), 500}};
|
|
auto a = FeedingCommunity::create(producer(), {10'000}, bundle(1000).matter, configs);
|
|
CHECK(a);
|
|
auto b = *a;
|
|
const auto total = sum(*a);
|
|
for (std::uint64_t tick = 1; tick <= 200; ++tick) {
|
|
// Capture/growth creates two recipe units, shared by two feeders.
|
|
std::array inputs{FeedIntent{1, 2, 1, bundle(1)}, FeedIntent{2, 3, 1, bundle(1)}};
|
|
const auto expected = a->advance(evo::Tick{tick}, inputs);
|
|
if (random.next() % 2 != 0) {
|
|
std::ranges::reverse(inputs);
|
|
}
|
|
const auto actual = b.advance(evo::Tick{tick}, inputs);
|
|
CHECK(expected && actual && *expected == *actual);
|
|
CHECK(copy(*a) == copy(b) && a->progress() == b.progress());
|
|
CHECK(sum(*a) == total);
|
|
}
|
|
}
|
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
|
for (const auto efficiency : {0U, 1000U}) {
|
|
const std::array configs{
|
|
ConsumerConfig{bundle(maximum), bundle(maximum), 1000},
|
|
ConsumerConfig{{}, bundle(maximum), static_cast<std::uint16_t>(efficiency)}};
|
|
auto community = FeedingCommunity::create(producer(), {}, {}, configs);
|
|
CHECK(community);
|
|
const std::array inputs{FeedIntent{1, 3, 2, bundle(maximum)}};
|
|
const auto result = community->advance(evo::Tick{1}, inputs);
|
|
CHECK(result && result->front().assimilated == bundle(efficiency == 0 ? 0 : maximum));
|
|
CHECK(sum(*community) == bundle(maximum));
|
|
}
|
|
}
|
|
void funded_reproduction() {
|
|
const std::array configs{ConsumerConfig{bundle(100), bundle(100), 1000}};
|
|
auto community =
|
|
FeedingCommunity::create(producer(), {100}, bundle(20).matter, configs, {}, {2, 750, {}});
|
|
CHECK(community);
|
|
const auto total = sum(*community);
|
|
// Four producer reservoirs, one consumer, two reserved slots and waste.
|
|
CHECK(community->balances().size() == 8);
|
|
CHECK(*community->born(2) && !*community->born(3) && !*community->born(4));
|
|
CHECK(!community->corpse(3) && community->corpse(3).error() == FeedingError::unborn_actor);
|
|
CHECK(!community->parent(3) && community->parent(3).error() == FeedingError::unborn_actor);
|
|
CHECK(!community->origin(4) && community->origin(4).error() == FeedingError::unborn_actor);
|
|
CHECK(community->parent(2) && *community->parent(2) == 0);
|
|
CHECK(community->origin(2) && community->origin(2)->value == 0);
|
|
const std::array early{FeedIntent{1, 2, 3, bundle(1)}};
|
|
const auto denied = community->advance(evo::Tick{1}, early);
|
|
CHECK(!denied && denied.error() == FeedingError::unborn_actor);
|
|
// 750/1000 of eight units endows six; the remainder is real heat and waste.
|
|
const std::array birth{ReproduceIntent{1, 2, bundle(8)}};
|
|
CHECK(community->advance(evo::Tick{1}, {}, {}, birth));
|
|
CHECK(community->last_births().size() == 1);
|
|
CHECK(community->last_births()[0] == (ReproduceReceipt{1, 3, bundle(6), bundle(2)}));
|
|
CHECK(community->balances()[4] == bundle(92) && community->balances()[5] == bundle(6));
|
|
CHECK(community->balances()[3].energy.value == 4); // Growth heat plus construction heat.
|
|
CHECK(community->balances().back() == (Inventory{{}, bundle(2).matter}));
|
|
CHECK(*community->born(3) && *community->active(3));
|
|
CHECK(*community->parent(3) == 2 && community->origin(3)->value == 1);
|
|
CHECK(sum(*community) == total);
|
|
// The child acts from the next tick using its duplicated parent configuration.
|
|
const std::array meal{FeedIntent{1, 3, 2, bundle(1)}};
|
|
CHECK(community->advance(evo::Tick{2}, meal));
|
|
CHECK(community->balances()[4] == bundle(91) && community->balances()[5] == bundle(7));
|
|
CHECK(community->last_births().empty() && sum(*community) == total);
|
|
}
|
|
void reproduction_limits_and_rollback() {
|
|
const std::array configs{ConsumerConfig{bundle(100), bundle(100), 1000}};
|
|
auto community = FeedingCommunity::create(producer(), {100}, bundle(20).matter, configs, {},
|
|
{1, 1000, bundle(10)});
|
|
CHECK(community);
|
|
const auto before = copy(*community);
|
|
const auto progress = community->progress();
|
|
const auto reject = [&](std::span<const ReproduceIntent> births, FeedingError expected) {
|
|
const auto result = community->advance(evo::Tick{1}, {}, {}, births);
|
|
CHECK(!result && result.error() == expected);
|
|
CHECK(copy(*community) == before && community->progress() == progress);
|
|
CHECK(community->last_births().empty() && *community->active(2) && !*community->born(3));
|
|
};
|
|
const std::array small{ReproduceIntent{1, 2, bundle(9)}};
|
|
reject(small, FeedingError::invalid_endowment); // Below the configured endowment floor.
|
|
const std::array nothing{ReproduceIntent{1, 2, {}}};
|
|
reject(nothing, FeedingError::invalid_endowment);
|
|
const std::array huge{ReproduceIntent{1, 2, bundle(101)}};
|
|
reject(huge, FeedingError::insufficient_inventory);
|
|
const std::array reserved{ReproduceIntent{1, 3, bundle(10)}};
|
|
reject(reserved, FeedingError::unborn_actor);
|
|
// Two reserved slots, so an unchecked cascade could otherwise fund both births.
|
|
const std::array lineage{ConsumerConfig{bundle(100), bundle(100), 1000}};
|
|
auto generations =
|
|
FeedingCommunity::create(producer(), {100}, bundle(20).matter, lineage, {}, {2, 1000, {}});
|
|
CHECK(generations);
|
|
const auto untouched = copy(*generations);
|
|
const std::array cascade{ReproduceIntent{1, 2, bundle(10)}, ReproduceIntent{2, 3, bundle(5)}};
|
|
const auto chained = generations->advance(evo::Tick{1}, {}, {}, cascade);
|
|
CHECK(!chained && chained.error() == FeedingError::unborn_actor);
|
|
CHECK(copy(*generations) == untouched && generations->progress().tick.value == 0);
|
|
CHECK(generations->last_births().empty());
|
|
CHECK(!*generations->born(3) && !*generations->born(4));
|
|
// The same pair succeeds once the child has existed since the start of a tick.
|
|
const std::array parent_only{ReproduceIntent{1, 2, bundle(10)}};
|
|
CHECK(generations->advance(evo::Tick{1}, {}, {}, parent_only));
|
|
CHECK(generations->last_births()[0].child == 3);
|
|
const std::array grandchild{ReproduceIntent{1, 3, bundle(5)}};
|
|
CHECK(generations->advance(evo::Tick{2}, {}, {}, grandchild));
|
|
CHECK(generations->last_births()[0].child == 4 && *generations->parent(4) == 3);
|
|
for (const auto parent : {0U, 1U, 99U}) {
|
|
const std::array invalid{ReproduceIntent{1, parent, bundle(10)}};
|
|
reject(invalid, FeedingError::invalid_batch);
|
|
}
|
|
const std::array unsequenced{ReproduceIntent{0, 2, bundle(10)}};
|
|
reject(unsequenced, FeedingError::invalid_batch);
|
|
const std::array twice{ReproduceIntent{1, 2, bundle(10)}, ReproduceIntent{2, 2, bundle(10)}};
|
|
reject(twice, FeedingError::invalid_batch); // One funded birth per parent per tick.
|
|
const std::vector<ReproduceIntent> many(max_consumers + 1, ReproduceIntent{1, 2, bundle(10)});
|
|
reject(many, FeedingError::invalid_batch);
|
|
// Energy is abundant but one element is not; the whole tick must roll back.
|
|
const std::array starved{ConsumerConfig{{{1000}, {Femtograms{1}}}, {}, 0}};
|
|
auto scarce = FeedingCommunity::create(producer(), {}, {}, starved, {}, {1, 1000, {}});
|
|
CHECK(scarce);
|
|
const auto opening = copy(*scarce);
|
|
const std::array unfunded{ReproduceIntent{1, 2, {{10}, {Femtograms{2}}}}};
|
|
const auto refused = scarce->advance(evo::Tick{1}, {}, {}, unfunded);
|
|
CHECK(!refused && refused.error() == FeedingError::insufficient_inventory);
|
|
CHECK(copy(*scarce) == opening && !*scarce->born(3));
|
|
const std::array affordable{ReproduceIntent{1, 2, {{10}, {Femtograms{1}}}}};
|
|
CHECK(scarce->advance(evo::Tick{1}, {}, {}, affordable));
|
|
const std::array pair{ConsumerConfig{bundle(100), bundle(100), 1000},
|
|
ConsumerConfig{bundle(100), bundle(100), 1000}};
|
|
auto limited =
|
|
FeedingCommunity::create(producer(), {100}, bundle(20).matter, pair, {}, {1, 1000, {}});
|
|
CHECK(limited);
|
|
const auto available = copy(*limited);
|
|
const std::array both{ReproduceIntent{1, 2, bundle(10)}, ReproduceIntent{2, 3, bundle(10)}};
|
|
const auto overflowing = limited->advance(evo::Tick{1}, {}, {}, both);
|
|
CHECK(!overflowing && overflowing.error() == FeedingError::no_capacity);
|
|
CHECK(copy(*limited) == available); // Capacity rejects; it never funds a smaller child.
|
|
const std::array single{ReproduceIntent{1, 2, bundle(10)}};
|
|
CHECK(limited->advance(evo::Tick{1}, {}, {}, single));
|
|
CHECK(limited->last_births()[0].child == 4);
|
|
const std::array queued{ReproduceIntent{1, 3, bundle(10)}};
|
|
const auto full = limited->advance(evo::Tick{2}, {}, {}, queued);
|
|
CHECK(!full && full.error() == FeedingError::no_capacity);
|
|
const std::array death{TerminalEvent{2}};
|
|
CHECK(limited->advance(evo::Tick{2}, {}, death));
|
|
const auto orphaned = limited->advance(evo::Tick{3}, {}, {}, single);
|
|
CHECK(!orphaned && orphaned.error() == FeedingError::inactive_actor);
|
|
CHECK(*limited->parent(4) == 2 && limited->origin(4)->value == 1);
|
|
CHECK(!*limited->active(2) && *limited->parent(2) == 0); // Ancestry outlives the parent.
|
|
}
|
|
void lineage_identity_and_permutation() {
|
|
const std::array configs{ConsumerConfig{bundle(100), bundle(100), 1000}};
|
|
auto community =
|
|
FeedingCommunity::create(producer(), {}, {}, configs, {bundle(1000)}, {2, 1000, {}});
|
|
CHECK(community);
|
|
const auto total = sum(*community);
|
|
const std::array first{ReproduceIntent{1, 2, bundle(10)}};
|
|
CHECK(community->advance(evo::Tick{1}, {}, {}, first));
|
|
CHECK(community->last_births()[0].child == 3);
|
|
const std::array kill{TerminalEvent{3}};
|
|
CHECK(community->advance(evo::Tick{2}, {}, kill));
|
|
CHECK(!*community->active(3) && *community->corpse(3) == Inventory{});
|
|
CHECK(*community->parent(3) == 2 && community->origin(3)->value == 1);
|
|
const std::array second{ReproduceIntent{1, 2, bundle(10)}};
|
|
CHECK(community->advance(evo::Tick{3}, {}, {}, second));
|
|
CHECK(community->last_births()[0].child == 4); // A retired identity is never reused.
|
|
CHECK(*community->parent(4) == 2 && community->origin(4)->value == 3);
|
|
CHECK(*community->parent(3) == 2 && sum(*community) == total);
|
|
for (std::uint64_t seed = 0; seed < 32; ++seed) {
|
|
evo::Random random{seed};
|
|
const std::array parents{ConsumerConfig{bundle(1000), bundle(10), 1000},
|
|
ConsumerConfig{bundle(1000), bundle(10), 1000}};
|
|
auto a = FeedingCommunity::create(producer(), {10'000}, bundle(1000).matter, parents, {},
|
|
{8, 600, {}});
|
|
CHECK(a);
|
|
auto b = *a;
|
|
const auto conserved = sum(*a);
|
|
for (std::uint64_t tick = 1; tick <= 4; ++tick) {
|
|
std::array births{ReproduceIntent{1, 2, bundle(4)}, ReproduceIntent{2, 3, bundle(4)}};
|
|
CHECK(a->advance(evo::Tick{tick}, {}, {}, births));
|
|
if (random.next() % 2 != 0) {
|
|
std::ranges::reverse(births);
|
|
}
|
|
CHECK(b.advance(evo::Tick{tick}, {}, {}, births));
|
|
CHECK(copy(*a) == copy(b) && a->progress() == b.progress());
|
|
CHECK(std::ranges::equal(a->last_births(), b.last_births()));
|
|
CHECK(sum(*a) == conserved);
|
|
}
|
|
const std::array exhausted{ReproduceIntent{1, 2, bundle(4)}};
|
|
CHECK(!a->advance(evo::Tick{5}, {}, {}, exhausted));
|
|
}
|
|
for (const auto construction : {0U, 1000U}) {
|
|
const std::array funded{ConsumerConfig{bundle(50), {}, 0}};
|
|
auto endpoint = FeedingCommunity::create(producer(), {}, {}, funded, {},
|
|
{1, static_cast<std::uint16_t>(construction), {}});
|
|
CHECK(endpoint);
|
|
const auto conserved = sum(*endpoint);
|
|
const std::array birth{ReproduceIntent{1, 2, bundle(50)}};
|
|
CHECK(endpoint->advance(evo::Tick{1}, {}, {}, birth));
|
|
const auto receipt = endpoint->last_births()[0];
|
|
CHECK(receipt.endowment == bundle(construction == 0 ? 0 : 50));
|
|
CHECK(receipt.overhead == bundle(construction == 0 ? 50 : 0));
|
|
// A zero endowment is an empty living child, not a free one: the parent still paid.
|
|
CHECK(*endpoint->active(3) && sum(*endpoint) == conserved);
|
|
}
|
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
|
const std::array extremes{ConsumerConfig{bundle(maximum), {}, 0}};
|
|
auto extreme =
|
|
FeedingCommunity::create(producer(), {}, {}, extremes, {}, {1, 1000, bundle(maximum)});
|
|
CHECK(extreme);
|
|
const std::array birth{ReproduceIntent{1, 2, bundle(maximum)}};
|
|
CHECK(extreme->advance(evo::Tick{1}, {}, {}, birth));
|
|
CHECK(extreme->last_births()[0].endowment == bundle(maximum));
|
|
CHECK(*extreme->parent(3) == 2 && sum(*extreme) == bundle(maximum));
|
|
CHECK(!FeedingCommunity::create(producer(), {}, {}, configs, {}, {1, 1001, {}}));
|
|
const std::vector<ConsumerConfig> saturated(max_consumers, configs[0]);
|
|
CHECK(!FeedingCommunity::create(producer(), {}, {}, saturated, {}, {1, 1000, {}}));
|
|
CHECK(FeedingCommunity::create(producer(), {}, {}, saturated, {}, {0, 1000, {}}));
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
capture_feeding_and_terminal();
|
|
validation_and_rollback();
|
|
ordered_replay();
|
|
corpse_preservation_and_rollback();
|
|
decomposition_reference();
|
|
nutrient_return_and_waste();
|
|
funded_reproduction();
|
|
reproduction_limits_and_rollback();
|
|
lineage_identity_and_permutation();
|
|
std::cout << "PASS shared feeding, reproduction, lineage identity and conservation\n";
|
|
return 0;
|
|
} catch (...) {
|
|
return 1;
|
|
}
|