use yufmath::api::Yufmath;
use yufmath::core::{Expression, Number, BinaryOperator, UnaryOperator};
use num_bigint::BigInt;
#[test]
fn test_integrate_basic_expressions() {
let yuf = Yufmath::new();
let expr = Expression::Number(Number::Integer(BigInt::from(5)));
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::BinaryOp { op: BinaryOperator::Multiply, left, right } => {
match (left.as_ref(), right.as_ref()) {
(Expression::Number(Number::Integer(n)), Expression::Variable(var)) => {
assert_eq!(n, &BigInt::from(5));
assert_eq!(var, "x");
}
_ => panic!("期望得到 5x"),
}
}
_ => panic!("期望得到乘法表达式"),
}
let expr = Expression::Variable("x".to_string());
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::BinaryOp { op: BinaryOperator::Divide, .. } => {
}
_ => panic!("期望得到除法表达式"),
}
}
#[test]
fn test_integrate_polynomial() {
let yuf = Yufmath::new();
let expr = Expression::BinaryOp {
op: BinaryOperator::Power,
left: Box::new(Expression::Variable("x".to_string())),
right: Box::new(Expression::Number(Number::Integer(BigInt::from(2)))),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::BinaryOp { op: BinaryOperator::Divide, .. } => {
}
_ => panic!("期望得到除法表达式"),
}
}
#[test]
fn test_integrate_reciprocal() {
let yuf = Yufmath::new();
let expr = Expression::BinaryOp {
op: BinaryOperator::Power,
left: Box::new(Expression::Variable("x".to_string())),
right: Box::new(Expression::Number(Number::Integer(BigInt::from(-1)))),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::UnaryOp { op: UnaryOperator::Ln, operand } => {
match operand.as_ref() {
Expression::UnaryOp { op: UnaryOperator::Abs, operand: inner } => {
match inner.as_ref() {
Expression::Variable(var) => assert_eq!(var, "x"),
_ => panic!("期望得到变量 x"),
}
}
_ => panic!("期望得到绝对值"),
}
}
_ => panic!("期望得到 ln 表达式"),
}
}
#[test]
fn test_integrate_trigonometric() {
let yuf = Yufmath::new();
let expr = Expression::UnaryOp {
op: UnaryOperator::Sin,
operand: Box::new(Expression::Variable("x".to_string())),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::UnaryOp { op: UnaryOperator::Negate, operand } => {
match operand.as_ref() {
Expression::UnaryOp { op: UnaryOperator::Cos, operand: inner } => {
match inner.as_ref() {
Expression::Variable(var) => assert_eq!(var, "x"),
_ => panic!("期望得到变量 x"),
}
}
_ => panic!("期望得到 cos(x)"),
}
}
_ => panic!("期望得到负号表达式"),
}
let expr = Expression::UnaryOp {
op: UnaryOperator::Cos,
operand: Box::new(Expression::Variable("x".to_string())),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::UnaryOp { op: UnaryOperator::Sin, operand } => {
match operand.as_ref() {
Expression::Variable(var) => assert_eq!(var, "x"),
_ => panic!("期望得到变量 x"),
}
}
_ => panic!("期望得到 sin(x)"),
}
}
#[test]
fn test_integrate_exponential() {
let yuf = Yufmath::new();
let expr = Expression::UnaryOp {
op: UnaryOperator::Exp,
operand: Box::new(Expression::Variable("x".to_string())),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::UnaryOp { op: UnaryOperator::Exp, operand } => {
match operand.as_ref() {
Expression::Variable(var) => assert_eq!(var, "x"),
_ => panic!("期望得到变量 x"),
}
}
_ => panic!("期望得到 e^x"),
}
}
#[test]
fn test_integrate_sum() {
let yuf = Yufmath::new();
let expr = Expression::BinaryOp {
op: BinaryOperator::Add,
left: Box::new(Expression::Variable("x".to_string())),
right: Box::new(Expression::Number(Number::Integer(BigInt::from(3)))),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::BinaryOp { op: BinaryOperator::Add, .. } => {
}
_ => panic!("期望得到加法表达式"),
}
}
#[test]
fn test_integrate_constant_multiple() {
let yuf = Yufmath::new();
let expr = Expression::BinaryOp {
op: BinaryOperator::Multiply,
left: Box::new(Expression::Number(Number::Integer(BigInt::from(3)))),
right: Box::new(Expression::Variable("x".to_string())),
};
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::BinaryOp { op: BinaryOperator::Multiply, .. } => {
}
_ => panic!("期望得到乘法表达式"),
}
}
#[test]
fn test_integrate_other_variable() {
let yuf = Yufmath::new();
let expr = Expression::Variable("y".to_string());
let result = yuf.integrate(&expr, "x").unwrap();
match result {
Expression::BinaryOp { op: BinaryOperator::Multiply, left, right } => {
match (left.as_ref(), right.as_ref()) {
(Expression::Variable(var1), Expression::Variable(var2)) => {
assert_eq!(var1, "y");
assert_eq!(var2, "x");
}
_ => panic!("期望得到 yx"),
}
}
_ => panic!("期望得到乘法表达式"),
}
}