test: display rounding and percent rules (K-U9)
Tests encode the display rules: half away from zero, no negative zero, no percent of a zero or negative base, plus a scoped source check that no round_dp or float conversion remains in the money display paths. view_model's pct_change(-100, -50) expectation moves from Some(50.0) to None under the new rule. docs/proofs/display.md is the unit note (no Kani, no Lean for display, per the audit). Co-Authored-By: Muse Wonder 1.0
This commit is contained in:
1 parent
a7d0c7f2d3
commit
2463c10af9
3 files changed
+251
-3
No files matched your search
@@ -0,0 +1,119 @@
|
|||||||
|
//! K-U9: one display rounding, and no float in a money path.
|
||||||
|
//!
|
||||||
|
//! The behavioural half uses the public formatting API. The source
|
||||||
|
//! half is a scoped grep: the display files that shorten or ratio
|
||||||
|
//! money must not name `round_dp` (they go through
|
||||||
|
//! `ledger_import::round_display` or `kernel::percent_of`), and a
|
||||||
|
//! float conversion of a decimal may appear only at the xlsx cell
|
||||||
|
//! boundary, where the cell itself is a binary double and the value
|
||||||
|
//! is written as a number only when it is exactly representable.
|
||||||
|
|
||||||
|
use rust_decimal::Decimal;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
fn dec(s: &str) -> Decimal {
|
||||||
|
Decimal::from_str(s).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crate_src(krate: &str, file: &str) -> String {
|
||||||
|
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
|
||||||
|
std::fs::read_to_string(root.join(krate).join("src").join(file))
|
||||||
|
.unwrap_or_else(|e| panic!("{krate}/src/{file}: {e}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn all_rs(dir: &Path, out: &mut Vec<PathBuf>) {
|
||||||
|
for e in std::fs::read_dir(dir).unwrap() {
|
||||||
|
let p = e.unwrap().path();
|
||||||
|
if p.is_dir() {
|
||||||
|
all_rs(&p, out);
|
||||||
|
} else if p.extension().is_some_and(|x| x == "rs") {
|
||||||
|
out.push(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Display rounding: half away from zero, never a negative zero.
|
||||||
|
#[test]
|
||||||
|
fn display_rounding_is_half_away_and_never_negative_zero() {
|
||||||
|
let r = ledger_import::round_display;
|
||||||
|
assert_eq!(r(dec("1.005"), 2), dec("1.01"));
|
||||||
|
assert_eq!(r(dec("-1.005"), 2), dec("-1.01"));
|
||||||
|
assert_eq!(r(dec("2.5"), 0), dec("3"));
|
||||||
|
assert_eq!(r(dec("-0.004"), 2).to_string(), "0.00");
|
||||||
|
assert_eq!(ledger_import::format_amount(dec("-0.004")), "0.00");
|
||||||
|
assert_eq!(ledger_import::format_amount(dec("-1.005")), "(1.01)");
|
||||||
|
// The viewer re-exports the same function.
|
||||||
|
assert_eq!(ledger_gui::fmt::round_display(dec("1.005"), 2), dec("1.01"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A percentage of a zero or negative base is shown as nothing.
|
||||||
|
#[test]
|
||||||
|
fn a_percent_of_a_zero_or_negative_base_is_none() {
|
||||||
|
assert_eq!(
|
||||||
|
ledger_gui::fmt::pct_change(dec("200"), dec("250")),
|
||||||
|
Some(dec("25.0"))
|
||||||
|
);
|
||||||
|
assert_eq!(ledger_gui::fmt::pct_change(dec("0"), dec("5")), None);
|
||||||
|
assert_eq!(ledger_gui::fmt::pct_change(dec("-100"), dec("-50")), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// No `round_dp` left in the display paths, and no float conversion
|
||||||
|
/// of a decimal outside the xlsx cell boundary.
|
||||||
|
#[test]
|
||||||
|
fn no_round_dp_or_float_conversion_in_money_display_paths() {
|
||||||
|
for (krate, file) in [
|
||||||
|
("ledger-gui", "fmt.rs"),
|
||||||
|
("ledger-gui", "books.rs"),
|
||||||
|
("ledger-gui", "demo.rs"),
|
||||||
|
("ledger-gui", "pages/profit.rs"),
|
||||||
|
("ledger-gui", "pages/reports.rs"),
|
||||||
|
("ledger-gui", "pages/overview.rs"),
|
||||||
|
("ledger-gui", "pages/cash.rs"),
|
||||||
|
("ledger-gui", "pages/capital.rs"),
|
||||||
|
("ledger-import", "report.rs"),
|
||||||
|
("ledger-import", "pdf.rs"),
|
||||||
|
("ledger-import", "xlsx.rs"),
|
||||||
|
("ledger-import", "format.rs"),
|
||||||
|
("ledger-package", "text.rs"),
|
||||||
|
] {
|
||||||
|
assert!(
|
||||||
|
!crate_src(krate, file).contains("round_dp"),
|
||||||
|
"{krate}/src/{file} must use round_display / kernel rounding"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// fmt's percent and compact paths (everything above `bytes`, which
|
||||||
|
// sizes a byte count, not money) name no float at all.
|
||||||
|
let fmt_src = crate_src("ledger-gui", "fmt.rs");
|
||||||
|
let (money_half, _) = fmt_src
|
||||||
|
.split_once("pub fn bytes")
|
||||||
|
.expect("fmt::bytes exists");
|
||||||
|
assert!(
|
||||||
|
!money_half.contains("f64"),
|
||||||
|
"fmt money paths must be Decimal"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!money_half.contains("f32"),
|
||||||
|
"fmt money paths must be Decimal"
|
||||||
|
);
|
||||||
|
|
||||||
|
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
|
||||||
|
for krate in [
|
||||||
|
"ledger-gui",
|
||||||
|
"ledger-import",
|
||||||
|
"ledger-package",
|
||||||
|
"ledger-mcp",
|
||||||
|
] {
|
||||||
|
let mut files = Vec::new();
|
||||||
|
all_rs(&root.join(krate).join("src"), &mut files);
|
||||||
|
for f in files {
|
||||||
|
let text = std::fs::read_to_string(&f).unwrap();
|
||||||
|
if text.contains("to_f64") {
|
||||||
|
assert!(
|
||||||
|
f.ends_with("xlsx.rs"),
|
||||||
|
"to_f64 only at the xlsx cell boundary: {f:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -324,9 +324,10 @@ fn origins_and_parties() {
|
|||||||
fn percent_change_and_tax_forms() {
|
fn percent_change_and_tax_forms() {
|
||||||
let d = |s: &str| s.parse::<Decimal>().unwrap();
|
let d = |s: &str| s.parse::<Decimal>().unwrap();
|
||||||
assert_eq!(fmt::pct_change(d("0"), d("5")), None);
|
assert_eq!(fmt::pct_change(d("0"), d("5")), None);
|
||||||
assert_eq!(fmt::pct_change(d("100"), d("150")), Some(50.0));
|
assert_eq!(fmt::pct_change(d("100"), d("150")), Some(d("50.0")));
|
||||||
assert_eq!(fmt::pct_change(d("200"), d("150")), Some(-25.0));
|
assert_eq!(fmt::pct_change(d("200"), d("150")), Some(d("-25.0")));
|
||||||
assert_eq!(fmt::pct_change(d("-100"), d("-50")), Some(50.0));
|
// K-U9: no percent of a negative base (it misleads); Decimal, never f64.
|
||||||
|
assert_eq!(fmt::pct_change(d("-100"), d("-50")), None);
|
||||||
use ledger_core::TaxForm::*;
|
use ledger_core::TaxForm::*;
|
||||||
for (f, number) in [
|
for (f, number) in [
|
||||||
(ScheduleC, "Schedule C"),
|
(ScheduleC, "Schedule C"),
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
# K-U9: display rounding, and percentages in Decimal
|
||||||
|
|
||||||
|
_Folded into `docs/verification.md` by the integrator; this note is the
|
||||||
|
unit's own record._
|
||||||
|
|
||||||
|
Kernel sweep unit U9 (`plan/kernel-sweep-audit.md` section 3, "display
|
||||||
|
rounding"). Display only: no kernel function, no Kani harness, no Lean
|
||||||
|
theorem (the audit and `docs/proofs/README.md` say so for U9). The
|
||||||
|
rounding it shares is the kernel's already-proved `round_places`
|
||||||
|
(K-U2, law 40); the percentages it shares are `kernel::percent_of`
|
||||||
|
(K-U6).
|
||||||
|
|
||||||
|
## The API
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// ledger-import::format (re-exported at the crate root)
|
||||||
|
pub fn round_display(amount: Decimal, places: u32) -> Decimal
|
||||||
|
```
|
||||||
|
|
||||||
|
Half away from zero to `places`, through
|
||||||
|
`ledger_core::kernel::round_places(..., Rounding::HalfAwayFromZero)`,
|
||||||
|
never a negative zero. It is the rounding half of
|
||||||
|
`ledger-import::report::format_amount`, lifted out so every renderer
|
||||||
|
shares it: `format_amount` and `amount_parts` call it at two places,
|
||||||
|
`ledger-import::pdf`'s percent column at one place,
|
||||||
|
`ledger-import::xlsx::exact_f64` at two places (before its exactness
|
||||||
|
check), `ledger-package::text`'s `money`/`share_text` at the amount's
|
||||||
|
own scale (the identity there: padding only, never rounding, as those
|
||||||
|
functions promise), and `ledger-gui::fmt` re-exports it for the pages.
|
||||||
|
|
||||||
|
## The percent rule (one rule for every percent)
|
||||||
|
|
||||||
|
A percent of a zero or negative whole is `None`, and the page shows
|
||||||
|
nothing (a KPI tile keeps no sub-line) or "—" (a table cell). That is
|
||||||
|
`kernel::percent_of`'s rule, and K-U9 adopts it everywhere:
|
||||||
|
|
||||||
|
- `ledger-gui::fmt::pct_change` returns `Option<Decimal>` (it was
|
||||||
|
`Option<f64>`, dividing money amounts in binary floating point):
|
||||||
|
`(now - prev) / prev * 100` to one place, half away from zero, via
|
||||||
|
`percent_of`. A negative `prev` gave a signed percent before; now
|
||||||
|
`None`.
|
||||||
|
- `ledger-gui::pages::profit::pct` (margins, shares of invoiced
|
||||||
|
income): `percent_of` at one place, half to even — the books' margin
|
||||||
|
mode, as `contribution_margin_pct` uses. A negative revenue base
|
||||||
|
gave a percent before; now `None`, shown as "—".
|
||||||
|
- `ledger-gui::pages::reports`' change column: was
|
||||||
|
`diff / base.abs() * 100`, a percent of a negative base with the sign
|
||||||
|
hidden; now `percent_of(diff, base, ...)`, `None` for base <= 0.
|
||||||
|
The "% of income" column likewise adds no column when total income
|
||||||
|
is zero or negative (it added none for zero before).
|
||||||
|
- `ledger-gui::pages::capital::percent`: a member's share is a stored
|
||||||
|
fraction, not a ratio of two amounts, so it stays exact decimal
|
||||||
|
multiplication by 100 with no rounding; only a negative zero is
|
||||||
|
dropped. No float was involved.
|
||||||
|
|
||||||
|
## Sites routed
|
||||||
|
|
||||||
|
- `ledger-import`: `report::format_amount`, `report::amount_parts`,
|
||||||
|
`pdf` percent column, `xlsx::exact_f64` — through `round_display`.
|
||||||
|
- `ledger-package::text::money`, `share_text` — through
|
||||||
|
`round_display` at their own scale (identity; see above).
|
||||||
|
- `ledger-gui::fmt::compact`: was `f64` (`try_into`, `{:.1}` prints).
|
||||||
|
Now decimal division by 1,000 / 1,000,000 and `round_display`. Two
|
||||||
|
displayed figures change, both f64 artefacts: 1,250,000 shows
|
||||||
|
"$1.3M" (was "$1.2M": 1.25 in binary is slightly under), 10,500 shows
|
||||||
|
"$11k" (was "$10k": float formatting rounds 10.5 to even).
|
||||||
|
- `ledger-gui::fmt::pct_change` and its caller
|
||||||
|
`pages/overview::change` ("▲ 12% vs …" text and the 0.5% colour
|
||||||
|
threshold): Decimal throughout.
|
||||||
|
- `pages/overview` runway months, `pages/cash` average net and runway,
|
||||||
|
`pages/profit` spending row, `pages/reports` change / share / TSV,
|
||||||
|
`books` monthly-burn average, `demo` platform fee: `round_dp(1|2)`
|
||||||
|
through `round_display` (checked division where the old code divided
|
||||||
|
raw under a guard).
|
||||||
|
- `books`' two share splits truncated to cents with `ToZero`: that is
|
||||||
|
a leftover policy, not display rounding, so it now goes through
|
||||||
|
`kernel::round_places(..., TowardZero)` as `truncate_cents`, with no
|
||||||
|
change of value.
|
||||||
|
|
||||||
|
## Floats kept, with why
|
||||||
|
|
||||||
|
- `ledger-gui::charts` (`f(Decimal) -> f64`, `ticks`, sparklines,
|
||||||
|
`pages/overview`/`cash` spark vectors, `widgets::Kpi::spark`): pixel
|
||||||
|
coordinates and axis scales computed for layout only. Every hover
|
||||||
|
and label that states a figure reads the original `Decimal`
|
||||||
|
(`fmt::dollars`, `fmt::compact`); no printed amount is computed in
|
||||||
|
a float.
|
||||||
|
- egui layout `f32`s (column widths, row heights), colour maths in
|
||||||
|
`schemes.rs`, image sizes in `receipts.rs`: geometry and colour,
|
||||||
|
never money.
|
||||||
|
- Time `f64`s (`app::since_update`, `widgets` timers,
|
||||||
|
`pages/reports::pdf_done`, `pages/transactions::since_update`):
|
||||||
|
egui's clock, never money.
|
||||||
|
- `fmt::bytes` (`n as f64`): a byte count for "12.3 KB", not money.
|
||||||
|
- `ledger-import::labels` geometry (`f64` millimetre/point layout),
|
||||||
|
`asset_register`'s life-in-months cell (`f64::from(months)`: a
|
||||||
|
count), `xlsx` entry-id cells (`as f64`: an id): layout and counts.
|
||||||
|
- `ledger-import::xlsx::exact_f64` (`to_f64`): the one sanctioned
|
||||||
|
conversion — an xlsx cell is a binary double, and a value is
|
||||||
|
written as a number only when it is whole cents of at most 15
|
||||||
|
significant digits, exactly representable; anything else is text.
|
||||||
|
- `ledger-mcp::types` `visit_f64`: serde's visitor signature for
|
||||||
|
refusing a float TIN, not a computation.
|
||||||
|
- `ledger-import::templates::equal_shares`: owner-percentage template
|
||||||
|
(audit class (c)), last-absorbs so the shares sum to 100; not a
|
||||||
|
display rounding of a money amount, left as is.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
- `crates/ledger-import/src/format.rs`: half away from zero at ties,
|
||||||
|
never a negative zero.
|
||||||
|
- `ledger-gui::fmt` tests: `pct_change` decimal-or-none (zero and
|
||||||
|
negative bases), `compact` decimal ties.
|
||||||
|
- `ledger-gui::pages::profit` tests: no percent of a negative base.
|
||||||
|
- `ledger-gui::pages::reports` tests: the change column has no
|
||||||
|
percent from a zero or negative base; no "% of income" column for
|
||||||
|
negative income.
|
||||||
|
- `crates/ledger-gui/tests/display_rounding.rs`: the rules through
|
||||||
|
the public API, plus a scoped source check (no `round_dp` in the
|
||||||
|
display files; no `f64` in `fmt`'s money half; `to_f64` only in
|
||||||
|
`xlsx.rs`).
|
||||||
|
|
||||||
|
No golden changes. Displayed values that changed are the two
|
||||||
|
`compact` ties above and the percents of negative bases (now "—" /
|
||||||
|
no sub-line instead of a signed figure). One existing test encoded
|
||||||
|
the old negative-base percent (`ledger-gui/tests/view_model.rs`:
|
||||||
|
`pct_change(-100, -50)` was `Some(50.0)`); it now expects `None`,
|
||||||
|
per the rule above.
|
||||||
Reference in new issue
Block a user