Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m18s
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 5m33s
Foundation / checks (postgres) (push) Canceled after 5m29s
Foundation / checks (tsan) (push) Canceled after 5m28s
Foundation / checks (client) (push) Canceled after 5m37s
Foundation / checks (network) (push) Canceled after 5m39s
584 lines
29 KiB
C++
584 lines
29 KiB
C++
#include "evo/biology/lineage.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <source_location>
|
|
#include <span>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <tuple>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
#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("lineage line " + std::to_string(location.line()));
|
|
}
|
|
}
|
|
#define CHECK(expression) check(static_cast<bool>(expression))
|
|
Genome descend(std::uint64_t id, std::uint64_t parent, std::uint32_t conversion = 400,
|
|
std::uint32_t schema = genome_schema_v1) {
|
|
GenomeDraft source;
|
|
source.schema_version = schema;
|
|
source.id = id;
|
|
source.origin_event = id;
|
|
source.development_version = 1;
|
|
if (parent != 0) {
|
|
source.parents = {parent};
|
|
}
|
|
source.capabilities = {{Capability::energy_conversion, conversion, 0},
|
|
{Capability::membrane, 120, 0},
|
|
{Capability::target_size, 8'000'000, 0}};
|
|
const auto genome = Genome::create(source);
|
|
check(genome.has_value());
|
|
return *genome;
|
|
}
|
|
// A founder with two branches, one of which branches again:
|
|
// 1 -> 2 -> 4, 5
|
|
// -> 3
|
|
LineageArchive branching() {
|
|
LineageArchive archive;
|
|
check(archive.admit(descend(1, 0, 400)).has_value());
|
|
check(archive.admit(descend(2, 1, 409)).has_value());
|
|
check(archive.admit(descend(3, 1, 391)).has_value());
|
|
check(archive.admit(descend(4, 2, 418)).has_value());
|
|
check(archive.admit(descend(5, 2, 400)).has_value());
|
|
return archive;
|
|
}
|
|
|
|
void a_genome_outlives_every_organism_that_carried_it() {
|
|
auto archive = branching();
|
|
CHECK(archive.record_birth(100, 4, evo::Tick{10}));
|
|
CHECK(archive.record_birth(101, 4, evo::Tick{12}));
|
|
CHECK(archive.living_carriers(4) && *archive.living_carriers(4) == 2);
|
|
CHECK(*archive.status(4) == LineageStatus::living);
|
|
CHECK(archive.record_death(100, evo::Tick{30}));
|
|
CHECK(*archive.living_carriers(4) == 1 && *archive.status(4) == LineageStatus::living);
|
|
CHECK(archive.record_death(101, evo::Tick{31}));
|
|
// No organism carries it now. Everything about the genome is still here.
|
|
CHECK(*archive.living_carriers(4) == 0);
|
|
CHECK(*archive.status(4) == LineageStatus::absent);
|
|
const auto kept = archive.find(4);
|
|
CHECK(kept && kept->id() == 4 && kept->parents()[0] == 2);
|
|
CHECK(*kept == descend(4, 2, 418));
|
|
const auto line = archive.ancestry(4);
|
|
CHECK(line && (*line == std::vector<std::uint64_t>{1, 2, 4}));
|
|
CHECK(archive.children(2) && archive.children(2)->size() == 2);
|
|
// The organisms themselves are still on record, with their terminal ticks.
|
|
const auto first = archive.organism(100);
|
|
CHECK(first && !first->alive && first->genome == 4);
|
|
CHECK(first->born.value == 10 && first->died.value == 30);
|
|
}
|
|
|
|
void ancestry_and_descendants_resolve_through_a_dead_middle() {
|
|
auto archive = branching();
|
|
// Every carrier of the middle genome dies; its children are untouched.
|
|
CHECK(archive.record_birth(200, 2, evo::Tick{5}));
|
|
CHECK(archive.record_birth(201, 4, evo::Tick{6}));
|
|
CHECK(archive.record_death(200, evo::Tick{20}));
|
|
CHECK(*archive.status(2) == LineageStatus::absent);
|
|
CHECK(*archive.status(4) == LineageStatus::living);
|
|
const auto through = archive.ancestry(4);
|
|
CHECK(through && (*through == std::vector<std::uint64_t>{1, 2, 4}));
|
|
const auto kids = archive.children(2);
|
|
CHECK(kids && (*kids == std::vector<std::uint64_t>{4, 5}));
|
|
// The founder has no living carrier either, and is equally intact.
|
|
CHECK(*archive.status(1) == LineageStatus::absent);
|
|
CHECK(archive.find(1) && archive.find(1)->parents().empty());
|
|
CHECK(archive.ancestry(1) && archive.ancestry(1)->size() == 1);
|
|
}
|
|
|
|
void branching_lineages_resolve_their_divergence() {
|
|
auto archive = branching();
|
|
CHECK(archive.common_ancestor(4, 5) && *archive.common_ancestor(4, 5) == 2);
|
|
CHECK(*archive.common_ancestor(4, 3) == 1);
|
|
CHECK(*archive.common_ancestor(2, 3) == 1);
|
|
CHECK(*archive.common_ancestor(4, 2) == 2); // An ancestor of itself.
|
|
CHECK(*archive.common_ancestor(4, 4) == 4);
|
|
CHECK(*archive.common_ancestor(1, 5) == 1);
|
|
// A separate founder shares nothing.
|
|
CHECK(archive.admit(descend(9, 0, 500)));
|
|
CHECK(archive.common_ancestor(4, 9) && *archive.common_ancestor(4, 9) == 0);
|
|
const auto unknown = archive.common_ancestor(4, 77);
|
|
CHECK(!unknown && unknown.error() == HistoryError::unknown_genome);
|
|
}
|
|
|
|
void admission_refuses_duplicates_and_dangling_parents() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(1, 0)));
|
|
// Re-admitting an ID would destroy the earlier record.
|
|
const auto repeated = archive.admit(descend(1, 0, 500));
|
|
CHECK(!repeated && repeated.error() == HistoryError::duplicate_genome);
|
|
CHECK(archive.find(1) && archive.find(1)->content_digest() == descend(1, 0).content_digest());
|
|
// A child before its parent would leave ancestry dangling, and would be the
|
|
// only way to build a cycle.
|
|
const auto orphan = archive.admit(descend(5, 4));
|
|
CHECK(!orphan && orphan.error() == HistoryError::unknown_parent);
|
|
CHECK(archive.archived() == 1);
|
|
const auto missing = archive.ancestry(4);
|
|
CHECK(!missing && missing.error() == HistoryError::unknown_genome);
|
|
// Admitting in order is fine, and the graph stays acyclic by that order.
|
|
CHECK(archive.admit(descend(4, 1)));
|
|
CHECK(archive.admit(descend(5, 4)));
|
|
CHECK(archive.ancestry(5) && (*archive.ancestry(5) == std::vector<std::uint64_t>{1, 4, 5}));
|
|
}
|
|
|
|
void death_is_terminal_and_births_are_unique() {
|
|
auto archive = branching();
|
|
const auto nobody = archive.record_death(500, evo::Tick{1});
|
|
CHECK(!nobody && nobody.error() == HistoryError::unknown_organism);
|
|
const auto nowhere = archive.record_birth(300, 77, evo::Tick{1});
|
|
CHECK(!nowhere && nowhere.error() == HistoryError::unknown_genome);
|
|
const auto anonymous = archive.record_birth(0, 1, evo::Tick{1});
|
|
CHECK(!anonymous && anonymous.error() == HistoryError::invalid_identity);
|
|
CHECK(archive.record_birth(300, 3, evo::Tick{8}));
|
|
// An identity is never reused, whether its holder lives or not.
|
|
const auto twice = archive.record_birth(300, 5, evo::Tick{9});
|
|
CHECK(!twice && twice.error() == HistoryError::duplicate_organism);
|
|
const auto backwards = archive.record_death(300, evo::Tick{7});
|
|
CHECK(!backwards && backwards.error() == HistoryError::invalid_tick);
|
|
CHECK(archive.record_death(300, evo::Tick{40}));
|
|
const auto again = archive.record_death(300, evo::Tick{41});
|
|
CHECK(!again && again.error() == HistoryError::already_dead);
|
|
// Nothing brings it back, and its record keeps the first terminal tick.
|
|
const auto reborn = archive.record_birth(300, 3, evo::Tick{50});
|
|
CHECK(!reborn && reborn.error() == HistoryError::duplicate_organism);
|
|
const auto record = archive.organism(300);
|
|
CHECK(record && !record->alive && record->died.value == 40);
|
|
CHECK(*archive.status(3) == LineageStatus::absent);
|
|
}
|
|
|
|
void absence_is_never_reported_as_extinction() {
|
|
auto archive = branching();
|
|
// Nothing was ever born: every lineage is absent, and none is extinct.
|
|
// Unsigned literals: a bare list deduces initializer_list<int>, and the
|
|
// sign-changing conversion to each element fails Clang's -Wconversion.
|
|
for (const std::uint64_t genome : {1U, 2U, 3U, 4U, 5U}) {
|
|
CHECK(*archive.status(genome) == LineageStatus::absent);
|
|
CHECK(*archive.living_carriers(genome) == 0);
|
|
CHECK(archive.find(genome)); // Still retained regardless.
|
|
}
|
|
// The enumeration itself offers no way to say extinct.
|
|
CHECK(static_cast<int>(LineageStatus::living) != static_cast<int>(LineageStatus::absent));
|
|
// A lineage can return from absence: absence is about carriers, not the
|
|
// record, and the archive never claimed the lineage was finished.
|
|
CHECK(archive.record_birth(400, 5, evo::Tick{1}));
|
|
CHECK(*archive.status(5) == LineageStatus::living);
|
|
CHECK(archive.record_death(400, evo::Tick{2}));
|
|
CHECK(*archive.status(5) == LineageStatus::absent);
|
|
CHECK(archive.record_birth(401, 5, evo::Tick{3}));
|
|
CHECK(*archive.status(5) == LineageStatus::living);
|
|
}
|
|
|
|
// Retention is a property of the interface: the type offers no removal
|
|
// operation, so no caller can choose to delete a record. Written as concepts
|
|
// rather than bare requires-expressions so the check is a substitution failure
|
|
// instead of a hard error.
|
|
template <typename Archive>
|
|
concept erases = requires(Archive& archive, std::uint64_t id) { archive.erase(id); };
|
|
template <typename Archive>
|
|
concept removes = requires(Archive& archive, std::uint64_t id) { archive.remove(id); };
|
|
template <typename Archive>
|
|
concept clears = requires(Archive& archive) { archive.clear(); };
|
|
template <typename Archive>
|
|
concept prunes = requires(Archive& archive) { archive.prune(); };
|
|
// Forgetting has forms that are not named like removal. Replacing an archive
|
|
// wholesale discards every record it held, and swapping needs the same
|
|
// assignment that makes replacement possible.
|
|
template <typename Archive>
|
|
concept replaceable = requires(Archive& archive) { archive = Archive{}; };
|
|
template <typename Archive>
|
|
concept exchangeable = requires(Archive& left, Archive& right) { std::swap(left, right); };
|
|
|
|
void the_archive_cannot_be_replaced_or_rewound() {
|
|
static_assert(!replaceable<LineageArchive>);
|
|
static_assert(!exchangeable<LineageArchive>);
|
|
static_assert(!std::is_copy_assignable_v<LineageArchive>);
|
|
static_assert(!std::is_move_assignable_v<LineageArchive>);
|
|
// These checks are live: an ordinary container admits all of them.
|
|
static_assert(replaceable<std::vector<std::uint64_t>>);
|
|
static_assert(exchangeable<std::vector<std::uint64_t>>);
|
|
// Copying stays available, because a clone takes nothing from the original.
|
|
static_assert(std::is_copy_constructible_v<LineageArchive>);
|
|
auto archive = branching();
|
|
CHECK(archive.record_birth(800, 3, evo::Tick{1}));
|
|
const auto taken_while_living = archive; // A copy from before the death.
|
|
CHECK(archive.record_death(800, evo::Tick{9}));
|
|
CHECK(!archive.organism(800)->alive);
|
|
// The copy is a separate archive. Nothing can write it back over the
|
|
// original, so the terminal record stands.
|
|
CHECK(taken_while_living.organism(800)->alive);
|
|
CHECK(!archive.organism(800)->alive);
|
|
// A move decays to a copy, so the source still holds every record rather
|
|
// than being emptied the way a moved-from container would be.
|
|
// NOLINTNEXTLINE(performance-move-const-arg)
|
|
const auto moved = std::move(archive);
|
|
// NOLINTNEXTLINE(bugprone-use-after-move): the point of the check.
|
|
CHECK(archive.archived() == moved.archived() && archive.recorded() == 1);
|
|
// NOLINTNEXTLINE(bugprone-use-after-move)
|
|
CHECK(archive.find(1) && !archive.organism(800)->alive);
|
|
}
|
|
|
|
void the_archive_offers_no_way_to_forget() {
|
|
static_assert(!erases<LineageArchive>);
|
|
static_assert(!removes<LineageArchive>);
|
|
static_assert(!clears<LineageArchive>);
|
|
static_assert(!prunes<LineageArchive>);
|
|
// The check itself is live: a container that does erase by key is detected.
|
|
static_assert(erases<std::map<std::uint64_t, std::uint64_t>>);
|
|
static_assert(clears<std::map<std::uint64_t, std::uint64_t>>);
|
|
auto archive = branching();
|
|
const auto before = archive.archived();
|
|
CHECK(archive.record_birth(700, 1, evo::Tick{1}));
|
|
CHECK(archive.record_death(700, evo::Tick{2}));
|
|
// Death changed nothing about how much is retained.
|
|
CHECK(archive.archived() == before && archive.recorded() == 1);
|
|
}
|
|
|
|
void capacity_refuses_loudly_rather_than_dropping() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(1, 0)));
|
|
for (std::uint64_t id = 2; id <= max_archived_genomes; ++id) {
|
|
CHECK(archive.admit(descend(id, id - 1)));
|
|
}
|
|
CHECK(archive.archived() == max_archived_genomes);
|
|
const auto overflowing = archive.admit(descend(max_archived_genomes + 1, max_archived_genomes));
|
|
CHECK(!overflowing && overflowing.error() == HistoryError::archive_full);
|
|
// Refusing to record is not deleting: everything admitted is still here.
|
|
CHECK(archive.archived() == max_archived_genomes);
|
|
CHECK(archive.find(1) && archive.find(max_archived_genomes));
|
|
const auto deep = archive.ancestry(max_archived_genomes);
|
|
CHECK(deep && deep->size() == max_archived_genomes);
|
|
CHECK(deep->front() == 1 && deep->back() == max_archived_genomes);
|
|
CHECK(*archive.common_ancestor(max_archived_genomes, 1) == 1);
|
|
}
|
|
std::uint64_t field(const std::vector<std::uint8_t>& b, std::size_t at, unsigned width) {
|
|
std::uint64_t value{};
|
|
for (unsigned i = 0; i < width; ++i)
|
|
value |= static_cast<std::uint64_t>(b[at + i]) << (8U * i);
|
|
return value;
|
|
}
|
|
void set(std::vector<std::uint8_t>& b, std::size_t at, std::uint64_t value, unsigned width) {
|
|
for (unsigned i = 0; i < width; ++i)
|
|
b[at + i] = static_cast<std::uint8_t>((value >> (8U * i)) & 0xFFU);
|
|
}
|
|
// Recomputes the trailing FNV so a test reaches the check it is aimed at.
|
|
void reseal(std::vector<std::uint8_t>& b) {
|
|
std::uint64_t h = 14695981039346656037ULL;
|
|
for (std::size_t i = 0; i + 8 < b.size(); ++i)
|
|
h = (h ^ b[i]) * 1099511628211ULL;
|
|
set(b, b.size() - 8, h, 8);
|
|
}
|
|
LineageArchive with_history() {
|
|
auto archive = branching();
|
|
CHECK(archive.record_birth(100, 4, evo::Tick{10}));
|
|
CHECK(archive.record_birth(101, 5, evo::Tick{12}));
|
|
CHECK(archive.record_birth(90, 1, evo::Tick{0}));
|
|
CHECK(archive.record_death(90, evo::Tick{0}));
|
|
CHECK(archive.record_death(100, evo::Tick{30}));
|
|
return archive;
|
|
}
|
|
|
|
void the_archive_round_trips_canonically() {
|
|
const auto archive = with_history();
|
|
const auto bytes = archive.encode();
|
|
CHECK(field(bytes, 0, 8) == 0x3152414C4F564541ULL && field(bytes, 8, 4) == 1);
|
|
CHECK(field(bytes, 16, 4) == 5 && field(bytes, 20, 4) == 3 &&
|
|
field(bytes, 24, 8) == bytes.size());
|
|
const auto decoded = LineageArchive::decode(bytes);
|
|
CHECK(decoded && decoded->encode() == bytes);
|
|
CHECK(decoded->archived() == 5 && decoded->recorded() == 3);
|
|
CHECK(*decoded->find(4) == *archive.find(4));
|
|
const auto dead = decoded->organism(100);
|
|
CHECK(dead && !dead->alive && dead->died.value == 30 && dead->born.value == 10);
|
|
CHECK(decoded->ancestry(4) == archive.ancestry(4));
|
|
CHECK(decoded->living_organisms() == std::vector<std::uint64_t>{101});
|
|
CHECK(decoded->highest_genome() == 5 && decoded->highest_organism() == 101);
|
|
// A clone decodes into a new archive; the original is untouched.
|
|
CHECK(archive.recorded() == 3 && archive.encode() == bytes);
|
|
// Admission order does not change the bytes, only the content does.
|
|
LineageArchive reordered;
|
|
for (const auto& [id, parent, c] :
|
|
{std::tuple{1, 0, 400U}, std::tuple{3, 1, 391U}, std::tuple{2, 1, 409U},
|
|
std::tuple{5, 2, 400U}, std::tuple{4, 2, 418U}})
|
|
CHECK(reordered.admit(
|
|
descend(static_cast<std::uint64_t>(id), static_cast<std::uint64_t>(parent), c)));
|
|
CHECK(reordered.record_birth(90, 1, evo::Tick{0}));
|
|
CHECK(reordered.record_birth(101, 5, evo::Tick{12}));
|
|
CHECK(reordered.record_birth(100, 4, evo::Tick{10}));
|
|
CHECK(reordered.record_death(100, evo::Tick{30}));
|
|
CHECK(reordered.record_death(90, evo::Tick{0}));
|
|
CHECK(reordered.encode() == bytes);
|
|
// A child with a smaller identity than its parent still follows its parent.
|
|
LineageArchive inverted;
|
|
CHECK(inverted.admit(descend(50, 0)) && inverted.admit(descend(7, 50)));
|
|
const auto inverted_bytes = inverted.encode();
|
|
CHECK(LineageArchive::decode(inverted_bytes));
|
|
LineageArchive empty;
|
|
CHECK(LineageArchive::decode(empty.encode()) && empty.encode().size() == 40);
|
|
}
|
|
|
|
void the_archive_decoder_refuses_damage_and_disorder() {
|
|
const auto bytes = with_history().encode();
|
|
const auto refused = [&](auto change, ArchiveCodecError error, bool seal = true) {
|
|
auto b = bytes;
|
|
change(b);
|
|
if (seal)
|
|
reseal(b);
|
|
const auto d = LineageArchive::decode(b);
|
|
return !d && d.error() == error;
|
|
};
|
|
CHECK(refused([](auto& b) { b[40] ^= 1U; }, ArchiveCodecError::checksum, false));
|
|
CHECK(refused([](auto& b) { b[0] ^= 1U; }, ArchiveCodecError::format));
|
|
CHECK(refused([](auto& b) { b[8] = 3; }, ArchiveCodecError::version));
|
|
CHECK(refused([](auto& b) { b[12] = 2; }, ArchiveCodecError::version));
|
|
CHECK(refused([](auto& b) { set(b, 16, max_archived_genomes + 1, 4); },
|
|
ArchiveCodecError::bounds));
|
|
CHECK(refused([](auto& b) { set(b, 20, max_archived_organisms + 1, 4); },
|
|
ArchiveCodecError::bounds));
|
|
CHECK(refused([](auto& b) { set(b, 24, b.size() + 1, 8); }, ArchiveCodecError::format));
|
|
CHECK(refused(
|
|
[](auto& b) {
|
|
b.insert(b.end() - 8, 0);
|
|
set(b, 24, b.size(), 8);
|
|
},
|
|
ArchiveCodecError::format));
|
|
CHECK(refused([](auto& b) { set(b, 20, 2, 4); }, ArchiveCodecError::format));
|
|
// Genome body damage (its own digest), then an organism record naming no genome.
|
|
CHECK(refused([](auto& b) { b[40] ^= 1U; }, ArchiveCodecError::genome));
|
|
const auto organisms = bytes.size() - 8 - (3 * archived_organism_bytes);
|
|
CHECK(refused([&](auto& b) { set(b, organisms + 8, 77, 8); }, ArchiveCodecError::history));
|
|
CHECK(refused([&](auto& b) { b[organisms + 32] = 2; }, ArchiveCodecError::format));
|
|
// Alive with a death tick; death before birth; organisms out of order.
|
|
const auto alive = organisms + (2 * archived_organism_bytes); // Organism 101.
|
|
CHECK(refused([&](auto& b) { set(b, alive + 24, 5, 8); }, ArchiveCodecError::format));
|
|
const auto dead = organisms + archived_organism_bytes; // Organism 100.
|
|
CHECK(refused([&](auto& b) { set(b, dead + 24, 9, 8); }, ArchiveCodecError::history));
|
|
CHECK(refused(
|
|
[&](auto& b) {
|
|
std::swap_ranges(b.begin() + static_cast<std::ptrdiff_t>(organisms),
|
|
b.begin() + static_cast<std::ptrdiff_t>(organisms + 33),
|
|
b.begin() + static_cast<std::ptrdiff_t>(dead));
|
|
},
|
|
ArchiveCodecError::noncanonical));
|
|
// A child encoded before its parent is refused by admission itself.
|
|
LineageArchive pair;
|
|
CHECK(pair.admit(descend(1, 0)) && pair.admit(descend(2, 1)));
|
|
auto swapped = pair.encode();
|
|
const auto first = field(swapped, 32, 2);
|
|
std::vector<std::uint8_t> parent(swapped.begin() + 32,
|
|
swapped.begin() + 34 + static_cast<std::ptrdiff_t>(first));
|
|
std::vector<std::uint8_t> child(swapped.begin() + 34 + static_cast<std::ptrdiff_t>(first),
|
|
swapped.end() - 8);
|
|
std::ranges::copy(child, swapped.begin() + 32);
|
|
std::ranges::copy(parent, swapped.begin() + 32 + static_cast<std::ptrdiff_t>(child.size()));
|
|
reseal(swapped);
|
|
const auto dangling = LineageArchive::decode(swapped);
|
|
CHECK(!dangling && dangling.error() == ArchiveCodecError::history);
|
|
CHECK(!LineageArchive::decode(std::span{bytes}.first(39)));
|
|
}
|
|
|
|
void genome_revisions_preserve_birth_and_replay_current_genome() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(10, 0)));
|
|
CHECK(archive.record_birth(50, 10, evo::Tick{100}));
|
|
CHECK(archive.current_genome(50) && *archive.current_genome(50) == 10);
|
|
const auto successor = descend(11, 10, 420, genome_schema_v2);
|
|
CHECK(archive.record_mutation(50, successor, evo::Tick{101}));
|
|
CHECK(archive.revisions().size() == 1);
|
|
CHECK(archive.current_genome(50) && *archive.current_genome(50) == 11);
|
|
const auto birth = archive.organism(50);
|
|
CHECK(birth && birth->genome == 10 && birth->born.value == 100 && birth->alive);
|
|
CHECK(archive.find(11) && archive.find(11)->parents()[0] == 10);
|
|
CHECK(archive.ancestry(11) && (*archive.ancestry(11) == std::vector<std::uint64_t>{10, 11}));
|
|
|
|
auto bytes = archive.encode();
|
|
CHECK(field(bytes, 8, 4) == lineage_archive_revision_format);
|
|
CHECK(field(bytes, 24, 4) == 1 && field(bytes, 32, 8) == bytes.size());
|
|
const auto decoded = LineageArchive::decode(bytes);
|
|
CHECK(decoded && decoded->encode() == bytes && decoded->revisions().size() == 1);
|
|
CHECK(decoded->current_genome(50) && *decoded->current_genome(50) == 11);
|
|
CHECK(decoded->organism(50) && decoded->organism(50)->genome == 10);
|
|
|
|
CHECK(archive.record_death(50, evo::Tick{102}));
|
|
CHECK(archive.current_genome(50) && *archive.current_genome(50) == 11);
|
|
CHECK(!archive.record_mutation(50, descend(12, 11), evo::Tick{103}));
|
|
const auto died_bytes = archive.encode();
|
|
auto died = LineageArchive::decode(died_bytes);
|
|
CHECK(died && died->current_genome(50) && *died->current_genome(50) == 11);
|
|
CHECK(!died->record_death(50, evo::Tick{101}));
|
|
}
|
|
|
|
void mutation_history_upgrade_and_failures_are_explicit() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(10, 0)));
|
|
CHECK(archive.record_birth(50, 10, evo::Tick{10}));
|
|
const auto format1 = archive.encode();
|
|
archive.upgrade_to_revision_format();
|
|
const auto format2 = archive.encode();
|
|
CHECK(field(format1, 8, 4) == lineage_archive_format);
|
|
CHECK(field(format2, 8, 4) == lineage_archive_revision_format);
|
|
CHECK(field(format2, 24, 4) == 0);
|
|
const auto migrated = LineageArchive::decode(format2);
|
|
CHECK(migrated && migrated->encode() == format2 && migrated->revisions().empty());
|
|
|
|
auto fresh = LineageArchive{};
|
|
CHECK(fresh.admit(descend(10, 0)) && fresh.record_birth(50, 10, evo::Tick{10}));
|
|
const auto wrong_parent = descend(11, 9);
|
|
CHECK(!fresh.record_mutation(50, wrong_parent, evo::Tick{11}));
|
|
CHECK(fresh.archived() == 1 && fresh.current_genome(50) && *fresh.current_genome(50) == 10);
|
|
CHECK(!fresh.record_mutation(50, descend(12, 10), evo::Tick{10}));
|
|
CHECK(!fresh.record_mutation(50, descend(13, 10), evo::Tick{9}));
|
|
CHECK(fresh.record_mutation(50, descend(11, 10), evo::Tick{11}));
|
|
CHECK(!fresh.record_mutation(50, descend(14, 11), evo::Tick{11}));
|
|
CHECK(fresh.record_mutation(50, descend(14, 11), evo::Tick{12}));
|
|
CHECK(!fresh.record_death(50, evo::Tick{12}));
|
|
CHECK(fresh.record_death(50, evo::Tick{13}));
|
|
CHECK(fresh.current_genome(50) && *fresh.current_genome(50) == 14);
|
|
}
|
|
|
|
// ADR-0099: the header names the highest genome schema held, so an archive of schema 1
|
|
// records keeps its exact bytes, and a mixed archive round-trips.
|
|
void schemas_mix_without_rewriting() {
|
|
const auto old = with_history().encode();
|
|
CHECK(field(old, 12, 4) == genome_schema_v1);
|
|
auto archive = branching();
|
|
CHECK(archive.admit(descend(6, 4, 400, genome_schema_v2)));
|
|
const auto bytes = archive.encode();
|
|
CHECK(field(bytes, 12, 4) == genome_schema_v2);
|
|
const auto decoded = LineageArchive::decode(bytes);
|
|
CHECK(decoded && decoded->encode() == bytes);
|
|
CHECK(decoded->find(6)->schema_version() == 2 && decoded->find(4)->schema_version() == 1);
|
|
// A header that understates its records is refused.
|
|
auto understated = bytes;
|
|
set(understated, 12, genome_schema_v1, 4);
|
|
reseal(understated);
|
|
CHECK(!LineageArchive::decode(understated));
|
|
auto unknown = old;
|
|
set(unknown, 12, 3, 4);
|
|
reseal(unknown);
|
|
CHECK(!LineageArchive::decode(unknown));
|
|
}
|
|
|
|
void a_full_archive_round_trips() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(1, 0)));
|
|
for (std::uint64_t id = 2; id <= max_archived_genomes; ++id)
|
|
CHECK(archive.admit(descend(id, id - 1)));
|
|
for (std::uint64_t o = 1; o <= max_archived_organisms; ++o) {
|
|
CHECK(archive.record_birth(o, 1 + (o % max_archived_genomes), evo::Tick{o}));
|
|
if (o % 3 == 0)
|
|
CHECK(archive.record_death(o, evo::Tick{o + 1}));
|
|
}
|
|
const auto bytes = archive.encode();
|
|
CHECK(bytes.size() <= max_archive_legacy_bytes);
|
|
const auto decoded = LineageArchive::decode(bytes);
|
|
CHECK(decoded && decoded->encode() == bytes && decoded->recorded() == max_archived_organisms);
|
|
LineageArchive destination;
|
|
const std::array<std::uint64_t, 1> selected{8191}; // Alive; carries the deepest genome.
|
|
const auto imported = destination.merge_living_from(archive, selected);
|
|
CHECK(imported && imported->recorded() == 1 && imported->archived() == max_archived_genomes);
|
|
const auto imported_ancestry = imported->ancestry(max_archived_genomes);
|
|
CHECK(imported_ancestry && imported_ancestry->size() == max_archived_genomes);
|
|
CHECK(archive.encode() == bytes && destination.recorded() == 0);
|
|
LineageArchive unrelated;
|
|
CHECK(unrelated.admit(descend(5'000'000, 0)));
|
|
CHECK(unrelated.record_birth(5'000'001, 5'000'000, evo::Tick{1}));
|
|
const auto full_before = imported->encode();
|
|
const auto overflow =
|
|
imported->merge_living_from(unrelated, std::array<std::uint64_t, 1>{5'000'001});
|
|
CHECK(!overflow && overflow.error() == HistoryError::archive_full &&
|
|
imported->encode() == full_before);
|
|
std::cout << "lineage_archive_full_bytes=" << bytes.size()
|
|
<< " max=" << max_archive_legacy_bytes << '\n';
|
|
}
|
|
void ownership_import_retains_exact_history() {
|
|
LineageArchive source;
|
|
CHECK(source.admit(descend(90, 0)));
|
|
CHECK(source.record_birth(500, 90, evo::Tick{10}));
|
|
CHECK(source.record_birth(501, 90, evo::Tick{10}));
|
|
source.upgrade_to_revision_format();
|
|
CHECK(source.record_mutation(500, descend(20, 90, 409), evo::Tick{11}));
|
|
CHECK(source.record_mutation(500, descend(10, 20, 418), evo::Tick{12}));
|
|
LineageArchive target;
|
|
CHECK(target.admit(descend(700, 0, 391)));
|
|
CHECK(target.record_birth(1000, 700, evo::Tick{1}));
|
|
CHECK(target.record_death(1000, evo::Tick{2}));
|
|
const auto source_before = source.encode(), target_before = target.encode();
|
|
const std::array<std::uint64_t, 1> selected{500};
|
|
const auto merged = target.merge_living_from(source, selected);
|
|
CHECK(merged && merged->recorded() == 2 && merged->archived() == 4);
|
|
CHECK(merged->organism(500) == source.organism(500));
|
|
CHECK(merged->current_genome(500) == source.current_genome(500));
|
|
CHECK(merged->ancestry(10) == source.ancestry(10));
|
|
CHECK(merged->revisions().size() == 2 && !merged->organism(501));
|
|
const auto retained_dead = merged->organism(1000);
|
|
CHECK(retained_dead && !retained_dead->alive && merged->find(700));
|
|
CHECK(source.encode() == source_before && target.encode() == target_before);
|
|
const auto repeated = merged->merge_living_from(source, selected);
|
|
CHECK(repeated && repeated->encode() == merged->encode());
|
|
const auto restored = LineageArchive::decode(merged->encode());
|
|
CHECK(restored && restored->encode() == merged->encode());
|
|
|
|
LineageArchive prefix;
|
|
CHECK(prefix.admit(descend(90, 0)));
|
|
CHECK(prefix.record_birth(500, 90, evo::Tick{10}));
|
|
prefix.upgrade_to_revision_format();
|
|
CHECK(prefix.record_mutation(500, descend(20, 90, 409), evo::Tick{11}));
|
|
const auto prefix_before = prefix.encode();
|
|
const auto continued = prefix.merge_living_from(source, selected);
|
|
CHECK(continued && continued->current_genome(500) == source.current_genome(500));
|
|
CHECK(!source.merge_living_from(prefix, selected)); // Older evidence cannot rewind history.
|
|
CHECK(prefix.encode() == prefix_before && source.encode() == source_before);
|
|
|
|
LineageArchive dead{*merged};
|
|
CHECK(dead.record_death(500, evo::Tick{20}));
|
|
const auto dead_before = dead.encode();
|
|
CHECK(!dead.merge_living_from(source, selected) && dead.encode() == dead_before);
|
|
CHECK(!target.merge_living_from(dead, selected));
|
|
LineageArchive conflict;
|
|
CHECK(conflict.admit(descend(90, 0, 300)));
|
|
const auto conflict_before = conflict.encode();
|
|
CHECK(!conflict.merge_living_from(source, selected) && conflict.encode() == conflict_before);
|
|
LineageArchive different_birth;
|
|
CHECK(different_birth.admit(descend(90, 0)));
|
|
CHECK(different_birth.record_birth(500, 90, evo::Tick{9}));
|
|
const auto birth_before = different_birth.encode();
|
|
CHECK(!different_birth.merge_living_from(source, selected) &&
|
|
different_birth.encode() == birth_before);
|
|
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 2>{500, 500}));
|
|
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 2>{501, 500}));
|
|
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 1>{0}));
|
|
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 1>{9999}));
|
|
CHECK(!target.merge_living_from(source, {}));
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
a_genome_outlives_every_organism_that_carried_it();
|
|
ancestry_and_descendants_resolve_through_a_dead_middle();
|
|
branching_lineages_resolve_their_divergence();
|
|
admission_refuses_duplicates_and_dangling_parents();
|
|
death_is_terminal_and_births_are_unique();
|
|
absence_is_never_reported_as_extinction();
|
|
the_archive_cannot_be_replaced_or_rewound();
|
|
the_archive_offers_no_way_to_forget();
|
|
capacity_refuses_loudly_rather_than_dropping();
|
|
the_archive_round_trips_canonically();
|
|
the_archive_decoder_refuses_damage_and_disorder();
|
|
genome_revisions_preserve_birth_and_replay_current_genome();
|
|
mutation_history_upgrade_and_failures_are_explicit();
|
|
a_full_archive_round_trips();
|
|
ownership_import_retains_exact_history();
|
|
schemas_mix_without_rewriting();
|
|
std::cout << "PASS lineage retention, branching ancestry and provisional absence\n";
|
|
return 0;
|
|
} catch (const std::exception& error) {
|
|
std::cerr << error.what() << '\n';
|
|
return 1;
|
|
}
|