Files
aevolution/tests/environmental_community_tests.cpp
T

250 lines
9.5 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 = {}) {
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;
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);
}
} // namespace
int main() {
try {
vectors();
rollback_limits();
codec_replay();
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
} catch (...) {
return 1;
}
}