rhai/tests/power_of.rs

58 lines
1.6 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, INT};
#[cfg(not(feature = "no_float"))]
use rhai::FLOAT;
2017-11-24 08:56:22 +01:00
#[test]
2020-03-02 15:11:56 +01:00
fn test_power_of() -> Result<(), EvalAltResult> {
2017-11-24 08:56:22 +01:00
let mut engine = Engine::new();
assert_eq!(engine.eval::<INT>("2 ~ 3")?, 8);
assert_eq!(engine.eval::<INT>("(-2 ~ 3)")?, -8);
#[cfg(not(feature = "no_float"))]
{
assert_eq!(
engine.eval::<FLOAT>("2.2 ~ 3.3")?,
2020-03-16 05:40:55 +01:00
13.489468760533388 as FLOAT
);
assert_eq!(engine.eval::<FLOAT>("2.0~-2.0")?, 0.25 as FLOAT);
assert_eq!(engine.eval::<FLOAT>("(-2.0~-2.0)")?, 0.25 as FLOAT);
assert_eq!(engine.eval::<FLOAT>("(-2.0~-2)")?, 0.25 as FLOAT);
assert_eq!(engine.eval::<INT>("4~3")?, 64);
}
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-24 08:56:22 +01:00
}
#[test]
2020-03-02 15:11:56 +01:00
fn test_power_of_equals() -> Result<(), EvalAltResult> {
2017-11-24 08:56:22 +01:00
let mut engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 2; x ~= 3; x")?, 8);
assert_eq!(engine.eval::<INT>("let x = -2; x ~= 3; x")?, -8);
#[cfg(not(feature = "no_float"))]
{
assert_eq!(
engine.eval::<FLOAT>("let x = 2.2; x ~= 3.3; x")?,
2020-03-16 05:40:55 +01:00
13.489468760533388 as FLOAT
);
assert_eq!(
engine.eval::<FLOAT>("let x = 2.0; x ~= -2.0; x")?,
0.25 as FLOAT
);
assert_eq!(
engine.eval::<FLOAT>("let x = -2.0; x ~= -2.0; x")?,
0.25 as FLOAT
);
assert_eq!(
engine.eval::<FLOAT>("let x = -2.0; x ~= -2; x")?,
0.25 as FLOAT
);
assert_eq!(engine.eval::<INT>("let x =4; x ~= 3; x")?, 64);
}
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-24 08:56:22 +01:00
}