use verif_arithmetic::prove::{self, Verdict, Witness};
use verif_arithmetic::*;
#[test]
fn checked_add_flag_is_exact() {
assert!(prove::checked_add_is_total().is_proved());
}
#[test]
fn saturating_add_bounds_hold() {
assert!(prove::saturating_add_bounds().is_proved());
}
#[test]
fn subtraction_cancels() {
for w in [8u8, 16] {
assert!(prove::sub_self_is_zero(w).is_proved(), "width {}", w);
}
}
#[test]
fn algebraic_laws_hold() {
assert_eq!(prove::algebraic_identities(), 20);
}
#[test]
fn bounded_products_do_not_wrap() {
assert!(prove::product_of_bounded_fits().is_proved());
}
#[test]
fn fixed_add_guard_is_sound() {
assert!(prove::fixed_add_no_overflow().is_proved());
}
#[test]
fn fixed_point_roundtrips() {
assert!(prove::fixed_roundtrip().is_proved());
assert!(prove::scale_identity().is_proved());
}
#[test]
fn accumulator_never_overflows() {
assert!(prove::accumulator_no_overflow(12, 1000).is_proved());
}
#[test]
fn safe_add_is_exact_at_every_width() {
for w in [8u8, 12, 16] {
assert!(prove::safe_add_is_exact(w).is_proved(), "width {}", w);
}
}
#[test]
fn bounded_sub_refinement_holds() {
assert!(prove::bounded_sub_is_total(0, 255).is_proved());
assert!(prove::bounded_sub_is_total(10, 100).is_proved());
assert!(prove::bounded_sub_is_total(5, 200).is_proved());
}
#[test]
fn saturating_add_is_monotone() {
assert!(prove::saturating_add_monotone(8, 7).is_proved());
}
#[test]
fn the_test_profile_is_optimised_without_losing_its_checks() {
let checks_on: bool = cfg!(debug_assertions);
assert!(checks_on, "the test profile must keep debug assertions on");
}
#[test]
fn the_min_conjunct_is_load_bearing() {
let (min, max) = (10u64, 100u64);
let mut refuted = false;
let mut a = 0u64;
'outer: while a <= max {
let mut b = 0u64;
while b <= max {
let in_range = a >= min && a <= max && b >= min && b <= max;
let no_underflow = b <= a;
let diff = a.wrapping_sub(b);
let result_ok = no_underflow && diff >= min && diff <= max;
let without_min = no_underflow && diff <= max;
if in_range && result_ok != without_min {
refuted = true;
break 'outer;
}
b += 1;
}
a += 1;
}
assert!(
refuted,
"without the min conjunct the claim must be falsifiable at (11, 10)"
);
}
#[test]
fn the_underflow_gate_is_load_bearing() {
let mut refuted = false;
let mut a = 0u64;
'outer: while a <= 255 {
let mut b = 0u64;
while b <= 255 {
let in_range = |v: u64| v <= 255;
let pre = in_range(a) && in_range(b);
let diff = a.wrapping_sub(b) & 0xFF;
let ungated = in_range(diff);
let want = b <= a;
if pre && ungated != want {
refuted = true;
break 'outer;
}
b += 1;
}
a += 1;
}
assert!(
refuted,
"without the underflow gate a wrapping difference is accepted"
);
}
#[test]
fn saturation_is_load_bearing() {
let (a, b) = (200u8, 200u8);
let sum = a.wrapping_add(b);
assert_eq!(sum, 144);
assert!(sum < a, "the wrapping sum is below a");
assert!(sum < b, "and below b");
let sat = u8::MAX;
assert!(sat >= a && sat >= b);
}
#[test]
fn the_fixed_add_bound_is_tight() {
let bound = 1u64 << 14;
let a = bound;
let b = bound;
let sum = (a + b) & 0xFFFF;
assert_eq!(sum, 1u64 << 15);
assert!(
sum > (1u64 << 15) - 1,
"the inclusive bound 2^14 lets the sum reach 2^15, which overflows i16"
);
assert_eq!((16384i32 + 16384) as i16, -32768);
}
#[test]
fn the_scale_multiply_is_load_bearing() {
let mut refuted = false;
for k in 0u64..16 {
let scaled = (k * 10) & 0xFF;
if scaled >> 4 != k {
refuted = true;
break;
}
}
assert!(refuted, "a scale of 10 must not satisfy the identity");
}
#[test]
fn the_algebraic_laws_are_falsifiable() {
let mask = 0xFFu64;
let refuted = (0u64..=mask).any(|x| (x | mask) & mask != mask);
assert!(!refuted, "x | mask == mask holds for the whole domain");
let always = (0u64..=mask).all(|x| x <= mask);
assert!(always, "`x <= mask` is a range fact, not a law with teeth");
}
#[test]
fn a_domain_past_the_budget_is_unknown_not_valid() {
assert_eq!(prove::safe_add_is_exact(24), Verdict::Unknown);
assert!(!prove::safe_add_is_exact(24).is_proved());
assert_eq!(prove::sub_self_is_zero(33), Verdict::Unknown);
}
#[test]
fn a_refuted_obligation_carries_a_witness() {
let v = prove::accumulator_no_overflow(8, 200);
match v {
Verdict::Counterexample(w) => {
assert_eq!(w.len, 2);
let sum = w.a.wrapping_add(w.b) & 0xFF;
assert!(sum < w.a, "the witness must actually overflow");
}
other => panic!("expected a counterexample, got {:?}", other),
}
}
#[test]
fn a_witness_is_constructible_and_named() {
let w = Witness {
a: 1,
b: 2,
len: 2,
};
assert_eq!(w.len, 2);
}
#[test]
fn runtime_behaviour_matches_the_proof() {
assert_eq!(checked_add(200, 55), Some(255));
assert_eq!(checked_add(u32::MAX, 1), None);
assert_eq!(saturating_add(u32::MAX, 1), u32::MAX);
assert_eq!(saturating_sub(0, 1), 0);
assert_eq!(mul_div(1000, 1000, 8), Some(125_000));
assert_eq!(mul_div(1, 1, 0), None);
assert_eq!(safe_add(1, 2), Some(3));
assert_eq!(safe_add(u32::MAX, 1), None);
assert_eq!(safe_sub(0, 1), None);
assert_eq!(safe_mul(1 << 16, 1 << 16), None);
assert_eq!(safe_div(10, 0), None);
assert_eq!(safe_rem(10, 0), None);
assert_eq!(safe_div(10, 3), Some(3));
let a = Fixed::from_int(3);
let b = Fixed::from_int(4);
assert_eq!(a.saturating_add(b), Fixed::from_int(7));
assert_eq!(a.to_int(), 3);
assert_eq!(a.saturating_mul_int(2), Fixed::from_int(6));
assert_eq!(a.checked_div_int(0), None);
assert_eq!(Fixed::MIN.checked_div_int(-1), None);
assert_eq!(
Fixed::from_bits(i32::MIN).checked_div_int(2),
Some(Fixed(i32::MIN / 2))
);
}
#[test]
fn fixed_to_int_truncates_toward_zero() {
assert_eq!(Fixed::from_bits(-32768).to_int(), 0);
assert_eq!(Fixed::from_bits(-1).to_int(), 0);
assert_eq!(Fixed::from_bits(-65536).to_int(), -1);
assert_eq!(Fixed::from_bits(-65537).to_int(), -1);
assert_eq!(Fixed::from_bits(65535).to_int(), 0);
assert_eq!(Fixed::from_bits(i32::MIN).to_int(), -32768);
assert_eq!(Fixed::from_bits(i32::MAX).to_int(), 32767);
for bits in [
i32::MIN,
-70000,
-65536,
-32768,
-1,
0,
1,
32768,
65535,
i32::MAX,
] {
assert_eq!(Fixed::from_bits(bits).to_int(), bits / 65536, "bits {}", bits);
}
}
#[test]
fn fixed_from_int_saturates_instead_of_wrapping() {
assert_eq!(Fixed::from_int(0).to_bits(), 0);
assert_eq!(Fixed::from_int(1).to_bits(), 65536);
assert_eq!(Fixed::from_int(-1).to_bits(), -65536);
assert_eq!(Fixed::from_int(32767).to_bits(), 32767 << 16);
assert_eq!(Fixed::from_int(-32768).to_bits(), i32::MIN);
assert_eq!(Fixed::from_int(32768), Fixed::MAX);
assert_eq!(Fixed::from_int(40000), Fixed::MAX);
assert_eq!(Fixed::from_int(-32769), Fixed::MIN);
assert_eq!(Fixed::from_int(i32::MAX), Fixed::MAX);
}
#[test]
fn bounded_type_behaves() {
type Pwm = Bounded<0, 1000>;
assert_eq!(Pwm::new(500).unwrap().get(), 500);
assert!(Pwm::new(1001).is_none());
assert_eq!(Pwm::clamp(5000).unwrap().get(), 1000);
assert_eq!(Pwm::clamp(0).unwrap().get(), 0);
let a = Pwm::new(600).unwrap();
let b = Pwm::new(500).unwrap();
assert_eq!(a.add(b), None);
assert_eq!(a.sub(b).unwrap().get(), 100);
}
#[test]
fn an_empty_bounded_range_admits_nothing() {
type Empty = Bounded<100, 10>;
assert_eq!(Empty::new(50), None, "new already refuses an empty range");
assert_eq!(Empty::clamp(50), None, "clamp must refuse it too");
assert_eq!(Empty::clamp(0), None);
assert_eq!(Empty::clamp(200), None);
type Pwm = Bounded<0, 1000>;
assert_eq!(Pwm::clamp(5000).unwrap().get(), 1000);
assert_eq!(Pwm::clamp(500).unwrap().get(), 500);
}
#[test]
fn bounded_sub_rejects_a_difference_below_min() {
type R = Bounded<10, 100>;
let a = R::new(11).unwrap();
let b = R::new(10).unwrap();
assert_eq!(a.sub(b), None, "1 is below min = 10");
}
#[test]
fn a_zero_width_domain_is_unknown_not_valid() {
assert_eq!(prove::safe_add_is_exact(0), Verdict::Unknown);
assert_eq!(prove::sub_self_is_zero(0), Verdict::Unknown);
assert_eq!(prove::accumulator_no_overflow(0, 0), Verdict::Unknown);
assert_eq!(prove::saturating_add_monotone(0, 1), Verdict::Unknown);
assert_eq!(prove::restricted_domain_matches(0, 1, 1), Verdict::Unknown);
assert_eq!(prove::bounded_walk_covers_its_rectangle(0, 1), Verdict::Unknown);
assert!(prove::safe_add_is_exact(1).is_proved(), "a 1-bit domain is a domain");
}
#[test]
fn the_restricted_domain_matches_the_precondition() {
assert!(prove::restricted_domain_matches(8, 200, 200).is_proved());
assert!(prove::restricted_domain_matches(6, 50, 50).is_proved());
assert!(
prove::restricted_domain_matches(16, prove::FIXED_ADD_BOUND, prove::FIXED_ADD_BOUND)
.is_proved(),
"the obligation's own rectangle must be the precondition's set"
);
}
#[test]
fn a_rectangle_that_omits_admitted_inputs_is_caught() {
assert_eq!(
prove::restricted_domain_matches(8, 200, 199),
Verdict::Counterexample(Witness { a: 0, b: 200, len: 2 }),
"a rectangle narrower than the precondition must be reported"
);
assert_eq!(
prove::restricted_domain_matches(8, 199, 200),
Verdict::Counterexample(Witness { a: 0, b: 200, len: 2 }),
"a rectangle wider than the precondition must be reported too"
);
}
#[test]
fn a_bound_past_the_domain_is_refused() {
assert_eq!(prove::restricted_domain_matches(8, 256, 256), Verdict::Unknown);
assert_eq!(prove::restricted_domain_matches(8, 255, 300), Verdict::Unknown);
assert!(prove::restricted_domain_matches(8, 255, 255).is_proved());
}
#[test]
fn the_bounded_walk_covers_its_rectangle() {
for bound in [0u64, 1, 2, 3, 7, 50, 199, 200, 255] {
assert!(
prove::bounded_walk_covers_its_rectangle(8, bound).is_proved(),
"the walk must cover exactly [0, {}]^2, edges included",
bound
);
}
assert!(prove::bounded_walk_covers_its_rectangle(4, 9).is_proved());
}
#[test]
fn runtime_ops_match_their_proved_statements() {
for (a, b) in [
(0u32, 0u32),
(1, 1),
(200, 55),
(1000, 1000),
(1 << 31, 1 << 31),
(u32::MAX, 0),
(u32::MAX, 1),
(u32::MAX, u32::MAX),
] {
let wrapped = a.wrapping_add(b);
assert_eq!(
wrapped < a,
wrapped < b,
"the two overflow tests must agree at ({a}, {b})"
);
assert_eq!(
checked_add(a, b).is_none(),
wrapped < a,
"checked_add must report overflow exactly when the sum wraps at ({a}, {b})"
);
assert_eq!(safe_add(a, b), checked_add(a, b), "safe_add at ({a}, {b})");
}
for (a, b) in [
(0u32, 0u32),
(200, 55),
(1000, 1000),
(1 << 31, 1 << 31),
(u32::MAX, 1),
] {
let sat = saturating_add(a, b);
assert!(sat >= a && sat >= b, "sat below an operand at ({a}, {b})");
match checked_add(a, b) {
Some(sum) => assert_eq!(sat, sum, "without overflow sat must be the sum at ({a}, {b})"),
None => assert_eq!(sat, u32::MAX, "overflow must clamp to MAX at ({a}, {b})"),
}
}
for x in [0u32, 1, 1000, 1 << 31, u32::MAX] {
assert_eq!(safe_sub(x, x), Some(0), "x - x must be 0 at {x}");
assert_eq!(checked_sub(x, x), Some(0));
}
for k in [0i32, 1, -1, 32767, -32768] {
assert_eq!(
Fixed::from_int(k).to_int(),
k,
"the round-trip must be lossless at {k}"
);
}
assert_eq!(Fixed::from_int(32768), Fixed::MAX, "from_int saturates at the top");
assert_eq!(Fixed::from_int(-32769), Fixed::MIN, "and at the bottom");
assert_eq!(mul_div(100_000, 100_000, 1000), Some(10_000_000));
assert_eq!(mul_div(1000, 1000, 8), Some(125_000));
assert_eq!(mul_div(1, 1, 0), None, "a zero divisor is the only failure mode");
let naive = 100_000u32.wrapping_mul(100_000) / 1000;
assert_ne!(
mul_div(100_000, 100_000, 1000),
Some(naive),
"the widening must change the answer, or it is not load-bearing"
);
}