Files
aevolution/tests/cellular_habitat_tests.cpp
T
dingo e781e929d8
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (network) (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 49s
Foundation / checks (ecs) (push) Canceled after 42s
Foundation / checks (client) (push) Canceled after 49s
Require contact for cellular patch uptake
2026-10-10 21:18:08 -04:00

154 lines
5.9 KiB
C++

#include "evo/biology/cellular_habitat.hpp"
#include <cstdio>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
namespace {
using namespace evo::biology;
using evo::accounting::Inventory;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("cellular habitat line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
CellularHabitatConfig config() {
return {7,
9,
11,
3'000,
2'000,
{{{0, 0}, 51, {{1'000}, {{{20}, {10}, {5}, {2}}}}},
{{0, 1'000}, 51, {{0}, {{{0}, {0}, {0}, {0}}}}},
{{1'000, 0}, 52, {{2'000}, {{{30}, {15}, {7}, {3}}}}}}};
}
MetabolicBudget uptake_budget() {
MetabolicBudget result{};
result.rate_microseconds = 1'000'000;
result.intake_limit = {{1'000}, {{{100}, {50}, {25}, {10}}}};
return result;
}
void create_query_and_propose() {
const auto opening = config();
const auto made = CellularHabitat::create(opening);
CHECK(made);
CHECK((*made)->config() == opening);
CHECK((*made)->patches().size() == 3);
CHECK((*made)->find({1'000, 0}) == 2);
CHECK(!(*made)->find({2'000, 1'000}));
CHECK((*made)->totals() == Inventory{{3'000}, {{{50}, {25}, {12}, {5}}}});
const auto plan = (*made)->propose_uptake({0, 0}, {0, 0}, 20, {}, uptake_budget());
CHECK(plan && plan->patch == CellularPatchAddress{0, 0});
CHECK(plan->transfer.acquired == Inventory{{50}, {{{5}, {2}, {1}, {0}}}});
CHECK(plan->transfer.patch_after == Inventory{{950}, {{{15}, {8}, {4}, {2}}}});
CHECK(plan->transfer.reserve_after == plan->transfer.acquired);
CHECK((*made)->patches()[0].stock == Inventory{{1'000}, {{{20}, {10}, {5}, {2}}}});
CHECK(!(*made)->propose_uptake({2'000, 0}, {0, 0}, 20, {}, uptake_budget()));
const auto distant = (*made)->propose_uptake({1'000, 0}, {0, 0}, 20, {}, uptake_budget());
CHECK(!distant && distant.error() == CellularHabitatError::not_in_contact);
}
void canonical_snapshot_round_trip() {
const auto made = CellularHabitat::create(config());
CHECK(made);
const auto bytes = (*made)->snapshot();
CHECK(bytes.size() == cellular_habitat_header_bytes +
cellular_patch_record_bytes * (*made)->patches().size() + 8);
const auto restored = CellularHabitat::restore(bytes, 7, 9, 11);
CHECK(restored);
CHECK((*restored)->config() == (*made)->config());
CHECK((*restored)->totals() == (*made)->totals());
CHECK((*restored)->snapshot() == bytes);
CHECK(!CellularHabitat::restore(bytes, 8, 9, 11));
CHECK(!CellularHabitat::restore(bytes, 7, 9, 12));
auto corrupt = bytes;
corrupt[20] ^= 1;
const auto refused = CellularHabitat::restore(corrupt, 7, 9, 11);
CHECK(!refused && refused.error() == CellularHabitatError::checksum);
CHECK(
!CellularHabitat::restore(std::span{bytes}.first(cellular_habitat_header_bytes), 7, 9, 11));
auto oversized = bytes;
oversized.resize(max_cellular_habitat_bytes + 1);
CHECK(!CellularHabitat::restore(oversized, 7, 9, 11));
}
void perception_is_bounded_by_body_to_patch_distance() {
CellularHabitatConfig opening{7,
9,
11,
10'000,
10'000,
{{{0, 0}, 51, {{1'000}, {}}},
{{4'000, 4'000}, 52, {{2'000}, {}}},
{{5'000, 5'000}, 53, {{3'000}, {}}},
{{6'000, 0}, 54, {{4'000}, {}}}}};
const auto made = CellularHabitat::create(std::move(opening));
CHECK(made);
const auto visible = (*made)->perceive({0, 0}, 1'000);
CHECK(visible);
CHECK(visible->size() == 3);
CHECK((*visible)[0] == CellularPatchPerception{{0, 0}, {{1'000}, {}}});
CHECK((*visible)[1] == CellularPatchPerception{{4'000, 4'000}, {{2'000}, {}}});
CHECK((*visible)[2] == CellularPatchPerception{{6'000, 0}, {{4'000}, {}}});
CHECK(!(*made)->perceive({9'001, 0}, 1'000));
CHECK(!(*made)->perceive({0, 0}, 0));
}
void invalid_openings_refuse() {
auto invalid = config();
invalid.width_um = 3'001;
CHECK(!CellularHabitat::create(invalid));
invalid = config();
invalid.patches[1].address.x_um = 500;
CHECK(!CellularHabitat::create(invalid));
invalid = config();
invalid.patches[1].source = 0;
CHECK(!CellularHabitat::create(invalid));
invalid = config();
std::swap(invalid.patches[0], invalid.patches[1]);
CHECK(!CellularHabitat::create(invalid));
invalid = config();
invalid.patches[1].address = invalid.patches[0].address;
CHECK(!CellularHabitat::create(invalid));
invalid = {7,
9,
11,
2'000,
1'000,
{{{0, 0}, 1, {{std::numeric_limits<std::uint64_t>::max()}, {}}},
{{1'000, 0}, 2, {{1}, {}}}}};
const auto overflow = CellularHabitat::create(invalid);
CHECK(!overflow && overflow.error() == CellularHabitatError::overflow);
invalid = {7, 9, 11, 1'000'000, 1'000'000, {}};
for (std::uint32_t y = 0; y < 64'000; y += cellular_patch_pitch_um)
for (std::uint32_t x = 0; x < 65'000; x += cellular_patch_pitch_um)
invalid.patches.push_back({{x, y}, 1, {}});
CHECK(invalid.patches.size() > max_cellular_patches);
CHECK(!CellularHabitat::create(std::move(invalid)));
}
} // namespace
int main() {
try {
create_query_and_propose();
canonical_snapshot_round_trip();
perception_is_bounded_by_body_to_patch_distance();
invalid_openings_refuse();
std::puts("PASS cellular habitat: finite patches, perception and canonical recovery");
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
}