2020-03-15 15:39:58 +01:00
|
|
|
#![cfg(not(feature = "no_optimize"))]
|
|
|
|
|
|
|
|
use rhai::{Engine, EvalAltResult, OptimizationLevel, INT};
|
|
|
|
|
|
|
|
#[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>> {
|
2020-03-15 15:39:58 +01:00
|
|
|
assert_eq!(engine.eval::<INT>(r"if true { 42 } else { 123 }")?, 42);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(r"if 1 == 1 || 2 > 3 { 42 } else { 123 }")?,
|
|
|
|
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)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
let ast = engine.compile("{ const DECISION = false; if DECISION { 42 } }")?;
|
|
|
|
|
2020-06-26 17:24:35 +02:00
|
|
|
assert!(format!("{:?}", ast).starts_with("AST([], "));
|
2020-06-13 15:57:46 +02:00
|
|
|
|
|
|
|
engine.set_optimization_level(OptimizationLevel::Full);
|
|
|
|
|
|
|
|
let ast = engine.compile("if 1 == 2 { 42 }")?;
|
|
|
|
|
2020-06-26 17:24:35 +02:00
|
|
|
assert!(format!("{:?}", ast).starts_with("AST([], "));
|
2020-06-13 15:57:46 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|