use crate::conventions::{round_dollar, Usd};
use crate::tax::method::{qdcgt_line16, TAX_TABLE_CEILING};
use crate::tax::testonly::ty2024_table;
use std::collections::BTreeSet;
pub fn usd(x: f64) -> Usd {
Usd::try_from(x).expect("finite oracle figure")
}
pub fn round_leaf(oracle_line: f64) -> Usd {
round_dollar(usd(oracle_line))
}
pub fn sum_round(components: &[f64]) -> Usd {
components.iter().map(|&c| round_dollar(usd(c))).sum()
}
pub fn rate_on_printed(rate: Usd, printed_operand: Usd) -> Usd {
round_dollar(rate * printed_operand)
}
pub fn table_l16(
status: crate::tax::FilingStatus,
ti: Usd,
qd_l3a: Usd,
net_ltcg_qd_excl: Usd,
) -> Usd {
let table = ty2024_table();
let schedule = table
.ordinary
.get(&status)
.expect("TY2024 ordinary schedule for this filing status");
let bp = table
.ltcg
.get(&status)
.expect("TY2024 §1(h) breakpoints for this filing status");
qdcgt_line16(schedule, bp, ti, qd_l3a, net_ltcg_qd_excl)
}
pub fn consulted_table(
status: crate::tax::FilingStatus,
ti: Usd,
qd_l3a: Usd,
net_ltcg_qd_excl: Usd,
) -> bool {
let _ = status;
let z = Usd::ZERO;
let ti = ti.max(z);
let pref_full = qd_l3a.max(z) + net_ltcg_qd_excl.max(z); let l5 = (ti - pref_full).max(z); l5 < TAX_TABLE_CEILING || ti < TAX_TABLE_CEILING
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OracleId {
Ots,
Taxcalc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct L16Operands {
pub status: crate::tax::FilingStatus,
pub ti: Usd,
pub qd_l3a: Usd,
pub net_ltcg_qd_excl: Usd,
}
pub fn taxcalc_methodology_class(reproduced_ops: &L16Operands) -> bool {
consulted_table(
reproduced_ops.status,
reproduced_ops.ti,
reproduced_ops.qd_l3a,
reproduced_ops.net_ltcg_qd_excl,
)
}
pub fn provenance_class_fires(
oracle_ops: Option<&L16Operands>,
reproduced_ops: &L16Operands,
oracle_l16: f64,
) -> bool {
let Some(o) = oracle_ops else {
return false; };
let oracle_printed = round_leaf(oracle_l16);
table_l16(o.status, o.ti, o.qd_l3a, o.net_ltcg_qd_excl) == oracle_printed
&& table_l16(
reproduced_ops.status,
reproduced_ops.ti,
reproduced_ops.qd_l3a,
reproduced_ops.net_ltcg_qd_excl,
) != oracle_printed
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KnownDefect {
pub fu_id: &'static str,
pub btctax_value: Usd,
}
#[allow(clippy::too_many_arguments)]
pub fn stacking_ok(
figure: Usd,
ots_l16: f64,
taxcalc_l16: Option<f64>,
ots_ops: Option<&L16Operands>,
taxcalc_ops: Option<&L16Operands>,
reproduced_ops: &L16Operands,
known_defect: Option<&KnownDefect>,
) -> bool {
if let Some(kd) = known_defect {
return figure == kd.btctax_value;
}
let ots_ok =
figure == round_leaf(ots_l16) || provenance_class_fires(ots_ops, reproduced_ops, ots_l16);
let taxcalc_ok = match taxcalc_l16 {
None => true,
Some(v) => {
figure == round_leaf(v)
|| taxcalc_methodology_class(reproduced_ops)
|| provenance_class_fires(taxcalc_ops, reproduced_ops, v)
}
};
ots_ok && taxcalc_ok
}
#[derive(Debug, Clone, Default)]
pub struct LivenessLedger {
fired: BTreeSet<&'static str>,
pinned: BTreeSet<&'static str>,
}
impl LivenessLedger {
pub fn record_fire(&mut self, class: &'static str) {
self.fired.insert(class);
}
pub fn declare_pinned(&mut self, class: &'static str) {
self.pinned.insert(class);
}
pub fn dead(&self, declared: &[&'static str]) -> Vec<&'static str> {
declared
.iter()
.copied()
.filter(|c| !self.fired.contains(c) && !self.pinned.contains(c))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tax::FilingStatus; use rust_decimal_macros::dec;
#[test]
fn sum_round_cross_foots_the_legs_not_the_exact_total() {
assert_eq!(sum_round(&[274.50, 499.50]), dec!(775)); assert_ne!(sum_round(&[274.50, 499.50]), round_leaf(274.50 + 499.50)); }
#[test]
fn table_l16_reproduces_ots_above_ceiling() {
let got = table_l16(FilingStatus::Mfj, usd(253_942.94), usd(0.0), usd(0.0));
assert_eq!(got, dec!(47031)); }
#[test]
fn consulted_table_tracks_the_worksheet_operands() {
assert!(consulted_table(
FilingStatus::Single,
usd(112_400.0),
usd(8_000.0),
usd(25_000.0)
));
assert!(!consulted_table(
FilingStatus::Mfj,
usd(253_943.0),
usd(0.0),
usd(0.0)
));
}
#[test]
fn methodology_class_fires_on_qdcgt_both_slices() {
let ops = L16Operands {
status: FilingStatus::Single,
ti: usd(112_400.0),
qd_l3a: usd(8_000.0),
net_ltcg_qd_excl: usd(25_000.0),
};
assert!(taxcalc_methodology_class(&ops));
}
#[test]
fn taxcalc_provenance_cannot_fire_below_ceiling() {
let ops = L16Operands {
status: FilingStatus::Single,
ti: usd(70_008.908),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
assert!(!provenance_class_fires(Some(&ops), &ops, 10_454.96));
}
#[test]
fn provenance_class_keeps_teeth_against_a_semantics_mismatch() {
let ops = L16Operands {
status: FilingStatus::Mfj,
ti: usd(253_942.94),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
assert!(!provenance_class_fires(Some(&ops), &ops, 99_999.0)); }
#[test]
fn provenance_conjunct2_blocks_over_absorption_on_identical_leaves() {
let ops = L16Operands {
status: FilingStatus::Mfj,
ti: usd(253_942.94),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
assert!(!provenance_class_fires(Some(&ops), &ops, 47_031.31));
}
#[test]
fn provenance_class_fires_on_distinct_operands() {
let oracle_ops = L16Operands {
status: FilingStatus::Mfj,
ti: usd(253_942.94),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
let reproduced_ops = L16Operands {
status: FilingStatus::Single,
ti: usd(70_008.908),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
assert!(provenance_class_fires(
Some(&oracle_ops),
&reproduced_ops,
47_031.31
));
}
#[test]
fn provenance_class_cannot_fire_without_baked_oracle_leaves() {
let ops = L16Operands {
status: FilingStatus::Single,
ti: usd(112_400.0),
qd_l3a: usd(8_000.0),
net_ltcg_qd_excl: usd(25_000.0),
};
assert!(!provenance_class_fires(None, &ops, 17_477.0)); }
#[test]
fn stacking_ok_absorbs_a_below_ceiling_methodology_dissent() {
let ops = L16Operands {
status: FilingStatus::Single,
ti: usd(70_008.908),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
assert!(stacking_ok(
usd(10_459.0),
10_459.0,
Some(10_454.96),
None,
None,
&ops,
None
));
}
#[test]
fn stacking_ok_rejects_btctax_alone_against_both() {
let ops = L16Operands {
status: FilingStatus::Mfj,
ti: usd(253_942.94),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
assert!(!stacking_ok(
usd(47_030.0),
47_031.31,
Some(47_031.31),
None,
None,
&ops,
None
));
}
#[test]
fn stacking_ok_known_defect_pin_holds_then_goes_stale() {
let ops = L16Operands {
status: FilingStatus::Mfj,
ti: usd(253_942.94),
qd_l3a: usd(0.0),
net_ltcg_qd_excl: usd(0.0),
};
let kd = KnownDefect {
fu_id: "FU-EXAMPLE",
btctax_value: usd(47_030.0),
};
assert!(stacking_ok(
usd(47_030.0),
47_031.31,
Some(47_031.31),
None,
None,
&ops,
Some(&kd)
));
assert!(!stacking_ok(
usd(47_029.0),
47_031.31,
Some(47_031.31),
None,
None,
&ops,
Some(&kd)
));
assert!(!stacking_ok(
usd(47_031.0),
47_031.31,
Some(47_031.31),
None,
None,
&ops,
Some(&kd)
));
}
#[test]
fn liveness_flags_a_dead_class() {
let mut l = LivenessLedger::default();
l.declare_pinned("ots_provenance"); l.record_fire("taxcalc_methodology");
assert_eq!(
l.dead(&[
"taxcalc_methodology",
"ots_provenance",
"taxcalc_provenance"
]),
vec!["taxcalc_provenance"]
);
}
}