From 67bf2280a812fa10af678120a18175597f0922f2 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sun, 8 Nov 2026 21:15:16 -0700 Subject: [PATCH] test(core): bridge the small cash-flow and bounds helpers The traceability gate asks every public kernel function for a bridge property. Three amount-generic helpers had none: take, entry_movements and flows_of (the statement of cash flows' split) and running_bounds and bounds_kept (a document's position against its total). The properties compare the i64 instantiation Kani proves with the Decimal one the engine runs, on random inputs, so law 10 and law 34 hold at both types. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/kernel_bridge/control.rs | 42 +++++++- .../tests/kernel_bridge/entries.rs | 98 +++++++++++++++++++ .../ledger-core/tests/kernel_bridge/main.rs | 1 + 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 crates/ledger-core/tests/kernel_bridge/entries.rs diff --git a/crates/ledger-core/tests/kernel_bridge/control.rs b/crates/ledger-core/tests/kernel_bridge/control.rs index 537679dc..25566dd9 100644 --- a/crates/ledger-core/tests/kernel_bridge/control.rs +++ b/crates/ledger-core/tests/kernel_bridge/control.rs @@ -1,4 +1,42 @@ //! U8 bridge properties: control and aging (kernel sweep). //! -//! Empty in U0: the unit adds its `Decimal`-vs-integer properties here, -//! beside `main.rs` and the other units. +//! `kernel::running_bounds` and `kernel::bounds_kept` at `i64` cents (what +//! Kani proves, law 34) against the same on `Decimal` (what the engine +//! runs), on random documents. + +use super::{arb_cents, dec}; +use ledger_core::kernel; +use proptest::prelude::*; + +proptest! { + #![proptest_config(ProptestConfig::with_cases(1000))] + + /// The same running balances are within bounds, or leave them at the + /// same step, at both types. + #[test] + fn running_bounds_agree( + total in arb_cents(), + deltas in prop::collection::vec(arb_cents(), 0..7), + ) { + let wide: Vec<_> = deltas.iter().map(|&c| dec(c)).collect(); + prop_assert_eq!( + kernel::running_bounds(total, &deltas), + kernel::running_bounds(dec(total), &wide) + ); + } + + /// The same step keeps (or breaks) a document's bounds at both types. + #[test] + fn bounds_kept_agree( + total in arb_cents(), + before in prop::collection::vec(arb_cents(), 0..7), + after in prop::collection::vec(arb_cents(), 0..7), + ) { + let wide_before: Vec<_> = before.iter().map(|&c| dec(c)).collect(); + let wide_after: Vec<_> = after.iter().map(|&c| dec(c)).collect(); + prop_assert_eq!( + kernel::bounds_kept(total, &before, &after), + kernel::bounds_kept(dec(total), &wide_before, &wide_after) + ); + } +} diff --git a/crates/ledger-core/tests/kernel_bridge/entries.rs b/crates/ledger-core/tests/kernel_bridge/entries.rs new file mode 100644 index 00000000..08d6c671 --- /dev/null +++ b/crates/ledger-core/tests/kernel_bridge/entries.rs @@ -0,0 +1,98 @@ +//! Entries bridge properties: the small amount-generic pieces the +//! statement of cash flows builds on. +//! +//! `kernel::take`, `entry_movements` and `flows_of` at `i64` cents (what +//! Kani proves, law 10) against the same on `Decimal` (what the engine +//! runs), on random lines. + +use super::{arb_cents, dec}; +use ledger_core::Side; +use ledger_core::kernel::{self, Flow}; +use proptest::prelude::*; + +fn arb_flow() -> impl Strategy { + prop_oneof![ + Just(Flow::Cash), + Just(Flow::Operating), + Just(Flow::Investing), + Just(Flow::Financing), + ] +} + +fn arb_side() -> impl Strategy { + prop_oneof![Just(Side::Debit), Just(Side::Credit)] +} + +fn arb_flow_lines() -> impl Strategy> { + prop::collection::vec((arb_flow(), arb_side(), arb_cents()), 0..6) +} + +fn to_dec(lines: &[(Flow, Side, i64)]) -> Vec<(Flow, Side, rust_decimal::Decimal)> { + lines.iter().map(|&(f, s, a)| (f, s, dec(a))).collect() +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(1000))] + + /// `kernel::take` takes the same part of a movement at both types. + #[test] + fn take_agrees(remaining in arb_cents(), movement in arb_cents()) { + prop_assert_eq!( + kernel::take(remaining, movement).map(dec), + kernel::take(dec(remaining), dec(movement)) + ); + } + + /// `kernel::entry_movements` nets the cash and each section the same. + #[test] + fn entry_movements_agree(lines in arb_flow_lines()) { + let i = kernel::entry_movements(&lines) + .map(|(cash, moved)| (dec(cash), moved.map(dec))); + let d = kernel::entry_movements(&to_dec(&lines)); + prop_assert_eq!(i, d); + } + + /// `kernel::flows_of` splits the cash the same, and refuses the same + /// lines that do not net to zero. + #[test] + fn flows_of_agree( + cash in arb_cents(), + moved in (arb_cents(), arb_cents(), arb_cents()) + .prop_map(|(a, b, c)| [a, b, c]), + ) { + let i = kernel::flows_of(cash, moved); + let d = kernel::flows_of(dec(cash), moved.map(dec)); + prop_assert_eq!( + i.map(|f| (dec(f.cash), f.flows.map(dec), f.noncash.map(dec))), + d.map(|f| (f.cash, f.flows, f.noncash)) + ); + } + + /// `kernel::catch_ups` gives every suffix's catch-up at both types. + #[test] + fn catch_ups_agree( + pairs in prop::collection::vec((arb_cents(), arb_cents()), 0..6), + ) { + let scheduled: Vec = pairs.iter().map(|&(s, _)| s).collect(); + let posted: Vec = pairs.iter().map(|&(_, p)| p).collect(); + let mut i = vec![0i64; scheduled.len()]; + let mut d = vec![dec(0); scheduled.len()]; + let ri = kernel::catch_ups(&scheduled, &posted, &mut i); + let rd = kernel::catch_ups( + &scheduled.iter().map(|&c| dec(c)).collect::>(), + &posted.iter().map(|&c| dec(c)).collect::>(), + &mut d, + ); + prop_assert_eq!(ri, rd); + prop_assert_eq!(i.into_iter().map(dec).collect::>(), d); + } + + /// `kernel::unrecovered_cost` is the same shortfall at both types. + #[test] + fn unrecovered_cost_agree(revenue in arb_cents(), expenses in arb_cents()) { + prop_assert_eq!( + kernel::unrecovered_cost(revenue, expenses).map(dec), + kernel::unrecovered_cost(dec(revenue), dec(expenses)) + ); + } +} diff --git a/crates/ledger-core/tests/kernel_bridge/main.rs b/crates/ledger-core/tests/kernel_bridge/main.rs index f8a4ea80..e91c8b54 100644 --- a/crates/ledger-core/tests/kernel_bridge/main.rs +++ b/crates/ledger-core/tests/kernel_bridge/main.rs @@ -9,6 +9,7 @@ // One file per kernel-sweep unit; the properties above stay here. mod allocate; mod control; +mod entries; mod fx; mod payroll; mod ratio;