diff --git a/crates/ledger-core/src/kernel/schedule.rs b/crates/ledger-core/src/kernel/schedule.rs index 7cd5b28c..aab50142 100644 --- a/crates/ledger-core/src/kernel/schedule.rs +++ b/crates/ledger-core/src/kernel/schedule.rs @@ -18,7 +18,8 @@ use super::{Amount, Rounding, plug_last, round_step}; /// ```text /// GOAL: an equal-months schedule books exactly its total across its /// months, each month a whole number of the currency's smallest -/// unit, and no month is ever more than a rounding from the rest. +/// unit: every month but the last the same rounded share, the +/// last what the others leave (INVARIANT 4 bounds how far it is). /// SOURCE: BS-prepaid-1 (a schedule recognizes exactly its total over its /// months, each month rounded to the currency's minor unit), BS-prepaid-2 /// (equal months is one of the two methods an engine picks), diff --git a/crates/ledger-core/src/schedules.rs b/crates/ledger-core/src/schedules.rs index e1a118b3..f267e85a 100644 --- a/crates/ledger-core/src/schedules.rs +++ b/crates/ledger-core/src/schedules.rs @@ -348,10 +348,15 @@ fn plan_in_places( /// Values never change: every rescale below is /// to or from trailing zeros, which the debug assertions check. /// -/// A zero total is never registered (validate_schedule_shape), and a -/// one-month plan is its total; both are left as the kernel gives them. +/// A one-month plan is its total, left as the kernel gives it. A zero +/// total is never registered (validate_schedule_shape), but the plan of +/// one prints as it always did: every month a bare 0 (the kernel's last +/// month is `0 - 0`, which `Decimal` prints as `-0`). fn present_as_rounded(parts: &mut [Decimal], total: Decimal, places: u32) { if total.is_zero() { + for part in parts.iter_mut() { + *part = Decimal::ZERO; + } return; } let Some((last, rest)) = parts.split_last_mut() else { @@ -834,6 +839,24 @@ mod tests { NaiveDate::from_ymd_opt(y, m, day).unwrap() } + /// A zero total's plan prints every month as a bare 0, as the cent + /// rule always has: never `-0` (the K-U5 review's probe found the last + /// month of a zero plan printed `-0`; a replayed month is compared as + /// text). + #[test] + fn a_zero_plan_prints_zero_every_month() { + for months in [1, 2, 3, 12] { + for scale in [0, 2, 5] { + let plan = amortization_plan(Decimal::new(0, scale), d(2026, 1, 1), months) + .expect("a zero total plans"); + assert!( + plan.iter().all(|(_, a)| a.to_string() == "0"), + "{months} months at scale {scale}: {plan:?}" + ); + } + } + } + #[test] fn plans_sum_exactly_and_land_on_month_ends() { let plan = amortization_plan(Decimal::new(100_000, 2), d(2026, 11, 15), 3).unwrap(); diff --git a/docs/spec/claims/prepaid.md b/docs/spec/claims/prepaid.md index f4866d60..23cd9304 100644 --- a/docs/spec/claims/prepaid.md +++ b/docs/spec/claims/prepaid.md @@ -85,4 +85,5 @@ refuses as newer books. `an_unknown_rounding_is_newer_not_corrupt`, `schedule_minor_unit_is_its_own_feature`, `even_parts_sum`, `even_parts_all_but_last_equal`, `even_parts_last_absorbs`, -`even_parts_within_half_step`. +`even_parts_within_half_step`, +`even_parts_refuses_no_months_and_a_bad_step`.