#include "sc2k/io/native_thing_continuation.hpp" #include #include #include #include #include #include #include "test_harness.hpp" using namespace sc2k; namespace { void set_u16(std::vector& bytes, std::size_t offset, std::uint16_t value) { bytes[offset] = static_cast(value); bytes[offset + 1] = static_cast(value >> 8); } void set_u32(std::vector& bytes, std::size_t offset, std::uint32_t value) { for (int byte = 0; byte < 4; ++byte) bytes[offset + static_cast(byte)] = static_cast(value >> (8 * byte)); } sim::ThingContinuationState populated_state() { sim::ThingContinuationState state; state.vehicleRng.seed(0x12345678u); sim::CargoShipContinuation first; first.slot = 5; first.rawGoalX = 20; first.rawGoalY = 21; first.dwell = 172; first.berthX = 30; first.berthY = 31; first.pierX = 30; first.pierY = 32; first.waypoints = {{10, 11}, {15, 16}, {30, 31}}; first.cursor = 2; state.cargoShips.push_back(first); sim::CargoShipContinuation second; second.slot = 17; second.rawGoalX = 100; second.rawGoalY = 101; second.failed = true; state.cargoShips.push_back(second); CHECK(sim::validate_thing_continuation(state) == sim::ThingContinuationValidationError::None); return state; } City matching_city() { City city; for (int x = 8; x <= 35; ++x) for (int y = 8; y <= 35; ++y) { city.xter.at(x, y) = 0x10; city.xbit.at(x, y) = 0x04; } city.xbld.at(30, 32) = 0xdf; constexpr std::uint8_t slot = 5; city.xthg[slot * 12 + 0] = 3; city.xthg[slot * 12 + 2] = 3; city.xthg[slot * 12 + 3] = 10; city.xthg[slot * 12 + 4] = 11; city.xthg[slot * 12 + 8] = 20; city.xthg[slot * 12 + 9] = 21; city.xtxt.at(10, 11) = static_cast( static_cast(xtxt::kThingFirst) + slot); sim::ThingContinuationState state = populated_state(); state.cargoShips.resize(1); CHECK(sim::thing_continuation_matches_city(state, city)); return city; } void test_round_trip_and_shape() { const sim::ThingContinuationState state = populated_state(); const auto first = io::encode_native_thing_continuation(state); const auto second = io::encode_native_thing_continuation(state); CHECK(first.has_value()); CHECK(first == second); if (!first) return; constexpr std::size_t expected = io::kNativeThingContinuationHeaderBytes + 2 * io::kNativeCargoShipContinuationBytes + 3 * io::kNativeCargoShipWaypointBytes; CHECK(first->size() == expected); CHECK((*first)[0] == 'T' && (*first)[1] == 'H' && (*first)[2] == 'N' && (*first)[3] == '1'); CHECK(io::decode_native_thing_continuation(*first) == state); const sim::ThingContinuationState empty; const auto encodedEmpty = io::encode_native_thing_continuation(empty); CHECK(encodedEmpty.has_value()); CHECK(encodedEmpty && encodedEmpty->size() == io::kNativeThingContinuationHeaderBytes); CHECK(encodedEmpty && io::decode_native_thing_continuation(*encodedEmpty) == empty); } void test_structural_reserved_and_overflow_rejection() { const auto encoded = io::encode_native_thing_continuation(populated_state()); CHECK(encoded.has_value()); if (!encoded) return; for (std::size_t size = 0; size < encoded->size(); ++size) { const std::span prefix(encoded->data(), size); CHECK(!io::decode_native_thing_continuation(prefix)); } auto malformed = *encoded; malformed.push_back(0); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; malformed[3] = '2'; CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 4, 2); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 6, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 14, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 12, 40); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; malformed[17] = 2; CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 18, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 26, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u32(malformed, 40, std::numeric_limits::max()); CHECK(!io::decode_native_thing_continuation(malformed)); std::vector oversized( io::kMaximumNativeThingContinuationPayloadBytes + 1, 0); CHECK(!io::decode_native_thing_continuation(oversized)); } void test_semantic_and_canonical_rejection() { const auto encoded = io::encode_native_thing_continuation(populated_state()); CHECK(encoded.has_value()); if (!encoded) return; auto malformed = *encoded; malformed[16] = 0; CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 20, 0xfffeu); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 20, 0xffffu); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 24, sim::kMaximumCargoShipDwellTicks + 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u32(malformed, 36, 4); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 48, 10); set_u16(malformed, 50, 11); CHECK(!io::decode_native_thing_continuation(malformed)); // The second variable-size record begins after the first record's three waypoints. malformed = *encoded; constexpr std::size_t secondRecord = io::kNativeThingContinuationHeaderBytes + io::kNativeCargoShipContinuationBytes + 3 * io::kNativeCargoShipWaypointBytes; malformed[secondRecord] = 4; CHECK(!io::decode_native_thing_continuation(malformed)); sim::ThingContinuationState invalid = populated_state(); invalid.cargoShips[0].cursor = 99; CHECK(!io::encode_native_thing_continuation(invalid)); invalid = populated_state(); invalid.cargoShips[0].waypoints.resize( sim::kMaximumCargoShipWaypoints + 1, {1, 1}); CHECK(!io::encode_native_thing_continuation(invalid)); } void test_city_checked_overloads() { City city = matching_city(); sim::ThingContinuationState state = populated_state(); state.cargoShips.resize(1); const auto encoded = io::encode_native_thing_continuation(state, city); CHECK(encoded.has_value()); CHECK(encoded && io::decode_native_thing_continuation(*encoded, city) == state); city.xtxt.at(10, 11) = 0; CHECK(!io::encode_native_thing_continuation(state, city)); CHECK(encoded && !io::decode_native_thing_continuation(*encoded, city)); } } // namespace int main() { test_round_trip_and_shape(); test_structural_reserved_and_overflow_rejection(); test_semantic_and_canonical_rejection(); test_city_checked_overloads(); if (failures == 0) { std::printf("native thing continuation: all checks passed\n"); return 0; } std::printf("native thing continuation: %d failure(s)\n", failures); return 1; }