Foundation / checks (deploy) (push) Successful in 3m27s
Foundation / checks (ecs) (push) Successful in 1m58s
Foundation / checks (client) (push) Successful in 12m20s
Foundation / checks (network) (push) Successful in 13m53s
Foundation / checks (tsan) (push) Successful in 8m9s
Foundation / checks (checks) (push) Successful in 33m4s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 21m43s
Rules 3 is rules 2 with irradiance, growth, intake and decay per second. The community derives its per-tick form once at creation and restore (rationals exact via the tick in numerator and denominator, caps rounded down); steps and the snapshot layout are rules 2's; the codec reads formats 1-3. At 50 ms a x20 per-second twin steps identically through feeding, death and decay and across a snapshot; at 25 ms caps halve. Rules 1/2 and the development-v1 opening are unchanged. Development version 1 outputs stay tied to their 50 ms quantum and the tick-abstract M2 fixtures are not migrated (recorded). environmental-qualification's "unknown rules" case moves from 3 to 4. Refresh the dev session window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
622 lines
27 KiB
C++
622 lines
27 KiB
C++
#include "evo/processes/environmental_community.hpp"
|
|
#include "evo/processes/environmental_schedule.hpp"
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <limits>
|
|
#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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
void scheduled_intervals() {
|
|
for (std::uint64_t rules : {1U, 2U}) {
|
|
auto c = config();
|
|
c.rules = rules;
|
|
c.growth_numerator = 1;
|
|
if (rules == 2)
|
|
c.decay.fill(1);
|
|
auto create = [&]() {
|
|
auto made =
|
|
EnvironmentalCommunity::create(c,
|
|
resources({100, 100, 100, 100, 0, 0, 100, 0, 100, 0},
|
|
{10, 0, 0, 20, 10, 10, 0, 30, 0, 0}),
|
|
8000000);
|
|
CHECK(made);
|
|
return std::move(*made);
|
|
};
|
|
std::array<EnvironmentalInput, 130> inputs{};
|
|
std::array<EnvironmentalReport, 130> expected{};
|
|
auto reference = create();
|
|
for (std::size_t i = 0; i < inputs.size(); ++i) {
|
|
inputs[i] = {i % 7 == 0 ? 260000U : 290000U, i % 3 == 0 ? 0U : 1U, {}};
|
|
if (i == 1 || i == 8)
|
|
inputs[i].bite[0] = 1;
|
|
if (rules == 2 && i == 16)
|
|
inputs[i].terminal[1] = 1;
|
|
if (rules == 2 && i == 32)
|
|
inputs[i].terminal[0] = 1;
|
|
auto r = reference->advance(i + 1, inputs[i]);
|
|
CHECK(r);
|
|
expected[i] = *r;
|
|
}
|
|
for (std::size_t width : {1U, 3U, 17U, 64U}) {
|
|
auto o = create();
|
|
for (std::size_t begin = 0; begin < inputs.size();) {
|
|
const auto end = std::min(begin + width, inputs.size());
|
|
auto r = schedule_environment(*o, {identity, begin, end, 50000,
|
|
std::span{inputs}.subspan(begin, end - begin)});
|
|
CHECK(r && !r->failure && r->committed == end - begin && r->last_tick == end);
|
|
for (std::size_t j = 0; j < r->committed; ++j)
|
|
CHECK(r->reports[j] == expected[begin + j]);
|
|
auto saved = o->snapshot();
|
|
CHECK(saved);
|
|
auto pause = schedule_environment(*o, {identity, end, end, 50000, {}});
|
|
CHECK(pause && !pause->failure && pause->committed == 0 && pause->last_tick == end);
|
|
CHECK(o->snapshot() == saved);
|
|
auto restored = EnvironmentalCommunity::restore(*saved, identity, 8000000);
|
|
CHECK(restored);
|
|
o = std::move(*restored);
|
|
begin = end;
|
|
}
|
|
CHECK(o->snapshot() == reference->snapshot());
|
|
}
|
|
}
|
|
}
|
|
void interval_boundaries() {
|
|
auto o = make();
|
|
std::array<EnvironmentalInput, 65> inputs{};
|
|
inputs.fill({290000, 1, {}});
|
|
const auto initial = o->snapshot();
|
|
CHECK(initial);
|
|
const auto reject = [&](EnvironmentalInterval request, EnvironmentalIntervalError error) {
|
|
auto r = schedule_environment(*o, request);
|
|
CHECK(!r && r.error() == error);
|
|
CHECK(o->snapshot() == initial);
|
|
};
|
|
reject({{8, 9}, 0, 1, 50000, std::span{inputs}.first(1)}, EnvironmentalIntervalError::identity);
|
|
reject({identity, 0, 1, 50001, std::span{inputs}.first(1)},
|
|
EnvironmentalIntervalError::quantum);
|
|
reject({identity, 1, 2, 50000, std::span{inputs}.first(1)}, EnvironmentalIntervalError::stale);
|
|
reject({identity, 0, 65, 50000, inputs}, EnvironmentalIntervalError::backlog);
|
|
reject({identity, 0, std::numeric_limits<std::uint64_t>::max(), 50000, {}},
|
|
EnvironmentalIntervalError::backlog);
|
|
reject({identity, 0, 1, 50000, {}}, EnvironmentalIntervalError::input_count);
|
|
reject({identity, 0, 0, 50000, std::span{inputs}.first(1)},
|
|
EnvironmentalIntervalError::input_count);
|
|
auto maximum = schedule_environment(*o, {identity, 0, 64, 50000, std::span{inputs}.first(64)});
|
|
CHECK(maximum && !maximum->failure && maximum->committed == 64);
|
|
const auto saved = o->snapshot();
|
|
CHECK(saved);
|
|
auto stale = schedule_environment(*o, {identity, 0, 64, 50000, std::span{inputs}.first(64)});
|
|
CHECK(!stale && stale.error() == EnvironmentalIntervalError::stale);
|
|
auto reversed = schedule_environment(*o, {identity, 64, 63, 50000, {}});
|
|
CHECK(!reversed && reversed.error() == EnvironmentalIntervalError::range);
|
|
CHECK(o->snapshot() == saved);
|
|
|
|
for (std::size_t failed_at : {0U, 2U, 3U}) {
|
|
auto partial = make();
|
|
auto reference = make();
|
|
std::array<EnvironmentalInput, 4> commands{};
|
|
commands.fill({290000, 1, {}});
|
|
commands[failed_at].temperature_mK = 0;
|
|
auto r = schedule_environment(*partial, {identity, 0, 4, 50000, commands});
|
|
CHECK(r && r->failure && r->committed == failed_at && r->last_tick == failed_at);
|
|
const auto failure = r->failure;
|
|
if (!failure)
|
|
throw std::runtime_error("missing interval failure");
|
|
CHECK(failure->tick == failed_at + 1 && failure->cause == CommunityError::input);
|
|
for (std::size_t j = 0; j < failed_at; ++j)
|
|
CHECK(reference->advance(j + 1, commands[j]) == r->reports[j]);
|
|
CHECK(partial->snapshot() == reference->snapshot());
|
|
auto bytes = partial->snapshot();
|
|
CHECK(bytes);
|
|
auto restored = EnvironmentalCommunity::restore(*bytes, identity, 8000000);
|
|
CHECK(restored);
|
|
commands[failed_at].temperature_mK = 290000;
|
|
auto remaining = std::span{commands}.subspan(failed_at);
|
|
auto resumed = schedule_environment(**restored, {identity, failed_at, 4, 50000, remaining});
|
|
CHECK(resumed && !resumed->failure && resumed->committed == remaining.size());
|
|
for (std::size_t j = failed_at; j < commands.size(); ++j)
|
|
CHECK(reference->advance(j + 1, commands[j]));
|
|
CHECK((*restored)->snapshot() == reference->snapshot());
|
|
}
|
|
}
|
|
} // namespace
|
|
// Rules 3 (ADR-0099): rules 2 with every rate per second. At 50 ms a per-second twin of a
|
|
// rules 2 configuration (rates times 20) steps identically, through feeding, a death and
|
|
// its decay, and across a snapshot; at another tick the caps scale and round down.
|
|
void rules3_per_second() {
|
|
auto two = config();
|
|
two.rules = 2;
|
|
two.growth_numerator = 3;
|
|
two.growth_denominator = 2;
|
|
two.decay.fill(2);
|
|
auto three = two;
|
|
three.rules = 3;
|
|
three.irradiance_numerator *= 20;
|
|
three.growth_numerator *= 20;
|
|
for (auto& v : three.intake)
|
|
v *= 20;
|
|
for (auto& v : three.decay)
|
|
v *= 20;
|
|
const std::array<std::uint64_t, 10> stock{5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
|
|
auto made_a = EnvironmentalCommunity::create(
|
|
two, resources({100, 100, 100, 100, 0, 0, 100, 0, 10, 0}, stock, stock), 8000000);
|
|
auto made_b = EnvironmentalCommunity::create(
|
|
three, resources({100, 100, 100, 100, 0, 0, 100, 0, 10, 0}, stock, stock), 8000000);
|
|
CHECK(made_a && made_b);
|
|
auto a = std::move(*made_a);
|
|
auto b = std::move(*made_b);
|
|
const auto same = [&] {
|
|
for (std::size_t k = 0; k < 10; ++k)
|
|
if (*a->resources().fields().read(0, static_cast<Field>(k)) !=
|
|
*b->resources().fields().read(0, static_cast<Field>(k)))
|
|
return false;
|
|
return a->resources().organisms()[0].balances == b->resources().organisms()[0].balances &&
|
|
a->resources().organisms()[1].balances == b->resources().organisms()[1].balances;
|
|
};
|
|
const auto input = [](std::uint64_t tick) {
|
|
EnvironmentalInput in{290000, tick % 7 == 0 ? 0U : 1U, {}, {}};
|
|
if (tick == 6)
|
|
in.bite = {1, 1, 1, 1, 1};
|
|
if (tick == 20)
|
|
in.terminal = {0, 1};
|
|
return in;
|
|
};
|
|
for (std::uint64_t tick = 1; tick <= 40; ++tick) {
|
|
const auto ra = a->advance(tick, input(tick));
|
|
const auto rb = b->advance(tick, input(tick));
|
|
CHECK(ra.has_value() == rb.has_value());
|
|
if (ra)
|
|
CHECK(*ra == *rb);
|
|
CHECK(same());
|
|
}
|
|
// Rules 3 persists as rules 3 and replays from its snapshot.
|
|
const auto saved = b->snapshot();
|
|
CHECK(saved && (*saved)[8] == 3);
|
|
auto restored = EnvironmentalCommunity::restore(*saved, identity, 8000000);
|
|
CHECK(restored && (*restored)->snapshot() == saved);
|
|
b = std::move(*restored);
|
|
for (std::uint64_t tick = 41; tick <= 60; ++tick) {
|
|
const auto ra = a->advance(tick, input(tick));
|
|
const auto rb = b->advance(tick, input(tick));
|
|
CHECK(ra && rb && *ra == *rb && same());
|
|
}
|
|
// At 25 ms a cap of 2000 per second is 50 per tick: 51 is over it, 50 is within it.
|
|
auto fast = three;
|
|
fast.tick_microseconds = 25000;
|
|
auto quick = make(fast);
|
|
const auto over = quick->advance(1, {290000, 0, {51, 0, 0, 0, 0}, {}});
|
|
CHECK(!over && over.error() == CommunityError::input);
|
|
const auto within = quick->advance(1, {290000, 0, {50, 0, 0, 0, 0}, {}});
|
|
CHECK(!within && within.error() == CommunityError::insufficient); // Past the cap check.
|
|
// A rate that cannot be converted at the tick is a configuration error.
|
|
auto huge = three;
|
|
huge.irradiance_numerator = std::numeric_limits<std::uint64_t>::max();
|
|
CHECK(!EnvironmentalCommunity::create(huge, resources(), 8000000));
|
|
auto slow = three;
|
|
slow.tick_microseconds = 1'000'001;
|
|
CHECK(!EnvironmentalCommunity::create(slow, resources(), 8000000));
|
|
}
|
|
|
|
int main() {
|
|
try {
|
|
vectors();
|
|
rollback_limits();
|
|
codec_replay();
|
|
terminal_decay();
|
|
terminal_codec_validation();
|
|
decay_continuation();
|
|
scheduled_intervals();
|
|
interval_boundaries();
|
|
rules3_per_second();
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
} catch (...) {
|
|
return 1;
|
|
}
|
|
}
|