// tests/test_terrain_repair.cpp — exact monotone slope closure, saddle removal, and deterministic // projection of arbitrary vertex targets into the canonical terrain domain. #include "sc2k/terrain/repair.hpp" #include #include #include #include #include "test_harness.hpp" int main() { using namespace sc2k::terrain; const auto same_heights = [](const Model& left, const Model& right) { return left.heights().size() == right.heights().size() && std::equal(left.heights().begin(), left.heights().end(), right.heights().begin()); }; // An isolated three-level peak is repaired by lowering only its centre to the highest legal value. std::vector peak(9, 0); peak[4] = 3 * kHeightQuantum; RepairResult repaired_peak = repair({2, 2}, peak); CHECK(repaired_peak.terrain.has_value()); CHECK(repaired_peak.terrain->validate().valid()); CHECK(repaired_peak.stats.direction == RepairDirection::Lower); CHECK(repaired_peak.stats.changed_vertices == 1); CHECK(repaired_peak.terrain->vertex({1, 1}) == kHeightQuantum); // The symmetric pit chooses the upward closure and likewise changes only its centre. std::vector pit(9, 0); pit[4] = -3 * kHeightQuantum; RepairResult repaired_pit = repair({2, 2}, pit); CHECK(repaired_pit.terrain.has_value()); CHECK(repaired_pit.terrain->validate().valid()); CHECK(repaired_pit.stats.direction == RepairDirection::Raise); CHECK(repaired_pit.stats.changed_vertices == 1); CHECK(repaired_pit.terrain->vertex({1, 1}) == -kHeightQuantum); // The forbidden alternating saddle is resolved monotonically and cannot survive construction. std::vector saddle = { kHeightQuantum, 0, 0, kHeightQuantum, }; RepairResult repaired_saddle = repair({1, 1}, saddle); CHECK(repaired_saddle.terrain.has_value()); CHECK(repaired_saddle.terrain->validate().valid()); CHECK(repaired_saddle.stats.saddle_adjustments == 1); CHECK(repaired_saddle.stats.changed_vertices == 1); // Candidate values are safely clamped and quantized before repair. std::vector fractional = {1, 127, 128, -129}; RepairResult repaired_fractional = repair({1, 1}, fractional); CHECK(repaired_fractional.terrain.has_value()); CHECK(repaired_fractional.terrain->validate().valid()); for (const Height height : repaired_fractional.terrain->heights()) CHECK(height_is_quantized(height)); CHECK(!repair({2, 2}, std::vector(8)).terrain.has_value()); CHECK(!repair({2, 2}, std::vector(9), std::vector(8)).terrain.has_value()); // A hard ceiling is an invariant, not a preference: repairs may lower neighbouring vertices to // meet the slope bound but may never solve a contradiction by raising the constrained vertex. { std::vector targets(9, 5 * kHeightQuantum); targets[4] = 2 * kHeightQuantum; std::vector ceilings(9, kMaximumHeight); ceilings[4] = 3 * kHeightQuantum; RepairResult bounded = repair({2, 2}, targets, {}, ceilings); CHECK(bounded.terrain.has_value()); CHECK(bounded.terrain->vertex({1, 1}) <= 3 * kHeightQuantum); CHECK(bounded.terrain->validate().valid()); CHECK(!repair({2, 2}, targets, {}, std::vector(8)).terrain.has_value()); } // Property sweep: hostile random targets of widely varying heights always produce valid, // deterministic terrain. This covers mixed cliffs and interacting saddle corrections. std::uint32_t random = 0xC0FFEEu; auto next_random = [&] { random = random * 1664525u + 1013904223u; return random; }; for (int trial = 0; trial < 100; ++trial) { std::vector targets(9 * 8); std::vector weights(targets.size()); for (std::size_t i = 0; i < targets.size(); ++i) { targets[i] = (static_cast(next_random() % 25) - 12) * kHeightQuantum; weights[i] = static_cast(1 + next_random() % 16); } RepairResult first = repair({8, 7}, targets, weights); RepairResult second = repair({8, 7}, targets, weights); CHECK(first.terrain.has_value()); CHECK(second.terrain.has_value()); if (!first.terrain || !second.terrain) continue; CHECK(first.terrain->validate().valid()); CHECK(same_heights(*first.terrain, *second.terrain)); CHECK(first.stats.weighted_error == second.stats.weighted_error); CHECK(first.stats.direction == second.stats.direction); } if (failures == 0) { std::printf("terrain repair: exact peak/pit closure, saddle removal, 100 random fields\n"); return 0; } std::printf("terrain repair: %d failure(s)\n", failures); return 1; }