use std::f32::consts::{E, PI};
use expy::{Context, eval, eval_in};
#[test]
fn increment() {
let x = 5;
let mut ctx = Context::empty();
ctx.set("x", x);
assert_eq!(eval_in(&mut ctx, "x + 1").unwrap().unwrap_integer(), x + 1);
}
#[test]
fn add_multiple_vars() {
let x = 7;
let y = 12;
let mut ctx = Context::empty();
ctx.set("x", x);
ctx.set("y", y);
assert_eq!(eval_in(&mut ctx, "x + y").unwrap().unwrap_integer(), x + y);
}
#[test]
fn power() {
assert_eq!(eval("2^16").unwrap().unwrap_integer(), 65536);
assert_eq!(eval("e^2.5").unwrap().unwrap_float(), E.powf(2.5));
}
#[test]
fn identity_function() {
assert_eq!(eval("id(true)").unwrap().unwrap_bool(), true);
assert_eq!(eval("id(false)").unwrap().unwrap_bool(), false);
assert_eq!(eval("id(42)").unwrap().unwrap_integer(), 42);
assert_eq!(eval("id(3.14)").unwrap().unwrap_float(), 3.14);
let mut ctx = Context::new();
ctx.set("x", true); assert_eq!(eval_in(&mut ctx, "id(x)").unwrap().unwrap_bool(), true);
ctx.set("x", false); assert_eq!(eval_in(&mut ctx, "id(x)").unwrap().unwrap_bool(), false);
ctx.set("x", 42); assert_eq!(eval_in(&mut ctx, "id(x)").unwrap().unwrap_integer(), 42);
ctx.set("x", 3.14); assert_eq!(eval_in(&mut ctx, "id(x)").unwrap().unwrap_float(), 3.14);
}
#[test]
fn absolute_value() {
assert_eq!(eval("abs(1)").unwrap().unwrap_integer(), 1);
assert_eq!(eval("abs(-4)").unwrap().unwrap_integer(), 4);
assert_eq!(eval("abs(-17.8)").unwrap().unwrap_float(), 17.8);
assert_eq!(eval("abs(1.234)").unwrap().unwrap_float(), 1.234);
}
#[test]
fn trig_functions() {
assert_eq!(eval("sin(0.)").unwrap().unwrap_float(), 0f32.sin());
assert_eq!(eval("cos(pi)").unwrap().unwrap_float(), PI.cos());
assert_eq!(eval("tan(pi/4)").unwrap().unwrap_float(), (PI/4.).tan());
}