1
//! How many attempts a unit takes to settle its homes, read off the counters.
2

            
3
use super::super::behaviour_oracle::observe;
4
use super::{Hosts, acceptance_dir, corpus_files, file_name, read};
5
use crate::compiler::stats::CompileStats;
6
use crate::compiler::{CompileMode, Compiler};
7
use crate::reader::Reader;
8
use crate::runtime::SymbolTable;
9

            
10
12
fn stats_of(source: &str, mode: CompileMode) -> CompileStats {
11
12
    let program = Reader::parse(source).expect("parses");
12
12
    let mut compiler = Compiler::new();
13
12
    compiler
14
12
        .compile_with_mode(&program, &mut SymbolTable::with_builtins_for_wasm(), mode)
15
12
        .expect("compiles");
16
12
    compiler.stats()
17
12
}
18

            
19
29
fn fixture(name: &str) -> String {
20
29
    let path = corpus_files()
21
29
        .into_iter()
22
29
        .chain([acceptance_dir().join(name)])
23
948
        .find(|path| file_name(path) == name)
24
29
        .expect("a fixture");
25
29
    read(&path)
26
29
}
27

            
28
1
fn distinct_chain(depth: usize) -> String {
29
1
    let mut nest = format!("(+ *v{depth}* 1/2)");
30
1
    for level in (1..=depth).rev() {
31
6
        nest = format!("(setf *v{level}* (+ *v{level}* {nest}))");
32
    }
33
1
    let defs: String = (1..=depth)
34
6
        .map(|i| format!("(defvar *v{i}* 0)\n"))
35
        .collect();
36
6
    let reads: Vec<String> = (1..=depth).map(|i| format!("*v{i}*")).collect();
37
1
    format!("{defs}{nest}\n(list {})\n", reads.join(" "))
38
1
}
39

            
40
#[test]
41
1
fn units_settle_in_the_attempts_their_chains_need() {
42
1
    let table = [
43
1
        ("fx0-home-chain.nms", CompileMode::Eval, 4),
44
1
        ("fx0-tagsync-chain.nms", CompileMode::Eval, 1),
45
1
        ("tag-sync.nms", CompileMode::Script, 1),
46
1
        ("metro.nms", CompileMode::Script, 1),
47
1
        ("fx0-dead-store.nms", CompileMode::Eval, 1),
48
1
        ("fx0-special-binder.nms", CompileMode::Eval, 2),
49
1
        ("fx0-binder-in-macro.nms", CompileMode::Eval, 1),
50
1
        ("fx0-local-index-to-ratio.nms", CompileMode::Eval, 2),
51
1
        ("fx0-r17c.nms", CompileMode::Eval, 1),
52
1
        ("fx0-mir-nil-f.nms", CompileMode::Eval, 2),
53
1
        ("fx0-let-setf-mix.nms", CompileMode::Eval, 2),
54
1
    ];
55
1
    for (name, mode, attempts) in table {
56
11
        assert_eq!(
57
11
            stats_of(&fixture(name), mode).unit_attempts,
58
11
            attempts,
59
            "{name}"
60
        );
61
    }
62
1
}
63

            
64
#[test]
65
1
fn independent_homes_that_widen_settle_together() {
66
1
    let source = "(defvar *a* 0) (defvar *b* 0) (defvar *c* 0) (setf *a* 1/2) (setf *b* 1/3) (setf *c* 1/4) (list *a* *b* *c*)";
67
1
    assert_eq!(stats_of(source, CompileMode::Eval).unit_attempts, 4);
68
1
}
69

            
70
#[test]
71
1
fn a_chain_of_index_homes_beside_a_fraction_is_refused() {
72
1
    let program = Reader::parse(&distinct_chain(6)).expect("parses");
73
1
    let err = Compiler::new()
74
1
        .compile_with_mode(
75
1
            &program,
76
1
            &mut SymbolTable::with_builtins_for_wasm(),
77
1
            CompileMode::Eval,
78
1
        )
79
1
        .expect_err("a mix, CL a list of sums");
80
1
    assert!(
81
1
        err.to_string()
82
1
            .contains("+ cannot mix an index (count) with a scalar"),
83
        "{err}"
84
    );
85
1
}
86

            
87
#[test]
88
1
fn a_do_variable_settles_in_the_attempts_its_stores_need() {
89
1
    let table = [
90
1
        ("fx0-do-ratio-store-body.nms", 2),
91
1
        ("fx0-do-ratio-store-body-list.nms", 2),
92
1
        ("fx0-do-nil-store-count.nms", 1),
93
1
        ("fx0-do-prev-count.nms", 1),
94
1
        ("fx0-do-star-nil-step.nms", 2),
95
1
        ("fx0-do-two-stores-acc.nms", 2),
96
1
        ("fx0-do-cons-count-acc.nms", 1),
97
1
        ("fx0-do-if-ratio-step.nms", 1),
98
1
        ("fx0-do-cons-ratio-car.nms", 1),
99
1
        ("fx0-do-string-nil-step.nms", 1),
100
1
        ("fx0-do-body-cons.nms", 1),
101
1
        ("fx0-do-length-acc.nms", 1),
102
1
        ("fx0-do-const-twin.nms", 1),
103
1
    ];
104
1
    for (name, attempts) in table {
105
13
        let (_, stats) = observe(
106
13
            &fixture(name),
107
13
            CompileMode::Eval,
108
13
            &Hosts::Differential.specs(),
109
13
        );
110
13
        assert_eq!(stats.unit_attempts, attempts, "{name}");
111
    }
112
1
}
113

            
114
#[test]
115
1
fn a_dolist_variable_settles_in_the_attempts_its_stores_need() {
116
1
    let table = [
117
1
        ("fx0-do-dolist-var-store.nms", 3),
118
1
        ("fx0-dolist-string-store.nms", 3),
119
1
        ("fx0-dolist-index-store.nms", 1),
120
1
        ("fx0-dolist-var-unstored.nms", 1),
121
1
        ("fx0-dolist-struct-list.nms", 2),
122
1
    ];
123
1
    for (name, attempts) in table {
124
5
        let (_, stats) = observe(
125
5
            &fixture(name),
126
5
            CompileMode::Eval,
127
5
            &Hosts::Differential.specs(),
128
5
        );
129
5
        assert_eq!(stats.unit_attempts, attempts, "{name}");
130
    }
131
1
}