1
use super::{MAX_ELAB_DEPTH, bounded, cost, nesting};
2
use crate::ast::Expr;
3
use crate::runtime::{Closure, Pair, SymbolTable, Value};
4

            
5
5
fn deep_cons_chain(depth: usize) -> Expr {
6
5
    let mut tail = Expr::Nil;
7
5
    for _ in 0..depth {
8
1670
        tail = Expr::cons(Expr::Nil, tail);
9
    }
10
5
    tail
11
5
}
12

            
13
#[test]
14
1
fn leaf_costs_one() {
15
1
    assert_eq!(cost(&Expr::Nil).unwrap(), 1);
16
1
    assert_eq!(cost(&Expr::T).unwrap(), 1);
17
1
}
18

            
19
#[test]
20
1
fn a_flat_list_costs_one_per_element_plus_the_list_itself() {
21
1
    let list = Expr::List(vec![Expr::Nil, Expr::T, Expr::Nil]);
22
1
    assert_eq!(cost(&list).unwrap(), 4);
23
1
}
24

            
25
#[test]
26
1
fn a_cons_cell_costs_itself_plus_both_children() {
27
1
    let cell = Expr::cons(Expr::T, Expr::Nil);
28
1
    assert_eq!(cost(&cell).unwrap(), 3);
29
1
}
30

            
31
#[test]
32
1
fn nesting_within_the_limit_succeeds_on_a_bounded_stack() {
33
1
    let handle = std::thread::Builder::new()
34
1
        .stack_size(2 * 1024 * 1024)
35
1
        .spawn(|| cost(&deep_cons_chain(MAX_ELAB_DEPTH - 1)))
36
1
        .expect("spawn bounded-stack thread");
37
1
    let result = handle.join().expect("must not overflow the stack");
38
1
    assert!(result.is_ok());
39
1
}
40

            
41
#[test]
42
1
fn nesting_past_the_limit_is_a_structured_error_not_a_stack_overflow() {
43
1
    let handle = std::thread::Builder::new()
44
1
        .stack_size(2 * 1024 * 1024)
45
1
        .spawn(|| cost(&deep_cons_chain(MAX_ELAB_DEPTH * 4)))
46
1
        .expect("spawn bounded-stack thread");
47
1
    let result = handle.join().expect("must not overflow the stack");
48
1
    let err = result.expect_err("nesting past the limit must be rejected");
49
1
    assert!(matches!(err, crate::error::Error::Resource(_)));
50
1
    assert!(format!("{err}").contains("elaboration depth limit"));
51
1
}
52

            
53
#[test]
54
1
fn lambda_optional_defaults_are_counted() {
55
    use crate::ast::LambdaParams;
56
1
    let mut params = LambdaParams::simple(vec!["x".to_string()]);
57
1
    params.optional.push(("y".to_string(), Some(Expr::T)));
58
1
    let lambda = Expr::Lambda(params, Box::new(Expr::Nil));
59
1
    assert_eq!(cost(&lambda).unwrap(), 3);
60
1
}
61

            
62
#[test]
63
1
fn bounded_returns_a_datum_within_the_limit_unchanged() {
64
1
    let cell = Expr::cons(Expr::T, Expr::Nil);
65
1
    assert_eq!(bounded(&SymbolTable::new(), cell.clone()).unwrap(), cell);
66
1
}
67

            
68
#[test]
69
1
fn bounded_rejects_a_datum_past_the_limit_on_a_bounded_stack() {
70
1
    let handle = std::thread::Builder::new()
71
1
        .stack_size(2 * 1024 * 1024)
72
1
        .spawn(|| bounded(&SymbolTable::new(), deep_cons_chain(MAX_ELAB_DEPTH * 4)).map(|_| ()))
73
1
        .expect("spawn bounded-stack thread");
74
1
    let result = handle.join().expect("must not overflow the stack");
75
1
    let err = result.expect_err("nesting past the limit must be rejected");
76
1
    assert!(format!("{err}").contains("elaboration depth limit"));
77
1
}
78

            
79
3
fn nested_quoted_lists(levels: usize) -> Expr {
80
3
    let mut datum = Expr::Nil;
81
3
    for _ in 0..levels {
82
258
        datum = Expr::quoted_code(Expr::List(vec![datum]));
83
    }
84
3
    datum
85
3
}
86

            
87
2
fn nested_structs(levels: usize) -> Expr {
88
2
    let mut value = Value::Nil;
89
2
    for _ in 0..levels {
90
639
        value = Value::Struct {
91
639
            name: "NODE".to_string(),
92
639
            fields: vec![("NEXT".to_string(), value)],
93
639
        };
94
    }
95
2
    Expr::RuntimeValue(value)
96
2
}
97

            
98
#[test]
99
1
fn a_quote_around_a_list_adds_no_level() {
100
1
    assert!(cost(&nested_quoted_lists(MAX_ELAB_DEPTH)).is_ok());
101
1
    assert!(cost(&nested_quoted_lists(MAX_ELAB_DEPTH + 1)).is_err());
102
1
}
103

            
104
#[test]
105
1
fn a_quote_around_a_list_is_still_charged_as_a_node() {
106
1
    assert_eq!(cost(&nested_quoted_lists(1)).unwrap(), 3);
107
1
}
108

            
109
#[test]
110
1
fn nested_struct_values_are_depth_checked() {
111
1
    assert!(cost(&nested_structs(MAX_ELAB_DEPTH - 1)).is_ok());
112
1
    let err = cost(&nested_structs(MAX_ELAB_DEPTH * 4)).expect_err("too deep");
113
1
    assert!(format!("{err}").contains("elaboration depth limit"));
114
1
}
115

            
116
#[test]
117
1
fn pair_vector_and_closure_values_are_walked() {
118
1
    let pair = Value::Pair(Box::new(Pair::new(Value::T, Value::Nil)));
119
1
    let vector = Value::Vector(vec![pair, Value::T]);
120
1
    let closure = Value::Closure(Closure::new(0, vec![vector]));
121
1
    assert_eq!(cost(&Expr::RuntimeValue(closure)).unwrap(), 7);
122
1
}
123

            
124
#[test]
125
1
fn nesting_is_the_deepest_level_of_the_datum() {
126
1
    assert_eq!(nesting(&Expr::Nil).unwrap(), 0);
127
1
    assert_eq!(nesting(&deep_cons_chain(7)).unwrap(), 7);
128
1
}
129

            
130
#[test]
131
1
fn nesting_refuses_a_datum_past_the_ceiling() {
132
1
    let err = nesting(&deep_cons_chain(MAX_ELAB_DEPTH * 4)).expect_err("too deep");
133
1
    assert!(matches!(err, crate::error::Error::Resource(_)), "{err}");
134
1
}