#include "evo/core/random.hpp" #include "evo/processes/producer.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("producer contract line " + std::to_string(location.line())); } } #define CHECK(expression) check(static_cast(expression)) ProducerConfig config() { return { {{5}, 2}, {3, 2}, 600, {1}, {Femtograms{1}, Femtograms{2}, Femtograms{3}, Femtograms{4}}}; } std::vector balances(const ProducerProcess& process) { return {process.balances().begin(), process.balances().end()}; } void audit(const ProducerProcess& process, std::uint64_t energy, const std::array& matter) { Inventory sum{}; for (const auto& balance : process.balances()) { CHECK(balance.energy.value <= energy - sum.energy.value); sum.energy.value += balance.energy.value; for (std::size_t index = 0; index < 4; ++index) { CHECK(balance.matter[index].value <= matter[index].value - sum.matter[index].value); sum.matter[index].value += balance.matter[index].value; } } CHECK(sum.energy.value == energy && sum.matter == matter); } void hand_calculated() { const std::array matter{Femtograms{10}, Femtograms{20}, Femtograms{30}, Femtograms{40}}; auto process = ProducerProcess::create(config(), {10}, matter); CHECK(process); const std::array expected{ ProducerReport{{2}, {1}, {1}, 1, 0}, ProducerReport{{3}, {2}, {1}, 2, 0}, ProducerReport{{2}, {1}, {1}, 1, 0}, ProducerReport{{3}, {2}, {1}, 2, 0}}; for (std::size_t index = 0; index < expected.size(); ++index) { const auto result = process->advance(evo::Tick{index + 1}); CHECK(result && *result == expected[index]); audit(*process, 10, matter); } CHECK(process->balances()[0].energy.value == 0); CHECK(process->balances()[2].energy.value == 0); CHECK(process->balances()[2].matter[0].value == 6); CHECK(process->balances()[3].energy.value == 10); const auto stopped = process->advance(evo::Tick{5}); CHECK(stopped && stopped->captured.value == 0 && stopped->grown_units == 0); CHECK(process->balances()[3].energy.value == 10); // Heat cannot power growth. } void limits_and_rejection() { const std::array matter{Femtograms{10}, Femtograms{20}, Femtograms{30}, Femtograms{40}}; for (int invalid = 0; invalid < 5; ++invalid) { auto settings = config(); if (invalid == 0) { settings.capture.ticks = 0; } if (invalid == 1) { settings.growth.ticks = 0; } if (invalid == 2) { settings.efficiency_per_thousand = 1001; } if (invalid == 3) { settings.growth_cost = {}; } if (invalid == 4) { settings.recipe = {}; } const auto result = ProducerProcess::create(settings, {10}, matter); CHECK(!result && result.error() == ProducerError::invalid_config); } auto process = ProducerProcess::create(config(), {10}, matter); CHECK(process); CHECK(process->advance(evo::Tick{1})); const auto before = balances(*process); const auto progress = process->progress(); for (const auto target : {0U, 1U, 3U}) { const auto result = process->advance(evo::Tick{target}); CHECK(!result && result.error() == ProducerError::wrong_tick); CHECK(process->progress() == progress && balances(*process) == before); } for (const auto efficiency : {0U, 1000U}) { auto settings = config(); settings.efficiency_per_thousand = static_cast(efficiency); settings.capture = {{std::numeric_limits::max()}, 1}; settings.growth = {0, 1}; auto full = ProducerProcess::create(settings, {std::numeric_limits::max()}, matter); CHECK(full); const auto result = full->advance(evo::Tick{1}); CHECK(result && result->retained.value == (efficiency == 0 ? 0 : std::numeric_limits::max())); audit(*full, std::numeric_limits::max(), matter); } auto boundary_config = config(); constexpr auto maximum = std::numeric_limits::max(); boundary_config.capture = {{maximum}, 2}; boundary_config.growth = {0, 1}; boundary_config.efficiency_per_thousand = 500; auto boundary = ProducerProcess::create(boundary_config, {maximum}, matter); CHECK(boundary); const auto first = boundary->advance(evo::Tick{1}); CHECK(first && first->captured.value == 9223372036854775807ULL); CHECK(first->retained.value == 4611686018427387903ULL); const auto second = boundary->advance(evo::Tick{2}); CHECK(second && second->captured.value == 9223372036854775808ULL); CHECK(second->retained.value == 4611686018427387904ULL); audit(*boundary, maximum, matter); boundary_config.capture = {{maximum}, 1}; boundary_config.growth = {maximum, 1}; boundary_config.efficiency_per_thousand = 1000; boundary_config.growth_cost = {maximum}; boundary_config.recipe = {Femtograms{maximum}, Femtograms{}, Femtograms{}, Femtograms{}}; auto massive = ProducerProcess::create(boundary_config, {maximum}, boundary_config.recipe); CHECK(massive); const auto grown = massive->advance(evo::Tick{1}); CHECK(grown && grown->grown_units == 1 && grown->unmet_growth_units == maximum - 1); audit(*massive, maximum, boundary_config.recipe); auto dark_config = config(); dark_config.capture.energy = {}; auto dark = ProducerProcess::create(dark_config, {10}, matter); CHECK(dark); CHECK(dark->advance(evo::Tick{1})->grown_units == 0); CHECK(dark->balances()[0].energy.value == 10); for (std::size_t limiting = 0; limiting < 4; ++limiting) { auto scarce = matter; scarce[limiting] = {}; auto limited = ProducerProcess::create(config(), {10}, scarce); CHECK(limited); CHECK(limited->advance(evo::Tick{1})->grown_units == 0); audit(*limited, 10, scarce); } } void fractional_reference_and_replay() { for (std::uint64_t seed = 0; seed < 32; ++seed) { evo::Random random{seed}; auto settings = config(); settings.capture = {{random.next() % 17}, static_cast(1 + random.next() % 13)}; settings.growth = {random.next() % 9, static_cast(1 + random.next() % 7)}; settings.efficiency_per_thousand = static_cast(random.next() % 1001); settings.growth_cost.value = 1 + random.next() % 5; const std::array matter{Femtograms{50}, Femtograms{100}, Femtograms{150}, Femtograms{200}}; constexpr std::uint64_t initial_energy = 200; auto process = ProducerProcess::create(settings, {initial_energy}, matter); CHECK(process); auto replay = *process; std::uint64_t total_capture{}, total_retained{}, total_growth{}; for (std::uint64_t tick = 1; tick <= 1000; ++tick) { // Small-domain independent reference: cumulative products fit u64. const auto cumulative_capture = tick * settings.capture.energy.value / settings.capture.ticks; const auto actual_capture = std::min(cumulative_capture, initial_energy); const auto retained = actual_capture * settings.efficiency_per_thousand / 1000; const auto growth_quota = tick * settings.growth.units / settings.growth.ticks - (tick - 1) * settings.growth.units / settings.growth.ticks; const auto grown = std::min({growth_quota, (retained - total_growth * settings.growth_cost.value) / settings.growth_cost.value, 50 - total_growth}); const auto result = process->advance(evo::Tick{tick}); const auto repeated = replay.advance(evo::Tick{tick}); CHECK(result && repeated && *result == *repeated); CHECK(result->captured.value == actual_capture - total_capture); CHECK(result->retained.value == retained - total_retained); CHECK(result->grown_units == grown); total_capture = actual_capture; total_retained = retained; total_growth += grown; CHECK(process->balances()[2].energy.value == total_retained - total_growth * settings.growth_cost.value); CHECK(process->balances()[2].matter[0].value == total_growth); CHECK(process->progress() == replay.progress()); CHECK(balances(*process) == balances(replay)); audit(*process, initial_energy, matter); } } } } // namespace int main() try { hand_calculated(); limits_and_rejection(); fractional_reference_and_replay(); std::cout << "PASS producer conservation, fractional reference and replay\n"; return 0; } catch (...) { return 1; }