#include "evo/biology/cellular_uptake.hpp" #include #include #include #include #include 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 uptake line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) MetabolicBudget budget() { MetabolicBudget result{}; result.rate_microseconds = 1'000'000; result.intake_limit = {{1'001}, {{{101}, {53}, {31}, {17}}}}; return result; } void channel_limited_atomic_proposal() { constexpr auto maximum = std::numeric_limits::max(); const Inventory patch{{101}, {{{100}, {1}, {100}, {50}}}}; const Inventory reserve{{49}, {{{maximum - 3}, {0}, {0}, {0}}}}; const auto result = propose_cellular_uptake(patch, reserve, budget()); CHECK(result); CHECK(result->acquired == Inventory{{50}, {{{3}, {1}, {1}, {0}}}}); CHECK(result->patch_after == Inventory{{51}, {{{97}, {0}, {99}, {50}}}}); CHECK(result->reserve_after == Inventory{{99}, {{{maximum}, {1}, {1}, {0}}}}); CHECK(result->patch_after.energy.value + result->reserve_after.energy.value == patch.energy.value + reserve.energy.value); for (std::size_t channel = 0; channel < patch.matter.size(); ++channel) { CHECK(patch.matter[channel].value - result->patch_after.matter[channel].value == result->acquired.matter[channel].value); CHECK(result->reserve_after.matter[channel].value - reserve.matter[channel].value == result->acquired.matter[channel].value); } } void empty_and_full_channels() { constexpr auto maximum = std::numeric_limits::max(); const auto no_stock = propose_cellular_uptake({}, {}, budget()); CHECK(no_stock && no_stock->acquired == Inventory{} && no_stock->patch_after == Inventory{}); Inventory patch{}; patch.energy.value = maximum; patch.matter.fill({maximum}); Inventory reserve{}; reserve.energy.value = maximum; reserve.matter.fill({maximum}); const auto full = propose_cellular_uptake(patch, reserve, budget()); CHECK(full && full->acquired == Inventory{}); CHECK(full->patch_after == patch && full->reserve_after == reserve); } void unsupported_rate_period() { auto invalid = budget(); invalid.rate_microseconds = 50'000; CHECK(!propose_cellular_uptake({}, {}, invalid)); } } // namespace int main() { try { channel_limited_atomic_proposal(); empty_and_full_channels(); unsupported_rate_period(); std::puts("PASS cellular uptake: finite channel limits and atomic proposal"); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } }