#include "sc2k/io/native_water_pollution.hpp" #include #include #include #include #include #include #include #include "test_harness.hpp" using namespace sc2k; static 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); } static 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)); } static void set_u64( std::vector& bytes, std::size_t offset, std::uint64_t value) { for (int byte = 0; byte < 8; ++byte) bytes[offset + static_cast(byte)] = static_cast(value >> (8 * byte)); } static sim::WaterPollutionState state_for( std::uint32_t rows, std::uint32_t columns) { const std::optional state = sim::make_water_pollution_state(rows, columns); CHECK(state.has_value()); return state.value_or(sim::WaterPollutionState{}); } static sim::WaterPollutionState populated_state() { sim::WaterPollutionState state = state_for(5, 3); state.lastProcessedPeriod = 7; state.deliveredExposure = {7, 20000, 9, 3}; state.lastConsumedExposurePeriod = 7; state.cells = {0, 1999, 2000, 10999, 11000, 32767}; CHECK(sim::valid_water_pollution_state_for_extent(state, 5, 3)); return state; } static void test_round_trip_and_extent_binding() { const sim::WaterPollutionState state = populated_state(); const auto first = io::encode_native_water_pollution(state); const auto second = io::encode_native_water_pollution(state); CHECK(first.has_value()); CHECK(first == second); if (!first) return; CHECK(first->size() == io::kNativeWaterPollutionHeaderBytes + 12); CHECK((*first)[0] == 'W' && (*first)[1] == 'P' && (*first)[2] == 'L' && (*first)[3] == '1'); CHECK(io::decode_native_water_pollution(*first, 5, 3) == state); CHECK(!io::decode_native_water_pollution(*first, 6, 3)); CHECK(!io::decode_native_water_pollution(*first, 5, 4)); CHECK(!io::decode_native_water_pollution(*first, 0, 3)); const sim::WaterPollutionState fresh = state_for(4, 6); const auto encodedFresh = io::encode_native_water_pollution(fresh); CHECK(encodedFresh.has_value()); CHECK(encodedFresh && io::decode_native_water_pollution(*encodedFresh, 4, 6) == fresh); const sim::WaterPollutionState maximum = state_for( io::kMaximumNativeWaterPollutionDimension, 1); const auto encodedMaximum = io::encode_native_water_pollution(maximum); CHECK(encodedMaximum.has_value()); CHECK(encodedMaximum && io::decode_native_water_pollution(*encodedMaximum, io::kMaximumNativeWaterPollutionDimension, 1) == maximum); } static void test_structural_rejection() { const auto encoded = io::encode_native_water_pollution(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_water_pollution(prefix, 5, 3)); } std::vector malformed = *encoded; malformed.push_back(0); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; malformed[3] = '2'; CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u16(malformed, 4, 2); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u16(malformed, 6, 2); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u32(malformed, 8, 6); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u32(malformed, 12, 4); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u32(malformed, 16, 2); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u32(malformed, 20, 3); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u64(malformed, 24, std::numeric_limits::max()); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u64(malformed, 80, 1); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); } static void test_semantic_rejection() { const auto encoded = io::encode_native_water_pollution(populated_state()); CHECK(encoded.has_value()); if (!encoded) return; std::vector malformed = *encoded; set_u64(malformed, 32, static_cast(-2)); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u64(malformed, 40, 8); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u64(malformed, 48, 32768); set_u64(malformed, 56, 1); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u64(malformed, 56, std::numeric_limits::max()); set_u64(malformed, 64, 1); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u64(malformed, 72, 8); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); malformed = *encoded; set_u16(malformed, io::kNativeWaterPollutionHeaderBytes, 32768); CHECK(!io::decode_native_water_pollution(malformed, 5, 3)); sim::WaterPollutionState invalid = populated_state(); invalid.cells[0] = 32768; CHECK(!io::encode_native_water_pollution(invalid)); invalid = populated_state(); invalid.cells.pop_back(); CHECK(!io::encode_native_water_pollution(invalid)); invalid = populated_state(); invalid.policyVersion = 2; CHECK(!io::encode_native_water_pollution(invalid)); CHECK(!io::encode_native_water_pollution(sim::WaterPollutionState{})); const sim::WaterPollutionState tooLarge = state_for( io::kMaximumNativeWaterPollutionDimension + 1, 1); CHECK(sim::valid_water_pollution_state(tooLarge)); CHECK(!io::encode_native_water_pollution(tooLarge)); } int main() { test_round_trip_and_extent_binding(); test_structural_rejection(); test_semantic_rejection(); if (failures == 0) { std::printf("native water pollution: all checks passed\n"); return 0; } std::printf("native water pollution: %d failure(s)\n", failures); return 1; }