#include "evo/biology/cellular_burn.hpp" #include #include #include #include // Matter as energy (rules 13, ADR-0179): energy first, then the largest burnable channel at its // rate; exclusions hold except for upkeep; nothing changes when the cost cannot be paid. namespace { using namespace evo; using namespace evo::biology; void check(bool condition, std::source_location at = std::source_location::current()) { if (!condition) throw std::runtime_error("cellular burn line " + std::to_string(at.line())); } #define CHECK(expression) check(static_cast(expression)) accounting::Inventory reserve(std::uint64_t energy, std::uint64_t c, std::uint64_t n, std::uint64_t p, std::uint64_t o) { return {{energy}, {accounting::Femtograms{c}, accounting::Femtograms{n}, accounting::Femtograms{p}, accounting::Femtograms{o}}}; } // 10 pJ per 1,000 fg on every channel unless stated. CellularBurn flat(std::uint8_t excluded = 0) { return cellular_burn({10, 10, 10, 10}, {100, 100, 100, 100}, excluded); } void energy_first() { auto r = reserve(500, 1'000, 1'000, 1'000, 1'000); const auto paid = cellular_pay(r, 300, flat(), false); CHECK(paid && paid->energy == 300 && paid->spent == accounting::Inventory{}); CHECK(r == reserve(200, 1'000, 1'000, 1'000, 1'000)); } void largest_channel_then_next() { // 100 pJ of energy, then 150 pJ from matter: carbon (largest) covers 100 pJ with 10,000 fg, // then nitrogen (now largest) covers the last 50 pJ with 5,000 fg. auto r = reserve(100, 10'000, 8'000, 1'000, 2'000); const auto paid = cellular_pay(r, 250, flat(), false); CHECK(paid && paid->energy == 100 && paid->spent == reserve(0, 10'000, 5'000, 0, 0)); CHECK(r == reserve(0, 0, 3'000, 1'000, 2'000)); // Ties go to the lower channel. auto tie = reserve(0, 3'000, 3'000, 0, 0); const auto first = cellular_pay(tie, 10, flat(), false); CHECK(first && first->spent == reserve(0, 1'000, 0, 0, 0)); } void whole_femtograms_round_up() { // 1 pJ at 10 pJ per 1,000 fg needs 100 fg; 1 pJ at 3 pJ per 1,000 fg needs 334 fg. auto r = reserve(0, 1'000, 0, 0, 0); const auto paid = cellular_pay(r, 1, flat(), false); CHECK(paid && paid->spent.matter[0].value == 100); auto odd = reserve(0, 1'000, 0, 0, 0); const auto thirds = cellular_pay(odd, 1, cellular_burn({3, 3, 3, 3}, {100, 100, 100, 100}, 0), false); CHECK(thirds && thirds->spent.matter[0].value == 334); } void exclusions_and_upkeep_override() { // Carbon excluded: nitrogen pays, though carbon is larger. auto r = reserve(0, 10'000, 2'000, 0, 0); const auto paid = cellular_pay(r, 10, flat(0b0001), false); CHECK(paid && paid->spent == reserve(0, 0, 1'000, 0, 0)); // Only excluded matter left: refused for thrust, paid for upkeep. auto kept = reserve(0, 10'000, 0, 0, 0); CHECK(cellular_payable_energy(kept, flat(0b0001), false) == 0); CHECK(!cellular_pay(kept, 10, flat(0b0001), false) && kept == reserve(0, 10'000, 0, 0, 0)); const auto upkeep = cellular_pay(kept, 10, flat(0b0001), true); CHECK(upkeep && upkeep->spent == reserve(0, 1'000, 0, 0, 0)); } void refusal_changes_nothing() { // 5 pJ of energy plus 10 pJ of matter cannot pay 16 pJ. auto r = reserve(5, 1'000, 0, 0, 0); CHECK(cellular_payable_energy(r, flat(), false) == 15); CHECK(!cellular_pay(r, 16, flat(), false) && r == reserve(5, 1'000, 0, 0, 0)); const auto exact = cellular_pay(r, 15, flat(), false); CHECK(exact && exact->energy == 5 && exact->spent == reserve(0, 1'000, 0, 0, 0) && r == reserve(0, 0, 0, 0, 0)); } void efficiency_scales_rates() { const auto burn = cellular_burn({10, 10, 10, 10}, {105, 99, 100, 100}, 0xFF); CHECK(burn.pj_per_kfg[0] == 10 && burn.pj_per_kfg[1] == 9 && burn.excluded == 0x0F); const auto finer = cellular_burn({10'000, 10'000, 0, 10'000}, {105, 99, 100, 100}, 0); CHECK(finer.pj_per_kfg[0] == 10'500 && finer.pj_per_kfg[1] == 9'900); // A zero rate cannot be burned. auto r = reserve(0, 0, 0, 5'000, 0); CHECK(cellular_payable_energy(r, finer, true) == 0 && !cellular_pay(r, 1, finer, true)); } // Every femtogram and picojoule is accounted: reserve before = reserve after + paid. void conservation_property() { for (std::uint64_t seed = 1; seed < 400; ++seed) { const auto x = seed * 2'654'435'761ULL; const auto before = reserve(x % 997, x % 50'021, (x >> 8) % 40'009, (x >> 16) % 3'001, (x >> 24) % 70'001); const auto burn = cellular_burn({7 + seed % 13, 9, 11 + seed % 5, 13}, {100, static_cast(80 + seed % 40), 100, 120}, static_cast(seed % 16)); auto after = before; const auto cost = (x >> 32) % 2'000; const auto paid = cellular_pay(after, cost, burn, seed % 3 == 0); if (!paid) { CHECK(after == before); continue; } CHECK(after.energy.value + paid->energy == before.energy.value); for (std::size_t c = 0; c < 4; ++c) CHECK(after.matter[c].value + paid->spent.matter[c].value == before.matter[c].value); CHECK(paid->spent.energy.value == 0); } } } // namespace int main() { try { energy_first(); largest_channel_then_next(); whole_femtograms_round_up(); exclusions_and_upkeep_override(); refusal_changes_nothing(); efficiency_scales_rates(); conservation_property(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } std::puts("PASS cellular matter-as-energy payments"); return 0; }