// Validates the demographic-evolution model (sim/demographics.hpp). The month-to-month evolution has no // ground truth in the bundled saves, so it is checked by INVARIANTS: the headline LE/EQ stay in their sane // band (and near the saved value on a near-equilibrium city), the age histograms stay non-negative, and // the total tracks the target population — over a 24-month run, with no explosion / NaN / drift to zero. #include "sc2k/sim/demographics.hpp" #include "sc2k/io/sc2_file.hpp" #include "sc2k/render/asset_loader.hpp" #include #include using namespace sc2k; #include "test_harness.hpp" #include "test_paths.hpp" static long hist_total(const City& c) { long s = 0; for (int b = 0; b < 20; ++b) s += c.misc_be32(sc2k::misc::kAgeHistRaw + 12 * b); return s; } int main() { const std::string root = cities_dir(); if (!exists(root + "TOKYO.SC2")) { std::printf("demographics: SKIP (no CITIES/)\n"); return 0; } for (const char* nm : {"TOKYO", "LONDON", "BAYVIEW"}) { City c; if (!io::load_sc2(root + nm + std::string(".SC2"), c).ok) continue; MsRand rng(0x2050); const int le0 = c.misc_be32(sc2k::misc::kLifeExpectancy), eq0 = c.misc_be32(sc2k::misc::kEducationQuotient); long prev = 10L * c.zone_pop(0); // 24 months of evolution on the (static-zoning) city — a near-equilibrium run. for (int m = 0; m < 24; ++m) { demographics_step(c, prev, rng); prev = 10L * c.zone_pop(0); const int le = c.misc_be32(sc2k::misc::kLifeExpectancy), eq = c.misc_be32(sc2k::misc::kEducationQuotient); CHECK(le >= 0 && le <= 120); // life expectancy stays in a sane band CHECK(eq >= 0 && eq <= 250); // education quotient likewise for (int b = 0; b < 20; ++b) CHECK(c.misc_be32(sc2k::misc::kAgeHistRaw + 12 * b) >= 0); // no negative brackets } const int le1 = c.misc_be32(sc2k::misc::kLifeExpectancy), eq1 = c.misc_be32(sc2k::misc::kEducationQuotient); // On a fixed-zoning city the population target is steady, so LE/EQ should drift only mildly. CHECK(std::abs(le1 - le0) <= 20); CHECK(std::abs(eq1 - eq0) <= 40); // The histogram total should remain close to the target population (conservation), not collapse. const long tot = hist_total(c), tgt = 10L * c.zone_pop(0); CHECK(tot > tgt / 2 && tot < tgt * 2 + 1000); std::printf(" %-9s LE %d->%d EQ %d->%d histTotal=%ld target=%ld\n", nm, le0, le1, eq0, eq1, tot, tgt); } if (failures == 0) std::printf("demographics: all checks passed\n"); else std::printf("demographics: %d FAILED\n", failures); return failures ? 1 : 0; }