#include "evo/core/random.hpp" #include "evo/processes/feeding.hpp" #include #include #include #include #include #include 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(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 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::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::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 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 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 too_many(max_consumers + 1, configs[0]); CHECK(!FeedingCommunity::create(producer(), {}, {}, too_many)); too_many.pop_back(); CHECK(FeedingCommunity::create(producer(), {}, {}, too_many)); } 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::max(); for (const auto efficiency : {0U, 1000U}) { const std::array configs{ ConsumerConfig{bundle(maximum), bundle(maximum), 1000}, ConsumerConfig{{}, bundle(maximum), static_cast(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)); } } } // namespace int main() try { capture_feeding_and_terminal(); validation_and_rollback(); ordered_replay(); std::cout << "PASS shared feeding, rollback, terminality and conservation\n"; return 0; } catch (...) { return 1; }