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 SN-0 rows: the CL string natives (CONCATENATE, PRINC-TO-STRING,
6
//! FORMAT) and the rewritten string sample, pinned at today's values with the
7
//! SBCL value next to each row. SN-1..SN-4 flip them.
8

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

            
12
use persist::{Row, assert_pinned, row};
13

            
14
const NO_CONCATENATE: &str = "err: Undefined symbol: CONCATENATE";
15
const NO_PRINC: &str = "err: Undefined symbol: PRINC-TO-STRING";
16
const NO_PRIN1: &str = "err: Undefined symbol: PRIN1-TO-STRING";
17
const NO_FORMAT: &str = "err: Undefined symbol: FORMAT";
18
const NO_DATE: &str = "err: Undefined symbol: TRANSACTION-DATE";
19

            
20
struct Case {
21
    label: &'static str,
22
    units: &'static [&'static str],
23
    pinned: &'static [&'static str],
24
}
25

            
26
const fn case(
27
    label: &'static str,
28
    units: &'static [&'static str],
29
    pinned: &'static [&'static str],
30
) -> Case {
31
    Case {
32
        label,
33
        units,
34
        pinned,
35
    }
36
}
37

            
38
const MAKE_GREETING: &str =
39
    "(defun make-greeting (name) (concatenate 'string \"Hello, \" name \"!\"))";
40
const FORMAT_AMOUNT: &str = "(defun format-amount (amount currency) \
41
     (concatenate 'string (princ-to-string amount) \" \" currency))";
42
const SPLIT_CSV: &str = "(defun split->csv (s) (format nil \"~{~a~^,~}\" \
43
     (list (split-account-id s) (split-amount s) (split-commodity-id s))))";
44
const PRINT_SPLIT: &str = "(defun print-split (s) (print (concatenate 'string \"  \" \
45
     (split-account-id s) \" \" (format-amount (split-amount s) (split-commodity-id s)))))";
46
const PRINT_TX: &str = "(defun print-transaction (tx) \
47
     (print (format nil \"~a: ~a\" (transaction-date tx) (transaction-note tx))) \
48
     (mapc #'print-split (split:list-for-transaction tx)))";
49

            
50
const CONCATENATE_CASES: &[Case] = &[
51
    // SBCL "Hello, Ray!"
52
    case(
53
        "S1",
54
        &["(concatenate 'string \"Hello, \" \"Ray\" \"!\")"],
55
        &[NO_CONCATENATE],
56
    ),
57
    // SBCL "Hello, Ray!"
58
    case(
59
        "S2",
60
        &[
61
            "(let ((s (if (< (entity-count) 1) \"Ray\" \"Bob\"))) (concatenate 'string \"Hello, \" s \"!\"))",
62
        ],
63
        &[NO_CONCATENATE],
64
    ),
65
    // SBCL "ab"
66
    case(
67
        "S3",
68
        &["(concatenate 'string \"a\" nil \"b\")"],
69
        &[NO_CONCATENATE],
70
    ),
71
    // SBCL TYPE-ERROR at run time
72
    case("S4", &["(concatenate 'string \"a\" 1)"], &[NO_CONCATENATE]),
73
    // SBCL ""
74
    case("S5", &["(concatenate 'string)"], &[NO_CONCATENATE]),
75
    // SBCL (#\a #\b)
76
    case(
77
        "S6",
78
        &["(concatenate 'list \"a\" \"b\")"],
79
        &[NO_CONCATENATE],
80
    ),
81
];
82

            
83
const PRINC_CASES: &[Case] = &[
84
    // SBCL "5"
85
    case("P1", &["(princ-to-string 5)"], &[NO_PRINC]),
86
    // SBCL "1/2"
87
    case("P2", &["(princ-to-string 1/2)"], &[NO_PRINC]),
88
    // SBCL "K"
89
    case("P3", &["(princ-to-string :k)"], &[NO_PRINC]),
90
    // SBCL "NIL"
91
    case("P4", &["(princ-to-string nil)"], &[NO_PRINC]),
92
    // SBCL "(1 a B C NIL T)"
93
    case(
94
        "P5",
95
        &["(princ-to-string (list 1 \"a\" :b 'c nil t))"],
96
        &[NO_PRINC],
97
    ),
98
    // SBCL "x"
99
    case("P6", &["(princ-to-string \"x\")"], &[NO_PRINC]),
100
    // SBCL "0"
101
    case("P7", &["(princ-to-string (entity-count))"], &[NO_PRINC]),
102
    // SBCL "\"x\"": PRIN1 stays out of the plan
103
    case("P8", &["(prin1-to-string \"x\")"], &[NO_PRIN1]),
104
    // SBCL "#<FUNCTION (LAMBDA () ...)>"
105
    case("P9", &["(princ-to-string (lambda () 1))"], &[NO_PRINC]),
106
];
107

            
108
const FORMAT_CASES: &[Case] = &[
109
    // SBCL "1/2 USD"
110
    case("F1", &["(format nil \"~a ~a\" 1/2 \"USD\")"], &[NO_FORMAT]),
111
    // SBCL "a,1,B"
112
    case(
113
        "F2",
114
        &["(format nil \"~{~a~^,~}\" (list \"a\" 1 :b))"],
115
        &[NO_FORMAT],
116
    ),
117
    // SBCL "3 items" and a newline
118
    case("F3", &["(format nil \"~d items~%\" 3)"], &[NO_FORMAT]),
119
    // SBCL "A=1, B=2"
120
    case(
121
        "F4",
122
        &["(format nil \"~{~a=~a~^, ~}\" (list :a 1 :b 2))"],
123
        &[NO_FORMAT],
124
    ),
125
    // SBCL "0"
126
    case("F5", &["(format nil \"~a\" (entity-count))"], &[NO_FORMAT]),
127
    // SBCL ""
128
    case("F6", &["(format nil \"~{~a~^,~}\" nil)"], &[NO_FORMAT]),
129
    // SBCL "~"
130
    case("F7", &["(format nil \"~~\")"], &[NO_FORMAT]),
131
    // SBCL FORMAT-ERROR at run time
132
    case("F8", &["(format nil \"~a\")"], &[NO_FORMAT]),
133
    // SBCL "\"x\""
134
    case("F9", &["(format nil \"~s\" \"x\")"], &[NO_FORMAT]),
135
    // SBCL "0,2"
136
    case(
137
        "F10",
138
        &["(format nil \"~{~a~^,~}\" (list (entity-count) 2))"],
139
        &[NO_FORMAT],
140
    ),
141
    // SBCL "x|7"
142
    case("F11", &["(format nil \"~A|~D\" \"x\" 7)"], &[NO_FORMAT]),
143
    // SBCL "1"
144
    case(
145
        "F12",
146
        &["(let ((c \"~a\")) (format nil c 1))"],
147
        &[NO_FORMAT],
148
    ),
149
    // SBCL FORMAT-ERROR at run time
150
    case(
151
        "F13",
152
        &["(format nil \"~{~a=~a~^, ~}\" (list :a 1 :b))"],
153
        &[NO_FORMAT],
154
    ),
155
    // SBCL "x"
156
    case("F14", &["(format nil \"x\" 1 2)"], &[NO_FORMAT]),
157
    // SBCL prints 1 and returns NIL
158
    case("F15", &["(format t \"~a\" 1)"], &[NO_FORMAT]),
159
];
160

            
161
const SAMPLE_CASES: &[Case] = &[
162
    // SBCL "Hello, World!"
163
    case(
164
        "SA1",
165
        &[MAKE_GREETING, "(make-greeting \"World\")"],
166
        &["make-greeting", NO_CONCATENATE],
167
    ),
168
    // SBCL "25/2 USD"
169
    case(
170
        "SA2",
171
        &[FORMAT_AMOUNT, "(format-amount 25/2 \"USD\")"],
172
        &["format-amount", NO_CONCATENATE],
173
    ),
174
    // SBCL "acc-1,25/2,cur-1" for a split of acc-1, 25/2 and cur-1; lazy as SA4
175
    case("SA3", &[SPLIT_CSV], &["split->csv"]),
176
    // SBCL prints "<date>: Lunch" then the two split lines; the defuns compile
177
    // lazily, so only a call (db test, SN-4) meets the missing names
178
    case(
179
        "SA4",
180
        &[PRINT_SPLIT, PRINT_TX],
181
        &["print-split", "print-transaction"],
182
    ),
183
    // SBCL has no such function; the accessor is the SN-3 addition
184
    case("TD", &["(transaction-date 1)"], &[NO_DATE]),
185
];
186

            
187
4
fn rows(cases: &[Case]) -> Vec<Row> {
188
35
    cases.iter().map(|c| row(c.label, c.units)).collect()
189
4
}
190

            
191
4
fn pinned(cases: &[Case]) -> Vec<(&'static str, &'static [&'static str])> {
192
35
    cases.iter().map(|c| (c.label, c.pinned)).collect()
193
4
}
194

            
195
#[test]
196
1
fn concatenate_rows_read_as_pinned() {
197
1
    assert_pinned(&rows(CONCATENATE_CASES), &pinned(CONCATENATE_CASES));
198
1
}
199

            
200
#[test]
201
1
fn princ_rows_read_as_pinned() {
202
1
    assert_pinned(&rows(PRINC_CASES), &pinned(PRINC_CASES));
203
1
}
204

            
205
#[test]
206
1
fn format_rows_read_as_pinned() {
207
1
    assert_pinned(&rows(FORMAT_CASES), &pinned(FORMAT_CASES));
208
1
}
209

            
210
#[test]
211
1
fn sample_rows_read_as_pinned() {
212
1
    assert_pinned(&rows(SAMPLE_CASES), &pinned(SAMPLE_CASES));
213
1
}