use stet::Interpreter;
fn probe_with_cap(source: &str, max_local_vm: usize) -> bool {
let mut interp = Interpreter::builder().suppress_output().build();
let ctx = interp.context();
ctx.max_local_vm = max_local_vm;
if stet_engine::eval::parse_and_exec(ctx, source.as_bytes()).is_err() {
return false;
}
matches!(
ctx.o_stack.peek(0).map(|o| o.value),
Ok(stet_core::object::PsValue::Bool(true))
)
}
#[test]
fn exceeding_the_ceiling_raises_a_catchable_vmerror() {
assert!(
probe_with_cap(
"{ 64000000 array pop } stopped { $error /errorname get /VMerror eq } { false } ifelse",
64 * 1024 * 1024,
),
"an oversized allocation must raise a catchable /VMerror"
);
}
#[test]
fn exceeding_the_ceiling_halts_execution() {
assert!(
!probe_with_cap("64000000 array pop true", 64 * 1024 * 1024),
"execution must not continue past a failed allocation"
);
}
#[test]
fn allocation_within_the_ceiling_is_unaffected() {
assert!(
probe_with_cap("1000000 string pop 100000 array pop true", 64 * 1024 * 1024),
"a 1 MB string and a 100k array are ordinary"
);
}
#[test]
fn currentuserparams_reports_the_live_ceiling() {
assert!(
probe_with_cap(
"currentuserparams /MaxLocalVM get 67108864 eq",
64 * 1024 * 1024,
),
"currentuserparams must report the ceiling actually in force"
);
}
#[test]
fn the_default_ceiling_is_reported_not_zero() {
let mut interp = Interpreter::builder().suppress_output().build();
let ctx = interp.context();
let expected = ctx.max_local_vm as i64;
assert!(expected > 0, "the default ceiling must be positive");
assert!(
stet_engine::eval::parse_and_exec(
ctx,
format!("currentuserparams /MaxLocalVM get {expected} eq").as_bytes(),
)
.is_ok()
);
assert!(
matches!(
ctx.o_stack.peek(0).map(|o| o.value),
Ok(stet_core::object::PsValue::Bool(true))
),
"default MaxLocalVM must be reported as {expected}, not 0"
);
}
#[test]
fn setuserparams_still_sets_and_reports_the_ceiling() {
assert!(
probe_with_cap(
"<< /MaxLocalVM 33554432 >> setuserparams \
currentuserparams /MaxLocalVM get 33554432 eq",
64 * 1024 * 1024,
),
"setuserparams must override the ceiling and be reported back"
);
}