2020-03-15 15:39:58 +01:00
|
|
|
#![cfg(not(feature = "no_optimize"))]
|
|
|
|
|
2021-03-15 04:36:30 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, OptimizationLevel, INT};
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2021-07-01 06:27:29 +02:00
|
|
|
#[test]
|
|
|
|
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
2021-07-01 06:33:16 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-07-01 06:27:29 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
fn foo(x) { print(x); return; }
|
|
|
|
fn foo2(x) { if x > 0 {} return; }
|
|
|
|
42
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
#[test]
|
2020-06-13 15:57:46 +02:00
|
|
|
fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
|
2020-04-21 17:25:12 +02:00
|
|
|
fn run_test(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
|
2021-04-20 06:01:35 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("if true { 42 } else { 123 }")?, 42);
|
2020-03-15 15:39:58 +01:00
|
|
|
assert_eq!(
|
2021-04-20 06:01:35 +02:00
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
2020-03-15 15:39:58 +01:00
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r#"const abc = "hello"; if abc < "foo" { 42 } else { 123 }"#)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::None);
|
|
|
|
run_test(&mut engine)?;
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
|
|
|
run_test(&mut engine)?;
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
run_test(&mut engine)?;
|
|
|
|
|
2020-10-08 04:34:32 +02:00
|
|
|
// Override == operator
|
|
|
|
engine.register_fn("==", |_x: INT, _y: INT| false);
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-04-20 06:01:35 +02:00
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
2020-10-08 04:34:32 +02:00
|
|
|
123
|
|
|
|
);
|
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-04-20 06:01:35 +02:00
|
|
|
engine.eval::<INT>("if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
2020-10-08 04:34:32 +02:00
|
|
|
123
|
|
|
|
);
|
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2021-04-06 17:28:22 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2021-04-22 17:02:25 +02:00
|
|
|
#[cfg(not(feature = "no_position"))]
|
2020-06-13 15:57:46 +02:00
|
|
|
#[test]
|
|
|
|
fn test_optimizer_parse() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Simple);
|
|
|
|
|
2020-12-08 15:20:29 +01:00
|
|
|
let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } else { 123 } }")?;
|
|
|
|
|
2021-04-06 17:18:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", ast),
|
2021-05-22 13:14:24 +02:00
|
|
|
"AST { source: None, body: Block[Expr(123 @ 1:53)], functions: Module, resolver: None }"
|
2021-04-06 17:18:41 +02:00
|
|
|
);
|
2021-03-13 16:43:05 +01:00
|
|
|
|
|
|
|
let ast = engine.compile("const DECISION = false; if DECISION { 42 } else { 123 }")?;
|
|
|
|
|
2021-04-06 17:18:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", ast),
|
2021-08-01 05:59:53 +02:00
|
|
|
r#"AST { source: None, body: Block[Var(false @ 1:18, "DECISION" @ 1:7, Const, false, 1:1), Expr(123 @ 1:51)], functions: Module, resolver: None }"#
|
2021-04-06 17:18:41 +02:00
|
|
|
);
|
2020-12-08 15:20:29 +01:00
|
|
|
|
|
|
|
let ast = engine.compile("if 1 == 2 { 42 }")?;
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2021-04-06 17:18:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", ast),
|
2021-05-22 13:14:24 +02:00
|
|
|
"AST { source: None, body: Block[], functions: Module, resolver: None }"
|
2021-04-06 17:18:41 +02:00
|
|
|
);
|
2020-06-13 15:57:46 +02:00
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
2020-12-08 15:20:29 +01:00
|
|
|
let ast = engine.compile("abs(-42)")?;
|
2020-06-13 15:57:46 +02:00
|
|
|
|
2021-04-06 17:18:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
format!("{:?}", ast),
|
2021-05-22 13:14:24 +02:00
|
|
|
"AST { source: None, body: Block[Expr(42 @ 1:1)], functions: Module, resolver: None }"
|
2021-04-06 17:18:41 +02:00
|
|
|
);
|
2020-06-13 15:57:46 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|