58 lines
2.4 KiB
Rust
58 lines
2.4 KiB
Rust
//! Mandatory test 6: the forecast is a pure function of its inputs and seed.
|
|
|
|
use fiscus_insight::forecast::{forecast, ForecastInput};
|
|
use fiscus_ledger::Haler;
|
|
|
|
fn input() -> ForecastInput {
|
|
ForecastInput {
|
|
history: (0..90).map(|i| Haler(((i * 7919) % 120_000) as i64)).collect(),
|
|
spent: Haler(800_000),
|
|
days_left: 17,
|
|
limit: Haler(2_000_000),
|
|
known: Haler(19_900),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn same_seed_same_output() {
|
|
let a = forecast(&input(), 42, 5_000);
|
|
let b = forecast(&input(), 42, 5_000);
|
|
assert_eq!(a, b);
|
|
assert_eq!(format!("{a:?}"), format!("{b:?}"));
|
|
// Another seed is another sample (the chance moves a little, the shape not much).
|
|
let c = forecast(&input(), 43, 5_000);
|
|
assert_ne!((a.p10, a.p50, a.p90), (c.p10, c.p50, c.p90));
|
|
assert!(a.chance_bp.abs_diff(c.chance_bp) < 500);
|
|
}
|
|
|
|
#[test]
|
|
fn the_obvious_cases() {
|
|
// Every day costs exactly 100 CZK: ten days left from zero lands on 1 000 CZK in every run.
|
|
let flat = ForecastInput { history: vec![Haler(10_000); 30], spent: Haler(0), days_left: 10, limit: Haler(100_000), known: Haler(0) };
|
|
let f = forecast(&flat, 1, 1_000);
|
|
assert_eq!((f.chance_bp, f.p10, f.p50, f.p90), (10_000, Haler(100_000), Haler(100_000), Haler(100_000)));
|
|
let f = forecast(&ForecastInput { limit: Haler(99_999), ..flat.clone() }, 1, 1_000);
|
|
assert_eq!(f.chance_bp, 0);
|
|
// Already over the limit.
|
|
let over = ForecastInput { spent: Haler(200_000), ..flat.clone() };
|
|
assert_eq!(forecast(&over, 1, 1_000).chance_bp, 0);
|
|
// No history yet: what is spent plus what is known to come.
|
|
let fresh = ForecastInput { history: vec![], spent: Haler(50_000), days_left: 10, limit: Haler(60_000), known: Haler(5_000) };
|
|
let f = forecast(&fresh, 1, 100);
|
|
assert_eq!((f.chance_bp, f.p50, f.history_days), (10_000, Haler(55_000), 0));
|
|
// The last day of the month: nothing left to draw.
|
|
let last = ForecastInput { days_left: 0, ..flat };
|
|
assert_eq!(forecast(&last, 9, 10).p90, Haler(0));
|
|
}
|
|
|
|
#[test]
|
|
fn the_chance_tracks_the_limit() {
|
|
let mut prev = 0;
|
|
for limit in (1_000_000..4_000_000).step_by(250_000) {
|
|
let f = forecast(&ForecastInput { limit: Haler(limit), ..input() }, 7, 4_000);
|
|
assert!(f.chance_bp >= prev, "a higher limit is never less likely to hold");
|
|
prev = f.chance_bp;
|
|
}
|
|
assert_eq!(prev, 10_000);
|
|
}
|