// tests/test_native_terrain.cpp — deterministic TERN schema encoding, validation, and corruption rejection. #include "sc2k/io/native_terrain.hpp" #include #include #include #include #include "test_harness.hpp" int main() { using namespace sc2k; std::vector heights(12, 4 * terrain::kHeightQuantum); heights[5] += terrain::kHeightQuantum; const std::optional model = terrain::Model::create({3, 2}, heights); CHECK(model.has_value()); const auto first = io::encode_native_terrain(*model); const auto second = io::encode_native_terrain(*model); CHECK(first.has_value()); CHECK(second.has_value()); CHECK(first == second); CHECK(first->size() == 52 + heights.size() * sizeof(std::int32_t) + 8); const auto decoded = io::decode_native_terrain(*first); CHECK(decoded.has_value()); CHECK(decoded->extent() == model->extent()); CHECK(decoded->heights().size() == model->heights().size()); CHECK(std::equal(decoded->heights().begin(), decoded->heights().end(), model->heights().begin())); const terrain::Corners raised = {6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum}; std::vector patches = {{{0, 0}, raised}}; std::vector split_edges = { {{terrain::EdgeAxis::Row, {1, 0}}, {6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum}, {4 * terrain::kHeightQuantum, 5 * terrain::kHeightQuantum}, terrain::FaceMaterial::NaturalRock, terrain::FaceOwner::Negative}, {{terrain::EdgeAxis::Column, {0, 1}}, {6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum}, {4 * terrain::kHeightQuantum, 5 * terrain::kHeightQuantum}, terrain::FaceMaterial::Quay, terrain::FaceOwner::Negative}, }; const auto split_model = terrain::Model::create({3, 2}, heights, patches, split_edges); CHECK(split_model.has_value()); const auto split_payload = io::encode_native_terrain(*split_model); CHECK(split_payload.has_value()); const auto split_decoded = io::decode_native_terrain(*split_payload); CHECK(split_decoded.has_value()); CHECK(split_decoded->surface_patches().size() == split_model->surface_patches().size()); CHECK(std::equal(split_decoded->surface_patches().begin(), split_decoded->surface_patches().end(), split_model->surface_patches().begin())); CHECK(split_decoded->split_edges().size() == split_model->split_edges().size()); CHECK(std::equal(split_decoded->split_edges().begin(), split_decoded->split_edges().end(), split_model->split_edges().begin())); // Schema 1 has only the welded fabric and migrates to an empty discontinuity set. std::vector schema_one(first->begin(), first->begin() + 52 + heights.size() * sizeof(std::int32_t)); schema_one[3] = '1'; const auto migrated = io::decode_native_terrain(schema_one); CHECK(migrated.has_value()); CHECK(migrated->surface_patches().empty()); CHECK(migrated->split_edges().empty()); std::vector truncated = *first; truncated.pop_back(); CHECK(!io::decode_native_terrain(truncated).has_value()); std::vector badMagic = *first; badMagic[0] = 'X'; CHECK(!io::decode_native_terrain(badMagic).has_value()); std::vector oversized = *first; oversized[4] = 0x01; oversized[5] = 0x10; // width 4097, over the decoder limit CHECK(!io::decode_native_terrain(oversized).has_value()); const auto tooWide = terrain::Model::create({4097, 1}, std::vector(4098 * 2, 0)); CHECK(tooWide.has_value()); CHECK(!io::encode_native_terrain(*tooWide).has_value()); // A structurally correct payload containing a forbidden saddle is rejected by Model validation. const auto flat = terrain::Model::create({1, 1}, std::vector(4, 0)); std::vector saddle = *io::encode_native_terrain(*flat); auto patch_i32 = [&](std::size_t offset, std::int32_t value) { for (int byte = 0; byte < 4; ++byte) saddle[offset + byte] = static_cast(static_cast(value) >> (8 * byte)); }; patch_i32(52, terrain::kHeightQuantum); // row 0 absolute: high patch_i32(56, -terrain::kHeightQuantum); // row 0 delta: low patch_i32(60, 0); // row 1 absolute: low patch_i32(64, terrain::kHeightQuantum); // row 1 delta: high CHECK(!io::decode_native_terrain(saddle).has_value()); if (failures == 0) { std::printf("native terrain: deterministic row deltas and strict decode validation\n"); return 0; } std::printf("native terrain: %d failure(s)\n", failures); return 1; }