rhai/tests/operations.rs

169 lines
3.9 KiB
Rust
Raw Normal View History

#![cfg(not(feature = "unchecked"))]
2020-11-02 05:50:27 +01:00
use rhai::{Engine, EvalAltResult, INT};
#[test]
fn test_max_operations() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
2021-12-15 05:06:17 +01:00
#[cfg(not(feature = "no_optimize"))]
engine.set_optimization_level(rhai::OptimizationLevel::None);
engine.set_max_operations(500);
2020-12-12 03:10:27 +01:00
engine.on_progress(|count| {
if count % 100 == 0 {
2022-09-25 06:24:03 +02:00
println!("{count}");
}
2020-11-02 05:50:27 +01:00
None
});
2022-01-10 06:26:33 +01:00
engine.run("let x = 0; while x < 20 { x += 1; }")?;
assert!(matches!(
2023-04-10 17:23:59 +02:00
*engine.run("for x in 0..500 {}").unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorTooManyOperations(..)
));
engine.set_max_operations(0);
2022-01-10 06:26:33 +01:00
engine.run("for x in 0..10000 {}")?;
Ok(())
}
2022-01-10 06:29:22 +01:00
#[test]
fn test_max_operations_literal() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
#[cfg(not(feature = "no_optimize"))]
engine.set_optimization_level(rhai::OptimizationLevel::None);
engine.set_max_operations(10);
#[cfg(not(feature = "no_index"))]
engine.run("[1, 2, 3, 4, 5, 6, 7]")?;
#[cfg(not(feature = "no_index"))]
assert!(matches!(
2023-04-10 17:23:59 +02:00
*engine.run("[1, 2, 3, 4, 5, 6, 7, 8, 9]").unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorTooManyOperations(..)
2022-01-10 06:29:22 +01:00
));
#[cfg(not(feature = "no_object"))]
engine.run("#{a:1, b:2, c:3, d:4, e:5, f:6, g:7}")?;
#[cfg(not(feature = "no_object"))]
assert!(matches!(
*engine
.run("#{a:1, b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9}")
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorTooManyOperations(..)
2022-01-10 06:29:22 +01:00
));
Ok(())
}
#[test]
fn test_max_operations_functions() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_max_operations(500);
2020-12-12 03:10:27 +01:00
engine.on_progress(|count| {
if count % 100 == 0 {
2022-09-25 06:24:03 +02:00
println!("{count}");
}
2020-11-02 05:50:27 +01:00
None
});
2022-01-10 06:26:33 +01:00
engine.run(
r#"
2021-03-27 11:08:34 +01:00
print("Test1");
let x = 0;
while x < 28 {
print(x);
x += 1;
}
"#,
2020-05-17 16:19:49 +02:00
)?;
#[cfg(not(feature = "no_function"))]
2022-01-10 06:26:33 +01:00
engine.run(
2020-05-17 16:19:49 +02:00
r#"
2021-03-27 11:08:34 +01:00
print("Test2");
fn inc(x) { x + 1 }
let x = 0;
while x < 20 { x = inc(x); }
"#,
)?;
2020-05-17 16:19:49 +02:00
#[cfg(not(feature = "no_function"))]
assert!(matches!(
*engine
2022-01-10 06:26:33 +01:00
.run(
r#"
2020-05-17 16:19:49 +02:00
print("Test3");
fn inc(x) { x + 1 }
let x = 0;
2020-05-17 16:19:49 +02:00
2021-04-21 12:16:24 +02:00
while x < 36 {
2020-05-17 16:19:49 +02:00
print(x);
x = inc(x);
}
2021-03-27 11:08:34 +01:00
"#,
)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorTooManyOperations(..)
));
Ok(())
}
#[test]
fn test_max_operations_eval() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_max_operations(500);
2020-12-12 03:10:27 +01:00
engine.on_progress(|count| {
if count % 100 == 0 {
2022-09-25 06:24:03 +02:00
println!("{count}");
}
2020-11-02 05:50:27 +01:00
None
});
assert!(matches!(
*engine
2022-01-10 06:26:33 +01:00
.run(
r#"
2021-12-15 05:06:17 +01:00
let script = "for x in 0..500 {}";
eval(script);
2021-03-27 11:08:34 +01:00
"#
)
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:46:14 +01:00
EvalAltResult::ErrorInFunctionCall(.., err, _) if matches!(*err, EvalAltResult::ErrorTooManyOperations(..))
));
Ok(())
}
2020-06-14 08:25:47 +02:00
#[test]
fn test_max_operations_progress() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
2021-12-15 05:06:17 +01:00
#[cfg(not(feature = "no_optimize"))]
engine.set_optimization_level(rhai::OptimizationLevel::None);
2020-06-14 08:25:47 +02:00
engine.set_max_operations(500);
2020-12-12 03:10:27 +01:00
engine.on_progress(|count| {
2020-11-02 05:50:27 +01:00
if count < 100 {
None
} else {
Some((42 as INT).into())
}
});
2020-06-14 08:25:47 +02:00
assert!(matches!(
*engine
2022-01-10 06:26:33 +01:00
.run("for x in 0..500 {}")
2023-04-10 17:23:59 +02:00
.unwrap_err(),
2022-02-08 02:02:15 +01:00
EvalAltResult::ErrorTerminated(x, ..) if x.as_int()? == 42
2020-06-14 08:25:47 +02:00
));
Ok(())
}