#[cfg(test)]
mod tests {
use crate::kernel::{domain::Domain, expr::ExprData, pool::ExprPool};
use proptest::prelude::*;
fn domain_strategy() -> impl Strategy<Value = Domain> {
prop_oneof![
Just(Domain::Real),
Just(Domain::Complex),
Just(Domain::Integer),
Just(Domain::Positive),
Just(Domain::NonNegative),
Just(Domain::NonZero),
]
}
fn name_strategy() -> impl Strategy<Value = String> {
"[a-z]{1,4}".prop_map(|s| s)
}
fn atom_data_strategy() -> impl Strategy<Value = ExprData> {
prop_oneof![
(name_strategy(), domain_strategy()).prop_map(|(n, d)| ExprData::Symbol {
name: n,
domain: d,
commutative: true,
}),
(i64::MIN..=i64::MAX).prop_map(|n| ExprData::Integer(crate::kernel::expr::BigInt(
rug::Integer::from(n)
))),
(i32::MIN..=i32::MAX, 1_i32..=10000_i32).prop_map(|(n, d)| {
ExprData::Rational(crate::kernel::expr::BigRat(rug::Rational::from((
rug::Integer::from(n),
rug::Integer::from(d),
))))
}),
]
}
proptest! {
#[test]
fn same_data_same_id(data in atom_data_strategy()) {
let p = ExprPool::new();
let id1 = p.intern(data.clone());
let id2 = p.intern(data);
prop_assert_eq!(id1, id2);
}
#[test]
fn fresh_intern_grows_pool(data in atom_data_strategy()) {
let p = ExprPool::new();
let before = p.len();
p.intern(data.clone());
let after_first = p.len();
p.intern(data);
let after_second = p.len();
prop_assert_eq!(after_first, before + 1);
prop_assert_eq!(after_second, after_first);
}
#[test]
fn round_trip(data in atom_data_strategy()) {
let p = ExprPool::new();
let id = p.intern(data.clone());
prop_assert_eq!(p.get(id), data);
}
#[test]
fn distinct_symbols_distinct_ids(
name1 in name_strategy(),
name2 in name_strategy(),
d1 in domain_strategy(),
d2 in domain_strategy(),
) {
if name1 == name2 && d1 == d2 {
return Ok(());
}
let p = ExprPool::new();
let id1 = p.symbol(&name1, d1);
let id2 = p.symbol(&name2, d2);
if name1 != name2 || d1 != d2 {
prop_assert_ne!(id1, id2);
}
}
#[test]
fn id_equality_mirrors_structural_equality(
n in name_strategy(),
d in domain_strategy(),
) {
let p = ExprPool::new();
let id_a = p.symbol(&n, d);
let id_b = p.symbol(&n, d);
prop_assert_eq!(id_a, id_b);
}
}
}