905 lines
30 KiB
Rust
905 lines
30 KiB
Rust
use super::*;
|
|
|
|
impl super::Precedence for crate::Term
|
|
{
|
|
fn precedence_level(&self) -> i32
|
|
{
|
|
match &self
|
|
{
|
|
Self::Boolean(_)
|
|
| Self::SpecialInteger(_)
|
|
| Self::Integer(_)
|
|
| Self::String(_)
|
|
| Self::Variable(_)
|
|
| Self::Function(_)
|
|
| Self::UnaryOperation(
|
|
crate::UnaryOperation{operator: crate::UnaryOperator::AbsoluteValue, ..})
|
|
=> 0,
|
|
Self::UnaryOperation(
|
|
crate::UnaryOperation{operator: crate::UnaryOperator::Negative, ..})
|
|
=> 1,
|
|
Self::BinaryOperation(
|
|
crate::BinaryOperation{operator: crate::BinaryOperator::Exponentiate, ..})
|
|
=> 2,
|
|
Self::BinaryOperation(
|
|
crate::BinaryOperation{operator: crate::BinaryOperator::Multiply, ..})
|
|
| Self::BinaryOperation(
|
|
crate::BinaryOperation{operator: crate::BinaryOperator::Divide, ..})
|
|
| Self::BinaryOperation(
|
|
crate::BinaryOperation{operator: crate::BinaryOperator::Modulo, ..})
|
|
=> 3,
|
|
Self::BinaryOperation(crate::BinaryOperation{operator: crate::BinaryOperator::Add, ..})
|
|
| Self::BinaryOperation(
|
|
crate::BinaryOperation{operator: crate::BinaryOperator::Subtract, ..})
|
|
=> 4,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::SpecialInteger
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
match &self
|
|
{
|
|
Self::Infimum => write!(format, "#inf"),
|
|
Self::Supremum => write!(format, "#sup"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::SpecialInteger
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", &self)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::FunctionDeclaration
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{}/{}", &self.name, self.arity)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::FunctionDeclaration
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", &self)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::VariableDeclaration
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{}", &self.name)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::VariableDeclaration
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", &self)
|
|
}
|
|
}
|
|
|
|
pub(crate) struct TermDisplay<'term>
|
|
{
|
|
parentheses: Parentheses,
|
|
term: &'term crate::Term,
|
|
}
|
|
|
|
pub(crate) fn display_term(term: &crate::Term, parentheses: Parentheses) -> TermDisplay
|
|
{
|
|
TermDisplay
|
|
{
|
|
parentheses,
|
|
term,
|
|
}
|
|
}
|
|
|
|
impl<'term> std::fmt::Debug for TermDisplay<'term>
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
let precedence_level = self.term.precedence_level();
|
|
let requires_parentheses = match self.parentheses
|
|
{
|
|
Parentheses::Required => true,
|
|
Parentheses::PrecedenceBased(parent_precedence_level)
|
|
=> precedence_level > parent_precedence_level,
|
|
Parentheses::None => false,
|
|
};
|
|
let parentheses = Parentheses::PrecedenceBased(precedence_level);
|
|
|
|
if requires_parentheses
|
|
{
|
|
write!(format, "(")?;
|
|
}
|
|
|
|
match &self.term
|
|
{
|
|
crate::Term::Boolean(true) => write!(format, "true"),
|
|
crate::Term::Boolean(false) => write!(format, "false"),
|
|
crate::Term::SpecialInteger(value) => write!(format, "{:?}", value),
|
|
crate::Term::Integer(value) => write!(format, "{}", value),
|
|
crate::Term::String(value) => write!(format, "\"{}\"",
|
|
value.replace("\\", "\\\\").replace("\n", "\\n").replace("\t", "\\t")),
|
|
crate::Term::Variable(variable) => write!(format, "{:?}", variable.declaration),
|
|
crate::Term::Function(function) =>
|
|
{
|
|
write!(format, "{}", function.declaration.name)?;
|
|
|
|
assert!(function.declaration.arity == function.arguments.len(),
|
|
"number of function arguments differs from declaration (expected {}, got {})",
|
|
function.declaration.arity, function.arguments.len());
|
|
|
|
if !function.arguments.is_empty()
|
|
{
|
|
write!(format, "(")?;
|
|
|
|
let mut separator = "";
|
|
|
|
for argument in &function.arguments
|
|
{
|
|
write!(format, "{}{:?}", separator,
|
|
display_term(&argument, Parentheses::None))?;
|
|
|
|
separator = ", ";
|
|
}
|
|
|
|
write!(format, ")")?;
|
|
}
|
|
|
|
Ok(())
|
|
},
|
|
crate::Term::BinaryOperation(binary_operation) =>
|
|
{
|
|
let operator_string = match binary_operation.operator
|
|
{
|
|
crate::BinaryOperator::Add => "+",
|
|
crate::BinaryOperator::Subtract => "-",
|
|
crate::BinaryOperator::Multiply => "*",
|
|
crate::BinaryOperator::Divide => "/",
|
|
crate::BinaryOperator::Modulo => "%",
|
|
crate::BinaryOperator::Exponentiate => "**",
|
|
};
|
|
|
|
let left_parentheses = match
|
|
binary_operation.left.precedence_level() == precedence_level
|
|
// Exponentiation is right-associative and thus requires parentheses when
|
|
// nested on the left side
|
|
&& binary_operation.operator == crate::BinaryOperator::Exponentiate
|
|
{
|
|
true => Parentheses::Required,
|
|
false => parentheses,
|
|
};
|
|
|
|
// The subtraction, division, and modulo operators require parentheses around the
|
|
// right argument even if it has the same precedence
|
|
let operator_requires_right_priority = match binary_operation.operator
|
|
{
|
|
crate::BinaryOperator::Subtract
|
|
| crate::BinaryOperator::Divide
|
|
| crate::BinaryOperator::Modulo
|
|
=> true,
|
|
_ => false,
|
|
};
|
|
|
|
// Additionally, modulo operations nested to the right of another multiplicative
|
|
// operation always require parentheses
|
|
let right_requires_priority = match *binary_operation.right
|
|
{
|
|
crate::Term::BinaryOperation(
|
|
crate::BinaryOperation{operator: crate::BinaryOperator::Modulo, ..})
|
|
=> true,
|
|
_ => false,
|
|
};
|
|
|
|
let right_parentheses = match
|
|
binary_operation.right.precedence_level() == precedence_level
|
|
&& (operator_requires_right_priority || right_requires_priority)
|
|
{
|
|
true => Parentheses::Required,
|
|
false => parentheses,
|
|
};
|
|
|
|
write!(format, "{:?} {} {:?}",
|
|
display_term(&binary_operation.left, left_parentheses),
|
|
operator_string, display_term(&binary_operation.right, right_parentheses))
|
|
},
|
|
crate::Term::UnaryOperation(
|
|
crate::UnaryOperation{operator: crate::UnaryOperator::Negative, argument})
|
|
=> write!(format, "-{:?}", display_term(argument, parentheses)),
|
|
crate::Term::UnaryOperation(
|
|
crate::UnaryOperation{operator: crate::UnaryOperator::AbsoluteValue, argument})
|
|
=> write!(format, "|{:?}|", display_term(argument, Parentheses::None)),
|
|
}?;
|
|
|
|
if requires_parentheses
|
|
{
|
|
write!(format, ")")?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<'term> std::fmt::Display for TermDisplay<'term>
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", self)
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for crate::Term
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{:?}", display_term(&self, Parentheses::None))
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for crate::Term
|
|
{
|
|
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
|
{
|
|
write!(format, "{}", display_term(&self, Parentheses::None))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) mod tests
|
|
{
|
|
use crate::*;
|
|
|
|
macro_rules! assert
|
|
{
|
|
($term:expr, $output:expr) =>
|
|
{
|
|
assert_eq!(format($term), $output);
|
|
};
|
|
}
|
|
|
|
fn format(term: Box<ast::Term>) -> String
|
|
{
|
|
format!("{}", term)
|
|
}
|
|
|
|
pub(crate) fn absolute_value(argument: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::absolute_value(argument))
|
|
}
|
|
|
|
pub(crate) fn add(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::add(left, right))
|
|
}
|
|
|
|
pub(crate) fn constant(name: &str) -> Box<Term>
|
|
{
|
|
Box::new(Term::function(function_declaration(name, 0), vec![]))
|
|
}
|
|
|
|
pub(crate) fn divide(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::divide(left, right))
|
|
}
|
|
|
|
pub(crate) fn exponentiate(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::exponentiate(left, right))
|
|
}
|
|
|
|
pub(crate) fn false_() -> Box<Term>
|
|
{
|
|
Box::new(Term::false_())
|
|
}
|
|
|
|
pub(crate) fn function(name: &str, arguments: Vec<Box<Term>>) -> Box<Term>
|
|
{
|
|
Box::new(Term::function(function_declaration(name, arguments.len()), arguments))
|
|
}
|
|
|
|
pub(crate) fn function_declaration(name: &str, arity: usize) -> std::rc::Rc<FunctionDeclaration>
|
|
{
|
|
std::rc::Rc::new(FunctionDeclaration::new(name.to_string(), arity))
|
|
}
|
|
|
|
pub(crate) fn infimum() -> Box<Term>
|
|
{
|
|
Box::new(Term::infimum())
|
|
}
|
|
|
|
pub(crate) fn integer(value: i32) -> Box<Term>
|
|
{
|
|
Box::new(Term::integer(value))
|
|
}
|
|
|
|
pub(crate) fn modulo(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::modulo(left, right))
|
|
}
|
|
|
|
pub(crate) fn multiply(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::multiply(left, right))
|
|
}
|
|
|
|
pub(crate) fn negative(argument: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::negative(argument))
|
|
}
|
|
|
|
pub(crate) fn subtract(left: Box<Term>, right: Box<Term>) -> Box<Term>
|
|
{
|
|
Box::new(Term::subtract(left, right))
|
|
}
|
|
|
|
pub(crate) fn supremum() -> Box<Term>
|
|
{
|
|
Box::new(Term::supremum())
|
|
}
|
|
|
|
pub(crate) fn string(value: &str) -> Box<Term>
|
|
{
|
|
Box::new(Term::string(value.to_string()))
|
|
}
|
|
|
|
pub(crate) fn true_() -> Box<Term>
|
|
{
|
|
Box::new(Term::true_())
|
|
}
|
|
|
|
pub(crate) fn variable(name: &str) -> Box<Term>
|
|
{
|
|
Box::new(Term::variable(variable_declaration(name)))
|
|
}
|
|
|
|
pub(crate) fn variable_declaration(name: &str) -> std::rc::Rc<VariableDeclaration>
|
|
{
|
|
std::rc::Rc::new(VariableDeclaration::new(name.to_string()))
|
|
}
|
|
|
|
pub(crate) fn a() -> Box<Term>
|
|
{
|
|
constant("a")
|
|
}
|
|
|
|
pub(crate) fn b() -> Box<Term>
|
|
{
|
|
constant("b")
|
|
}
|
|
|
|
pub(crate) fn c() -> Box<Term>
|
|
{
|
|
constant("c")
|
|
}
|
|
|
|
pub(crate) fn d() -> Box<Term>
|
|
{
|
|
constant("d")
|
|
}
|
|
|
|
pub(crate) fn e() -> Box<Term>
|
|
{
|
|
constant("e")
|
|
}
|
|
|
|
pub(crate) fn abc() -> Terms
|
|
{
|
|
vec![a(), b(), c()]
|
|
}
|
|
|
|
#[test]
|
|
fn format_binary_operation()
|
|
{
|
|
assert!(add(constant("a"), constant("b")), "a + b");
|
|
assert!(subtract(constant("a"), constant("b")), "a - b");
|
|
assert!(multiply(constant("a"), constant("b")), "a * b");
|
|
assert!(divide(constant("a"), constant("b")), "a / b");
|
|
assert!(modulo(constant("a"), constant("b")), "a % b");
|
|
assert!(exponentiate(constant("a"), constant("b")), "a ** b");
|
|
}
|
|
|
|
#[test]
|
|
fn format_boolean()
|
|
{
|
|
assert!(true_(), "true");
|
|
assert!(false_(), "false");
|
|
}
|
|
|
|
#[test]
|
|
fn format_special_integer()
|
|
{
|
|
assert!(infimum(), "#inf");
|
|
assert!(supremum(), "#sup");
|
|
}
|
|
|
|
#[test]
|
|
fn format_integer()
|
|
{
|
|
assert!(integer(0), "0");
|
|
assert!(integer(1), "1");
|
|
assert!(integer(10000), "10000");
|
|
assert!(integer(-1), "-1");
|
|
assert!(integer(-10000), "-10000");
|
|
}
|
|
|
|
#[test]
|
|
fn format_string()
|
|
{
|
|
assert!(string(""), "\"\"");
|
|
assert!(string(" "), "\" \"");
|
|
assert!(string(" "), "\" \"");
|
|
assert!(string("a"), "\"a\"");
|
|
assert!(string("test test"), "\"test test\"");
|
|
assert!(string("123 123"), "\"123 123\"");
|
|
assert!(string("\ntest\n123\n"), "\"\\ntest\\n123\\n\"");
|
|
assert!(string("\ttest\t123\t"), "\"\\ttest\\t123\\t\"");
|
|
assert!(string("\\test\\123\\"), "\"\\\\test\\\\123\\\\\"");
|
|
assert!(string("🙂test🙂123🙂"), "\"🙂test🙂123🙂\"");
|
|
}
|
|
|
|
#[test]
|
|
fn format_function()
|
|
{
|
|
assert!(constant("a"), "a");
|
|
assert!(constant("constant"), "constant");
|
|
assert!(function("f", vec![constant("a")]), "f(a)");
|
|
assert!(function("f", vec![constant("a"), constant("b"), constant("c")]), "f(a, b, c)");
|
|
assert!(function("function", vec![constant("a"), constant("b"), constant("c")]),
|
|
"function(a, b, c)");
|
|
|
|
assert!(function("function", vec![
|
|
exponentiate(absolute_value(multiply(constant("a"), integer(-20))), integer(2)),
|
|
string("test"),
|
|
function("f", vec![multiply(add(constant("b"), constant("c")),
|
|
subtract(constant("b"), constant("c"))), infimum(), variable("X")])]),
|
|
"function(|a * -20| ** 2, \"test\", f((b + c) * (b - c), #inf, X))");
|
|
|
|
// TODO: escape functions that start with capital letters or that conflict with keywords
|
|
}
|
|
|
|
#[test]
|
|
fn format_function_declaration()
|
|
{
|
|
assert_eq!(format!("{}", function_declaration("a", 0)), "a/0");
|
|
assert_eq!(format!("{}", function_declaration("constant", 0)), "constant/0");
|
|
assert_eq!(format!("{}", function_declaration("f", 1)), "f/1");
|
|
assert_eq!(format!("{}", function_declaration("f", 3)), "f/3");
|
|
assert_eq!(format!("{}", function_declaration("function", 3)), "function/3");
|
|
}
|
|
|
|
#[test]
|
|
fn format_variable()
|
|
{
|
|
assert!(variable("X"), "X");
|
|
assert!(variable("Variable"), "Variable");
|
|
}
|
|
|
|
#[test]
|
|
fn format_combination_boolean_and_lower()
|
|
{
|
|
assert!(function("f", vec![true_(), true_(), true_()]), "f(true, true, true)");
|
|
assert!(function("f", vec![false_(), false_(), false_()]), "f(false, false, false)");
|
|
|
|
assert!(absolute_value(true_()), "|true|");
|
|
assert!(absolute_value(false_()), "|false|");
|
|
|
|
assert!(negative(true_()), "-true");
|
|
assert!(negative(false_()), "-false");
|
|
|
|
assert!(exponentiate(true_(), true_()), "true ** true");
|
|
assert!(exponentiate(false_(), false_()), "false ** false");
|
|
|
|
assert!(multiply(true_(), true_()), "true * true");
|
|
assert!(multiply(false_(), false_()), "false * false");
|
|
|
|
assert!(divide(true_(), true_()), "true / true");
|
|
assert!(divide(false_(), false_()), "false / false");
|
|
|
|
assert!(modulo(true_(), true_()), "true % true");
|
|
assert!(modulo(false_(), false_()), "false % false");
|
|
|
|
assert!(add(true_(), true_()), "true + true");
|
|
assert!(add(false_(), false_()), "false + false");
|
|
|
|
assert!(subtract(true_(), true_()), "true - true");
|
|
assert!(subtract(false_(), false_()), "false - false");
|
|
}
|
|
|
|
#[test]
|
|
fn format_combination_special_integer_and_lower()
|
|
{
|
|
assert!(function("f", vec![infimum(), infimum(), infimum()]), "f(#inf, #inf, #inf)");
|
|
assert!(function("f", vec![supremum(), supremum(), supremum()]), "f(#sup, #sup, #sup)");
|
|
|
|
assert!(absolute_value(infimum()), "|#inf|");
|
|
assert!(absolute_value(supremum()), "|#sup|");
|
|
|
|
assert!(negative(infimum()), "-#inf");
|
|
assert!(negative(supremum()), "-#sup");
|
|
|
|
assert!(exponentiate(infimum(), infimum()), "#inf ** #inf");
|
|
assert!(exponentiate(supremum(), supremum()), "#sup ** #sup");
|
|
|
|
assert!(multiply(infimum(), infimum()), "#inf * #inf");
|
|
assert!(multiply(supremum(), supremum()), "#sup * #sup");
|
|
|
|
assert!(divide(infimum(), infimum()), "#inf / #inf");
|
|
assert!(divide(supremum(), supremum()), "#sup / #sup");
|
|
|
|
assert!(modulo(infimum(), infimum()), "#inf % #inf");
|
|
assert!(modulo(supremum(), supremum()), "#sup % #sup");
|
|
|
|
assert!(add(infimum(), infimum()), "#inf + #inf");
|
|
assert!(add(supremum(), supremum()), "#sup + #sup");
|
|
|
|
assert!(subtract(infimum(), infimum()), "#inf - #inf");
|
|
assert!(subtract(supremum(), supremum()), "#sup - #sup");
|
|
}
|
|
|
|
#[test]
|
|
fn format_combination_integer_and_lower()
|
|
{
|
|
assert!(function("f", vec![integer(0), integer(0), integer(0)]), "f(0, 0, 0)");
|
|
assert!(function("f", vec![integer(10000), integer(10000), integer(10000)]),
|
|
"f(10000, 10000, 10000)");
|
|
assert!(function("f", vec![integer(-10000), integer(-10000), integer(-10000)]),
|
|
"f(-10000, -10000, -10000)");
|
|
|
|
assert!(absolute_value(integer(0)), "|0|");
|
|
assert!(absolute_value(integer(10000)), "|10000|");
|
|
assert!(absolute_value(integer(-10000)), "|-10000|");
|
|
|
|
assert!(negative(integer(0)), "-0");
|
|
assert!(negative(integer(10000)), "-10000");
|
|
assert!(negative(integer(-10000)), "--10000");
|
|
|
|
assert!(exponentiate(integer(0), integer(0)), "0 ** 0");
|
|
assert!(exponentiate(integer(10000), integer(10000)), "10000 ** 10000");
|
|
assert!(exponentiate(integer(-10000), integer(-10000)), "-10000 ** -10000");
|
|
|
|
assert!(multiply(integer(0), integer(0)), "0 * 0");
|
|
assert!(multiply(integer(10000), integer(10000)), "10000 * 10000");
|
|
assert!(multiply(integer(-10000), integer(-10000)), "-10000 * -10000");
|
|
|
|
assert!(divide(integer(0), integer(0)), "0 / 0");
|
|
assert!(divide(integer(10000), integer(10000)), "10000 / 10000");
|
|
assert!(divide(integer(-10000), integer(-10000)), "-10000 / -10000");
|
|
|
|
assert!(modulo(integer(0), integer(0)), "0 % 0");
|
|
assert!(modulo(integer(10000), integer(10000)), "10000 % 10000");
|
|
assert!(modulo(integer(-10000), integer(-10000)), "-10000 % -10000");
|
|
|
|
assert!(add(integer(0), integer(0)), "0 + 0");
|
|
assert!(add(integer(10000), integer(10000)), "10000 + 10000");
|
|
assert!(add(integer(-10000), integer(-10000)), "-10000 + -10000");
|
|
|
|
assert!(subtract(integer(0), integer(0)), "0 - 0");
|
|
assert!(subtract(integer(10000), integer(10000)), "10000 - 10000");
|
|
assert!(subtract(integer(-10000), integer(-10000)), "-10000 - -10000");
|
|
}
|
|
|
|
#[test]
|
|
fn format_combination_string_and_lower()
|
|
{
|
|
assert!(function("f", vec![string(""), string(""), string("")]), "f(\"\", \"\", \"\")");
|
|
assert!(function("f", vec![string("test 123"), string("test 123"), string("test 123")]),
|
|
"f(\"test 123\", \"test 123\", \"test 123\")");
|
|
assert!(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!(absolute_value(string("")), "|\"\"|");
|
|
assert!(absolute_value(string("test 123")), "|\"test 123\"|");
|
|
assert!(absolute_value(string("\\a\nb🙂c\t")), "|\"\\\\a\\nb🙂c\\t\"|");
|
|
|
|
assert!(negative(string("")), "-\"\"");
|
|
assert!(negative(string("test 123")), "-\"test 123\"");
|
|
assert!(negative(string("\\a\nb🙂c\t")), "-\"\\\\a\\nb🙂c\\t\"");
|
|
|
|
assert!(exponentiate(string(""), string("")), "\"\" ** \"\"");
|
|
assert!(exponentiate(string("test 123"), string("test 123")),
|
|
"\"test 123\" ** \"test 123\"");
|
|
assert!(exponentiate(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t")),
|
|
"\"\\\\a\\nb🙂c\\t\" ** \"\\\\a\\nb🙂c\\t\"");
|
|
|
|
assert!(multiply(string(""), string("")), "\"\" * \"\"");
|
|
assert!(multiply(string("test 123"), string("test 123")), "\"test 123\" * \"test 123\"");
|
|
assert!(multiply(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t")),
|
|
"\"\\\\a\\nb🙂c\\t\" * \"\\\\a\\nb🙂c\\t\"");
|
|
|
|
assert!(divide(string(""), string("")), "\"\" / \"\"");
|
|
assert!(divide(string("test 123"), string("test 123")), "\"test 123\" / \"test 123\"");
|
|
assert!(divide(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t")),
|
|
"\"\\\\a\\nb🙂c\\t\" / \"\\\\a\\nb🙂c\\t\"");
|
|
|
|
assert!(modulo(string(""), string("")), "\"\" % \"\"");
|
|
assert!(modulo(string("test 123"), string("test 123")), "\"test 123\" % \"test 123\"");
|
|
assert!(modulo(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t")),
|
|
"\"\\\\a\\nb🙂c\\t\" % \"\\\\a\\nb🙂c\\t\"");
|
|
|
|
assert!(add(string(""), string("")), "\"\" + \"\"");
|
|
assert!(add(string("test 123"), string("test 123")), "\"test 123\" + \"test 123\"");
|
|
assert!(add(string("\\a\nb🙂c\t"), string("\\a\nb🙂c\t")),
|
|
"\"\\\\a\\nb🙂c\\t\" + \"\\\\a\\nb🙂c\\t\"");
|
|
|
|
assert!(subtract(string(""), string("")), "\"\" - \"\"");
|
|
assert!(subtract(string("test 123"), string("test 123")), "\"test 123\" - \"test 123\"");
|
|
assert!(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!(variable("X"), "X");
|
|
assert!(variable("Variable"), "Variable");
|
|
|
|
assert!(absolute_value(variable("X")), "|X|");
|
|
assert!(absolute_value(variable("Variable")), "|Variable|");
|
|
|
|
assert!(negative(variable("X")), "-X");
|
|
assert!(negative(variable("Variable")), "-Variable");
|
|
|
|
assert!(exponentiate(variable("X"), variable("X")), "X ** X");
|
|
assert!(exponentiate(variable("Variable"), variable("Variable")), "Variable ** Variable");
|
|
|
|
assert!(multiply(variable("X"), variable("X")), "X * X");
|
|
assert!(multiply(variable("Variable"), variable("Variable")), "Variable * Variable");
|
|
|
|
assert!(divide(variable("X"), variable("X")), "X / X");
|
|
assert!(divide(variable("Variable"), variable("Variable")), "Variable / Variable");
|
|
|
|
assert!(modulo(variable("X"), variable("X")), "X % X");
|
|
assert!(modulo(variable("Variable"), variable("Variable")), "Variable % Variable");
|
|
|
|
assert!(add(variable("X"), variable("X")), "X + X");
|
|
assert!(add(variable("Variable"), variable("Variable")), "Variable + Variable");
|
|
|
|
assert!(subtract(variable("X"), variable("X")), "X - X");
|
|
assert!(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!(absolute_value(f1()), "|a|");
|
|
assert!(absolute_value(f2()), "|constant|");
|
|
assert!(absolute_value(f3()), "|f(a)|");
|
|
assert!(absolute_value(f4()), "|function(a, b, c)|");
|
|
assert!(function("f", vec![absolute_value(constant("a"))]), "f(|a|)");
|
|
assert!(function("f", vec![absolute_value(constant("a")), absolute_value(constant("b")),
|
|
absolute_value(constant("c"))]),
|
|
"f(|a|, |b|, |c|)");
|
|
|
|
assert!(negative(f1()), "-a");
|
|
assert!(negative(f2()), "-constant");
|
|
assert!(negative(f3()), "-f(a)");
|
|
assert!(negative(f4()), "-function(a, b, c)");
|
|
assert!(function("f", vec![negative(constant("a"))]), "f(-a)");
|
|
assert!(function("f", vec![negative(constant("a")), negative(constant("b")),
|
|
negative(constant("c"))]),
|
|
"f(-a, -b, -c)");
|
|
|
|
assert!(exponentiate(f1(), f1()), "a ** a");
|
|
assert!(exponentiate(f2(), f2()), "constant ** constant");
|
|
assert!(exponentiate(f3(), f3()), "f(a) ** f(a)");
|
|
assert!(exponentiate(f4(), f4()), "function(a, b, c) ** function(a, b, c)");
|
|
assert!(function("f", vec![exponentiate(constant("a"), constant("b"))]), "f(a ** b)");
|
|
assert!(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!(multiply(f1(), f1()), "a * a");
|
|
assert!(multiply(f2(), f2()), "constant * constant");
|
|
assert!(multiply(f3(), f3()), "f(a) * f(a)");
|
|
assert!(multiply(f4(), f4()), "function(a, b, c) * function(a, b, c)");
|
|
assert!(function("f", vec![multiply(constant("a"), constant("b"))]), "f(a * b)");
|
|
assert!(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!(divide(f1(), f1()), "a / a");
|
|
assert!(divide(f2(), f2()), "constant / constant");
|
|
assert!(divide(f3(), f3()), "f(a) / f(a)");
|
|
assert!(divide(f4(), f4()), "function(a, b, c) / function(a, b, c)");
|
|
assert!(function("f", vec![divide(constant("a"), constant("b"))]), "f(a / b)");
|
|
assert!(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!(modulo(f1(), f1()), "a % a");
|
|
assert!(modulo(f2(), f2()), "constant % constant");
|
|
assert!(modulo(f3(), f3()), "f(a) % f(a)");
|
|
assert!(modulo(f4(), f4()), "function(a, b, c) % function(a, b, c)");
|
|
assert!(function("f", vec![modulo(constant("a"), constant("b"))]), "f(a % b)");
|
|
assert!(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!(add(f1(), f1()), "a + a");
|
|
assert!(add(f2(), f2()), "constant + constant");
|
|
assert!(add(f3(), f3()), "f(a) + f(a)");
|
|
assert!(add(f4(), f4()), "function(a, b, c) + function(a, b, c)");
|
|
assert!(function("f", vec![add(constant("a"), constant("b"))]), "f(a + b)");
|
|
assert!(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!(subtract(f1(), f1()), "a - a");
|
|
assert!(subtract(f2(), f2()), "constant - constant");
|
|
assert!(subtract(f3(), f3()), "f(a) - f(a)");
|
|
assert!(subtract(f4(), f4()), "function(a, b, c) - function(a, b, c)");
|
|
assert!(function("f", vec![subtract(constant("a"), constant("b"))]), "f(a - b)");
|
|
assert!(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!(absolute_value(absolute_value(constant("a"))), "||a||");
|
|
assert!(absolute_value(negative(constant("a"))), "|-a|");
|
|
assert!(negative(absolute_value(constant("a"))), "-|a|");
|
|
assert!(absolute_value(add(constant("a"), constant("b"))), "|a + b|");
|
|
assert!(add(absolute_value(constant("a")), absolute_value(constant("b"))), "|a| + |b|");
|
|
assert!(absolute_value(subtract(constant("a"), constant("b"))), "|a - b|");
|
|
assert!(subtract(absolute_value(constant("a")), absolute_value(constant("b"))),
|
|
"|a| - |b|");
|
|
assert!(absolute_value(multiply(constant("a"), constant("b"))), "|a * b|");
|
|
assert!(multiply(absolute_value(constant("a")), absolute_value(constant("b"))),
|
|
"|a| * |b|");
|
|
assert!(absolute_value(divide(constant("a"), constant("b"))), "|a / b|");
|
|
assert!(
|
|
divide(absolute_value(constant("a")), absolute_value(constant("b"))), "|a| / |b|");
|
|
assert!(absolute_value(modulo(constant("a"), constant("b"))), "|a % b|");
|
|
assert!(modulo(absolute_value(constant("a")), absolute_value(constant("b"))), "|a| % |b|");
|
|
assert!(absolute_value(exponentiate(constant("a"), constant("b"))), "|a ** b|");
|
|
assert!(exponentiate(absolute_value(constant("a")), absolute_value(constant("b"))),
|
|
"|a| ** |b|");
|
|
}
|
|
|
|
#[test]
|
|
fn format_combination_negative_and_lower()
|
|
{
|
|
assert!(negative(negative(constant("a"))), "--a");
|
|
assert!(add(negative(constant("a")), negative(constant("b"))), "-a + -b");
|
|
assert!(negative(add(constant("a"), constant("b"))), "-(a + b)");
|
|
assert!(subtract(negative(constant("a")), negative(constant("b"))), "-a - -b");
|
|
assert!(negative(subtract(constant("a"), constant("b"))), "-(a - b)");
|
|
assert!(multiply(negative(constant("a")), negative(constant("b"))), "-a * -b");
|
|
assert!(negative(multiply(constant("a"), constant("b"))), "-(a * b)");
|
|
assert!(divide(negative(constant("a")), negative(constant("b"))), "-a / -b");
|
|
assert!(negative(divide(constant("a"), constant("b"))), "-(a / b)");
|
|
assert!(modulo(negative(constant("a")), negative(constant("b"))), "-a % -b");
|
|
assert!(negative(modulo(constant("a"), constant("b"))), "-(a % b)");
|
|
assert!(exponentiate(negative(constant("a")), negative(constant("b"))), "-a ** -b");
|
|
assert!(negative(exponentiate(constant("a"), constant("b"))), "-(a ** b)");
|
|
}
|
|
|
|
#[test]
|
|
fn format_combination_exponentiate_and_lower()
|
|
{
|
|
assert!(exponentiate(exponentiate(constant("a"), constant("b")),
|
|
exponentiate(constant("c"), constant("d"))),
|
|
"(a ** b) ** c ** d");
|
|
assert!(exponentiate(multiply(constant("a"), constant("b")),
|
|
multiply(constant("c"), constant("d"))),
|
|
"(a * b) ** (c * d)");
|
|
assert!(multiply(exponentiate(constant("a"), constant("b")),
|
|
exponentiate(constant("c"), constant("d"))),
|
|
"a ** b * c ** d");
|
|
assert!(exponentiate(divide(constant("a"), constant("b")),
|
|
divide(constant("c"), constant("d"))),
|
|
"(a / b) ** (c / d)");
|
|
assert!(divide(exponentiate(constant("a"), constant("b")),
|
|
exponentiate(constant("c"), constant("d"))),
|
|
"a ** b / c ** d");
|
|
assert!(exponentiate(modulo(constant("a"), constant("b")),
|
|
modulo(constant("c"), constant("d"))),
|
|
"(a % b) ** (c % d)");
|
|
assert!(modulo(exponentiate(constant("a"), constant("b")),
|
|
exponentiate(constant("c"), constant("d"))),
|
|
"a ** b % c ** d");
|
|
assert!(exponentiate(add(constant("a"), constant("b")), add(constant("c"), constant("d"))),
|
|
"(a + b) ** (c + d)");
|
|
assert!(add(exponentiate(constant("a"), constant("b")),
|
|
exponentiate(constant("c"), constant("d"))),
|
|
"a ** b + c ** d");
|
|
assert!(exponentiate(subtract(constant("a"), constant("b")),
|
|
subtract(constant("c"), constant("d"))),
|
|
"(a - b) ** (c - d)");
|
|
assert!(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!(multiply(multiply(constant("a"), constant("b")),
|
|
multiply(constant("c"), constant("d"))),
|
|
"a * b * c * d");
|
|
assert!(multiply(divide(constant("a"), constant("b")),
|
|
divide(constant("c"), constant("d"))),
|
|
"a / b * c / d");
|
|
assert!(multiply(modulo(constant("a"), constant("b")),
|
|
modulo(constant("c"), constant("d"))),
|
|
"a % b * (c % d)");
|
|
assert!(divide(multiply(constant("a"), constant("b")),
|
|
multiply(constant("c"), constant("d"))),
|
|
"a * b / (c * d)");
|
|
assert!(divide(divide(constant("a"), constant("b")), divide(constant("c"), constant("d"))),
|
|
"a / b / (c / d)");
|
|
assert!(divide(modulo(constant("a"), constant("b")), modulo(constant("c"), constant("d"))),
|
|
"a % b / (c % d)");
|
|
assert!(modulo(multiply(constant("a"), constant("b")),
|
|
multiply(constant("c"), constant("d"))),
|
|
"a * b % (c * d)");
|
|
assert!(modulo(divide(constant("a"), constant("b")), divide(constant("c"), constant("d"))),
|
|
"a / b % (c / d)");
|
|
assert!(modulo(modulo(constant("a"), constant("b")), modulo(constant("c"), constant("d"))),
|
|
"a % b % (c % d)");
|
|
assert!(multiply(add(constant("a"), constant("b")), add(constant("c"), constant("d"))),
|
|
"(a + b) * (c + d)");
|
|
assert!(add(multiply(constant("a"), constant("b")), multiply(constant("c"), constant("d"))),
|
|
"a * b + c * d");
|
|
assert!(multiply(subtract(constant("a"), constant("b")),
|
|
subtract(constant("c"), constant("d"))),
|
|
"(a - b) * (c - d)");
|
|
assert!(subtract(multiply(constant("a"), constant("b")),
|
|
multiply(constant("c"), constant("d"))),
|
|
"a * b - c * d");
|
|
assert!(divide(add(constant("a"), constant("b")), add(constant("c"), constant("d"))),
|
|
"(a + b) / (c + d)");
|
|
assert!(add(divide(constant("a"), constant("b")), divide(constant("c"), constant("d"))),
|
|
"a / b + c / d");
|
|
assert!(divide(subtract(constant("a"), constant("b")),
|
|
subtract(constant("c"), constant("d"))),
|
|
"(a - b) / (c - d)");
|
|
assert!(subtract(divide(constant("a"), constant("b")),
|
|
divide(constant("c"), constant("d"))),
|
|
"a / b - c / d");
|
|
assert!(modulo(add(constant("a"), constant("b")), add(constant("c"), constant("d"))),
|
|
"(a + b) % (c + d)");
|
|
assert!(add(modulo(constant("a"), constant("b")), modulo(constant("c"), constant("d"))),
|
|
"a % b + c % d");
|
|
assert!(modulo(subtract(constant("a"), constant("b")),
|
|
subtract(constant("c"), constant("d"))),
|
|
"(a - b) % (c - d)");
|
|
assert!(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!(add(add(constant("a"), constant("b")), add(constant("c"), constant("d"))),
|
|
"a + b + c + d");
|
|
assert!(add(subtract(constant("a"), constant("b")), subtract(constant("c"), constant("d"))),
|
|
"a - b + c - d");
|
|
assert!(subtract(add(constant("a"), constant("b")), add(constant("c"), constant("d"))),
|
|
"a + b - (c + d)");
|
|
assert!(subtract(subtract(constant("a"), constant("b")),
|
|
subtract(constant("c"), constant("d"))),
|
|
"a - b - (c - d)");
|
|
}
|
|
}
|