// Weather (sim/weather.hpp) — the 12-state seasonal Markov chain ported from the tail of // the tail of the original's monthly demand update + its display formatter. Verifies the // the original's tables, the transition/easing, the new-city seed, seasonal bias, and the status string. #include "sc2k/sim/weather.hpp" #include #include using namespace sc2k; using namespace sc2k::sim; #include "test_harness.hpp" int main() { // New-city seed matches the original: state 4 / heat 150 (50°F) / wind 10 / rain 15. Weather w0; CHECK(w0.state == 4 && w0.heat == 150 && w0.wind == 10 && w0.rain == 15); CHECK(w0.temperature_f() == 50); CHECK(weather_status(w0) == std::string("Rain 50°F 10mph 15mm")); // Every transition-table entry is a valid state 0..11 (no out-of-range successor); 100k steps. { Weather w; MsRand rng(1); int bad = 0; for (int i = 0; i < 100000; ++i) { weather_step(w, i & 3, rng); if (w.state > 11) ++bad; } CHECK(bad == 0); } // Easing: after a step the reading sits exactly halfway between its old value and the new state's target // (integer /2). Drive from a known state and check the arithmetic. { Weather w; w.heat = 100; w.wind = 0; w.rain = 0; w.state = 0; MsRand rng(7); const int oldH = w.heat, oldW = w.wind, oldR = w.rain; weather_step(w, 0, rng); CHECK(w.heat == (oldH + kWeatherHeat[w.state]) / 2); CHECK(w.wind == (oldW + kWeatherWind[w.state]) / 2); CHECK(w.rain == (oldR + kWeatherRain[w.state]) / 2); } // Seasonal bias: averaged over many runs, WINTER (season 0) is colder than SUMMER (season 2). { long winter = 0, summer = 0; const int N = 4000; MsRand rng(0xBEEF); for (int k = 0; k < N; ++k) { Weather ww; for (int m = 0; m < 6; ++m) weather_step(ww, 0, rng); winter += ww.temperature_f(); Weather ws; for (int m = 0; m < 6; ++m) weather_step(ws, 2, rng); summer += ws.temperature_f(); } std::printf(" winter avg=%.1f°F summer avg=%.1f°F\n", double(winter) / N, double(summer) / N); CHECK(winter < summer); } // Nearest-state seeding: exact target readings recover their own state; blank readings pick a real state. CHECK(weather_nearest_state(kWeatherHeat[2], kWeatherWind[2], kWeatherRain[2]) == 2); CHECK(weather_nearest_state(kWeatherHeat[9], kWeatherWind[9], kWeatherRain[9]) == 9); // Status word thresholds. { Weather w; w.heat = 210; w.wind = 0; w.rain = 0; CHECK(std::string(weather_word(w)) == "Hot"); } { Weather w; w.heat = 60; w.wind = 100; w.rain = 60; CHECK(std::string(weather_word(w)) == "Blizzard"); } { Weather w; w.heat = 175; w.wind = 100; w.rain = 60; CHECK(std::string(weather_word(w)) == "Storm"); } if (failures == 0) std::printf("test_weather: all checks passed\n"); else std::printf("test_weather: %d FAILURES\n", failures); return failures ? 1 : 0; }