Files
aevolution/tests/development_tests.cpp
T
dingoandClaude Opus 5 fd6faeb7f8 Stop the availability bound wrapping to zero
The helper summing available material added four unrestricted u64
channels without a check. With 2^62 in each, the sum reaches exactly 2^64
and wraps to zero, so the bound read as no material at all and both
planning and budgeting refused growth that a billion femtograms per
channel funds.

The sum now saturates. That is also the right answer rather than merely a
safe one: no step can need more than u64 femtograms, so a saturated bound
is correctly permissive.

Regressions in both suites use the exact 2^62 case and a single saturated
channel, and require the step that an ordinary supply funds. Both were
confirmed to fail without saturation.

The equivalent helper in metabolism.cpp is applied only to inventories
derived from bounded phenotype masses, never to caller input, so it is
left alone rather than given a branch no test could reach. That is
recorded in the validation report.

Native Release, clang-tidy analysis, ASan and format checks pass 19/19.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-23 13:19:29 -04:00

286 lines
14 KiB
C++

#include "evo/biology/development.hpp"
#include <iostream>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using namespace evo::biology;
void check(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
throw std::runtime_error("development line " + std::to_string(location.line()));
}
}
#define CHECK(expression) check(static_cast<bool>(expression))
// Target size 8e6 fg gives an exact cube root at full growth, so every derived
// value below is hand-calculable against ADR-0031's declared rules.
GenomeDraft draft() {
return {7,
1,
1,
{},
{{Capability::energy_conversion, 400, 0},
{Capability::membrane, 10, 0},
{Capability::target_size, 8'000'000, 0}}};
}
Genome build(const GenomeDraft& source) {
const auto genome = Genome::create(source);
check(genome.has_value());
return *genome;
}
// Development bounds growth by total mass, so a single channel carries the supply
// here; which elements a step consumes is settled in metabolism_tests.
constexpr evo::accounting::Inventory supply(std::uint64_t matter, std::uint64_t energy) {
return {{energy}, {evo::accounting::Femtograms{matter}, {}, {}, {}}};
}
constexpr EnvironmentState temperate{293'150, supply(1'000'000'000, 1'000'000'000)};
void hand_calculated_phenotype() {
const auto genome = build(draft());
const auto grown = derive_phenotype(genome, {1000, 0}, temperate);
CHECK(grown);
// 8e6 fg at density 1000 is 8000 um^3, an exact 20 um cube.
CHECK(grown->structural_mass == 8'000'000 && grown->extent == 20);
CHECK(grown->surface == 2400); // six 20x20 faces
CHECK(grown->membrane_mass == 28'800'000); // 2400 * 10 um * 1200
CHECK(grown->total_mass == 36'800'000);
CHECK(grown->maintenance == 73'600 + 86'400); // mass * 2 plus membrane * 3
CHECK(grown->conversion_per_thousand == 400);
CHECK(grown->movement_speed == 0 && grown->ingestion_rate == 0);
CHECK(grown->thermal_shortfall == 0);
// Half-built: 4e6 fg is 4000 um^3, and 15^3 = 3375 <= 4000 < 16^3.
const auto half = derive_phenotype(genome, {500, 0}, temperate);
CHECK(half && half->structural_mass == 4'000'000 && half->extent == 15);
CHECK(half->surface == 1350 && half->membrane_mass == 16'200'000);
CHECK(half->total_mass == 20'200'000);
}
void damage_is_not_genome() {
const auto genome = build(draft());
const auto before = genome.encode();
const auto intact = derive_phenotype(genome, {1000, 0}, temperate);
// A quarter lost: 6e6 fg is 6000 um^3, and 18^3 = 5832 <= 6000 < 19^3.
const auto hurt = derive_phenotype(genome, {1000, 250}, temperate);
CHECK(intact && hurt);
CHECK(hurt->structural_mass == 6'000'000 && hurt->extent == 18);
CHECK(hurt->surface == 1944 && hurt->membrane_mass == 23'328'000);
CHECK(hurt->total_mass == 29'328'000 && hurt->maintenance == 58'656 + 69'984);
CHECK(*hurt != *intact);
// The record a lineage inherits is untouched by what one organism lost.
CHECK(genome.encode() == before);
CHECK(genome.content_digest() == build(draft()).content_digest());
const auto repeated = derive_phenotype(genome, {1000, 250}, temperate);
CHECK(repeated && *repeated == *hurt);
}
void identical_inputs_give_identical_phenotypes() {
const auto genome = build(draft());
for (std::uint32_t realized = 0; realized <= 1000; realized += 125) {
for (std::uint32_t damage = 0; damage <= 1000; damage += 250) {
for (const auto temperature : {0U, 293'150U, 400'000U}) {
const EnvironmentState environment{temperature, supply(1'000'000, 1'000'000)};
const auto first = derive_phenotype(genome, {realized, damage}, environment);
const auto second = derive_phenotype(genome, {realized, damage}, environment);
CHECK(first && second && *first == *second);
// More growth never yields less structure at equal damage.
if (realized >= 125) {
const auto smaller =
derive_phenotype(genome, {realized - 125, damage}, environment);
CHECK(smaller && smaller->structural_mass <= first->structural_mass);
}
}
}
}
}
void capability_prerequisites() {
const auto bare = build(draft());
const auto without = derive_phenotype(bare, {1000, 0}, temperate);
CHECK(without && without->movement_speed == 0 && without->ingestion_rate == 0);
auto source = draft();
source.capabilities.push_back({Capability::movement, 250, 0});
source.capabilities.push_back({Capability::ingestion, 120'000, 0});
const auto capable = build(source);
const auto with = derive_phenotype(capable, {1000, 0}, temperate);
CHECK(with && with->movement_speed == 250);
CHECK(with->ingestion_rate == 4800); // 120000 * 2400 / 60000 reference surface
// Size is a real tradeoff: uptake scales with surface, maintenance with mass.
const auto smaller = derive_phenotype(capable, {500, 0}, temperate);
CHECK(smaller && smaller->ingestion_rate < with->ingestion_rate);
CHECK(smaller->maintenance < with->maintenance);
CHECK(smaller->movement_speed == with->movement_speed); // Speed is not size-scaled.
// Everything else about the two genomes is identical.
CHECK(with->structural_mass == without->structural_mass);
CHECK(with->maintenance == without->maintenance);
}
void thermal_shortfall_is_reported_not_enforced() {
auto source = draft();
source.capabilities.push_back({Capability::thermal_tolerance, 5'000, 0});
const auto tolerant = build(source);
const auto bare = build(draft());
for (const auto offset : {0U, 5'000U}) {
const EnvironmentState warm{293'150 + offset, {}};
const auto inside = derive_phenotype(tolerant, {1000, 0}, warm);
CHECK(inside && inside->thermal_shortfall == 0);
}
const EnvironmentState hot{293'150 + 8'000, {}};
const auto stressed = derive_phenotype(tolerant, {1000, 0}, hot);
CHECK(stressed && stressed->thermal_shortfall == 3'000);
const EnvironmentState cold{293'150 - 8'000, {}};
const auto chilled = derive_phenotype(tolerant, {1000, 0}, cold);
CHECK(chilled && chilled->thermal_shortfall == 3'000); // Symmetric about the reference.
// Without the capability every deviation is reported in full.
const auto exposed = derive_phenotype(bare, {1000, 0}, hot);
CHECK(exposed && exposed->thermal_shortfall == 8'000);
// Shortfall is a reported quantity: nothing else about the body changes.
CHECK(stressed->total_mass == exposed->total_mass);
CHECK(stressed->maintenance == exposed->maintenance);
}
void rule_versions_and_invalid_growth() {
CHECK(rules_for(1));
for (const auto version : {0U, 2U, 99U}) {
const auto missing = rules_for(version);
CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules);
}
auto source = draft();
source.development_version = 2;
const auto future = build(source);
const auto derived = derive_phenotype(future, {1000, 0}, temperate);
CHECK(!derived && derived.error() == DevelopmentError::unsupported_rules);
const auto planned = plan_development(future, {0, 0}, temperate);
CHECK(!planned && planned.error() == DevelopmentError::unsupported_rules);
const auto genome = build(draft());
for (const auto growth : {GrowthState{1001, 0}, GrowthState{0, 1001}}) {
const auto rejected = derive_phenotype(genome, growth, temperate);
CHECK(!rejected && rejected.error() == DevelopmentError::invalid_growth);
const auto refused = plan_development(genome, growth, temperate);
CHECK(!refused && refused.error() == DevelopmentError::invalid_growth);
}
}
void availability_bounds_do_not_wrap() {
const auto genome = build(draft());
// Four channels of 2^62 sum to exactly 2^64. A wrapped total reads as no
// material at all and refuses growth that is plainly funded.
constexpr std::uint64_t vast = 1ULL << 62U;
constexpr evo::accounting::Inventory enormous{
{std::numeric_limits<std::uint64_t>::max()},
{evo::accounting::Femtograms{vast}, evo::accounting::Femtograms{vast},
evo::accounting::Femtograms{vast}, evo::accounting::Femtograms{vast}}};
const EnvironmentState boundless{293'150, enormous};
const auto funded = plan_development(genome, {500, 0}, boundless);
CHECK(funded && funded->after == (GrowthState{550, 0}));
CHECK(funded->material_required == 2'632'000);
// An ordinary supply funds exactly the same step.
const auto ordinary = plan_development(genome, {500, 0}, temperate);
CHECK(ordinary && *ordinary == *funded);
// A single saturated channel is the other boundary.
constexpr evo::accounting::Inventory brimming{
{std::numeric_limits<std::uint64_t>::max()},
{evo::accounting::Femtograms{std::numeric_limits<std::uint64_t>::max()}, {}, {}, {}}};
const auto single = plan_development(genome, {500, 0}, {293'150, brimming});
CHECK(single && *single == *funded);
}
void plans_are_bounded_and_funded() {
const auto genome = build(draft());
// From half built, one step of 50 reaches 550: 4.4e6 fg is 4400 um^3 and
// 16^3 = 4096 <= 4400, so the extent grows from 15 to 16.
const auto funded = plan_development(genome, {500, 0}, temperate);
CHECK(funded);
CHECK(funded->after == (GrowthState{550, 0}));
CHECK(funded->material_required == 2'632'000); // 22832000 - 20200000
CHECK(funded->energy_required == 105'280); // material * 40 per 1000 fg
// One femtogram short of the full step buys exactly the largest step that fits.
const EnvironmentState tight{293'150, supply(2'631'999, 1'000'000'000)};
const auto reduced = plan_development(genome, {500, 0}, tight);
CHECK(reduced);
CHECK(reduced->after == (GrowthState{549, 0}));
CHECK(reduced->material_required == 2'624'000);
CHECK(reduced->material_required <= tight.available.matter[0].value);
// No material funds no growth, and nothing is granted.
const EnvironmentState barren{293'150, {}};
const auto refused = plan_development(genome, {500, 0}, barren);
CHECK(refused && refused->after == (GrowthState{500, 0}));
CHECK(refused->material_required == 0 && refused->energy_required == 0);
// A finished organism plans nothing further.
const auto complete = plan_development(genome, {1000, 0}, temperate);
CHECK(complete && complete->after == (GrowthState{1000, 0}));
CHECK(complete->material_required == 0);
// The last step is clamped to the target rather than overshooting it.
const auto final_step = plan_development(genome, {980, 0}, temperate);
CHECK(final_step && final_step->after == (GrowthState{1000, 0}));
// A plan never repairs damage.
const auto damaged = plan_development(genome, {500, 300}, temperate);
CHECK(damaged && damaged->after.damage_per_thousand == 300);
}
void flooring_consequences() {
// A body whose mass floors to nothing advances its counter for no material.
// This is the declared flooring, not a grant: no structure exists either way.
auto source = draft();
source.capabilities = {{Capability::energy_conversion, 1, 0},
{Capability::membrane, 1, 0},
{Capability::target_size, 1, 0}};
const auto tiny = build(source);
const EnvironmentState barren{293'150, {}};
const auto plan = plan_development(tiny, {500, 0}, barren);
CHECK(plan && plan->after == (GrowthState{550, 0}));
CHECK(plan->material_required == 0 && plan->energy_required == 0);
const auto derived = derive_phenotype(tiny, {550, 0}, barren);
CHECK(derived && derived->structural_mass == 0 && derived->total_mass == 0);
CHECK(derived->extent == 0 && derived->surface == 0 && derived->maintenance == 0);
// The smallest body that rounds to a whole cubic micrometre is still tiny.
source.capabilities = {{Capability::energy_conversion, 1, 0},
{Capability::membrane, 1, 0},
{Capability::target_size, 1000, 0}};
const auto unit = build(source);
const auto whole = derive_phenotype(unit, {1000, 0}, barren);
CHECK(whole && whole->structural_mass == 1000 && whole->extent == 1);
CHECK(whole->surface == 6 && whole->membrane_mass == 7200);
}
void extreme_sizes_are_exact() {
auto source = draft();
source.capabilities = {{Capability::energy_conversion, 1000, 0},
{Capability::membrane, 10'000, 0},
{Capability::target_size, 1'000'000'000, 0},
{Capability::ingestion, 1'000'000, 0}};
const auto largest = build(source);
const auto derived = derive_phenotype(largest, {1000, 0}, temperate);
CHECK(derived);
// 1e9 fg at density 1000 is 1e6 um^3, an exact 100 um cube.
CHECK(derived->structural_mass == 1'000'000'000 && derived->extent == 100);
CHECK(derived->surface == 60'000);
CHECK(derived->membrane_mass == 720'000'000'000); // 60000 * 10000 * 1200
CHECK(derived->total_mass == 721'000'000'000);
CHECK(derived->ingestion_rate == 1'000'000); // Exactly the reference surface.
CHECK(derived->maintenance == 1'442'000'000 + 2'160'000'000);
// Cube roots are exact on both sides of a boundary.
source.capabilities[2] = {Capability::target_size, 999'999'999, 0};
const auto under = derive_phenotype(build(source), {1000, 0}, temperate);
CHECK(under && under->extent == 99); // 999999 um^3 is one short of 100^3
}
} // namespace
int main() try {
hand_calculated_phenotype();
damage_is_not_genome();
identical_inputs_give_identical_phenotypes();
capability_prerequisites();
thermal_shortfall_is_reported_not_enforced();
rule_versions_and_invalid_growth();
availability_bounds_do_not_wrap();
plans_are_bounded_and_funded();
flooring_consequences();
extreme_sizes_are_exact();
std::cout << "PASS development rules, phenotype derivation and bounded plans\n";
return 0;
} catch (...) {
return 1;
}