Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181#include "sc2k/io/native_water_pollution.hpp"
#include <cstddef>#include <cstdint>#include <cstdio>#include <limits>#include <optional>#include <span>#include <vector>
#include "test_harness.hpp"
using namespace sc2k;
static void set_u16( std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint16_t value) { bytes[offset] = static_cast<std::uint8_t>(value); bytes[offset + 1] = static_cast<std::uint8_t>(value >> 8);}
static void set_u32( std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint32_t value) { for (int byte = 0; byte < 4; ++byte) bytes[offset + static_cast<std::size_t>(byte)] = static_cast<std::uint8_t>(value >> (8 * byte));}
static void set_u64( std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint64_t value) { for (int byte = 0; byte < 8; ++byte) bytes[offset + static_cast<std::size_t>(byte)] = static_cast<std::uint8_t>(value >> (8 * byte));}
static sim::WaterPollutionState state_for( std::uint32_t rows, std::uint32_t columns) { const std::optional<sim::WaterPollutionState> 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<const std::uint8_t> prefix(encoded->data(), size); CHECK(!io::decode_native_water_pollution(prefix, 5, 3)); } std::vector<std::uint8_t> 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<std::uint64_t>::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<std::uint8_t> malformed = *encoded; set_u64(malformed, 32, static_cast<std::uint64_t>(-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<std::uint64_t>::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;}