#![allow(clippy::unwrap_used)]
use elinvar_engine::logic::{Bounds, Expr, Formula, IntExpr, IntOp, Relation, Universe};
use elinvar_engine::{Options, count};
fn problem() -> (Bounds, Expr, Expr, Expr) {
let names: Vec<String> = (-4..=3).map(|v: i64| v.to_string()).collect();
let pairs: Vec<(&str, i64)> = names
.iter()
.map(|n| (n.as_str(), n.parse::<i64>().unwrap()))
.collect();
let u = Universe::new(names.clone())
.unwrap()
.with_ints(3, &pairs)
.unwrap();
let mut b = Bounds::new(&u);
let all = u.all(1).unwrap();
let rel = |name: &str, b: &mut Bounds| {
let r = Relation::unary(name);
b.bound_upper(&r, all.clone()).unwrap();
Expr::relation(&r)
};
let s = rel("s", &mut b);
let t = rel("t", &mut b);
let w = rel("u", &mut b);
(b, s, t, w)
}
fn instances(b: &Bounds, f: &Formula) -> u64 {
count(b, f, &Options::default().symmetry_breaking(false), 10_000)
.unwrap()
.expect("the default budget is enough")
}
#[test]
fn arithmetic_agrees_with_the_referee() {
for op in [
IntOp::Plus,
IntOp::Minus,
IntOp::Times,
IntOp::Divide,
IntOp::Remainder,
IntOp::ShiftLeft,
IntOp::ShiftRight,
IntOp::UnsignedShiftRight,
] {
let (b, s, t, u) = problem();
let f = Formula::and_all([
s.one(),
t.one(),
u.one(),
s.sum().apply(op, &t.sum()).equals(&u.sum()),
]);
assert_eq!(instances(&b, &f), 64, "{op:?}");
}
}
#[test]
fn negation_and_conditionals() {
let (b, s, t, u) = problem();
let f = Formula::and_all([s.one(), t.no(), u.one(), s.sum().negate().equals(&u.sum())]);
assert_eq!(instances(&b, &f), 8);
let (b, s, t, u) = problem();
let max = IntExpr::if_then_else(&s.sum().gt(&t.sum()), &s.sum(), &t.sum());
let f = Formula::and_all([s.one(), t.one(), u.one(), max.equals(&u.sum())]);
assert_eq!(instances(&b, &f), 64);
}
#[test]
fn comparisons_agree_with_the_referee() {
let (b, s, t, u) = problem();
let pair = |f: Formula| Formula::and_all([s.one(), t.one(), u.no(), f]);
assert_eq!(instances(&b, &pair(s.sum().lt(&t.sum()))), 28);
assert_eq!(instances(&b, &pair(s.sum().lte(&t.sum()))), 36);
assert_eq!(instances(&b, &pair(s.sum().equals(&t.sum()))), 8);
assert_eq!(instances(&b, &pair(s.sum().gt(&t.sum()))), 28);
assert_eq!(instances(&b, &pair(s.sum().gte(&t.sum()))), 36);
}
#[test]
fn counts_and_sums_of_sets() {
let (b, s, t, u) = problem();
let f = Formula::and_all([t.no(), u.one(), s.count().equals(&u.sum())]);
assert_eq!(instances(&b, &f), 256);
let (b, s, t, u) = problem();
let f = Formula::and_all([t.no(), u.one(), s.sum().equals(&u.sum())]);
assert_eq!(instances(&b, &f), 256);
}
#[test]
fn sums_over_bindings_and_the_set_of_an_integer() {
let (b, s, t, u) = problem();
let x = elinvar_engine::logic::Variable::new("x");
let per = IntExpr::sum_over(
vec![elinvar_engine::logic::Decl::new(&x, &s)],
&Expr::variable(&x).sum(),
);
let f = Formula::and_all([t.no(), u.one(), per.equals(&u.sum())]);
assert_eq!(instances(&b, &f), 256);
let (b, s, t, u) = problem();
let f = Formula::and_all([s.one(), t.one(), s.sum().plus(&t.sum()).to_set().equals(&u)]);
assert_eq!(instances(&b, &f), 64);
}
#[test]
fn the_builders_int_set() {
let mut m = elinvar_engine::Model::new();
let ints = m.ints();
let b = m.check(&Formula::truth()).bounds().unwrap();
let names: Vec<&str> = b.universe().atoms().collect();
assert_eq!(names.first(), Some(&"-8"));
assert_eq!(names.last(), Some(&"7"));
assert_eq!(b.universe().int_of(0), Some(-8));
let wide = m.check(&Formula::truth()).bitwidth(17);
let errors = wide.bounds().unwrap_err();
assert_eq!(errors[0].code, "model.bitwidth");
let narrow = m
.check(&ints.count().equals(&IntExpr::constant(0)))
.bitwidth(3);
assert_eq!(narrow.bounds().unwrap().universe().size(), 8);
}
#[test]
fn no_overflow_forbids_wrapping_arithmetic() {
let (b, s, t, u) = problem(); let positive_wraps_negative = Formula::and_all([
s.one(),
t.no(),
u.no(),
s.sum().gt(&IntExpr::constant(0)),
s.sum()
.plus(&IntExpr::constant(1))
.lt(&IntExpr::constant(0)),
]);
let default_out =
elinvar_engine::run(&b, &positive_wraps_negative, &Options::default()).unwrap();
assert_eq!(default_out.verdict, elinvar_engine::Verdict::Found);
let no_ovf_out = elinvar_engine::run(
&b,
&positive_wraps_negative,
&Options::default().no_overflow(true),
)
.unwrap();
assert_eq!(no_ovf_out.verdict, elinvar_engine::Verdict::None);
let mul_wraps = Formula::and_all([
s.one(),
t.no(),
u.no(),
s.sum().equals(&IntExpr::constant(2)),
s.sum()
.times(&IntExpr::constant(2))
.lt(&IntExpr::constant(0)),
]);
assert_eq!(
elinvar_engine::run(&b, &mul_wraps, &Options::default())
.unwrap()
.verdict,
elinvar_engine::Verdict::Found
);
assert_eq!(
elinvar_engine::run(&b, &mul_wraps, &Options::default().no_overflow(true))
.unwrap()
.verdict,
elinvar_engine::Verdict::None
);
let neg_wraps = Formula::and_all([
s.one(),
t.no(),
u.no(),
s.sum().equals(&IntExpr::constant(-4)),
s.sum().negate().lt(&IntExpr::constant(0)),
]);
assert_eq!(
elinvar_engine::run(&b, &neg_wraps, &Options::default())
.unwrap()
.verdict,
elinvar_engine::Verdict::Found
);
assert_eq!(
elinvar_engine::run(&b, &neg_wraps, &Options::default().no_overflow(true))
.unwrap()
.verdict,
elinvar_engine::Verdict::None
);
}