284 lines
14 KiB
C++
284 lines
14 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));
|
|
}
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
capture_feeding_and_terminal();
|
|
validation_and_rollback();
|
|
ordered_replay();
|
|
corpse_preservation_and_rollback();
|
|
decomposition_reference();
|
|
nutrient_return_and_waste();
|
|
std::cout << "PASS shared feeding, rollback, terminality and conservation\n";
|
|
return 0;
|
|
} catch (...) {
|
|
return 1;
|
|
}
|