2020-03-10 16:06:20 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2020-03-08 15:47:13 +01:00
|
|
|
|
2020-08-13 08:57:46 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
use rhai::FLOAT;
|
|
|
|
|
2020-03-08 15:47:13 +01:00
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_math() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-08 15:47:13 +01:00
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("1 + 2")?, 3);
|
|
|
|
assert_eq!(engine.eval::<INT>("1 - 2")?, -1);
|
|
|
|
assert_eq!(engine.eval::<INT>("2 * 3")?, 6);
|
|
|
|
assert_eq!(engine.eval::<INT>("1 / 2")?, 0);
|
|
|
|
assert_eq!(engine.eval::<INT>("3 % 2")?, 1);
|
|
|
|
|
2020-08-13 08:57:46 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2021-02-10 05:10:50 +01:00
|
|
|
assert!((engine.eval::<FLOAT>("sin(PI()/6.0)")? - 0.5).abs() < 0.001);
|
2020-08-13 08:57:46 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
2021-02-10 05:10:50 +01:00
|
|
|
assert!(engine.eval::<FLOAT>("cos(PI()/2.0)")?.abs() < 0.001);
|
2020-08-13 08:57:46 +02:00
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2020-03-08 15:47:13 +01:00
|
|
|
assert_eq!(
|
2020-03-29 11:15:12 +02:00
|
|
|
engine.eval::<INT>("abs(-9223372036854775807)")?,
|
2020-03-24 09:57:35 +01:00
|
|
|
9_223_372_036_854_775_807
|
2020-03-08 15:47:13 +01:00
|
|
|
);
|
|
|
|
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
2020-03-29 11:15:12 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("abs(-2147483647)")?, 2147483647);
|
2020-03-10 16:06:20 +01:00
|
|
|
|
2020-03-08 15:47:13 +01:00
|
|
|
// Overflow/underflow/division-by-zero errors
|
2020-03-08 16:14:18 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
{
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
{
|
2020-03-14 16:40:30 +01:00
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-29 11:15:12 +02:00
|
|
|
.eval::<INT>("abs(-9223372036854775808)")
|
2020-03-14 16:40:30 +01:00
|
|
|
.expect_err("expects negation overflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("9223372036854775807 + 1")
|
|
|
|
.expect_err("expects overflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("-9223372036854775808 - 1")
|
|
|
|
.expect_err("expects underflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("9223372036854775807 * 9223372036854775807")
|
|
|
|
.expect_err("expects overflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("9223372036854775807 / 0")
|
|
|
|
.expect_err("expects division by zero"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("9223372036854775807 % 0")
|
|
|
|
.expect_err("expects division by zero"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
2020-03-08 16:14:18 +01:00
|
|
|
}
|
2020-03-10 16:06:20 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
{
|
2020-03-14 16:40:30 +01:00
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("2147483647 + 1")
|
|
|
|
.expect_err("expects overflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("-2147483648 - 1")
|
|
|
|
.expect_err("expects underflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("2147483647 * 2147483647")
|
|
|
|
.expect_err("expects overflow"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("2147483647 / 0")
|
|
|
|
.expect_err("expects division by zero"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
|
|
|
assert!(matches!(
|
2020-04-21 17:25:12 +02:00
|
|
|
*engine
|
2020-03-14 16:40:30 +01:00
|
|
|
.eval::<INT>("2147483647 % 0")
|
|
|
|
.expect_err("expects division by zero"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorArithmetic(..)
|
2020-03-14 16:40:30 +01:00
|
|
|
));
|
2020-03-08 16:14:18 +01:00
|
|
|
}
|
2020-03-08 15:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-23 06:00:03 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_math_parse() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>(r#"parse_int("42")"#)?, 42);
|
|
|
|
assert_eq!(engine.eval::<INT>(r#"parse_int("42", 16)"#)?, 0x42);
|
|
|
|
assert_eq!(engine.eval::<INT>(r#"parse_int("abcdef", 16)"#)?, 0xabcdef);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|