1
//! A rejected operand is compiled once, so compile work grows linearly with
2
//! the nesting depth, never by a factor per level.
3

            
4
use crate::compiler::CompileMode;
5
use crate::compiler::behaviour_oracle::{Outcome, observe};
6
use crate::compiler::stats::CompileStats;
7

            
8
4
fn nested(depth: usize, kind: &str) -> String {
9
4
    let mut form = "(/ 1 0)".to_string();
10
4
    for level in 0..depth {
11
48
        form = match kind {
12
24
            "cmp" => format!("(if (< {form} {}) {level} {})", level + 1, level + 1),
13
24
            _ => format!("(if (= {form} 0) 0 1)"),
14
        };
15
    }
16
4
    format!("(handler-case {form} (division-by-zero (e) 7))")
17
4
}
18

            
19
4
fn compile(depth: usize, kind: &str) -> (CompileStats, Vec<Outcome>) {
20
4
    let (behaviours, stats) = observe(&nested(depth, kind), CompileMode::Eval, &[]);
21
8
    (stats, behaviours.into_iter().map(|b| b.outcome).collect())
22
4
}
23

            
24
const SEVEN: &str = "Some(I32) [Struct(2), I64(7), I64(1)]";
25

            
26
2
fn assert_linear(kind: &str) {
27
2
    let (shallow, _) = compile(8, kind);
28
2
    let (deep, outcomes) = compile(16, kind);
29
2
    assert!(
30
2
        outcomes
31
2
            .iter()
32
4
            .all(|outcome| *outcome == Outcome::Value(SEVEN.to_string())),
33
        "{kind}: {outcomes:?}"
34
    );
35
2
    assert!(
36
2
        deep.stack_compiles <= 3 * shallow.stack_compiles,
37
        "{kind}: {} operand compiles at depth 16 against {} at depth 8",
38
        deep.stack_compiles,
39
        shallow.stack_compiles
40
    );
41
2
}
42

            
43
#[test]
44
1
fn nested_comparisons_of_a_rejected_operand_compile_it_once() {
45
1
    assert_linear("cmp");
46
1
}
47

            
48
#[test]
49
1
fn nested_equalities_of_a_rejected_operand_compile_it_once() {
50
1
    assert_linear("eqchain");
51
1
}
52

            
53
const SEVEN_BOXED: &str = "Some(AnyRef) [I31(7)]";
54

            
55
#[test]
56
1
fn a_raising_operand_beside_a_scalar_is_caught_in_every_identity() {
57
1
    for name in ["eql", "eq", "equal"] {
58
3
        for (form, expected) in [
59
3
            (
60
3
                format!("(handler-case ({name} 1/2 (error 'x \"m\")) (x (e) 7))"),
61
3
                SEVEN,
62
3
            ),
63
3
            (
64
3
                format!("(handler-case ({name} (error 'x \"m\") 1/2) (x (e) 7))"),
65
3
                SEVEN,
66
3
            ),
67
3
            (
68
3
                format!(
69
3
                    "(defun boom () (error 'x \"m\")) (handler-case ({name} 1/2 (boom)) (x (e) 7))"
70
3
                ),
71
3
                SEVEN_BOXED,
72
3
            ),
73
3
        ] {
74
9
            let (behaviours, _) = observe(&form, CompileMode::Eval, &[]);
75
9
            assert!(
76
9
                behaviours
77
9
                    .iter()
78
18
                    .all(|b| b.outcome == Outcome::Value(expected.to_string())),
79
                "{form}: {behaviours:?}"
80
            );
81
        }
82
    }
83
1
}