Test term formatting
This commit is contained in:
parent
a3da369346
commit
23e1854346
@ -263,3 +263,738 @@ impl std::fmt::Display for crate::Term
|
||||
write!(format, "{}", display_term(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
use crate::*;
|
||||
|
||||
fn format(term: Box<ast::Term>) -> String
|
||||
{
|
||||
format!("{}", term)
|
||||
}
|
||||
|
||||
fn absolute_value(argument: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::absolute_value(argument))
|
||||
}
|
||||
|
||||
fn add(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::add(left, right))
|
||||
}
|
||||
|
||||
fn constant(name: &str) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::function(function_declaration(name, 0), vec![]))
|
||||
}
|
||||
|
||||
fn divide(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::divide(left, right))
|
||||
}
|
||||
|
||||
fn exponentiate(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::exponentiate(left, right))
|
||||
}
|
||||
|
||||
fn false_() -> Box<Term>
|
||||
{
|
||||
Box::new(Term::false_())
|
||||
}
|
||||
|
||||
fn function(name: &str, arguments: Vec<Box<Term>>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::function(function_declaration(name, arguments.len()), arguments))
|
||||
}
|
||||
|
||||
fn function_declaration(name: &str, arity: usize) -> std::rc::Rc<FunctionDeclaration>
|
||||
{
|
||||
std::rc::Rc::new(FunctionDeclaration::new(name.to_string(), arity))
|
||||
}
|
||||
|
||||
fn infimum() -> Box<Term>
|
||||
{
|
||||
Box::new(Term::infimum())
|
||||
}
|
||||
|
||||
fn integer(value: i32) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::integer(value))
|
||||
}
|
||||
|
||||
fn modulo(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::modulo(left, right))
|
||||
}
|
||||
|
||||
fn multiply(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::multiply(left, right))
|
||||
}
|
||||
|
||||
fn negative(argument: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::negative(argument))
|
||||
}
|
||||
|
||||
fn subtract(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::subtract(left, right))
|
||||
}
|
||||
|
||||
fn supremum() -> Box<Term>
|
||||
{
|
||||
Box::new(Term::supremum())
|
||||
}
|
||||
|
||||
fn string(value: &str) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::string(value.to_string()))
|
||||
}
|
||||
|
||||
fn true_() -> Box<Term>
|
||||
{
|
||||
Box::new(Term::true_())
|
||||
}
|
||||
|
||||
fn variable(name: &str) -> Box<Term>
|
||||
{
|
||||
Box::new(Term::variable(variable_declaration(name)))
|
||||
}
|
||||
|
||||
fn variable_declaration(name: &str) -> std::rc::Rc<VariableDeclaration>
|
||||
{
|
||||
std::rc::Rc::new(VariableDeclaration::new(name.to_string()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_binary_operation()
|
||||
{
|
||||
assert_eq!(format(add(constant("a"), constant("b"))), "a + b");
|
||||
assert_eq!(format(subtract(constant("a"), constant("b"))), "a - b");
|
||||
assert_eq!(format(multiply(constant("a"), constant("b"))), "a * b");
|
||||
assert_eq!(format(divide(constant("a"), constant("b"))), "a / b");
|
||||
assert_eq!(format(modulo(constant("a"), constant("b"))), "a % b");
|
||||
assert_eq!(format(exponentiate(constant("a"), constant("b"))), "a ** b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_boolean()
|
||||
{
|
||||
assert_eq!(format(true_()), "true");
|
||||
assert_eq!(format(false_()), "false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_special_integer()
|
||||
{
|
||||
assert_eq!(format(infimum()), "#inf");
|
||||
assert_eq!(format(supremum()), "#sup");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_integer()
|
||||
{
|
||||
assert_eq!(format(integer(0)), "0");
|
||||
assert_eq!(format(integer(1)), "1");
|
||||
assert_eq!(format(integer(10000)), "10000");
|
||||
assert_eq!(format(integer(-1)), "-1");
|
||||
assert_eq!(format(integer(-10000)), "-10000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_string()
|
||||
{
|
||||
assert_eq!(format(string("")), "\"\"");
|
||||
assert_eq!(format(string(" ")), "\" \"");
|
||||
assert_eq!(format(string(" ")), "\" \"");
|
||||
assert_eq!(format(string("a")), "\"a\"");
|
||||
assert_eq!(format(string("test test")), "\"test test\"");
|
||||
assert_eq!(format(string("123 123")), "\"123 123\"");
|
||||
assert_eq!(format(string("\ntest\n123\n")), "\"\\ntest\\n123\\n\"");
|
||||
assert_eq!(format(string("\ttest\t123\t")), "\"\\ttest\\t123\\t\"");
|
||||
assert_eq!(format(string("\\test\\123\\")), "\"\\\\test\\\\123\\\\\"");
|
||||
assert_eq!(format(string("🙂test🙂123🙂")), "\"🙂test🙂123🙂\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_function()
|
||||
{
|
||||
assert_eq!(format(constant("a")), "a");
|
||||
assert_eq!(format(constant("constant")), "constant");
|
||||
assert_eq!(format(function("f", vec![constant("a")])), "f(a)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![constant("a"), constant("b"), constant("c")])),
|
||||
"f(a, b, c)");
|
||||
assert_eq!(format(
|
||||
function("function", vec![constant("a"), constant("b"), constant("c")])),
|
||||
"function(a, b, c)");
|
||||
|
||||
// TODO: escape functions that start with capital letters or that conflict with keywords
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_variable()
|
||||
{
|
||||
assert_eq!(format(variable("X")), "X");
|
||||
assert_eq!(format(variable("Variable")), "Variable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_boolean_and_lower()
|
||||
{
|
||||
assert_eq!(format(function("f", vec![true_(), true_(), true_()])), "f(true, true, true)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![false_(), false_(), false_()])),
|
||||
"f(false, false, false)");
|
||||
|
||||
assert_eq!(format(absolute_value(true_())), "|true|");
|
||||
assert_eq!(format(absolute_value(false_())), "|false|");
|
||||
|
||||
assert_eq!(format(negative(true_())), "-true");
|
||||
assert_eq!(format(negative(false_())), "-false");
|
||||
|
||||
assert_eq!(format(exponentiate(true_(), true_())), "true ** true");
|
||||
assert_eq!(format(exponentiate(false_(), false_())), "false ** false");
|
||||
|
||||
assert_eq!(format(multiply(true_(), true_())), "true * true");
|
||||
assert_eq!(format(multiply(false_(), false_())), "false * false");
|
||||
|
||||
assert_eq!(format(divide(true_(), true_())), "true / true");
|
||||
assert_eq!(format(divide(false_(), false_())), "false / false");
|
||||
|
||||
assert_eq!(format(modulo(true_(), true_())), "true % true");
|
||||
assert_eq!(format(modulo(false_(), false_())), "false % false");
|
||||
|
||||
assert_eq!(format(add(true_(), true_())), "true + true");
|
||||
assert_eq!(format(add(false_(), false_())), "false + false");
|
||||
|
||||
assert_eq!(format(subtract(true_(), true_())), "true - true");
|
||||
assert_eq!(format(subtract(false_(), false_())), "false - false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_special_integer_and_lower()
|
||||
{
|
||||
assert_eq!(format(
|
||||
function("f", vec![infimum(), infimum(), infimum()])),
|
||||
"f(#inf, #inf, #inf)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![supremum(), supremum(), supremum()])),
|
||||
"f(#sup, #sup, #sup)");
|
||||
|
||||
assert_eq!(format(absolute_value(infimum())), "|#inf|");
|
||||
assert_eq!(format(absolute_value(supremum())), "|#sup|");
|
||||
|
||||
assert_eq!(format(negative(infimum())), "-#inf");
|
||||
assert_eq!(format(negative(supremum())), "-#sup");
|
||||
|
||||
assert_eq!(format(exponentiate(infimum(), infimum())), "#inf ** #inf");
|
||||
assert_eq!(format(exponentiate(supremum(), supremum())), "#sup ** #sup");
|
||||
|
||||
assert_eq!(format(multiply(infimum(), infimum())), "#inf * #inf");
|
||||
assert_eq!(format(multiply(supremum(), supremum())), "#sup * #sup");
|
||||
|
||||
assert_eq!(format(divide(infimum(), infimum())), "#inf / #inf");
|
||||
assert_eq!(format(divide(supremum(), supremum())), "#sup / #sup");
|
||||
|
||||
assert_eq!(format(modulo(infimum(), infimum())), "#inf % #inf");
|
||||
assert_eq!(format(modulo(supremum(), supremum())), "#sup % #sup");
|
||||
|
||||
assert_eq!(format(add(infimum(), infimum())), "#inf + #inf");
|
||||
assert_eq!(format(add(supremum(), supremum())), "#sup + #sup");
|
||||
|
||||
assert_eq!(format(subtract(infimum(), infimum())), "#inf - #inf");
|
||||
assert_eq!(format(subtract(supremum(), supremum())), "#sup - #sup");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_integer_and_lower()
|
||||
{
|
||||
assert_eq!(format(function("f", vec![integer(0), integer(0), integer(0)])),
|
||||
"f(0, 0, 0)");
|
||||
assert_eq!(format(function("f", vec![integer(10000), integer(10000), integer(10000)])),
|
||||
"f(10000, 10000, 10000)");
|
||||
assert_eq!(format(function("f", vec![integer(-10000), integer(-10000), integer(-10000)])),
|
||||
"f(-10000, -10000, -10000)");
|
||||
|
||||
assert_eq!(format(absolute_value(integer(0))), "|0|");
|
||||
assert_eq!(format(absolute_value(integer(10000))), "|10000|");
|
||||
assert_eq!(format(absolute_value(integer(-10000))), "|-10000|");
|
||||
|
||||
assert_eq!(format(negative(integer(0))), "-0");
|
||||
assert_eq!(format(negative(integer(10000))), "-10000");
|
||||
assert_eq!(format(negative(integer(-10000))), "--10000");
|
||||
|
||||
assert_eq!(format(exponentiate(integer(0), integer(0))), "0 ** 0");
|
||||
assert_eq!(format(exponentiate(integer(10000), integer(10000))), "10000 ** 10000");
|
||||
assert_eq!(format(exponentiate(integer(-10000), integer(-10000))), "-10000 ** -10000");
|
||||
|
||||
assert_eq!(format(multiply(integer(0), integer(0))), "0 * 0");
|
||||
assert_eq!(format(multiply(integer(10000), integer(10000))), "10000 * 10000");
|
||||
assert_eq!(format(multiply(integer(-10000), integer(-10000))), "-10000 * -10000");
|
||||
|
||||
assert_eq!(format(divide(integer(0), integer(0))), "0 / 0");
|
||||
assert_eq!(format(divide(integer(10000), integer(10000))), "10000 / 10000");
|
||||
assert_eq!(format(divide(integer(-10000), integer(-10000))), "-10000 / -10000");
|
||||
|
||||
assert_eq!(format(modulo(integer(0), integer(0))), "0 % 0");
|
||||
assert_eq!(format(modulo(integer(10000), integer(10000))), "10000 % 10000");
|
||||
assert_eq!(format(modulo(integer(-10000), integer(-10000))), "-10000 % -10000");
|
||||
|
||||
assert_eq!(format(add(integer(0), integer(0))), "0 + 0");
|
||||
assert_eq!(format(add(integer(10000), integer(10000))), "10000 + 10000");
|
||||
assert_eq!(format(add(integer(-10000), integer(-10000))), "-10000 + -10000");
|
||||
|
||||
assert_eq!(format(subtract(integer(0), integer(0))), "0 - 0");
|
||||
assert_eq!(format(subtract(integer(10000), integer(10000))), "10000 - 10000");
|
||||
assert_eq!(format(subtract(integer(-10000), integer(-10000))), "-10000 - -10000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_string_and_lower()
|
||||
{
|
||||
assert_eq!(format(function("f", vec![string(""), string(""), string("")])),
|
||||
"f(\"\", \"\", \"\")");
|
||||
assert_eq!(format(function("f", vec![string("test 123"), string("test 123"),
|
||||
string("test 123")])),
|
||||
"f(\"test 123\", \"test 123\", \"test 123\")");
|
||||
assert_eq!(format(function("f", vec![string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"),
|
||||
string("\\a\nb🙂c\t")])),
|
||||
"f(\"\\\\a\\nb🙂c\\t\", \"\\\\a\\nb🙂c\\t\", \"\\\\a\\nb🙂c\\t\")");
|
||||
|
||||
assert_eq!(format(absolute_value(string(""))), "|\"\"|");
|
||||
assert_eq!(format(absolute_value(string("test 123"))), "|\"test 123\"|");
|
||||
assert_eq!(format(absolute_value(string("\\a\nb🙂c\t"))), "|\"\\\\a\\nb🙂c\\t\"|");
|
||||
|
||||
assert_eq!(format(negative(string(""))), "-\"\"");
|
||||
assert_eq!(format(negative(string("test 123"))), "-\"test 123\"");
|
||||
assert_eq!(format(negative(string("\\a\nb🙂c\t"))), "-\"\\\\a\\nb🙂c\\t\"");
|
||||
|
||||
assert_eq!(format(exponentiate(string(""), string(""))), "\"\" ** \"\"");
|
||||
assert_eq!(format(
|
||||
exponentiate(string("test 123"), string("test 123"))),
|
||||
"\"test 123\" ** \"test 123\"");
|
||||
assert_eq!(format(
|
||||
exponentiate(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"))),
|
||||
"\"\\\\a\\nb🙂c\\t\" ** \"\\\\a\\nb🙂c\\t\"");
|
||||
|
||||
assert_eq!(format(multiply(string(""), string(""))), "\"\" * \"\"");
|
||||
assert_eq!(format(
|
||||
multiply(string("test 123"), string("test 123"))),
|
||||
"\"test 123\" * \"test 123\"");
|
||||
assert_eq!(format(
|
||||
multiply(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"))),
|
||||
"\"\\\\a\\nb🙂c\\t\" * \"\\\\a\\nb🙂c\\t\"");
|
||||
|
||||
assert_eq!(format(divide(string(""), string(""))), "\"\" / \"\"");
|
||||
assert_eq!(format(
|
||||
divide(string("test 123"), string("test 123"))),
|
||||
"\"test 123\" / \"test 123\"");
|
||||
assert_eq!(format(
|
||||
divide(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"))),
|
||||
"\"\\\\a\\nb🙂c\\t\" / \"\\\\a\\nb🙂c\\t\"");
|
||||
|
||||
assert_eq!(format(modulo(string(""), string(""))), "\"\" % \"\"");
|
||||
assert_eq!(format(
|
||||
modulo(string("test 123"), string("test 123"))),
|
||||
"\"test 123\" % \"test 123\"");
|
||||
assert_eq!(format(
|
||||
modulo(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"))),
|
||||
"\"\\\\a\\nb🙂c\\t\" % \"\\\\a\\nb🙂c\\t\"");
|
||||
|
||||
assert_eq!(format(add(string(""), string(""))), "\"\" + \"\"");
|
||||
assert_eq!(format(
|
||||
add(string("test 123"), string("test 123"))),
|
||||
"\"test 123\" + \"test 123\"");
|
||||
assert_eq!(format(
|
||||
add(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"))),
|
||||
"\"\\\\a\\nb🙂c\\t\" + \"\\\\a\\nb🙂c\\t\"");
|
||||
|
||||
assert_eq!(format(subtract(string(""), string(""))), "\"\" - \"\"");
|
||||
assert_eq!(format(
|
||||
subtract(string("test 123"), string("test 123"))),
|
||||
"\"test 123\" - \"test 123\"");
|
||||
assert_eq!(format(
|
||||
subtract(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t"))),
|
||||
"\"\\\\a\\nb🙂c\\t\" - \"\\\\a\\nb🙂c\\t\"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_variable_and_lower()
|
||||
{
|
||||
assert_eq!(format(variable("X")), "X");
|
||||
assert_eq!(format(variable("Variable")), "Variable");
|
||||
|
||||
assert_eq!(format(absolute_value(variable("X"))), "|X|");
|
||||
assert_eq!(format(absolute_value(variable("Variable"))), "|Variable|");
|
||||
|
||||
assert_eq!(format(negative(variable("X"))), "-X");
|
||||
assert_eq!(format(negative(variable("Variable"))), "-Variable");
|
||||
|
||||
assert_eq!(format(exponentiate(variable("X"), variable("X"))), "X ** X");
|
||||
assert_eq!(format(
|
||||
exponentiate(variable("Variable"), variable("Variable"))),
|
||||
"Variable ** Variable");
|
||||
|
||||
assert_eq!(format(multiply(variable("X"), variable("X"))), "X * X");
|
||||
assert_eq!(format(
|
||||
multiply(variable("Variable"), variable("Variable"))),
|
||||
"Variable * Variable");
|
||||
|
||||
assert_eq!(format(divide(variable("X"), variable("X"))), "X / X");
|
||||
assert_eq!(format(
|
||||
divide(variable("Variable"), variable("Variable"))), "Variable / Variable");
|
||||
|
||||
assert_eq!(format(modulo(variable("X"), variable("X"))), "X % X");
|
||||
assert_eq!(format(
|
||||
modulo(variable("Variable"), variable("Variable"))), "Variable % Variable");
|
||||
|
||||
assert_eq!(format(add(variable("X"), variable("X"))), "X + X");
|
||||
assert_eq!(format(
|
||||
add(variable("Variable"), variable("Variable"))), "Variable + Variable");
|
||||
|
||||
assert_eq!(format(subtract(variable("X"), variable("X"))), "X - X");
|
||||
assert_eq!(format(
|
||||
subtract(variable("Variable"), variable("Variable"))), "Variable - Variable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_function_and_lower()
|
||||
{
|
||||
let f1 = || constant("a");
|
||||
let f2 = || constant("constant");
|
||||
let f3 = || function("f", vec![constant("a")]);
|
||||
let f4 = || function("function", vec![constant("a"), constant("b"), constant("c")]);
|
||||
|
||||
assert_eq!(format(absolute_value(f1())), "|a|");
|
||||
assert_eq!(format(absolute_value(f2())), "|constant|");
|
||||
assert_eq!(format(absolute_value(f3())), "|f(a)|");
|
||||
assert_eq!(format(absolute_value(f4())), "|function(a, b, c)|");
|
||||
assert_eq!(format(function("f", vec![absolute_value(constant("a"))])), "f(|a|)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![absolute_value(constant("a")), absolute_value(constant("b")),
|
||||
absolute_value(constant("c"))])),
|
||||
"f(|a|, |b|, |c|)");
|
||||
|
||||
assert_eq!(format(negative(f1())), "-a");
|
||||
assert_eq!(format(negative(f2())), "-constant");
|
||||
assert_eq!(format(negative(f3())), "-f(a)");
|
||||
assert_eq!(format(negative(f4())), "-function(a, b, c)");
|
||||
assert_eq!(format(function("f", vec![negative(constant("a"))])), "f(-a)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![negative(constant("a")), negative(constant("b")),
|
||||
negative(constant("c"))])),
|
||||
"f(-a, -b, -c)");
|
||||
|
||||
assert_eq!(format(exponentiate(f1(), f1())), "a ** a");
|
||||
assert_eq!(format(exponentiate(f2(), f2())), "constant ** constant");
|
||||
assert_eq!(format(exponentiate(f3(), f3())), "f(a) ** f(a)");
|
||||
assert_eq!(format(exponentiate(f4(), f4())), "function(a, b, c) ** function(a, b, c)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![exponentiate(constant("a"), constant("b"))])),
|
||||
"f(a ** b)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")),
|
||||
exponentiate(constant("e"), constant("f"))])),
|
||||
"f(a ** b, c ** d, e ** f)");
|
||||
|
||||
assert_eq!(format(multiply(f1(), f1())), "a * a");
|
||||
assert_eq!(format(multiply(f2(), f2())), "constant * constant");
|
||||
assert_eq!(format(multiply(f3(), f3())), "f(a) * f(a)");
|
||||
assert_eq!(format(multiply(f4(), f4())), "function(a, b, c) * function(a, b, c)");
|
||||
assert_eq!(format(function("f", vec![multiply(constant("a"), constant("b"))])), "f(a * b)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")), multiply(constant("e"), constant("f"))])),
|
||||
"f(a * b, c * d, e * f)");
|
||||
|
||||
assert_eq!(format(divide(f1(), f1())), "a / a");
|
||||
assert_eq!(format(divide(f2(), f2())), "constant / constant");
|
||||
assert_eq!(format(divide(f3(), f3())), "f(a) / f(a)");
|
||||
assert_eq!(format(divide(f4(), f4())), "function(a, b, c) / function(a, b, c)");
|
||||
assert_eq!(format(function("f", vec![divide(constant("a"), constant("b"))])), "f(a / b)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")), divide(constant("e"), constant("f"))])),
|
||||
"f(a / b, c / d, e / f)");
|
||||
|
||||
assert_eq!(format(modulo(f1(), f1())), "a % a");
|
||||
assert_eq!(format(modulo(f2(), f2())), "constant % constant");
|
||||
assert_eq!(format(modulo(f3(), f3())), "f(a) % f(a)");
|
||||
assert_eq!(format(modulo(f4(), f4())), "function(a, b, c) % function(a, b, c)");
|
||||
assert_eq!(format(function("f", vec![modulo(constant("a"), constant("b"))])), "f(a % b)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")), modulo(constant("e"), constant("f"))])),
|
||||
"f(a % b, c % d, e % f)");
|
||||
|
||||
assert_eq!(format(add(f1(), f1())), "a + a");
|
||||
assert_eq!(format(add(f2(), f2())), "constant + constant");
|
||||
assert_eq!(format(add(f3(), f3())), "f(a) + f(a)");
|
||||
assert_eq!(format(add(f4(), f4())), "function(a, b, c) + function(a, b, c)");
|
||||
assert_eq!(format(function("f", vec![add(constant("a"), constant("b"))])), "f(a + b)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")), add(constant("e"), constant("f"))])),
|
||||
"f(a + b, c + d, e + f)");
|
||||
|
||||
assert_eq!(format(subtract(f1(), f1())), "a - a");
|
||||
assert_eq!(format(subtract(f2(), f2())), "constant - constant");
|
||||
assert_eq!(format(subtract(f3(), f3())), "f(a) - f(a)");
|
||||
assert_eq!(format(subtract(f4(), f4())), "function(a, b, c) - function(a, b, c)");
|
||||
assert_eq!(format(function("f", vec![subtract(constant("a"), constant("b"))])), "f(a - b)");
|
||||
assert_eq!(format(
|
||||
function("f", vec![subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")), subtract(constant("e"), constant("f"))])),
|
||||
"f(a - b, c - d, e - f)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_absolute_value_and_lower()
|
||||
{
|
||||
assert_eq!(format(absolute_value(absolute_value(constant("a")))), "||a||");
|
||||
assert_eq!(format(absolute_value(negative(constant("a")))), "|-a|");
|
||||
assert_eq!(format(negative(absolute_value(constant("a")))), "-|a|");
|
||||
assert_eq!(format(absolute_value(add(constant("a"), constant("b")))), "|a + b|");
|
||||
assert_eq!(format(
|
||||
add(absolute_value(constant("a")), absolute_value(constant("b")))),
|
||||
"|a| + |b|");
|
||||
assert_eq!(format(absolute_value(subtract(constant("a"), constant("b")))), "|a - b|");
|
||||
assert_eq!(format(
|
||||
subtract(absolute_value(constant("a")), absolute_value(constant("b")))),
|
||||
"|a| - |b|");
|
||||
assert_eq!(format(absolute_value(multiply(constant("a"), constant("b")))), "|a * b|");
|
||||
assert_eq!(format(
|
||||
multiply(absolute_value(constant("a")), absolute_value(constant("b")))),
|
||||
"|a| * |b|");
|
||||
assert_eq!(format(absolute_value(divide(constant("a"), constant("b")))), "|a / b|");
|
||||
assert_eq!(format(
|
||||
divide(absolute_value(constant("a")), absolute_value(constant("b")))),
|
||||
"|a| / |b|");
|
||||
assert_eq!(format(absolute_value(modulo(constant("a"), constant("b")))), "|a % b|");
|
||||
assert_eq!(format(
|
||||
modulo(absolute_value(constant("a")), absolute_value(constant("b")))),
|
||||
"|a| % |b|");
|
||||
assert_eq!(format(absolute_value(exponentiate(constant("a"), constant("b")))), "|a ** b|");
|
||||
assert_eq!(format(
|
||||
exponentiate(absolute_value(constant("a")), absolute_value(constant("b")))),
|
||||
"|a| ** |b|");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_negative_and_lower()
|
||||
{
|
||||
assert_eq!(format(negative(negative(constant("a")))), "--a");
|
||||
assert_eq!(format(add(negative(constant("a")), negative(constant("b")))), "-a + -b");
|
||||
assert_eq!(format(negative(add(constant("a"), constant("b")))), "-(a + b)");
|
||||
assert_eq!(format(subtract(negative(constant("a")), negative(constant("b")))), "-a - -b");
|
||||
assert_eq!(format(negative(subtract(constant("a"), constant("b")))), "-(a - b)");
|
||||
assert_eq!(format(multiply(negative(constant("a")), negative(constant("b")))), "-a * -b");
|
||||
assert_eq!(format(negative(multiply(constant("a"), constant("b")))), "-(a * b)");
|
||||
assert_eq!(format(divide(negative(constant("a")), negative(constant("b")))), "-a / -b");
|
||||
assert_eq!(format(negative(divide(constant("a"), constant("b")))), "-(a / b)");
|
||||
assert_eq!(format(modulo(negative(constant("a")), negative(constant("b")))), "-a % -b");
|
||||
assert_eq!(format(negative(modulo(constant("a"), constant("b")))), "-(a % b)");
|
||||
assert_eq!(format(exponentiate(negative(constant("a")), negative(constant("b")))), "-a ** -b");
|
||||
assert_eq!(format(negative(exponentiate(constant("a"), constant("b")))), "-(a ** b)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_exponentiate_and_lower()
|
||||
{
|
||||
assert_eq!(format(
|
||||
exponentiate(
|
||||
exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")))),
|
||||
"(a ** b) ** c ** d");
|
||||
assert_eq!(format(
|
||||
exponentiate(
|
||||
multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")))),
|
||||
"(a * b) ** (c * d)");
|
||||
assert_eq!(format(
|
||||
multiply(
|
||||
exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")))),
|
||||
"a ** b * c ** d");
|
||||
assert_eq!(format(
|
||||
exponentiate(
|
||||
divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")))),
|
||||
"(a / b) ** (c / d)");
|
||||
assert_eq!(format(
|
||||
divide(
|
||||
exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")))),
|
||||
"a ** b / c ** d");
|
||||
assert_eq!(format(
|
||||
exponentiate(
|
||||
modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")))),
|
||||
"(a % b) ** (c % d)");
|
||||
assert_eq!(format(
|
||||
modulo(
|
||||
exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")))),
|
||||
"a ** b % c ** d");
|
||||
assert_eq!(format(
|
||||
exponentiate(
|
||||
add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")))),
|
||||
"(a + b) ** (c + d)");
|
||||
assert_eq!(format(
|
||||
add(
|
||||
exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")))),
|
||||
"a ** b + c ** d");
|
||||
assert_eq!(format(
|
||||
exponentiate(
|
||||
subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")))),
|
||||
"(a - b) ** (c - d)");
|
||||
assert_eq!(format(
|
||||
subtract(
|
||||
exponentiate(constant("a"), constant("b")),
|
||||
exponentiate(constant("c"), constant("d")))),
|
||||
"a ** b - c ** d");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_multiplicative_binary_operations_and_lower()
|
||||
{
|
||||
assert_eq!(format(
|
||||
multiply(
|
||||
multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")))),
|
||||
"a * b * c * d");
|
||||
assert_eq!(format(
|
||||
multiply(
|
||||
divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")))),
|
||||
"a / b * c / d");
|
||||
assert_eq!(format(
|
||||
multiply(
|
||||
modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")))),
|
||||
"a % b * (c % d)");
|
||||
assert_eq!(format(
|
||||
divide(
|
||||
multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")))),
|
||||
"a * b / (c * d)");
|
||||
assert_eq!(format(
|
||||
divide(
|
||||
divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")))),
|
||||
"a / b / (c / d)");
|
||||
assert_eq!(format(
|
||||
divide(
|
||||
modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")))),
|
||||
"a % b / (c % d)");
|
||||
assert_eq!(format(
|
||||
modulo(
|
||||
multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")))),
|
||||
"a * b % (c * d)");
|
||||
assert_eq!(format(
|
||||
modulo(
|
||||
divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")))),
|
||||
"a / b % (c / d)");
|
||||
assert_eq!(format(
|
||||
modulo(
|
||||
modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")))),
|
||||
"a % b % (c % d)");
|
||||
assert_eq!(format(
|
||||
multiply(
|
||||
add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")))),
|
||||
"(a + b) * (c + d)");
|
||||
assert_eq!(format(
|
||||
add(
|
||||
multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")))),
|
||||
"a * b + c * d");
|
||||
assert_eq!(format(
|
||||
multiply(
|
||||
subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")))),
|
||||
"(a - b) * (c - d)");
|
||||
assert_eq!(format(
|
||||
subtract(
|
||||
multiply(constant("a"), constant("b")),
|
||||
multiply(constant("c"), constant("d")))),
|
||||
"a * b - c * d");
|
||||
assert_eq!(format(
|
||||
divide(
|
||||
add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")))),
|
||||
"(a + b) / (c + d)");
|
||||
assert_eq!(format(
|
||||
add(
|
||||
divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")))),
|
||||
"a / b + c / d");
|
||||
assert_eq!(format(
|
||||
divide(
|
||||
subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")))),
|
||||
"(a - b) / (c - d)");
|
||||
assert_eq!(format(
|
||||
subtract(
|
||||
divide(constant("a"), constant("b")),
|
||||
divide(constant("c"), constant("d")))),
|
||||
"a / b - c / d");
|
||||
assert_eq!(format(
|
||||
modulo(
|
||||
add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")))),
|
||||
"(a + b) % (c + d)");
|
||||
assert_eq!(format(
|
||||
add(
|
||||
modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")))),
|
||||
"a % b + c % d");
|
||||
assert_eq!(format(
|
||||
modulo(
|
||||
subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")))),
|
||||
"(a - b) % (c - d)");
|
||||
assert_eq!(format(
|
||||
subtract(
|
||||
modulo(constant("a"), constant("b")),
|
||||
modulo(constant("c"), constant("d")))),
|
||||
"a % b - c % d");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_combination_additive_binary_operations_and_lower()
|
||||
{
|
||||
assert_eq!(format(
|
||||
add(
|
||||
add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")))),
|
||||
"a + b + c + d");
|
||||
assert_eq!(format(
|
||||
add(
|
||||
subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")))),
|
||||
"a - b + c - d");
|
||||
assert_eq!(format(
|
||||
subtract(
|
||||
add(constant("a"), constant("b")),
|
||||
add(constant("c"), constant("d")))),
|
||||
"a + b - (c + d)");
|
||||
assert_eq!(format(
|
||||
subtract(
|
||||
subtract(constant("a"), constant("b")),
|
||||
subtract(constant("c"), constant("d")))),
|
||||
"a - b - (c - d)");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user