use super::*;
#[test]
fn test_sin_braced() {
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"),
}
}
#[test]
fn test_sin_parentheses() {
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"),
}
}
#[test]
fn test_sin_unbraced() {
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"),
}
}
#[test]
fn test_cos() {
let expr = parse_latex(r"\cos{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "cos");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_tan() {
let expr = parse_latex(r"\tan{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "tan");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_sec() {
let expr = parse_latex(r"\sec{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sec");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_csc() {
let expr = parse_latex(r"\csc{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "csc");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_cot() {
let expr = parse_latex(r"\cot{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "cot");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_arcsin() {
let expr = parse_latex(r"\arcsin{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "arcsin");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_arccos() {
let expr = parse_latex(r"\arccos{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "arccos");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_arctan() {
let expr = parse_latex(r"\arctan{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "arctan");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_sinh() {
let expr = parse_latex(r"\sinh{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sinh");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_cosh() {
let expr = parse_latex(r"\cosh{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "cosh");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_tanh() {
let expr = parse_latex(r"\tanh{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "tanh");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_ln() {
let expr = parse_latex(r"\ln{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "ln");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_log() {
let expr = parse_latex(r"\log{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "log");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_exp() {
let expr = parse_latex(r"\exp{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "exp");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_min() {
let expr = parse_latex(r"\min{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "min");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_max() {
let expr = parse_latex(r"\max{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "max");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_gcd() {
let expr = parse_latex(r"\gcd{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "gcd");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_lcm() {
let expr = parse_latex(r"\lcm{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "lcm");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_sin_of_addition() {
let expr = parse_latex(r"\sin(x + y)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sin");
assert_eq!(args.len(), 1);
assert!(matches!(
args[0].kind,
ExprKind::Binary {
op: BinaryOp::Add,
..
}
));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_cos_of_multiplication() {
let expr = parse_latex(r"\cos{2*x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "cos");
assert_eq!(args.len(), 1);
assert!(matches!(
args[0].kind,
ExprKind::Binary {
op: BinaryOp::Mul,
..
}
));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_tan_of_fraction() {
let expr = parse_latex(r"\tan{\frac{x}{2}}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "tan");
assert_eq!(args.len(), 1);
assert!(matches!(
args[0].kind,
ExprKind::Binary {
op: BinaryOp::Div,
..
}
));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_ln_of_power() {
let expr = parse_latex(r"\ln{x^2}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "ln");
assert_eq!(args.len(), 1);
assert!(matches!(
args[0].kind,
ExprKind::Binary {
op: BinaryOp::Pow,
..
}
));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_exp_of_negative() {
let expr = parse_latex(r"\exp{-x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "exp");
assert_eq!(args.len(), 1);
assert!(matches!(
args[0].kind,
ExprKind::Unary {
op: crate::ast::UnaryOp::Neg,
..
}
));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_sin_of_cos() {
let expr = parse_latex(r"\sin{\cos{x}}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sin");
assert_eq!(args.len(), 1);
match &args[0].kind {
ExprKind::Function {
name: inner_name,
args: inner_args,
} => {
assert_eq!(inner_name, "cos");
assert_eq!(inner_args.len(), 1);
assert_eq!(inner_args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected nested function"),
}
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_ln_of_exp() {
let expr = parse_latex(r"\ln{\exp{x}}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "ln");
assert_eq!(args.len(), 1);
match &args[0].kind {
ExprKind::Function {
name: inner_name, ..
} => {
assert_eq!(inner_name, "exp");
}
_ => panic!("Expected nested function"),
}
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_exp_of_ln() {
let expr = parse_latex(r"\exp{\ln{x}}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "exp");
assert_eq!(args.len(), 1);
match &args[0].kind {
ExprKind::Function {
name: inner_name, ..
} => {
assert_eq!(inner_name, "ln");
}
_ => panic!("Expected nested function"),
}
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_sin_plus_cos() {
let expr = parse_latex(r"\sin{x} + \cos{x}").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Add,
left,
right,
} => match (&left.kind, &right.kind) {
(ExprKind::Function { name: n1, .. }, ExprKind::Function { name: n2, .. }) => {
assert_eq!(n1, "sin");
assert_eq!(n2, "cos");
}
_ => panic!("Expected two functions"),
},
_ => panic!("Expected addition"),
}
}
#[test]
fn test_sin_times_cos() {
let expr = parse_latex(r"\sin{x} * \cos{x}").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => match (&left.kind, &right.kind) {
(ExprKind::Function { name: n1, .. }, ExprKind::Function { name: n2, .. }) => {
assert_eq!(n1, "sin");
assert_eq!(n2, "cos");
}
_ => panic!("Expected two functions"),
},
_ => panic!("Expected multiplication"),
}
}
#[test]
fn test_sin_squared() {
let expr = parse_latex(r"\sin{x}^2").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Pow,
left,
right,
} => {
match &left.kind {
ExprKind::Function { name, .. } => assert_eq!(name, "sin"),
_ => panic!("Expected function"),
}
assert_eq!(**right, Expression::integer(2));
}
_ => panic!("Expected power"),
}
}
#[test]
fn test_coefficient_times_sin() {
let expr = parse_latex(r"2 * \sin{x}").unwrap();
match &expr.kind {
ExprKind::Binary {
op: BinaryOp::Mul,
left,
right,
} => {
assert_eq!(**left, Expression::integer(2));
match &right.kind {
ExprKind::Function { name, .. } => assert_eq!(name, "sin"),
_ => panic!("Expected function"),
}
}
_ => panic!("Expected multiplication"),
}
}
#[test]
fn test_sin_of_alpha() {
let expr = parse_latex(r"\sin{\alpha}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sin");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("alpha".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_ln_of_pi() {
let expr = parse_latex(r"\ln{\pi}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "ln");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::constant(MathConstant::Pi));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_function_of_number() {
let expr = parse_latex(r"\sin{0}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "sin");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::integer(0));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_function_of_float() {
let expr = parse_latex(r"\cos{3.14}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "cos");
assert_eq!(args.len(), 1);
match &args[0].kind {
ExprKind::Float(f) => assert!((f.value() - 3.14).abs() < 1e-10),
_ => panic!("Expected float"),
}
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_det_braced() {
let expr = parse_latex(r"\det{A}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "det");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("A".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_trunc_braced() {
let expr = parse_latex(r"\trunc{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "trunc");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_trunc_paren() {
let expr = parse_latex(r"\trunc(x)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "trunc");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_trunc_nested_sin() {
let expr = parse_latex(r"\trunc{\sin{x}}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "trunc");
assert_eq!(args.len(), 1);
match &args[0].kind {
ExprKind::Function { name: inner, .. } => assert_eq!(inner, "sin"),
_ => panic!("Expected nested sin"),
}
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_rad_braced() {
let expr = parse_latex(r"\rad{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "rad");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_deg_braced() {
let expr = parse_latex(r"\deg{x}").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "deg");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Expression::variable("x".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_clamp_paren() {
let expr = parse_latex(r"\clamp(x, 0, 1)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "clamp");
assert_eq!(args.len(), 3);
assert_eq!(args[0], Expression::variable("x".to_string()));
assert_eq!(args[1], Expression::integer(0));
assert_eq!(args[2], Expression::integer(1));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_clamp_complex_arg() {
let expr = parse_latex(r"\clamp(x^2, -1, 1)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "clamp");
assert_eq!(args.len(), 3);
assert!(matches!(
args[0].kind,
ExprKind::Binary {
op: BinaryOp::Pow,
..
}
));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_lerp_paren() {
let expr = parse_latex(r"\lerp(a, b, t)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "lerp");
assert_eq!(args.len(), 3);
assert_eq!(args[0], Expression::variable("a".to_string()));
assert_eq!(args[1], Expression::variable("b".to_string()));
assert_eq!(args[2], Expression::variable("t".to_string()));
}
_ => panic!("Expected function call"),
}
}
#[test]
fn test_lerp_numeric_bounds() {
let expr = parse_latex(r"\lerp(0, 1, t)").unwrap();
match &expr.kind {
ExprKind::Function { name, args } => {
assert_eq!(name, "lerp");
assert_eq!(args.len(), 3);
assert_eq!(args[0], Expression::integer(0));
assert_eq!(args[1], Expression::integer(1));
assert_eq!(args[2], Expression::variable("t".to_string()));
}
_ => panic!("Expected function call"),
}
}