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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317#include "sc2k/io/native_annual_demographics.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 constexpr std::size_t record_offset(std::size_t age) { return io::kNativeAnnualDemographicsHeaderBytes + age * io::kNativeAnnualDemographicsRecordBytes;}
static constexpr std::size_t v1_record_offset(std::size_t age) { return io::kNativeAnnualDemographicsHeaderBytes + age * io::kNativeAnnualDemographicsV1RecordBytes;}
static constexpr std::size_t v2_record_offset(std::size_t age) { return io::kNativeAnnualDemographicsHeaderBytes + age * io::kNativeAnnualDemographicsV2RecordBytes;}
static sim::AnnualDemographicsState populated_state() { sim::AnnualDemographicsState state; state.provenance = sim::DemographicProvenance::ClassicMiscMigration; state.lastAnnualTransitionYear = 2050; state.lastMonthlyTransitionPeriod = 600; state.healthQuotientInitialized = true; state.healthQuotientUnits[0] = 200; state.healthQuotientUnits[20] = 44'321; state.healthQuotientUnits[65] = 1'234'567; // HQ belongs to the age slot and may legitimately outlive a populated cohort. state.healthQuotientUnits[70] = 88'000; state.healthQuotientUnits[89] = std::numeric_limits<std::uint32_t>::max(); state.educationQuotientInitialized = true; state.educationQuotientUnits[0] = 150'000'000; state.educationQuotientUnits[20] = 100'000'000; state.educationQuotientUnits[65] = 150'000'000; state.educationQuotientUnits[89] = 255'000'000; state.ages[0] = {3, 450, 240}; state.ages[20] = {7, 700, 560}; state.ages[65] = {11, 1650, 990}; state.ages[89] = {2, 510, 100}; CHECK(sim::validate_annual_demographics(state) == sim::AnnualDemographicsValidationError::None); return state;}
static sim::AnnualDemographicsState predecessor_state() { sim::AnnualDemographicsState state = populated_state(); state.educationQuotientInitialized = false; state.educationQuotientUnits.fill(0); return state;}
static std::vector<std::uint8_t> encode_v1_fixture( const sim::AnnualDemographicsState& state) { io::WireWriter writer; writer.reserve(io::kNativeAnnualDemographicsV1PayloadBytes); writer.write_bytes(std::span<const std::uint8_t>( reinterpret_cast<const std::uint8_t*>("DEM1"), 4)); writer.write_u16(1); writer.write_u8(static_cast<std::uint8_t>(state.provenance)); writer.write_u8(0); writer.write_i64(state.lastAnnualTransitionYear); writer.write_u16(static_cast<std::uint16_t>(state.ages.size())); writer.write_u16(io::kNativeAnnualDemographicsV1RecordBytes); writer.write_i32(state.lastMonthlyTransitionPeriod); for (std::size_t age = 0; age < state.ages.size(); ++age) { const sim::AnnualDemographicAge& value = state.ages[age]; writer.write_u8(static_cast<std::uint8_t>(age)); writer.write_u8(0); writer.write_u16(0); writer.write_u64(value.population); writer.write_u64(value.education); writer.write_u64(value.lifeHealth); } return std::move(writer.bytes());}
static void test_round_trip_and_wire_shape() { const sim::AnnualDemographicsState state = populated_state(); const auto first = io::encode_native_annual_demographics(state); const auto second = io::encode_native_annual_demographics(state); CHECK(first.has_value()); CHECK(first == second); if (!first) return; CHECK(first->size() == io::kNativeAnnualDemographicsPayloadBytes); CHECK((*first)[0] == 'D' && (*first)[1] == 'E' && (*first)[2] == 'M' && (*first)[3] == '3'); CHECK((*first)[4] == sim::kAnnualDemographicsPolicyVersion); CHECK((*first)[6] == static_cast<std::uint8_t>(sim::DemographicProvenance::ClassicMiscMigration)); CHECK((*first)[7] == 3); CHECK(io::decode_native_annual_demographics(*first) == state);
const sim::AnnualDemographicsState empty; const auto encodedEmpty = io::encode_native_annual_demographics(empty); CHECK(encodedEmpty.has_value()); CHECK(encodedEmpty && (*encodedEmpty)[7] == 0); CHECK(encodedEmpty && io::decode_native_annual_demographics(*encodedEmpty) == empty);}
static void test_v1_migration_is_explicit_and_lossless() { const sim::AnnualDemographicsState source = predecessor_state(); const std::vector<std::uint8_t> payload = encode_v1_fixture(source); CHECK(payload.size() == io::kNativeAnnualDemographicsV1PayloadBytes); CHECK(!io::decode_native_annual_demographics(payload)); CHECK(!io::decode_native_annual_demographics(payload, 2));
const auto migrated = io::decode_native_annual_demographics( payload, io::kNativeAnnualDemographicsV1SchemaVersion); CHECK(migrated.has_value()); if (!migrated) return; sim::AnnualDemographicsState expected = source; expected.healthQuotientInitialized = false; expected.healthQuotientUnits.fill(0); CHECK(*migrated == expected); CHECK(migrated->policyVersion == sim::kAnnualDemographicsPolicyVersion);
std::vector<std::uint8_t> malformed = payload; malformed[7] = 1; CHECK(!io::decode_native_annual_demographics( malformed, io::kNativeAnnualDemographicsV1SchemaVersion)); malformed = payload; malformed[v1_record_offset(0)] = 1; CHECK(!io::decode_native_annual_demographics( malformed, io::kNativeAnnualDemographicsV1SchemaVersion));
const auto current = io::encode_native_annual_demographics(source); CHECK(current.has_value()); CHECK(current && !io::decode_native_annual_demographics(*current, io::kNativeAnnualDemographicsV1SchemaVersion));}
static void test_v2_migration_retains_totals_and_health_authority() { const sim::AnnualDemographicsState source = predecessor_state(); const auto payload = io::encode_native_annual_demographics_v2(source); CHECK(payload.has_value()); if (!payload) return; CHECK(payload->size() == io::kNativeAnnualDemographicsV2PayloadBytes); CHECK((*payload)[0] == 'D' && (*payload)[1] == 'E' && (*payload)[2] == 'M' && (*payload)[3] == '2'); CHECK((*payload)[7] == 1); CHECK(!io::decode_native_annual_demographics(*payload));
const auto migrated = io::decode_native_annual_demographics( *payload, io::kNativeAnnualDemographicsV2SchemaVersion); CHECK(migrated == source); CHECK(migrated && !migrated->educationQuotientInitialized); CHECK(migrated && migrated->educationQuotientUnits[20] == 0);
std::vector<std::uint8_t> malformed = *payload; malformed[7] = 2; CHECK(!io::decode_native_annual_demographics( malformed, io::kNativeAnnualDemographicsV2SchemaVersion)); malformed = *payload; malformed[v2_record_offset(20)] = 19; CHECK(!io::decode_native_annual_demographics( malformed, io::kNativeAnnualDemographicsV2SchemaVersion));
CHECK(!io::encode_native_annual_demographics_v2(populated_state()));}
static void test_structural_rejection() { const auto encoded = io::encode_native_annual_demographics(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_annual_demographics(prefix)); } std::vector<std::uint8_t> malformed = *encoded; malformed.push_back(0); CHECK(!io::decode_native_annual_demographics(malformed));
malformed = *encoded; malformed[3] = '1'; CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; set_u16(malformed, 4, 2); CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; malformed[6] = 2; CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; malformed[7] = 4; CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; set_u64(malformed, 8, static_cast<std::uint64_t>(-2)); CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; set_u16(malformed, 16, 89); CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; set_u16(malformed, 18, io::kNativeAnnualDemographicsV1RecordBytes); CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; set_u32(malformed, 20, 0xfffffffeu); CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; malformed[record_offset(0)] = 1; CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; malformed[record_offset(0) + 1] = 1; CHECK(!io::decode_native_annual_demographics(malformed)); malformed = *encoded; set_u16(malformed, record_offset(0) + 2, 1); CHECK(!io::decode_native_annual_demographics(malformed));}
static void test_semantic_rejection() { const auto empty = io::encode_native_annual_demographics( sim::AnnualDemographicsState{}); CHECK(empty.has_value()); if (!empty) return;
std::vector<std::uint8_t> malformed = *empty; set_u64(malformed, record_offset(0) + 12, 1); CHECK(!io::decode_native_annual_demographics(malformed));
malformed = *empty; set_u64(malformed, record_offset(0) + 4, 1); set_u64(malformed, record_offset(0) + 12, 256); CHECK(!io::decode_native_annual_demographics(malformed));
malformed = *empty; set_u32(malformed, record_offset(0) + 28, 1); CHECK(!io::decode_native_annual_demographics(malformed));
malformed = *empty; set_u32(malformed, record_offset(0) + 32, 1); CHECK(!io::decode_native_annual_demographics(malformed));
sim::AnnualDemographicsState maximum; maximum.ages[0].population = sim::kMaximumAnnualDemographicPopulation; const auto encodedMaximum = io::encode_native_annual_demographics(maximum); CHECK(encodedMaximum.has_value()); if (encodedMaximum) { malformed = *encodedMaximum; set_u64(malformed, record_offset(1) + 4, 1); CHECK(!io::decode_native_annual_demographics(malformed)); }
sim::AnnualDemographicsState invalid = populated_state(); ++invalid.policyVersion; CHECK(!io::encode_native_annual_demographics(invalid)); invalid = populated_state(); invalid.provenance = static_cast<sim::DemographicProvenance>(2); CHECK(!io::encode_native_annual_demographics(invalid)); invalid = populated_state(); invalid.healthQuotientInitialized = false; CHECK(!io::encode_native_annual_demographics(invalid)); invalid = populated_state(); invalid.educationQuotientInitialized = false; CHECK(!io::encode_native_annual_demographics(invalid)); invalid = populated_state(); invalid.educationQuotientUnits[20] = 100'071'429; CHECK(!io::encode_native_annual_demographics(invalid)); invalid = populated_state(); invalid.ages[1].education = 1; CHECK(!io::encode_native_annual_demographics(invalid)); invalid = populated_state(); invalid.ages[0].education = std::numeric_limits<std::uint64_t>::max(); CHECK(!io::encode_native_annual_demographics(invalid));}
int main() { test_round_trip_and_wire_shape(); test_v1_migration_is_explicit_and_lossless(); test_v2_migration_retains_totals_and_health_authority(); test_structural_rejection(); test_semantic_rejection();
if (failures == 0) { std::printf("native annual demographics: all checks passed\n"); return 0; } std::printf("native annual demographics: %d failure(s)\n", failures); return 1;}