Files
aevolution/tests/environmental_community_tests.cpp
T

426 lines
18 KiB
C++

#include "evo/processes/environmental_community.hpp"
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::world;
using namespace evo::processes;
void check(bool b, std::source_location at = std::source_location::current()) {
if (!b)
throw std::runtime_error("environmental community line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr WorldIdentity identity{7, 9};
EnvironmentalConfig config() {
EnvironmentalConfig c;
c.world = 7;
c.frame = 9;
c.producer = 1;
c.consumer = 2;
c.source = 3;
c.tick_microseconds = 50000;
c.area_um2 = 1;
c.irradiance_numerator = 5;
c.irradiance_denominator = 3;
c.efficiency = 600;
c.growth_cost = 3;
c.recipe = {2, 4, 1, 1, 0};
c.intake.fill(100);
c.assimilation = 600;
c.producer_min_mK = c.consumer_min_mK = 270000;
c.producer_max_mK = c.consumer_max_mK = 310000;
return c;
}
std::unique_ptr<EnvironmentalTransfers>
resources(std::array<std::uint64_t, 10> e = {100, 100, 100, 100, 0, 0, 100, 0, 10, 0},
std::array<std::uint64_t, 10> p = {}, std::array<std::uint64_t, 10> consumer = {}) {
const FieldSite site{{0, 0, -16000}, {{0}, {0}, {0}, {10}, {20}, {3}}, {1}};
std::array<FieldOpening, 10> opening{};
for (std::size_t k = 0; k < 10; ++k)
opening[k] = {static_cast<Field>(k), {&e[k], 1}};
const FieldDefaults defaults{{evo::accounting::Femtograms{0}, evo::accounting::Femtograms{1000},
evo::accounting::Femtograms{0}}};
auto f = FieldStore::create(identity, 1, defaults, {&site, 1}, opening, 2000000);
CHECK(f);
auto t = ChunkStore::create({}, 1, 2000000, {{VolumeUnits{100}, VolumeUnits{100}}});
CHECK(t);
CHECK((*t)->load({0, 0, -1000}));
std::array<OrganismResources, 2> accounts{};
accounts[0].id = {1};
accounts[1].id = {2};
accounts[0].balances = p;
accounts[1].balances = consumer;
for (auto& a : accounts)
a.capacities.fill(1000);
auto r =
EnvironmentalTransfers::create(identity, std::move(*f), std::move(*t), accounts, 4000000);
CHECK(r);
return std::move(*r);
}
auto make(EnvironmentalConfig c = config()) {
auto o = EnvironmentalCommunity::create(c, resources(), 8000000);
CHECK(o);
return std::move(*o);
}
evo::accounting::Inventory audit(const EnvironmentalCommunity& o) {
evo::accounting::Inventory sum{};
constexpr std::array<std::size_t, 7> channel{3, 1, 2, 0, 1, 2, 3};
for (std::size_t k = 0; k < 10; ++k) {
auto amount = *o.resources().fields().read(0, static_cast<Field>(k));
for (const auto& a : o.resources().organisms())
amount += a.balances[k];
if (k < 7)
sum.matter[channel[k]].value += amount;
else
sum.energy.value += amount;
}
return sum;
}
void vectors() {
auto o = make();
const auto total = audit(*o);
EnvironmentalInput in{290000, 1, {}};
const std::array<std::uint64_t, 3> captured{1, 2, 2}, retained{0, 1, 2};
for (std::uint64_t t = 1; t <= 3; ++t) {
auto r = o->advance(t, in);
CHECK(r);
CHECK(r->captured == captured[t - 1] && r->retained == retained[t - 1]);
CHECK(audit(*o) == total);
}
CHECK(o->progress().source_remainder == 0 && o->progress().efficiency_remainder == 0);
auto c = config();
c.growth_numerator = 2;
std::array<std::uint64_t, 10> p{};
p[7] = 10;
auto growth =
EnvironmentalCommunity::create(c, resources({4, 1, 2, 8, 0, 0, 0, 0, 0, 0}, p), 8000000);
CHECK(growth);
auto r = (*growth)->advance(1, {290000, 0, {}});
CHECK(r && r->grown == 1 && r->unmet == 1);
CHECK((*growth)->resources().organisms()[0].balances[0] == 2 &&
(*growth)->resources().organisms()[0].balances[7] == 7);
p = {};
p[3] = 5;
p[4] = 3;
p[5] = 1;
p[7] = 7;
auto feed = EnvironmentalCommunity::create(config(), resources({}, p), 8000000);
CHECK(feed);
r = (*feed)->advance(1, {290000, 0, {5, 3, 1, 0, 7}});
CHECK(r);
CHECK(r->assimilated == std::array<std::uint64_t, 5>{3, 1, 0, 0, 4});
CHECK(r->waste == std::array<std::uint64_t, 5>{2, 2, 1, 0, 3});
CHECK(r->terminal_transition == 1);
auto frozen = (*feed)->advance(2, in);
CHECK(frozen && frozen->captured == 0);
CHECK(!(*feed)->advance(3, {290000, 0, {1, 0, 0, 0, 0}}));
}
void rollback_limits() {
auto c = config();
c.growth_numerator = 1;
auto o = make(c);
auto before = o->snapshot();
CHECK(before);
CHECK(!o->advance(1, {290000, 1, {100, 0, 0, 0, 0}}));
CHECK(o->snapshot() == before);
CHECK(!o->advance(2, {290000, 1, {}}));
CHECK(!o->advance(1, {0, 1, {}}));
CHECK(!o->advance(1, {290000, 2, {}}));
CHECK(o->snapshot() == before);
EnvironmentalInput input{290000, 1, {}};
auto report = o->advance(1, input);
CHECK(report);
auto after = o->snapshot();
CHECK(o->advance(1, input) == report);
input.source_enabled = 0;
CHECK(!o->advance(1, input));
CHECK(o->snapshot() == after);
auto off = make();
for (std::uint64_t t = 1; t < 5; ++t) {
auto r = off->advance(t, {290000, 0, {}});
CHECK(r && r->captured == 0);
}
CHECK(off->progress().source_remainder == 0);
auto cold = make();
CHECK(cold->advance(1, {269999, 1, {}})->captured == 0);
CHECK(cold->advance(2, {270000, 1, {}})->captured == 1);
CHECK(cold->advance(3, {310000, 1, {}})->captured == 2);
CHECK(cold->advance(4, {310001, 1, {}})->captured == 0);
auto exhausted = EnvironmentalCommunity::create(
config(), resources({0, 0, 0, 0, 0, 0, 0, 0, 2, 0}), 8000000);
CHECK(exhausted);
CHECK((*exhausted)->advance(1, {290000, 1, {}}));
auto progress = (*exhausted)->progress();
auto r = (*exhausted)->advance(2, {290000, 1, {}});
CHECK(r && r->source_status == 2);
CHECK((*exhausted)->progress().source_remainder == progress.source_remainder &&
(*exhausted)->progress().last_funded == 1);
c = config();
c.area_um2 = 201;
CHECK(!EnvironmentalCommunity::create(c, resources(), 8000000));
CHECK(!EnvironmentalCommunity::create(config(), resources(), 1));
}
void codec_replay() {
for (std::uint64_t seed = 0; seed < 8; ++seed) {
auto c = config();
c.growth_numerator = 1;
auto o = make(c);
auto continuous = make(c);
const auto total = audit(*o);
for (std::uint64_t t = 1; t <= 90; ++t) {
EnvironmentalInput input{290000, (t + seed) % 3 == 0 ? 0ULL : 1ULL, {}};
auto r = o->advance(t, input);
CHECK(continuous->advance(t, input) == r);
CHECK(r);
CHECK(audit(*o) == total);
if (t == 17 || t == 65 || t == 90) {
auto b = o->snapshot();
CHECK(b);
CHECK(continuous->snapshot() == b);
auto restored = EnvironmentalCommunity::restore(*b, identity, 8000000);
CHECK(restored);
CHECK((*restored)->snapshot() == b);
CHECK((*restored)->advance(t, input) == r);
if (t > 64)
CHECK(!(*restored)->advance(1, input));
o = std::move(*restored);
}
}
}
auto tight_resources = resources();
const auto tight_budget = sizeof(EnvironmentalCommunity) +
2 * EnvironmentalCommunity::scratch_bytes +
tight_resources->accounted_bytes();
auto tight = EnvironmentalCommunity::create(config(), std::move(tight_resources), tight_budget);
CHECK(tight);
CHECK((*tight)->advance(1, {290000, 1, {}}));
auto tight_bytes = (*tight)->snapshot();
CHECK(tight_bytes);
CHECK(EnvironmentalCommunity::restore(*tight_bytes, identity, tight_budget));
auto o = make();
auto initial = o->snapshot();
CHECK(initial);
CHECK(EnvironmentalCommunity::restore(*initial, identity, 8000000));
CHECK(o->advance(1, {290000, 1, {}}));
auto saved = o->snapshot();
CHECK(saved);
for (std::size_t n = 0; n < saved->size(); ++n)
CHECK(!EnvironmentalCommunity::restore(std::span{*saved}.first(n), identity, 8000000));
CHECK(!EnvironmentalCommunity::restore(*saved, {1, 2}, 8000000));
CHECK(!EnvironmentalCommunity::restore(*saved, identity, 1));
auto nested = o->resources().snapshot();
CHECK(nested);
CHECK(!EnvironmentalCommunity::restore(*nested, identity, 8000000));
auto fix = [](WorldBytes& b) {
std::uint64_t h = 14695981039346656037ULL;
for (auto v : std::span{b}.first(b.size() - 8))
h = (h ^ v) * 1099511628211ULL;
for (unsigned i = 0; i < 8; ++i)
b[b.size() - 8 + i] = static_cast<std::uint8_t>((h >> (8 * i)) & 255U);
};
for (std::size_t offset :
{std::size_t{8}, std::size_t{24}, std::size_t{24 + 8 * 9}, std::size_t{24 + 29 * 8 + 8},
std::size_t{24 + 29 * 8 + 6 * 8 + 36 * 8}, std::size_t{304}, std::size_t{664},
saved->size() - 9}) {
auto b = *saved;
b[offset] = 255;
fix(b);
CHECK(!EnvironmentalCommunity::restore(b, identity, 8000000));
}
auto corrupted = *saved;
corrupted[80] ^= 1;
CHECK(!EnvironmentalCommunity::restore(corrupted, identity, 8000000));
CHECK(o->snapshot() == saved);
}
void terminal_decay() {
auto c = config();
c.rules = 2;
c.decay.fill(3);
const std::array<std::uint64_t, 10> stock{5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
auto created = EnvironmentalCommunity::create(c, resources({}, stock, stock), 8000000);
CHECK(created);
auto o = std::move(*created);
const auto total = audit(*o);
const auto before = o->snapshot();
CHECK(before);
// A pending terminal fact invalidates feeding and rolls back the entire tick.
for (auto facts : {std::array<std::uint64_t, 2>{1, 0}, std::array<std::uint64_t, 2>{0, 1}}) {
CHECK(!o->advance(1, {290000, 1, {1, 0, 0, 0, 0}, facts}));
CHECK(o->snapshot() == before);
}
CHECK(!o->advance(1, {290000, 0, {}, {2, 0}}));
const EnvironmentalInput death{290000, 1, {}, {1, 1}};
auto report = o->advance(1, death);
CHECK(report);
CHECK(report->terminal_transition == 1 && report->consumer_transition == 1);
CHECK(report->captured == 0 && report->grown == 0);
for (std::size_t k = 0; k < 10; ++k) {
CHECK(report->decomposed[k] == 3);
CHECK(o->resources().organisms()[0].balances[k] == 2);
CHECK(o->resources().organisms()[1].balances[k] == 5);
}
const std::array<std::uint64_t, 10> returned{3, 6, 6, 3, 0, 0, 3, 0, 3, 6};
for (std::size_t k = 0; k < 10; ++k)
CHECK(o->resources().fields().read(0, static_cast<Field>(k)) == returned[k]);
CHECK(audit(*o) == total);
auto saved = o->snapshot();
CHECK(saved);
auto restored = EnvironmentalCommunity::restore(*saved, identity, 8000000);
CHECK(restored);
CHECK((*restored)->snapshot() == saved);
CHECK((*restored)->advance(1, death) == report);
CHECK(!(*restored)->advance(1, {290000, 0, {}, {1, 1}}));
CHECK(!(*restored)->advance(2, death)); // repeated death on a fresh tick
CHECK(!(*restored)->advance(2, {290000, 0, {1, 0, 0, 0, 0}}));
CHECK((*restored)->snapshot() == saved);
for (std::uint64_t t = 2; t <= 70; ++t) {
const EnvironmentalInput input{290000, 1, {}};
auto r = o->advance(t, input);
CHECK(r && (*restored)->advance(t, input) == r);
CHECK(audit(*o) == total);
CHECK(o->snapshot() == (*restored)->snapshot());
for (auto debit : r->decomposed)
CHECK(debit == (t < 4 ? 3 : t == 4 ? 1 : 0));
if (t == 2) {
CHECK(o->resources().organisms()[0].balances == std::array<std::uint64_t, 10>{});
CHECK(o->resources().organisms()[1].balances[4] == 4);
}
}
saved = o->snapshot();
CHECK(saved);
restored = EnvironmentalCommunity::restore(*saved, identity, 8000000);
CHECK(restored && (*restored)->snapshot() == saved);
CHECK(!(*restored)->advance(1, death)); // retired receipt cannot decay again
CHECK((*restored)->progress().producer_terminal == 1 &&
(*restored)->progress().consumer_terminal == 1);
// Full water destination leaves corpse water intact; zero caps freeze all inventory.
c.decay.fill(0);
auto frozen = EnvironmentalCommunity::create(c, resources({}, stock), 8000000);
CHECK(frozen && (*frozen)->advance(1, {290000, 0, {}, {1, 0}}));
CHECK((*frozen)->resources().organisms()[0].balances == stock);
c.decay.fill(100);
auto blocked = EnvironmentalCommunity::create(
c, resources({999, 0, 0, 0, 0, 0, 0, 0, 0, 0}, stock), 8000000);
CHECK(blocked);
auto r = (*blocked)->advance(1, {290000, 0, {}, {1, 0}});
CHECK(r && r->decomposed[0] == 1);
CHECK((*blocked)->resources().organisms()[0].balances[0] == 4);
r = (*blocked)->advance(2, {290000, 0, {}});
CHECK(r && r->decomposed[0] == 0);
saved = (*blocked)->snapshot();
CHECK(saved);
restored = EnvironmentalCommunity::restore(*saved, identity, 8000000);
CHECK(restored && (*restored)->resources().organisms()[0].balances[0] == 4);
// Consumer corpse supplies missing N/P; growth cannot spend it until the next tick.
c.growth_numerator = 1;
auto regrow = EnvironmentalCommunity::create(c,
resources({10, 0, 0, 20, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 9, 0, 0},
{0, 0, 0, 0, 2, 2, 0, 6, 0, 0}),
8000000);
CHECK(regrow);
const auto regrow_total = audit(**regrow);
r = (*regrow)->advance(1, {290000, 0, {}, {0, 1}});
CHECK(r && r->grown == 0 && r->decomposed[4] == 2 && r->decomposed[7] == 6);
r = (*regrow)->advance(2, {290000, 0, {}});
CHECK(r && r->grown == 1 && audit(**regrow) == regrow_total);
CHECK((*regrow)->resources().organisms()[0].balances[4] == 1);
// Extension data cannot be silently lost through the v1 codec.
c.rules = 1;
CHECK(!EnvironmentalCommunity::create(c, resources(), 8000000));
auto legacy = make();
CHECK(!legacy->advance(1, death));
}
void terminal_codec_validation() {
auto c = config();
c.rules = 2;
c.decay.fill(1);
auto created =
EnvironmentalCommunity::create(c, resources({}, {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}), 8000000);
CHECK(created);
CHECK((*created)->advance(1, {290000, 0, {}, {1, 1}}));
auto saved = (*created)->snapshot();
CHECK(saved);
for (std::size_t n = 0; n < saved->size(); ++n)
CHECK(!EnvironmentalCommunity::restore(std::span{*saved}.first(n), identity, 8000000));
// v2 offsets: config24, progress336, anchor392, count688, first input696,
// terminal flags752/760, report768, consumer transition912, decay920.
for (std::size_t offset :
{std::size_t{8}, std::size_t{24}, std::size_t{384}, std::size_t{680}, std::size_t{688},
std::size_t{752}, std::size_t{760}, std::size_t{912}, std::size_t{920}}) {
auto b = *saved;
b[offset] = 255;
std::uint64_t h = 14695981039346656037ULL;
for (auto v : std::span{b}.first(b.size() - 8))
h = (h ^ v) * 1099511628211ULL;
for (unsigned j = 0; j < 8; ++j)
b[b.size() - 8 + j] = static_cast<std::uint8_t>((h >> (8 * j)) & 255U);
CHECK(!EnvironmentalCommunity::restore(b, identity, 8000000));
}
CHECK((*created)->snapshot() == saved);
}
void decay_continuation() {
for (std::uint64_t sample = 0; sample < 8; ++sample) {
auto c = config();
c.rules = 2;
std::array<std::uint64_t, 10> producer{}, consumer{};
for (std::size_t k = 0; k < 10; ++k) {
producer[k] = 100 + sample * 5 + k * 3;
consumer[k] = 140 + sample + k;
c.decay[k] = (k + sample) % 5;
}
auto created =
EnvironmentalCommunity::create(c, resources({}, producer, consumer), 8000000);
CHECK(created);
auto o = std::move(*created);
const auto total = audit(*o);
for (std::uint64_t t = 1; t <= 90; ++t) {
const EnvironmentalInput input{290000, 0, {}, {t == 3 ? 1U : 0U, t == 3 ? 1U : 0U}};
auto r = o->advance(t, input);
CHECK(r);
CHECK(audit(*o) == total);
for (std::size_t k = 0; k < 10; ++k) {
// Closed-form cumulative quota, independent of runtime per-tick recurrence.
const auto quota = (t > 2 ? t - 2 : 0) * c.decay[k];
const auto first = quota < producer[k] ? producer[k] - quota : 0;
const auto second_quota = quota > producer[k] ? quota - producer[k] : 0;
const auto second = second_quota < consumer[k] ? consumer[k] - second_quota : 0;
CHECK(o->resources().organisms()[0].balances[k] == first);
CHECK(o->resources().organisms()[1].balances[k] == second);
}
if (t == 17 || t == 65 || t == 90) {
auto bytes = o->snapshot();
CHECK(bytes);
auto restored = EnvironmentalCommunity::restore(*bytes, identity, 8000000);
CHECK(restored);
CHECK((*restored)->snapshot() == bytes);
CHECK((*restored)->advance(t, input) == r);
CHECK((*restored)->snapshot() == bytes);
o = std::move(*restored);
}
}
}
}
} // namespace
int main() {
try {
vectors();
rollback_limits();
codec_replay();
terminal_decay();
terminal_codec_validation();
decay_continuation();
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
} catch (...) {
return 1;
}
}