Lines
100 %
Functions
Branches
use super::{MAX_ELAB_DEPTH, bounded, cost, nesting};
use crate::ast::Expr;
use crate::runtime::{Closure, Pair, SymbolTable, Value};
fn deep_cons_chain(depth: usize) -> Expr {
let mut tail = Expr::Nil;
for _ in 0..depth {
tail = Expr::cons(Expr::Nil, tail);
}
tail
#[test]
fn leaf_costs_one() {
assert_eq!(cost(&Expr::Nil).unwrap(), 1);
assert_eq!(cost(&Expr::T).unwrap(), 1);
fn a_flat_list_costs_one_per_element_plus_the_list_itself() {
let list = Expr::List(vec![Expr::Nil, Expr::T, Expr::Nil]);
assert_eq!(cost(&list).unwrap(), 4);
fn a_cons_cell_costs_itself_plus_both_children() {
let cell = Expr::cons(Expr::T, Expr::Nil);
assert_eq!(cost(&cell).unwrap(), 3);
fn nesting_within_the_limit_succeeds_on_a_bounded_stack() {
let handle = std::thread::Builder::new()
.stack_size(2 * 1024 * 1024)
.spawn(|| cost(&deep_cons_chain(MAX_ELAB_DEPTH - 1)))
.expect("spawn bounded-stack thread");
let result = handle.join().expect("must not overflow the stack");
assert!(result.is_ok());
fn nesting_past_the_limit_is_a_structured_error_not_a_stack_overflow() {
.spawn(|| cost(&deep_cons_chain(MAX_ELAB_DEPTH * 4)))
let err = result.expect_err("nesting past the limit must be rejected");
assert!(matches!(err, crate::error::Error::Resource(_)));
assert!(format!("{err}").contains("elaboration depth limit"));
fn lambda_optional_defaults_are_counted() {
use crate::ast::LambdaParams;
let mut params = LambdaParams::simple(vec!["x".to_string()]);
params.optional.push(("y".to_string(), Some(Expr::T)));
let lambda = Expr::Lambda(params, Box::new(Expr::Nil));
assert_eq!(cost(&lambda).unwrap(), 3);
fn bounded_returns_a_datum_within_the_limit_unchanged() {
assert_eq!(bounded(&SymbolTable::new(), cell.clone()).unwrap(), cell);
fn bounded_rejects_a_datum_past_the_limit_on_a_bounded_stack() {
.spawn(|| bounded(&SymbolTable::new(), deep_cons_chain(MAX_ELAB_DEPTH * 4)).map(|_| ()))
fn nested_quoted_lists(levels: usize) -> Expr {
let mut datum = Expr::Nil;
for _ in 0..levels {
datum = Expr::quoted_code(Expr::List(vec![datum]));
datum
fn nested_structs(levels: usize) -> Expr {
let mut value = Value::Nil;
value = Value::Struct {
name: "NODE".to_string(),
fields: vec![("NEXT".to_string(), value)],
};
Expr::RuntimeValue(value)
fn a_quote_around_a_list_adds_no_level() {
assert!(cost(&nested_quoted_lists(MAX_ELAB_DEPTH)).is_ok());
assert!(cost(&nested_quoted_lists(MAX_ELAB_DEPTH + 1)).is_err());
fn a_quote_around_a_list_is_still_charged_as_a_node() {
assert_eq!(cost(&nested_quoted_lists(1)).unwrap(), 3);
fn nested_struct_values_are_depth_checked() {
assert!(cost(&nested_structs(MAX_ELAB_DEPTH - 1)).is_ok());
let err = cost(&nested_structs(MAX_ELAB_DEPTH * 4)).expect_err("too deep");
fn pair_vector_and_closure_values_are_walked() {
let pair = Value::Pair(Box::new(Pair::new(Value::T, Value::Nil)));
let vector = Value::Vector(vec![pair, Value::T]);
let closure = Value::Closure(Closure::new(0, vec![vector]));
assert_eq!(cost(&Expr::RuntimeValue(closure)).unwrap(), 7);
fn nesting_is_the_deepest_level_of_the_datum() {
assert_eq!(nesting(&Expr::Nil).unwrap(), 0);
assert_eq!(nesting(&deep_cons_chain(7)).unwrap(), 7);
fn nesting_refuses_a_datum_past_the_ceiling() {
let err = nesting(&deep_cons_chain(MAX_ELAB_DEPTH * 4)).expect_err("too deep");
assert!(matches!(err, crate::error::Error::Resource(_)), "{err}");