use crate::ast::{BinaryOp, ExprKind, Expression, MathConstant};
use crate::parser::parse_latex;
#[test]
fn test_implicit_mult_number_letter() {
let expr = parse_latex("2x").unwrap();
match &expr.kind {
ExprKind::Binary { op, left, right } => {
assert_eq!(op, BinaryOp::Mul);
assert_eq!(*left, Expression::integer(2));
assert_eq!(*right, Expression::variable("x".to_string()));
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_letter_letter() {
let expr = parse_latex("xy").unwrap();
match &expr.kind {
ExprKind::Binary { op, left, right } => {
assert_eq!(op, BinaryOp::Mul);
assert_eq!(*left, Expression::variable("x".to_string()));
assert_eq!(*right, Expression::variable("y".to_string()));
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_number_paren() {
let expr = parse_latex("2(x+1)").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
assert_eq!(*left, Expression::integer(2));
match &right.kind {
ExprKind::Binary {
op: BinaryOp::Add, ..
} => {}
_ => panic!("Expected addition in right operand"),
}
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_number_pi() {
let expr = parse_latex(r"2\pi").unwrap();
match &expr.kind {
ExprKind::Binary { op, left, right } => {
assert_eq!(op, BinaryOp::Mul);
assert_eq!(*left, Expression::integer(2));
assert_eq!(*right, Expression::constant(MathConstant::Pi));
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_function_not_applied() {
let expr = parse_latex(r"\sin(x)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sin");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call, not implicit multiplication"),
}
}
#[test]
fn test_implicit_mult_var_paren() {
let expr = parse_latex("x(y)").unwrap();
match &expr.kind {
ExprKind::Binary { op, left, right } => {
assert_eq!(op, BinaryOp::Mul);
assert_eq!(*left, Expression::variable("x".to_string()));
assert_eq!(*right, Expression::variable("y".to_string()));
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_paren_paren() {
let expr = parse_latex("(a)(b)").unwrap();
match &expr.kind {
ExprKind::Binary { op, left, right } => {
assert_eq!(op, BinaryOp::Mul);
assert_eq!(*left, Expression::variable("a".to_string()));
assert_eq!(*right, Expression::variable("b".to_string()));
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_complex_expr() {
let expr = parse_latex("2xy").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
match &left.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left: ref ll,
right: ref lr,
} => {
assert_eq!(**ll, Expression::integer(2));
assert_eq!(**lr, Expression::variable("x".to_string()));
}
_ => panic!("Expected 2*x on left"),
}
assert_eq!(*right, Expression::variable("y".to_string()));
}
_ => panic!("Expected binary multiplication"),
}
}
#[test]
fn test_implicit_mult_with_addition() {
let expr = parse_latex("2x + 3y").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Add,
left,
right,
} => {
match &left.kind {
ExprKind::Binary {
op: BinaryOp::Mul, ..
} => {}
_ => panic!("Expected multiplication on left"),
}
match &right.kind {
ExprKind::Binary {
op: BinaryOp::Mul, ..
} => {}
_ => panic!("Expected multiplication on right"),
}
}
_ => panic!("Expected addition"),
}
}
#[test]
fn test_implicit_mult_with_power() {
let expr = parse_latex("2x^2").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
assert_eq!(*left, Expression::integer(2));
match &right.kind {
ExprKind::Binary {
op: BinaryOp::Pow,
left: ref pow_left,
right: ref pow_right,
} => {
assert_eq!(**pow_left, Expression::variable("x".to_string()));
assert_eq!(**pow_right, Expression::integer(2));
}
_ => panic!("Expected power on right"),
}
}
_ => panic!("Expected multiplication"),
}
}
#[test]
fn test_implicit_mult_brace() {
let expr = parse_latex("2{x+1}").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
assert_eq!(*left, Expression::integer(2));
match &right.kind {
ExprKind::Binary {
op: BinaryOp::Add, ..
} => {}
_ => panic!("Expected addition in braces"),
}
}
_ => panic!("Expected multiplication"),
}
}
#[test]
fn test_no_implicit_mult_with_explicit() {
let expr1 = parse_latex("2*x").unwrap();
let expr2 = parse_latex("2x").unwrap();
assert_eq!(expr1, expr2);
}
#[test]
fn test_implicit_mult_three_letters() {
let expr = parse_latex("abc").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
match &left.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left: ref ll,
right: ref lr,
} => {
assert_eq!(**ll, Expression::variable("a".to_string()));
assert_eq!(**lr, Expression::variable("b".to_string()));
}
_ => panic!("Expected a*b on left"),
}
assert_eq!(*right, Expression::variable("c".to_string()));
}
_ => panic!("Expected multiplication"),
}
}
#[test]
fn test_implicit_mult_complex() {
let expr = parse_latex("2x(y+1)").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
match &left.kind {
ExprKind::Binary {
op: BinaryOp::Mul, ..
} => {}
_ => panic!("Expected 2*x on left"),
}
match &right.kind {
ExprKind::Binary {
op: BinaryOp::Add, ..
} => {}
_ => panic!("Expected addition on right"),
}
}
_ => panic!("Expected multiplication"),
}
}