#include "evo/biology/cellular_tuning.hpp" #include #include #include #include #include #include namespace { using namespace evo::biology; void check(bool value, std::source_location at = std::source_location::current()) { if (!value) throw std::runtime_error("cellular tuning line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) CellularTuning v0() { return {.version = 1, .region_width_um = 60'000, .region_height_um = 60'000, .region_organism_cap = 2'000, .region_deposit_limit = 8'192, .region_perception_radius_um = 5'000, .founder_thrust_um_per_s2 = 400, .founder_reverse_thrust_um_per_s2 = 400, .founder_drag_ppm_per_tick = 95'163, .founder_lateral_drag_ppm_per_tick = 95'163, .founder_turn_heading_per_s = 65'536, .currents_min_count = 2, .currents_max_count = 4, .currents_min_width_um = 3'000, .currents_max_width_um = 6'000, .currents_min_speed_um_per_s = 60, .currents_max_speed_um_per_s = 300, .currents_grid_pitch_um = 250, .food_merged_bubble_cap_multiple = 8, .food_visible_moving_bubbles = 256, .food_induced_drift_radius_um = 200, .food_induced_drift_max_um_per_s = 100, .food_induced_drift_decay_ppm_per_tick = 95'163, .thermal_ambient_min_mk = 278'150, .thermal_ambient_max_mk = 313'150, .founder_preferred_mk = 293'150, .founder_comfort_band_mk = 5'000, .founder_body_tau_ms = 20'000, .founder_lethal_cold_mk = 273'150, .founder_lethal_heat_mk = 318'150, .thermal_off_preference_per_mille_per_k = 80, .thermal_transition_per_mille_per_k = 40, .thermal_multiplier_cap_per_mille = 8'000, .lifecycle_logout_delay_ms = 10'000, .mutation_step_points = 5, .mutation_paired_points = 2, .mutation_recovery_floor_per_mille = 100, .mutation_offer_interval_ms = 25'000, .mutation_offer_options = 3}; } // Replaces the first `key = ...` line of canonical text. std::string with(std::string text, const std::string& key, const std::string& value) { const auto at = text.find(key + " = "); check(at != std::string::npos); const auto end = text.find('\n', at); return text.replace(at, end - at, key + " = " + value); } CellularTuningErrorKind error_of(const std::string& text) { const auto parsed = parse_cellular_tuning(text); check(!parsed.has_value()); return parsed.error().kind; } void canonical_round_trip() { const auto text = encode_cellular_tuning(v0()); const auto parsed = parse_cellular_tuning(text); CHECK(parsed && *parsed == v0()); CHECK(encode_cellular_tuning(*parsed) == text); } void comments_separators_and_whitespace() { auto text = "# heading\n\n" + encode_cellular_tuning(v0()); text = with(text, "region.width_um", "60_000 # trailing comment\r"); text = with(text, "region.height_um", "\t60000"); const auto parsed = parse_cellular_tuning(text); CHECK(parsed && *parsed == v0()); } void refusals() { const auto base = encode_cellular_tuning(v0()); CHECK(error_of(base + "unknown.key = 1\n") == CellularTuningErrorKind::unknown_key); CHECK(error_of(base + "region.width_um = 60000\n") == CellularTuningErrorKind::duplicate_key); CHECK(error_of(base + "no equals sign\n") == CellularTuningErrorKind::syntax); CHECK(error_of(base + "Region.Width = 1\n") == CellularTuningErrorKind::syntax); CHECK(error_of(with(base, "region.width_um", "60k")) == CellularTuningErrorKind::invalid_value); CHECK(error_of(with(base, "region.width_um", "60__000")) == CellularTuningErrorKind::invalid_value); CHECK(error_of(with(base, "region.width_um", "")) == CellularTuningErrorKind::invalid_value); CHECK(error_of(with(base, "region.width_um", "999")) == CellularTuningErrorKind::out_of_range); CHECK(error_of(with(base, "founder.drag_ppm_per_tick", "1000000")) == CellularTuningErrorKind::out_of_range); CHECK(error_of(with(base, "region.width_um", "# missing")) == CellularTuningErrorKind::invalid_value); CHECK(error_of(with(base, "currents.min_count", "5")) == CellularTuningErrorKind::inconsistent); CHECK(error_of(with(base, "founder.preferred_mk", "320000")) == CellularTuningErrorKind::inconsistent); CHECK(error_of(with(base, "tuning.version", "2")) == CellularTuningErrorKind::unsupported_version); auto missing = base; missing.erase(missing.find("mutation.offer_options")); const auto parsed = parse_cellular_tuning(missing); CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key && parsed.error().key == "mutation.offer_options"); const auto line = parse_cellular_tuning(base + "\nbad\n"); CHECK(!line && line.error().line == 41); CHECK(error_of(std::string(max_cellular_tuning_bytes + 1, '#')) == CellularTuningErrorKind::too_large); } void founder_handling() { const auto handling = founder_cellular_handling(v0(), {1'000}, {900}); CHECK(handling.thrust_q10 == 20'480 && handling.reverse_thrust_q10 == 20'480); CHECK(handling.forward_drag_ppm == 95'163 && handling.lateral_drag_ppm == 95'163); CHECK(handling.turn == 3'276); CHECK(handling.thrust_cost.value == 1'000 && handling.reverse_cost.value == 900); } // The checked-in DEV tuning file must parse; CTest passes its path when it is present. void repository_file(const char* path) { std::ifstream file(path, std::ios::binary); CHECK(file.good()); const std::string text{std::istreambuf_iterator(file), std::istreambuf_iterator()}; const auto parsed = parse_cellular_tuning(text); if (!parsed) std::fprintf(stderr, "%s: line %zu %s: %s\n", path, parsed.error().line, parsed.error().key.c_str(), std::string{describe(parsed.error().kind)}.c_str()); CHECK(parsed && *parsed == v0()); } } // namespace int main(int argc, char** argv) { try { canonical_round_trip(); comments_separators_and_whitespace(); refusals(); founder_handling(); if (argc > 1) repository_file(argv[1]); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } std::puts("cellular tuning contracts passed"); return 0; }