#![allow(clippy::collapsible_if)]
use crate::state::lattice::{AbstractDomain, Lattice};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct IntervalFact {
pub lo: Option<i64>,
pub hi: Option<i64>,
}
impl IntervalFact {
pub fn top() -> Self {
Self { lo: None, hi: None }
}
pub fn bottom() -> Self {
Self {
lo: Some(1),
hi: Some(0),
}
}
pub fn exact(n: i64) -> Self {
Self {
lo: Some(n),
hi: Some(n),
}
}
pub fn is_top(&self) -> bool {
self.lo.is_none() && self.hi.is_none()
}
pub fn is_bottom(&self) -> bool {
matches!((self.lo, self.hi), (Some(l), Some(h)) if l > h)
}
pub fn is_proven_bounded(&self) -> bool {
self.lo.is_some() && self.hi.is_some() && !self.is_bottom()
}
pub fn join(&self, other: &Self) -> Self {
if self.is_bottom() {
return other.clone();
}
if other.is_bottom() {
return self.clone();
}
Self {
lo: match (self.lo, other.lo) {
(Some(a), Some(b)) => Some(a.min(b)),
_ => None, },
hi: match (self.hi, other.hi) {
(Some(a), Some(b)) => Some(a.max(b)),
_ => None,
},
}
}
pub fn meet(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
let lo = match (self.lo, other.lo) {
(Some(a), Some(b)) => Some(a.max(b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
};
let hi = match (self.hi, other.hi) {
(Some(a), Some(b)) => Some(a.min(b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
};
let result = Self { lo, hi };
if result.is_bottom() {
Self::bottom()
} else {
result
}
}
pub fn widen(&self, other: &Self) -> Self {
if self.is_bottom() {
return other.clone();
}
if other.is_bottom() {
return self.clone();
}
let lo = if self.lo == other.lo {
self.lo
} else {
None };
let hi = if self.hi == other.hi {
self.hi
} else {
None };
Self { lo, hi }
}
pub fn leq(&self, other: &Self) -> bool {
if self.is_bottom() {
return true;
}
if other.is_bottom() {
return false;
}
let lo_ok = match (self.lo, other.lo) {
(_, None) => true, (None, Some(_)) => false, (Some(a), Some(b)) => a >= b,
};
let hi_ok = match (self.hi, other.hi) {
(_, None) => true,
(None, Some(_)) => false,
(Some(a), Some(b)) => a <= b,
};
lo_ok && hi_ok
}
pub fn add(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
Self {
lo: checked_add_opt(self.lo, other.lo),
hi: checked_add_opt(self.hi, other.hi),
}
}
pub fn sub(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
Self {
lo: checked_sub_opt(self.lo, other.hi),
hi: checked_sub_opt(self.hi, other.lo),
}
}
pub fn mul(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
if self.is_top() || other.is_top() {
return Self::top();
}
match (self.lo, self.hi, other.lo, other.hi) {
(Some(a_lo), Some(a_hi), Some(b_lo), Some(b_hi)) => {
let products = [
a_lo.checked_mul(b_lo),
a_lo.checked_mul(b_hi),
a_hi.checked_mul(b_lo),
a_hi.checked_mul(b_hi),
];
let lo = products.iter().filter_map(|p| *p).min();
let hi = products.iter().filter_map(|p| *p).max();
if products.iter().any(|p| p.is_none()) {
Self {
lo: if lo.is_some() && products[..2].iter().all(|p| p.is_some()) {
lo
} else {
None
},
hi: if hi.is_some() && products[2..].iter().all(|p| p.is_some()) {
hi
} else {
None
},
}
} else {
Self { lo, hi }
}
}
_ => Self::top(),
}
}
pub fn div(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
match (self.lo, self.hi, other.lo, other.hi) {
(Some(a_lo), Some(a_hi), Some(b_lo), Some(b_hi)) => {
if b_lo <= 0 && b_hi >= 0 {
return Self::top();
}
let quotients = [
a_lo.checked_div(b_lo),
a_lo.checked_div(b_hi),
a_hi.checked_div(b_lo),
a_hi.checked_div(b_hi),
];
let lo = quotients.iter().filter_map(|q| *q).min();
let hi = quotients.iter().filter_map(|q| *q).max();
Self { lo, hi }
}
_ => Self::top(),
}
}
pub fn modulo(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
match (other.lo, other.hi) {
(Some(b_lo), Some(b_hi)) => {
if b_lo <= 0 && b_hi >= 0 {
return Self::top(); }
let abs_max = b_lo.unsigned_abs().max(b_hi.unsigned_abs());
if abs_max == 0 {
return Self::top();
}
let bound = (abs_max - 1) as i64;
if self.lo.is_some_and(|l| l >= 0) {
Self {
lo: Some(0),
hi: Some(bound),
}
} else {
Self {
lo: Some(-bound),
hi: Some(bound),
}
}
}
_ => Self::top(),
}
}
pub fn bit_and(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
if let (Some(a), Some(b)) = (self.as_singleton(), other.as_singleton()) {
return Self::exact(a & b);
}
if self.as_singleton() == Some(0) || other.as_singleton() == Some(0) {
return Self::exact(0);
}
if let Some(m) = other.as_singleton() {
if m >= 0 {
return Self {
lo: Some(0),
hi: Some(m),
};
}
}
if let Some(m) = self.as_singleton() {
if m >= 0 {
return Self {
lo: Some(0),
hi: Some(m),
};
}
}
let a_nonneg = self.lo.is_some_and(|l| l >= 0);
let b_nonneg = other.lo.is_some_and(|l| l >= 0);
if a_nonneg && b_nonneg {
let hi = match (self.hi, other.hi) {
(Some(a), Some(b)) => Some(a.min(b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
};
return Self { lo: Some(0), hi };
}
Self::top()
}
pub fn bit_or(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
if let (Some(a), Some(b)) = (self.as_singleton(), other.as_singleton()) {
return Self::exact(a | b);
}
if other.as_singleton() == Some(0) {
return self.clone();
}
if self.as_singleton() == Some(0) {
return other.clone();
}
let a_nonneg = self.lo.is_some_and(|l| l >= 0);
let b_nonneg = other.lo.is_some_and(|l| l >= 0);
if a_nonneg && b_nonneg {
if let (Some(a_hi), Some(b_hi)) = (self.hi, other.hi) {
let max_hi = a_hi.max(b_hi);
let lo = self.lo.unwrap_or(0).max(other.lo.unwrap_or(0));
return Self {
lo: Some(lo),
hi: Some(next_pow2_minus1(max_hi)),
};
}
}
Self::top()
}
pub fn bit_xor(&self, other: &Self) -> Self {
if self.is_bottom() || other.is_bottom() {
return Self::bottom();
}
if let (Some(a), Some(b)) = (self.as_singleton(), other.as_singleton()) {
return Self::exact(a ^ b);
}
if other.as_singleton() == Some(0) {
return self.clone();
}
if self.as_singleton() == Some(0) {
return other.clone();
}
let a_nonneg = self.lo.is_some_and(|l| l >= 0);
let b_nonneg = other.lo.is_some_and(|l| l >= 0);
if a_nonneg && b_nonneg {
if let (Some(a_hi), Some(b_hi)) = (self.hi, other.hi) {
let max_hi = a_hi.max(b_hi);
return Self {
lo: Some(0),
hi: Some(next_pow2_minus1(max_hi)),
};
}
}
Self::top()
}
pub fn left_shift(&self, shift: &Self) -> Self {
if self.is_bottom() || shift.is_bottom() {
return Self::bottom();
}
match (self.lo, self.hi, shift.lo, shift.hi) {
(Some(a_lo), Some(a_hi), Some(s_lo), Some(s_hi))
if a_lo >= 0 && s_lo >= 0 && s_hi <= 63 =>
{
let result_lo = (a_lo as u64).checked_shl(s_lo as u32);
let result_hi = (a_hi as u64).checked_shl(s_hi as u32);
match (result_lo, result_hi) {
(Some(lo), Some(hi)) if lo <= i64::MAX as u64 && hi <= i64::MAX as u64 => {
Self {
lo: Some(lo as i64),
hi: Some(hi as i64),
}
}
_ => Self::top(), }
}
_ => Self::top(),
}
}
pub fn right_shift(&self, shift: &Self) -> Self {
if self.is_bottom() || shift.is_bottom() {
return Self::bottom();
}
match (self.lo, self.hi, shift.lo, shift.hi) {
(Some(a_lo), Some(a_hi), Some(s_lo), Some(s_hi))
if a_lo >= 0 && s_lo >= 0 && s_hi <= 63 =>
{
Self {
lo: Some(a_lo >> s_hi), hi: Some(a_hi >> s_lo), }
}
_ => Self::top(),
}
}
fn as_singleton(&self) -> Option<i64> {
match (self.lo, self.hi) {
(Some(lo), Some(hi)) if lo == hi => Some(lo),
_ => None,
}
}
}
fn next_pow2_minus1(n: i64) -> i64 {
if n <= 0 {
return 0;
}
let bits_needed = 64 - (n as u64).leading_zeros();
if bits_needed >= 63 {
return i64::MAX;
}
(1i64 << bits_needed) - 1
}
impl Lattice for IntervalFact {
fn bot() -> Self {
Self::bottom()
}
fn join(&self, other: &Self) -> Self {
self.join(other)
}
fn leq(&self, other: &Self) -> bool {
self.leq(other)
}
}
impl AbstractDomain for IntervalFact {
fn top() -> Self {
Self::top()
}
fn meet(&self, other: &Self) -> Self {
self.meet(other)
}
fn widen(&self, other: &Self) -> Self {
self.widen(other)
}
}
fn checked_add_opt(a: Option<i64>, b: Option<i64>) -> Option<i64> {
match (a, b) {
(Some(x), Some(y)) => x.checked_add(y), _ => None, }
}
fn checked_sub_opt(a: Option<i64>, b: Option<i64>) -> Option<i64> {
match (a, b) {
(Some(x), Some(y)) => x.checked_sub(y),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_values() {
let a = IntervalFact::exact(5);
assert_eq!(a.lo, Some(5));
assert_eq!(a.hi, Some(5));
assert!(a.is_proven_bounded());
assert!(!a.is_top());
assert!(!a.is_bottom());
}
#[test]
fn top_and_bottom() {
let t = IntervalFact::top();
assert!(t.is_top());
assert!(!t.is_bottom());
assert!(!t.is_proven_bounded());
let b = IntervalFact::bottom();
assert!(b.is_bottom());
assert!(!b.is_top());
assert!(!b.is_proven_bounded());
}
#[test]
fn join_commutative() {
let a = IntervalFact::exact(3);
let b = IntervalFact::exact(7);
assert_eq!(a.join(&b), b.join(&a));
}
#[test]
fn join_associative() {
let a = IntervalFact::exact(1);
let b = IntervalFact::exact(5);
let c = IntervalFact::exact(3);
assert_eq!(a.join(&b).join(&c), a.join(&b.join(&c)));
}
#[test]
fn join_idempotent() {
let a = IntervalFact {
lo: Some(2),
hi: Some(8),
};
assert_eq!(a.join(&a), a);
}
#[test]
fn join_hull() {
let a = IntervalFact {
lo: Some(2),
hi: Some(5),
};
let b = IntervalFact {
lo: Some(3),
hi: Some(9),
};
let j = a.join(&b);
assert_eq!(j.lo, Some(2));
assert_eq!(j.hi, Some(9));
}
#[test]
fn join_with_bottom_identity() {
let a = IntervalFact::exact(5);
assert_eq!(a.join(&IntervalFact::bottom()), a);
assert_eq!(IntervalFact::bottom().join(&a), a);
}
#[test]
fn meet_intersection() {
let a = IntervalFact {
lo: Some(1),
hi: Some(10),
};
let b = IntervalFact {
lo: Some(5),
hi: Some(15),
};
let m = a.meet(&b);
assert_eq!(m.lo, Some(5));
assert_eq!(m.hi, Some(10));
}
#[test]
fn meet_disjoint_is_bottom() {
let a = IntervalFact {
lo: Some(1),
hi: Some(3),
};
let b = IntervalFact {
lo: Some(5),
hi: Some(7),
};
assert!(a.meet(&b).is_bottom());
}
#[test]
fn leq_subset() {
let narrow = IntervalFact {
lo: Some(3),
hi: Some(5),
};
let wide = IntervalFact {
lo: Some(1),
hi: Some(10),
};
assert!(narrow.leq(&wide));
assert!(!wide.leq(&narrow));
}
#[test]
fn leq_top_greatest() {
let a = IntervalFact::exact(42);
assert!(a.leq(&IntervalFact::top()));
assert!(!IntervalFact::top().leq(&a));
}
#[test]
fn leq_bottom_least() {
assert!(IntervalFact::bottom().leq(&IntervalFact::exact(0)));
assert!(IntervalFact::bottom().leq(&IntervalFact::top()));
}
#[test]
fn widen_stable_bounds() {
let a = IntervalFact {
lo: Some(0),
hi: Some(10),
};
assert_eq!(a.widen(&a), a);
}
#[test]
fn widen_growing_upper() {
let old = IntervalFact {
lo: Some(0),
hi: Some(5),
};
let new = IntervalFact {
lo: Some(0),
hi: Some(10),
};
let w = old.widen(&new);
assert_eq!(w.lo, Some(0)); assert_eq!(w.hi, None); }
#[test]
fn widen_growing_lower() {
let old = IntervalFact {
lo: Some(5),
hi: Some(10),
};
let new = IntervalFact {
lo: Some(2),
hi: Some(10),
};
let w = old.widen(&new);
assert_eq!(w.lo, None); assert_eq!(w.hi, Some(10));
}
#[test]
fn add_exact() {
assert_eq!(
IntervalFact::exact(5).add(&IntervalFact::exact(3)),
IntervalFact::exact(8)
);
}
#[test]
fn add_ranges() {
let a = IntervalFact {
lo: Some(1),
hi: Some(5),
};
let b = IntervalFact {
lo: Some(2),
hi: Some(4),
};
let r = a.add(&b);
assert_eq!(r.lo, Some(3));
assert_eq!(r.hi, Some(9));
}
#[test]
fn sub_ranges() {
let a = IntervalFact {
lo: Some(0),
hi: Some(10),
};
let b = IntervalFact {
lo: Some(1),
hi: Some(3),
};
let r = a.sub(&b);
assert_eq!(r.lo, Some(-3)); assert_eq!(r.hi, Some(9)); }
#[test]
fn mul_ranges() {
let a = IntervalFact {
lo: Some(2),
hi: Some(5),
};
let b = IntervalFact {
lo: Some(3),
hi: Some(4),
};
let r = a.mul(&b);
assert_eq!(r.lo, Some(6)); assert_eq!(r.hi, Some(20)); }
#[test]
fn mul_negative() {
let a = IntervalFact {
lo: Some(-3),
hi: Some(2),
};
let b = IntervalFact {
lo: Some(1),
hi: Some(4),
};
let r = a.mul(&b);
assert_eq!(r.lo, Some(-12)); assert_eq!(r.hi, Some(8)); }
#[test]
fn div_no_zero() {
let a = IntervalFact {
lo: Some(10),
hi: Some(20),
};
let b = IntervalFact {
lo: Some(2),
hi: Some(5),
};
let r = a.div(&b);
assert_eq!(r.lo, Some(2)); assert_eq!(r.hi, Some(10)); }
#[test]
fn div_spans_zero_is_top() {
let a = IntervalFact::exact(10);
let b = IntervalFact {
lo: Some(-1),
hi: Some(1),
};
assert!(a.div(&b).is_top());
}
#[test]
fn modulo_positive() {
let a = IntervalFact {
lo: Some(0),
hi: Some(100),
};
let b = IntervalFact {
lo: Some(7),
hi: Some(7),
};
let r = a.modulo(&b);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(6));
}
#[test]
fn overflow_add() {
let a = IntervalFact::exact(i64::MAX);
let b = IntervalFact::exact(1);
let r = a.add(&b);
assert_eq!(r.hi, None);
}
#[test]
fn overflow_mul() {
let a = IntervalFact::exact(i64::MAX);
let b = IntervalFact::exact(2);
let r = a.mul(&b);
assert!(r.lo.is_none() || r.hi.is_none());
}
#[test]
fn bit_and_constant_mask() {
let x = IntervalFact {
lo: Some(0),
hi: Some(1000),
};
let mask = IntervalFact::exact(0xFF);
let r = x.bit_and(&mask);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(0xFF));
}
#[test]
fn bit_and_zero() {
let x = IntervalFact {
lo: Some(0),
hi: Some(1000),
};
let zero = IntervalFact::exact(0);
assert_eq!(x.bit_and(&zero), IntervalFact::exact(0));
assert_eq!(zero.bit_and(&x), IntervalFact::exact(0));
}
#[test]
fn bit_and_negative_operand_with_nonneg_mask() {
let x = IntervalFact {
lo: Some(-5),
hi: Some(10),
};
let mask = IntervalFact::exact(0xFF);
let r = x.bit_and(&mask);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(0xFF));
}
#[test]
fn bit_and_both_negative_no_singleton() {
let a = IntervalFact {
lo: Some(-100),
hi: Some(-1),
};
let b = IntervalFact {
lo: Some(-50),
hi: Some(-10),
};
assert!(a.bit_and(&b).is_top());
}
#[test]
fn bit_and_singletons() {
assert_eq!(
IntervalFact::exact(0xFF).bit_and(&IntervalFact::exact(0x0F)),
IntervalFact::exact(0x0F)
);
}
#[test]
fn bit_or_basic() {
let a = IntervalFact {
lo: Some(0),
hi: Some(0xF0),
};
let b = IntervalFact {
lo: Some(0),
hi: Some(0x0F),
};
let r = a.bit_or(&b);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(0xFF));
}
#[test]
fn bit_or_zero_identity() {
let x = IntervalFact {
lo: Some(3),
hi: Some(10),
};
let zero = IntervalFact::exact(0);
assert_eq!(x.bit_or(&zero), x);
assert_eq!(zero.bit_or(&x), x);
}
#[test]
fn bit_or_concrete_singletons() {
assert_eq!(
IntervalFact::exact(0xF0).bit_or(&IntervalFact::exact(0x0F)),
IntervalFact::exact(0xFF)
);
}
#[test]
fn bit_xor_basic() {
let a = IntervalFact {
lo: Some(0),
hi: Some(255),
};
let b = IntervalFact {
lo: Some(0),
hi: Some(255),
};
let r = a.bit_xor(&b);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(255)); }
#[test]
fn bit_xor_zero_identity() {
let x = IntervalFact {
lo: Some(3),
hi: Some(10),
};
let zero = IntervalFact::exact(0);
assert_eq!(x.bit_xor(&zero), x);
assert_eq!(zero.bit_xor(&x), x);
}
#[test]
fn bit_xor_same_singleton_to_zero() {
assert_eq!(
IntervalFact::exact(42).bit_xor(&IntervalFact::exact(42)),
IntervalFact::exact(0)
);
}
#[test]
fn left_shift_basic() {
assert_eq!(
IntervalFact::exact(1).left_shift(&IntervalFact::exact(3)),
IntervalFact::exact(8)
);
}
#[test]
fn left_shift_range() {
let x = IntervalFact {
lo: Some(0),
hi: Some(7),
};
let shift = IntervalFact {
lo: Some(1),
hi: Some(2),
};
let r = x.left_shift(&shift);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(28)); }
#[test]
fn left_shift_invalid_shift() {
let x = IntervalFact::exact(1);
assert!(x.left_shift(&IntervalFact::exact(64)).is_top());
assert!(x.left_shift(&IntervalFact::exact(-1)).is_top());
}
#[test]
fn left_shift_overflow_behavior() {
let x = IntervalFact::exact(i64::MAX);
let shift = IntervalFact::exact(1);
assert!(x.left_shift(&shift).is_top());
}
#[test]
fn right_shift_basic() {
assert_eq!(
IntervalFact::exact(16).right_shift(&IntervalFact::exact(2)),
IntervalFact::exact(4)
);
}
#[test]
fn right_shift_singleton_exactness() {
assert_eq!(
IntervalFact::exact(255).right_shift(&IntervalFact::exact(4)),
IntervalFact::exact(15)
);
}
#[test]
fn right_shift_range() {
let x = IntervalFact {
lo: Some(0),
hi: Some(255),
};
let shift = IntervalFact {
lo: Some(1),
hi: Some(3),
};
let r = x.right_shift(&shift);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(127));
}
#[test]
fn right_shift_negative_dividend() {
let x = IntervalFact {
lo: Some(-10),
hi: Some(10),
};
let shift = IntervalFact::exact(1);
assert!(x.right_shift(&shift).is_top());
}
#[test]
fn overflow_sub() {
let a = IntervalFact::exact(i64::MIN);
let b = IntervalFact::exact(1);
let r = a.sub(&b);
assert_eq!(r.lo, None, "underflow on i64::MIN - 1 must drop lo to None");
assert_eq!(r.hi, None, "i64::MIN - 1 underflows on hi too");
}
#[test]
fn div_i64_min_by_minus_one_does_not_panic() {
let a = IntervalFact::exact(i64::MIN);
let b = IntervalFact::exact(-1);
let r = a.div(&b);
assert!(
r.lo.is_none() || r.hi.is_none() || (r.lo.is_some() && r.hi.is_some()),
"div should never panic on i64::MIN / -1"
);
}
#[test]
fn modulo_negative_divisor_singleton() {
let a = IntervalFact {
lo: Some(0),
hi: Some(10),
};
let b = IntervalFact::exact(-3);
let r = a.modulo(&b);
assert_eq!(r.lo, Some(0));
assert_eq!(r.hi, Some(2));
}
#[test]
fn modulo_divisor_spans_zero_is_top() {
let a = IntervalFact {
lo: Some(0),
hi: Some(100),
};
let b = IntervalFact {
lo: Some(-1),
hi: Some(1),
};
let r = a.modulo(&b);
assert!(r.is_top(), "modulo by zero-spanning divisor must be Top");
}
#[test]
fn full_range_is_join_absorbing() {
let full = IntervalFact {
lo: Some(i64::MIN),
hi: Some(i64::MAX),
};
let small = IntervalFact {
lo: Some(0),
hi: Some(10),
};
let j = full.join(&small);
assert_eq!(j.lo, Some(i64::MIN), "join must not narrow lo");
assert_eq!(j.hi, Some(i64::MAX), "join must not narrow hi");
}
fn sample_intervals() -> Vec<IntervalFact> {
vec![
IntervalFact::bottom(),
IntervalFact::top(),
IntervalFact::exact(0),
IntervalFact::exact(-7),
IntervalFact {
lo: Some(2),
hi: Some(8),
},
IntervalFact {
lo: None,
hi: Some(10),
},
IntervalFact {
lo: Some(-5),
hi: None,
},
]
}
#[test]
fn join_with_top_is_top() {
for a in sample_intervals() {
let j = a.join(&IntervalFact::top());
assert!(j.is_top(), "x ⊔ ⊤ = ⊤ failed for {:?}", a);
let j2 = IntervalFact::top().join(&a);
assert!(j2.is_top(), "⊤ ⊔ x = ⊤ failed for {:?}", a);
}
}
#[test]
fn meet_idempotent() {
for a in sample_intervals() {
assert_eq!(a.meet(&a), a, "x ⊓ x = x failed for {:?}", a);
}
}
#[test]
fn meet_commutative() {
let xs = sample_intervals();
for a in &xs {
for b in &xs {
assert_eq!(
a.meet(b),
b.meet(a),
"meet not commutative for {:?} / {:?}",
a,
b
);
}
}
}
#[test]
fn meet_associative() {
let xs = sample_intervals();
for a in &xs {
for b in &xs {
for c in &xs {
let lhs = a.meet(b).meet(c);
let rhs = a.meet(&b.meet(c));
assert_eq!(lhs, rhs, "meet not associative for {:?},{:?},{:?}", a, b, c);
}
}
}
}
#[test]
fn meet_top_identity() {
for a in sample_intervals() {
assert_eq!(
a.meet(&IntervalFact::top()),
a,
"x ⊓ ⊤ = x failed for {:?}",
a
);
}
}
#[test]
fn meet_bottom_absorbing() {
for a in sample_intervals() {
assert!(
a.meet(&IntervalFact::bottom()).is_bottom(),
"x ⊓ ⊥ = ⊥ failed for {:?}",
a
);
}
}
#[test]
fn widen_idempotent() {
for a in sample_intervals() {
assert_eq!(a.widen(&a), a, "widen(x, x) = x failed for {:?}", a);
}
}
#[test]
fn widen_over_approximates_join() {
let xs = sample_intervals();
for a in &xs {
for b in &xs {
let j = a.join(b);
let w = a.widen(b);
assert!(
j.leq(&w),
"widen({:?}, {:?}) = {:?} does not over-approximate join = {:?}",
a,
b,
w,
j
);
}
}
}
#[test]
fn leq_reflexive() {
for a in sample_intervals() {
assert!(a.leq(&a), "x ⊑ x failed for {:?}", a);
}
}
#[test]
fn leq_transitive() {
let a = IntervalFact::exact(5);
let b = IntervalFact {
lo: Some(0),
hi: Some(10),
};
let c = IntervalFact::top();
assert!(a.leq(&b));
assert!(b.leq(&c));
assert!(a.leq(&c), "leq must be transitive");
}
#[test]
fn join_is_upper_bound() {
let xs = sample_intervals();
for a in &xs {
for b in &xs {
let j = a.join(b);
assert!(a.leq(&j), "a ⊑ a ⊔ b failed for {:?}, {:?}", a, b);
assert!(b.leq(&j), "b ⊑ a ⊔ b failed for {:?}, {:?}", a, b);
}
}
}
#[test]
fn meet_is_lower_bound() {
let xs = sample_intervals();
for a in &xs {
for b in &xs {
let m = a.meet(b);
assert!(m.leq(a), "a ⊓ b ⊑ a failed for {:?}, {:?}", a, b);
assert!(m.leq(b), "a ⊓ b ⊑ b failed for {:?}, {:?}", a, b);
}
}
}
#[test]
fn mul_by_zero_singleton_is_zero() {
let zero = IntervalFact::exact(0);
let inputs = [
IntervalFact::exact(42),
IntervalFact {
lo: Some(-100),
hi: Some(100),
},
IntervalFact {
lo: Some(i64::MIN),
hi: Some(i64::MAX),
},
IntervalFact::top(),
];
for a in inputs.iter() {
if !a.is_top() {
let r = a.mul(&zero);
assert_eq!(r, IntervalFact::exact(0), "x * 0 should be 0 for {:?}", a);
let r2 = zero.mul(a);
assert_eq!(r2, IntervalFact::exact(0), "0 * x should be 0 for {:?}", a);
}
}
}
#[test]
fn bottom_propagates_through_arith() {
let bot = IntervalFact::bottom();
let x = IntervalFact::exact(5);
assert!(bot.add(&x).is_bottom());
assert!(x.add(&bot).is_bottom());
assert!(bot.sub(&x).is_bottom());
assert!(bot.mul(&x).is_bottom());
assert!(bot.div(&x).is_bottom());
assert!(bot.modulo(&x).is_bottom());
assert!(bot.bit_and(&x).is_bottom());
assert!(bot.bit_or(&x).is_bottom());
assert!(bot.bit_xor(&x).is_bottom());
assert!(bot.left_shift(&x).is_bottom());
assert!(bot.right_shift(&x).is_bottom());
}
#[test]
fn div_by_exact_zero_is_top() {
let a = IntervalFact::exact(10);
let zero = IntervalFact::exact(0);
assert!(
a.div(&zero).is_top(),
"division by exact zero must escape to Top"
);
}
#[test]
fn modulo_by_exact_zero_is_top() {
let a = IntervalFact {
lo: Some(0),
hi: Some(100),
};
let zero = IntervalFact::exact(0);
assert!(a.modulo(&zero).is_top());
}
#[test]
fn add_with_top_is_top() {
let r = IntervalFact::exact(5).add(&IntervalFact::top());
assert!(r.is_top(), "5 + Top should be Top, got {:?}", r);
}
#[test]
fn sub_overflow_extreme() {
let a = IntervalFact::exact(i64::MAX);
let b = IntervalFact::exact(i64::MIN);
let r = a.sub(&b); assert!(
r.lo.is_none() || r.hi.is_none(),
"extreme subtraction must not panic and must drop a bound"
);
}
#[test]
fn widen_with_bottom() {
let x = IntervalFact::exact(5);
let bot = IntervalFact::bottom();
let w1 = bot.widen(&x);
assert_eq!(w1, x);
let w2 = x.widen(&bot);
assert_eq!(w2, x);
}
}