Add unit tests for precedence-0 formulas and lower

This commit is contained in:
Patrick Lühne 2020-04-01 14:45:40 +02:00
parent d57b3b3b62
commit 1a497254a8
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -406,6 +406,31 @@ mod tests
assert_eq!(format(greater_or_equal(constant("a"), constant("b"))), "a >= b");
assert_eq!(format(equal(constant("a"), constant("b"))), "a = b");
assert_eq!(format(not_equal(constant("a"), constant("b"))), "a != b");
assert_eq!(format(
greater(multiply(add(constant("a"), constant("b")), constant("c")),
absolute_value(subtract(constant("d"), constant("e"))))),
"(a + b) * c > |d - e|");
assert_eq!(format(
less(multiply(add(constant("a"), constant("b")), constant("c")),
absolute_value(subtract(constant("d"), constant("e"))))),
"(a + b) * c < |d - e|");
assert_eq!(format(
less_or_equal(multiply(add(constant("a"), constant("b")), constant("c")),
absolute_value(subtract(constant("d"), constant("e"))))),
"(a + b) * c <= |d - e|");
assert_eq!(format(
greater_or_equal(multiply(add(constant("a"), constant("b")), constant("c")),
absolute_value(subtract(constant("d"), constant("e"))))),
"(a + b) * c >= |d - e|");
assert_eq!(format(
equal(multiply(add(constant("a"), constant("b")), constant("c")),
absolute_value(subtract(constant("d"), constant("e"))))),
"(a + b) * c = |d - e|");
assert_eq!(format(
not_equal(multiply(add(constant("a"), constant("b")), constant("c")),
absolute_value(subtract(constant("d"), constant("e"))))),
"(a + b) * c != |d - e|");
}
#[test]
@ -515,4 +540,241 @@ mod tests
predicate("r", vec![])])),
"p <-> q <-> r");
}
#[test]
fn format_combination_boolean_and_lower()
{
assert_eq!(format(exists(vec![variable_declaration("X")], true_())), "exists X true");
assert_eq!(format(exists(vec![variable_declaration("X")], false_())), "exists X false");
assert_eq!(format(
exists(vec![variable_declaration("X"), variable_declaration("Y"),
variable_declaration("Z")], true_())),
"exists X, Y, Z true");
assert_eq!(format(
exists(vec![variable_declaration("X"), variable_declaration("Y"),
variable_declaration("Z")], false_())),
"exists X, Y, Z false");
assert_eq!(format(for_all(vec![variable_declaration("X")], true_())), "forall X true");
assert_eq!(format(for_all(vec![variable_declaration("X")], false_())), "forall X false");
assert_eq!(format(
for_all(vec![variable_declaration("X"), variable_declaration("Y"),
variable_declaration("Z")], true_())),
"forall X, Y, Z true");
assert_eq!(format(
for_all(vec![variable_declaration("X"), variable_declaration("Y"),
variable_declaration("Z")], false_())),
"forall X, Y, Z false");
assert_eq!(format(not(true_())), "not true");
assert_eq!(format(not(false_())), "not false");
assert_eq!(format(and(vec![true_()])), "true");
assert_eq!(format(and(vec![true_(), true_(), true_()])), "true and true and true");
assert_eq!(format(and(vec![false_()])), "false");
assert_eq!(format(and(vec![false_(), false_(), false_()])), "false and false and false");
assert_eq!(format(or(vec![true_()])), "true");
assert_eq!(format(or(vec![true_(), true_(), true_()])), "true or true or true");
assert_eq!(format(or(vec![false_()])), "false");
assert_eq!(format(or(vec![false_(), false_(), false_()])), "false or false or false");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, true_(), true_())),
"true -> true");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, true_(), true_())),
"true <- true");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, false_(), false_())),
"false -> false");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, false_(), false_())),
"false <- false");
assert_eq!(format(if_and_only_if(vec![true_()])), "true");
assert_eq!(format(
if_and_only_if(vec![true_(), true_(), true_()])),
"true <-> true <-> true");
assert_eq!(format(if_and_only_if(vec![false_()])), "false");
assert_eq!(format(
if_and_only_if(vec![false_(), false_(), false_()])),
"false <-> false <-> false");
}
#[test]
fn format_combination_compare_and_lower()
{
let term_1 = || multiply(add(constant("a"), constant("b")), constant("c"));
let term_2 = || absolute_value(subtract(constant("d"), constant("e")));
let term_3 = || exponentiate(constant("a"), exponentiate(constant("b"), constant("c")));
let term_4 = || negative(function("f", vec![integer(5), add(variable("X"), integer(3))]));
assert_eq!(format(
exists(vec![variable_declaration("X")], greater(term_1(), term_2()))),
"exists X (a + b) * c > |d - e|");
assert_eq!(format(
exists(vec![variable_declaration("X")], less(term_1(), term_2()))),
"exists X (a + b) * c < |d - e|");
assert_eq!(format(
exists(vec![variable_declaration("X")], less_or_equal(term_1(), term_2()))),
"exists X (a + b) * c <= |d - e|");
assert_eq!(format(
exists(vec![variable_declaration("X")], greater_or_equal(term_1(), term_2()))),
"exists X (a + b) * c >= |d - e|");
assert_eq!(format(
exists(vec![variable_declaration("X")], equal(term_1(), term_2()))),
"exists X (a + b) * c = |d - e|");
assert_eq!(format(
exists(vec![variable_declaration("X")], not_equal(term_1(), term_2()))),
"exists X (a + b) * c != |d - e|");
assert_eq!(format(
for_all(vec![variable_declaration("X")], greater(term_1(), term_2()))),
"forall X (a + b) * c > |d - e|");
assert_eq!(format(
for_all(vec![variable_declaration("X")], less(term_1(), term_2()))),
"forall X (a + b) * c < |d - e|");
assert_eq!(format(
for_all(vec![variable_declaration("X")], less_or_equal(term_1(), term_2()))),
"forall X (a + b) * c <= |d - e|");
assert_eq!(format(
for_all(vec![variable_declaration("X")], greater_or_equal(term_1(), term_2()))),
"forall X (a + b) * c >= |d - e|");
assert_eq!(format(
for_all(vec![variable_declaration("X")], equal(term_1(), term_2()))),
"forall X (a + b) * c = |d - e|");
assert_eq!(format(
for_all(vec![variable_declaration("X")], not_equal(term_1(), term_2()))),
"forall X (a + b) * c != |d - e|");
assert_eq!(format(not(greater(term_1(), term_2()))), "not (a + b) * c > |d - e|");
assert_eq!(format(not(less(term_1(), term_2()))), "not (a + b) * c < |d - e|");
assert_eq!(format(not(less_or_equal(term_1(), term_2()))), "not (a + b) * c <= |d - e|");
assert_eq!(format(not(greater_or_equal(term_1(), term_2()))), "not (a + b) * c >= |d - e|");
assert_eq!(format(not(equal(term_1(), term_2()))), "not (a + b) * c = |d - e|");
assert_eq!(format(not(not_equal(term_1(), term_2()))), "not (a + b) * c != |d - e|");
assert_eq!(format(
and(vec![greater(term_1(), term_2()), greater(term_3(), term_4()),
greater(term_2(), term_4())])),
"(a + b) * c > |d - e| and a ** b ** c > -f(5, X + 3) and |d - e| > -f(5, X + 3)");
assert_eq!(format(
and(vec![less(term_1(), term_2()), less(term_3(), term_4()),
less(term_2(), term_4())])),
"(a + b) * c < |d - e| and a ** b ** c < -f(5, X + 3) and |d - e| < -f(5, X + 3)");
assert_eq!(format(
and(vec![less_or_equal(term_1(), term_2()), less_or_equal(term_3(), term_4()),
less_or_equal(term_2(), term_4())])),
"(a + b) * c <= |d - e| and a ** b ** c <= -f(5, X + 3) and |d - e| <= -f(5, X + 3)");
assert_eq!(format(
and(vec![greater_or_equal(term_1(), term_2()), greater_or_equal(term_3(), term_4()),
greater_or_equal(term_2(), term_4())])),
"(a + b) * c >= |d - e| and a ** b ** c >= -f(5, X + 3) and |d - e| >= -f(5, X + 3)");
assert_eq!(format(
and(vec![equal(term_1(), term_2()), equal(term_3(), term_4()),
equal(term_2(), term_4())])),
"(a + b) * c = |d - e| and a ** b ** c = -f(5, X + 3) and |d - e| = -f(5, X + 3)");
assert_eq!(format(
and(vec![not_equal(term_1(), term_2()), not_equal(term_3(), term_4()),
not_equal(term_2(), term_4())])),
"(a + b) * c != |d - e| and a ** b ** c != -f(5, X + 3) and |d - e| != -f(5, X + 3)");
assert_eq!(format(
or(vec![greater(term_1(), term_2()), greater(term_3(), term_4()),
greater(term_2(), term_4())])),
"(a + b) * c > |d - e| or a ** b ** c > -f(5, X + 3) or |d - e| > -f(5, X + 3)");
assert_eq!(format(
or(vec![less(term_1(), term_2()), less(term_3(), term_4()),
less(term_2(), term_4())])),
"(a + b) * c < |d - e| or a ** b ** c < -f(5, X + 3) or |d - e| < -f(5, X + 3)");
assert_eq!(format(
or(vec![less_or_equal(term_1(), term_2()), less_or_equal(term_3(), term_4()),
less_or_equal(term_2(), term_4())])),
"(a + b) * c <= |d - e| or a ** b ** c <= -f(5, X + 3) or |d - e| <= -f(5, X + 3)");
assert_eq!(format(
or(vec![greater_or_equal(term_1(), term_2()), greater_or_equal(term_3(), term_4()),
greater_or_equal(term_2(), term_4())])),
"(a + b) * c >= |d - e| or a ** b ** c >= -f(5, X + 3) or |d - e| >= -f(5, X + 3)");
assert_eq!(format(
or(vec![equal(term_1(), term_2()), equal(term_3(), term_4()),
equal(term_2(), term_4())])),
"(a + b) * c = |d - e| or a ** b ** c = -f(5, X + 3) or |d - e| = -f(5, X + 3)");
assert_eq!(format(
or(vec![not_equal(term_1(), term_2()), not_equal(term_3(), term_4()),
not_equal(term_2(), term_4())])),
"(a + b) * c != |d - e| or a ** b ** c != -f(5, X + 3) or |d - e| != -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, greater(term_1(), term_2()),
greater(term_3(), term_4()))),
"(a + b) * c > |d - e| -> a ** b ** c > -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, less(term_1(), term_2()),
less(term_3(), term_4()))),
"(a + b) * c < |d - e| -> a ** b ** c < -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, less_or_equal(term_1(), term_2()),
less_or_equal(term_3(), term_4()))),
"(a + b) * c <= |d - e| -> a ** b ** c <= -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, greater_or_equal(term_1(), term_2()),
greater_or_equal(term_3(), term_4()))),
"(a + b) * c >= |d - e| -> a ** b ** c >= -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, equal(term_1(), term_2()),
equal(term_3(), term_4()))),
"(a + b) * c = |d - e| -> a ** b ** c = -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::LeftToRight, not_equal(term_1(), term_2()),
not_equal(term_3(), term_4()))),
"(a + b) * c != |d - e| -> a ** b ** c != -f(5, X + 3)");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, greater(term_1(), term_2()),
greater(term_3(), term_4()))),
"a ** b ** c > -f(5, X + 3) <- (a + b) * c > |d - e|");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, less(term_1(), term_2()),
less(term_3(), term_4()))),
"a ** b ** c < -f(5, X + 3) <- (a + b) * c < |d - e|");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, less_or_equal(term_1(), term_2()),
less_or_equal(term_3(), term_4()))),
"a ** b ** c <= -f(5, X + 3) <- (a + b) * c <= |d - e|");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, greater_or_equal(term_1(), term_2()),
greater_or_equal(term_3(), term_4()))),
"a ** b ** c >= -f(5, X + 3) <- (a + b) * c >= |d - e|");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, equal(term_1(), term_2()),
equal(term_3(), term_4()))),
"a ** b ** c = -f(5, X + 3) <- (a + b) * c = |d - e|");
assert_eq!(format(
implies(ImplicationDirection::RightToLeft, not_equal(term_1(), term_2()),
not_equal(term_3(), term_4()))),
"a ** b ** c != -f(5, X + 3) <- (a + b) * c != |d - e|");
assert_eq!(format(
if_and_only_if(vec![greater(term_1(), term_2()), greater(term_3(), term_4()),
greater(term_2(), term_4())])),
"(a + b) * c > |d - e| <-> a ** b ** c > -f(5, X + 3) <-> |d - e| > -f(5, X + 3)");
assert_eq!(format(
if_and_only_if(vec![less(term_1(), term_2()), less(term_3(), term_4()),
less(term_2(), term_4())])),
"(a + b) * c < |d - e| <-> a ** b ** c < -f(5, X + 3) <-> |d - e| < -f(5, X + 3)");
assert_eq!(format(
if_and_only_if(vec![less_or_equal(term_1(), term_2()),
less_or_equal(term_3(), term_4()), less_or_equal(term_2(), term_4())])),
"(a + b) * c <= |d - e| <-> a ** b ** c <= -f(5, X + 3) <-> |d - e| <= -f(5, X + 3)");
assert_eq!(format(
if_and_only_if(vec![greater_or_equal(term_1(), term_2()),
greater_or_equal(term_3(), term_4()), greater_or_equal(term_2(), term_4())])),
"(a + b) * c >= |d - e| <-> a ** b ** c >= -f(5, X + 3) <-> |d - e| >= -f(5, X + 3)");
assert_eq!(format(
if_and_only_if(vec![equal(term_1(), term_2()), equal(term_3(), term_4()),
equal(term_2(), term_4())])),
"(a + b) * c = |d - e| <-> a ** b ** c = -f(5, X + 3) <-> |d - e| = -f(5, X + 3)");
assert_eq!(format(
if_and_only_if(vec![not_equal(term_1(), term_2()), not_equal(term_3(), term_4()),
not_equal(term_2(), term_4())])),
"(a + b) * c != |d - e| <-> a ** b ** c != -f(5, X + 3) <-> |d - e| != -f(5, X + 3)");
}
}