Files
aevolution/tests/cellular_motion_tests.cpp
T
dingo 6816eda6a5
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (client) (push) Failing after 4m23s
Foundation / checks (network) (push) Failing after 3m5s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 32s
Foundation / checks (checks) (push) Canceled after 5m42s
Foundation / checks (postgres) (push) Canceled after 1m19s
Debit reserve for proposed cellular motion
2026-10-10 21:18:57 -04:00

124 lines
5.7 KiB
C++

#include "evo/biology/cellular_motion.hpp"
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::biology;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("cellular motion line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
MetabolicBudget budget(std::uint32_t speed_um_per_second = 200,
std::uint64_t movement_pj_per_second = 1'000) {
MetabolicBudget result{};
result.phenotype.movement_speed = speed_um_per_second;
result.movement = {movement_pj_per_second};
result.rate_microseconds = 1'000'000;
return result;
}
void movement_proposals() {
const CellularArea area{400, 300};
const CellularPosition start{100, 100};
const auto east = propose_cellular_motion(area, start, 20, {1000, 0}, budget());
CHECK(east && east->position == CellularPosition{110, 100});
CHECK(east->distance_um == 10 && east->movement_cost.value == 50);
const auto partial = propose_cellular_motion(area, start, 20, {500, 0}, budget());
CHECK(partial && partial->position == CellularPosition{105, 100});
CHECK(partial->distance_um == 5 && partial->movement_cost.value == 25);
const auto diagonal = propose_cellular_motion(area, start, 20, {707, 707}, budget());
CHECK(diagonal && diagonal->position == CellularPosition{107, 107});
CHECK(diagonal->distance_um == 10 && diagonal->movement_cost.value == 50);
const auto west = propose_cellular_motion(area, start, 20, {-1000, 0}, budget());
CHECK(west && west->position == CellularPosition{90, 100});
CHECK(west->movement_cost.value == 50);
const auto stopped = propose_cellular_motion(area, start, 20, {0, 0}, budget());
CHECK(stopped && stopped->position == start && stopped->distance_um == 0 &&
stopped->movement_cost.value == 0);
}
void edges_and_invalid_state() {
const auto east_edge = propose_cellular_motion({400, 300}, {370, 100}, 20, {1000, 0}, budget());
CHECK(east_edge && east_edge->position == CellularPosition{380, 100});
CHECK(east_edge->distance_um == 10 && east_edge->movement_cost.value == 50);
const auto pinned = propose_cellular_motion({400, 300}, {380, 100}, 20, {1000, 0}, budget());
CHECK(pinned && pinned->position == CellularPosition{380, 100});
CHECK(pinned->distance_um == 0 && pinned->movement_cost.value == 0);
const auto south_edge = propose_cellular_motion({400, 300}, {100, 1}, 20, {0, -1000}, budget());
CHECK(south_edge && south_edge->position == CellularPosition{100, 0});
CHECK(south_edge->distance_um == 1 && south_edge->movement_cost.value == 5);
CHECK(!propose_cellular_motion({0, 300}, {0, 0}, 20, {}, budget()));
CHECK(!propose_cellular_motion({cellular_motion_axis_limit_um + 1, 300}, {0, 0}, 20, {},
budget()));
CHECK(!propose_cellular_motion({400, 300}, {0, 0}, 0, {}, budget()));
CHECK(!propose_cellular_motion({400, 300}, {390, 0}, 20, {}, budget()));
CHECK(!propose_cellular_motion({400, 300}, {0, 0}, 401, {}, budget()));
CHECK(!propose_cellular_motion({400, 300}, {100, 100}, 20, {1000, 1000}, budget()));
auto wrong_rate_period = budget();
wrong_rate_period.rate_microseconds = 50'000;
CHECK(!propose_cellular_motion({400, 300}, {100, 100}, 20, {}, wrong_rate_period));
// The per-tick speed rounds down by ADR-0099; no phantom sub-micrometre step.
const auto quantized =
propose_cellular_motion({400, 300}, {100, 100}, 20, {1000, 0}, budget(19));
CHECK(quantized && quantized->position == CellularPosition{100, 100});
CHECK(quantized->movement_cost.value == 0);
}
void movement_funding_is_one_candidate() {
using evo::accounting::Inventory;
const Inventory reserve{{100}, {{{1}, {2}, {3}, {4}}}};
const Inventory heat{{7}, {{{5}, {6}, {7}, {8}}}};
const auto funded = propose_funded_cellular_motion({400, 300}, {100, 100}, 20, {1000, 0},
budget(), reserve, heat);
CHECK(funded && funded->position == CellularPosition{110, 100});
CHECK(funded->movement_cost.value == 50);
CHECK(funded->reserve_after == Inventory{{50}, {{{1}, {2}, {3}, {4}}}});
CHECK(funded->heat_after == Inventory{{57}, {{{5}, {6}, {7}, {8}}}});
auto poor = reserve;
poor.energy.value = 49;
const auto refused =
propose_funded_cellular_motion({400, 300}, {100, 100}, 20, {1000, 0}, budget(), poor, heat);
CHECK(!refused && refused.error() == CellularMotionError::insufficient_reserve);
CHECK(poor.energy.value == 49 && heat.energy.value == 7);
auto full_heat = heat;
full_heat.energy.value = UINT64_MAX;
const auto overflow = propose_funded_cellular_motion({400, 300}, {100, 100}, 20, {1000, 0},
budget(), reserve, full_heat);
CHECK(!overflow && overflow.error() == CellularMotionError::overflow);
}
void contact_geometry() {
CHECK(cellular_footprints_contact({0, 0}, 20, {20, 0}, 20)); // Exact edge touch.
CHECK(cellular_footprints_contact({0, 0}, 20, {19, 19}, 20));
CHECK(!cellular_footprints_contact({0, 0}, 20, {21, 0}, 20));
CHECK(!cellular_footprints_contact({0, 0}, 0, {0, 0}, 20));
CHECK(cellular_footprints_contact({1'000'000, 1'000'000}, 20, {1'000'020, 1'000'020}, 20));
}
} // namespace
int main() {
try {
movement_proposals();
edges_and_invalid_state();
movement_funding_is_one_candidate();
contact_geometry();
std::puts("PASS cellular motion: phenotype speed, energy cost, bounds and contact");
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
}