1
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
2
// Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
//! PERSIST fixpoint F0 rows at the REPL. The programs are the `fx0-*` acceptance
6
//! files, whose first line holds the SBCL value and the slice that flips the
7
//! row. The REPL has `entity-count` where a script has `rpc-protocol-version`.
8

            
9
#[path = "persist/rows.rs"]
10
mod persist;
11
#[path = "persist/stdout.rs"]
12
mod stdout;
13

            
14
use persist::{Row, assert_pinned, assert_pinned_wide, row};
15
use stdout::session;
16

            
17
type Cases = &'static [(&'static str, &'static str)];
18

            
19
const VALUE_CASES: Cases = &[
20
    ("fx0-inlined-const-key", "(1 1/4 2)"),
21
    ("fx0-inlined-const-key-labels", "(2/1 1/4)"),
22
    ("fx0-home-chain", "(nil nil (1))"),
23
    ("fx0-read-before-store", "(3 2 1)"),
24
    ("fx0-seen-keeps", "1"),
25
    ("fx0-tagsync-chain", "(2)"),
26
    ("fx0-closure-list-cons-car", "2"),
27
    ("fx0-closure-list-add", "2"),
28
    ("fx0-closure-list-arg", "5"),
29
    ("fx0-closure-list-dolist", "3"),
30
    ("fx0-closure-cons-nil-cdr", "2"),
31
    ("fx0-closure-cons-literal-nil", "2"),
32
    ("fx0-closure-cons-list-cdr", "3"),
33
    ("fx0-closure-global-cons-list", "2"),
34
    ("fx0-store-datum-first-writer", "1/1"),
35
    ("fx0-dead-store", "0"),
36
    ("fx0-binder-in-macro", "((1) (1/2))"),
37
    ("fx0-special-binder", "1/2"),
38
    ("fx0-closure-store-acc", "(2 1)"),
39
    ("fx0-closure-store-widens", "1/2"),
40
    ("fx0-closure-list-join", "(1 2)"),
41
    ("fx0-g3", "1"),
42
    ("fx0-let-setf-mix", "10"),
43
    ("fx0-local-index-to-ratio", "1/2"),
44
    ("fx0-local-runtime-index-to-nil", "nil"),
45
    ("fx0-local-runtime-index-to-ratio", "1/2"),
46
    ("fx0-local-mixed-use", "1/1"),
47
    ("fx0-local-nil-to-index", "0"),
48
    ("fx0-mir-nil-f", "6"),
49
    ("fx0-r17c", "3"),
50
    ("fx0-v5", "1"),
51
    ("fx0-r10c", "1"),
52
    ("fx0-list-init-widens", "(1/2)"),
53
    ("fx0-quoted-init-widens", "(1)"),
54
    ("fx0-nil-accumulator-ratio", "1/2"),
55
    ("fx0-nil-accumulator-cons", "(1/2)"),
56
    ("fx0-n1-binder-index-store-ratio-home", "1/2"),
57
    ("fx0-n1-global-index-store-ratio-home", "1/2"),
58
    ("fx0-n1-binder-unreachable-store", "0"),
59
    ("fx0-n1-global-unreachable-store", "0"),
60
    ("fx0-do-dolist-var-store", "1/2"),
61
    ("fx0-dolist-string-store", "(5 5)"),
62
    ("fx0-dolist-index-store", "(3 2)"),
63
    ("fx0-dolist-var-unstored", "(2 0)"),
64
    ("fx0-dolist-struct-list", "(2 0)"),
65
    ("fx0-n3-labels-two-defuns", "(1 0/1)"),
66
    ("fx0-n3-labels-vs-defun", "(1 0/1)"),
67
    ("fx0-n3-lambda-two-defuns", "(1 0/1)"),
68
    ("fx0-n3-sharp-split", "(1 0/1)"),
69
    ("fx0-let-hint-ratio", "(1/2)"),
70
    ("fx0-let-hint-nil", "(5)"),
71
    ("fx0-let-hint-helper", "(1/2)"),
72
    ("fx0-let-hint-star", "(1/2)"),
73
    ("fx0-let-hint-runtime-ratio", "(1/2)"),
74
    ("fx0-let-hint-runtime-param", "(1/2)"),
75
    ("fx0-let-hint-runtime-nil", "(nil)"),
76
    ("fx0-let-nil-init-count", "(0)"),
77
    ("fx0-let-nil-init-count-ratio", "(1/2)"),
78
    ("fx0-let-nil-init-ratio-count", "(0)"),
79
    ("fx0-let-nil-init-count-nil", "(nil)"),
80
    ("fx0-let-nil-init-sibling-defun", "((4 2))"),
81
    ("fx0-let-nil-init-sibling-cons", "((2))"),
82
    ("fx0-let-nil-init-sibling-runtime", "((5))"),
83
    ("fx0-let-star-nil-init-sibling", "((2))"),
84
    ("fx0-do-cons-count-acc", "(2 1 0)"),
85
    ("fx0-do-if-ratio-step", "1/2"),
86
    ("fx0-do-cons-ratio-car", "1/2"),
87
    ("fx0-do-string-nil-step", "nil"),
88
    ("fx0-do-body-cons", "(1 0)"),
89
    ("fx0-do-length-acc", "3"),
90
    ("fx0-do-ratio-store-body", "1/2"),
91
    ("fx0-do-ratio-store-body-list", "(1/2)"),
92
    ("fx0-do-nil-store-count", "1"),
93
    ("fx0-do-prev-count", "2"),
94
    ("fx0-do-star-nil-step", "nil"),
95
    ("fx0-do-two-stores-acc", "(1 1/2 0 1/2)"),
96
    ("fx0-n5-const-list-best", "3"),
97
    ("fx0-n5-quoted-list-best", "3"),
98
    ("fx0-n5-mixed-list-best", "\"a\""),
99
    ("fx0-n5-runtime-list-best", "2"),
100
    ("fx0-n5-do-best", "2"),
101
];
102

            
103
const ERROR_CASES: Cases = &[
104
    ("fx0-store-init-half", MIX_ADD),
105
    ("fx0-store-init-half-defun", MIX_ADD),
106
    ("fx0-store-datum-let", MIX_ADD),
107
    ("fx0-store-datum-defun", MIX_ADD),
108
    ("fx0-store-datum-macro", MIX_ADD),
109
    ("fx0-sum-accumulator", MIX_ADD),
110
    ("fx0-global-sum", MIX_ADD),
111
    ("fx0-do-const-twin", MIX_ADD),
112
    ("fx0-do-count-ratio-acc", MIX_ADD),
113
    (
114
        "fx0-fbb-ap",
115
        "err: Compile error: CONS cdr must be a pair or nil, got i32",
116
    ),
117
    (
118
        "fx0-fbb-fc-arg",
119
        "err: Compile error: CONS cdr must be a pair or nil, got i32",
120
    ),
121
    (
122
        "fx0-fbb-fc-fnop",
123
        "err: Compile error: the function operand is computed at runtime; it must be a lambda or a function name",
124
    ),
125
    ("fx0-n1-if-store-ratio-home", MIX_GT),
126
    ("fx0-n3-same-body-lambdas", ANY_OPERAND),
127
    ("fx0-t15", ANY_OPERAND),
128
    ("fx0-two-instances", ANY_OPERAND),
129
    (
130
        "fx0-dolist-any-read",
131
        "err: Compile error: > expects numeric arguments, got an any value; narrow it with THE (index, scalar or m",
132
    ),
133
];
134

            
135
const MIX_ADD: &str = "err: Compile error: + cannot mix an index (count) with a scalar; bridge explicitly with `(index->scalar .";
136

            
137
const MIX_GT: &str = "err: Compile error: > cannot mix an index (count) with a scalar; bridge explicitly with `(index->scalar .";
138

            
139
const ANY_OPERAND: &str = "err: Compile error: + expects numeric arguments, got an any value; narrow it with THE (index, scalar or m";
140

            
141
99
fn source_path(stem: &str) -> String {
142
99
    format!(
143
99
        "{}/../nomiscript/tests/acceptance/{stem}.nms",
144
        env!("CARGO_MANIFEST_DIR")
145
    )
146
99
}
147

            
148
2
fn rows(cases: Cases) -> Vec<Row> {
149
2
    cases
150
2
        .iter()
151
98
        .map(|(stem, _)| {
152
98
            let source = std::fs::read_to_string(source_path(stem)).unwrap_or_default();
153
98
            row(
154
98
                stem,
155
98
                &[&source.replace("rpc-protocol-version", "entity-count")],
156
98
            )
157
98
        })
158
        .collect()
159
2
}
160

            
161
2
fn pinned(cases: Cases) -> Vec<(&'static str, &'static [&'static str])> {
162
2
    cases
163
2
        .iter()
164
98
        .map(|(stem, value)| (*stem, std::slice::from_ref(value)))
165
        .collect()
166
2
}
167

            
168
#[test]
169
1
fn fixpoint_value_rows_read_as_pinned() {
170
1
    assert_pinned(&rows(VALUE_CASES), &pinned(VALUE_CASES));
171
1
}
172

            
173
#[test]
174
1
fn fixpoint_error_rows_read_as_pinned() {
175
1
    assert_pinned_wide(&rows(ERROR_CASES), &pinned(ERROR_CASES), 100);
176
1
}
177

            
178
#[test]
179
1
fn the_read_before_store_row_prints_the_accumulator() {
180
1
    let source = std::fs::read_to_string(source_path("fx0-read-before-store")).unwrap_or_default();
181
1
    let run = session(&[&source]);
182
1
    assert!(!run.stderr.contains("panicked"), "{}", run.stderr);
183
1
    assert!(run.stdout.contains("> 3\n(3 2 1)\n"), "{}", run.stdout);
184
1
}