2020-03-14 13:06:40 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
use rhai::FLOAT;
|
2017-11-24 08:56:22 +01:00
|
|
|
|
2020-03-16 06:08:53 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-11-01 08:48:48 +01:00
|
|
|
const EPSILON: FLOAT = 0.000_001;
|
2020-03-16 06:08:53 +01:00
|
|
|
|
2017-11-24 08:56:22 +01:00
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_power_of() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-24 08:56:22 +01:00
|
|
|
|
2021-02-10 05:41:27 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("2 ** 3")?, 8);
|
|
|
|
assert_eq!(engine.eval::<INT>("(-2 ** 3)")?, -8);
|
|
|
|
assert_eq!(engine.eval::<INT>("2 ** 3 ** 2")?, 512);
|
2020-03-10 12:48:47 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
2020-03-16 06:08:53 +01:00
|
|
|
assert!(
|
2021-02-10 05:41:27 +01:00
|
|
|
(engine.eval::<FLOAT>("2.2 ** 3.3")? - 13.489_468_760_533_386 as FLOAT).abs()
|
|
|
|
<= EPSILON
|
2020-03-10 16:06:20 +01:00
|
|
|
);
|
2021-02-10 05:41:27 +01:00
|
|
|
assert!((engine.eval::<FLOAT>("2.0**-2.0")? - 0.25 as FLOAT).abs() < EPSILON);
|
|
|
|
assert!((engine.eval::<FLOAT>("(-2.0**-2.0)")? - 0.25 as FLOAT).abs() < EPSILON);
|
|
|
|
assert!((engine.eval::<FLOAT>("(-2.0**-2)")? - 0.25 as FLOAT).abs() < EPSILON);
|
|
|
|
assert_eq!(engine.eval::<INT>("4**3")?, 64);
|
2020-03-10 12:48:47 +01:00
|
|
|
}
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2017-11-24 08:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_power_of_equals() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2017-11-24 08:56:22 +01:00
|
|
|
|
2021-02-10 05:41:27 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let x = 2; x **= 3; x")?, 8);
|
|
|
|
assert_eq!(engine.eval::<INT>("let x = -2; x **= 3; x")?, -8);
|
2020-03-10 12:48:47 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
2020-03-16 06:08:53 +01:00
|
|
|
assert!(
|
2021-02-10 05:41:27 +01:00
|
|
|
(engine.eval::<FLOAT>("let x = 2.2; x **= 3.3; x")? - 13.489_468_760_533_386 as FLOAT)
|
2020-03-24 09:57:35 +01:00
|
|
|
.abs()
|
2020-03-16 06:08:53 +01:00
|
|
|
<= EPSILON
|
2020-03-10 16:06:20 +01:00
|
|
|
);
|
2020-03-24 09:57:35 +01:00
|
|
|
assert!(
|
2021-02-10 05:41:27 +01:00
|
|
|
(engine.eval::<FLOAT>("let x = 2.0; x **= -2.0; x")? - 0.25 as FLOAT).abs() < EPSILON
|
2020-03-10 16:06:20 +01:00
|
|
|
);
|
2020-03-24 09:57:35 +01:00
|
|
|
assert!(
|
2021-02-10 05:41:27 +01:00
|
|
|
(engine.eval::<FLOAT>("let x = -2.0; x **= -2.0; x")? - 0.25 as FLOAT).abs() < EPSILON
|
2020-03-10 16:06:20 +01:00
|
|
|
);
|
2020-03-24 09:57:35 +01:00
|
|
|
assert!(
|
2021-02-10 05:41:27 +01:00
|
|
|
(engine.eval::<FLOAT>("let x = -2.0; x **= -2; x")? - 0.25 as FLOAT).abs() < EPSILON
|
2020-03-10 12:48:47 +01:00
|
|
|
);
|
2021-02-10 05:41:27 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let x =4; x **= 3; x")?, 64);
|
2020-03-10 12:48:47 +01:00
|
|
|
}
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2017-11-24 08:56:22 +01:00
|
|
|
}
|