Lines
100 %
Functions
Branches
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! PERSIST S6 rows with their Common Lisp values: a global that holds a
//! closure carries it to the next unit.
#[path = "persist/rows.rs"]
mod persist;
use persist::{Row, assert_pinned, row};
const K: &str = "(defvar *k* (lambda (x) (* x 2)))";
const F: &str = "(defvar *f* (let ((r 1/10)) (lambda (x) (* x r))))";
const LOOP: &str = "(defun lp (n) (lp (+ n 1)))";
fn rows() -> Vec<Row> {
vec![
row("P1", &[K, "(funcall *k* 3)"]),
row("P2", &[F, "(funcall *f* 50/1)"]),
row("P3", &[K, "(defun twice (v) (funcall *k* v))", "(twice 4)"]),
row("P4", &[K, "(mapcar *k* (list 1 2 3))"]),
row(
"P5",
&[K, "(setf *k* (lambda (x) (+ x 1)))", "(funcall *k* 3)"],
),
"P6",
&[
K,
LOOP,
"(setf *k* (lambda (x) (+ x 1))) (lp (entity-count))",
"(funcall *k* 3)",
],
"P7",
"(defvar *c* (let ((n 0)) (lambda () (setf n (+ n 1)))))",
"(funcall *c*)",
"P8",
"(defvar *fs* (list (lambda (x) x)))",
"(funcall (car *fs*) 4)",
"P9",
"(defvar *rate* 1/10)",
"(defvar *g* (lambda (x) (* x *rate*)))",
"(setf *rate* 1/5)",
"(funcall *g* 10/1)",
row("P10", &[K, "(funcall *k* 3)", "(funcall *k* 4)"]),
"P11",
"(defun adder (n) (lambda (x) (+ x n)))",
"(defvar *a5* (adder 5))",
"(funcall *a5* 1)",
"P12",
"(defvar *g* (let ((f (lambda (x) (* x 2)))) (lambda (y) (funcall f y))))",
"(funcall *g* 3)",
"P13",
"(defvar *l* (let ((xs (list 1 2 3))) (lambda () (length xs))))",
"(funcall *l*)",
"P14",
"(defvar *s* (let ((s \"hi\")) (lambda () s)))",
"(funcall *s*)",
"P15",
"(defvar *m* (let ((n 0)) (setf n 5) (lambda () n)))",
"(funcall *m*)",
"P16",
&[K, "(defvar *k* (lambda (x) (* x 3)))", "(funcall *k* 3)"],
"P17",
"(defvar *i* (let ((b (> (entity-count) -1))) (lambda () b)))",
"(funcall *i*)",
"P18",
"(defvar *fs* (let ((r 2)) (list (lambda (x) (* x r)))))",
"P19",
"(defvar *g* (let ((f (if (> (entity-count) -1) (lambda (x) 1) (lambda (x) 2)))) (lambda (y) (funcall f y))))",
"P20",
&["(let ((r 2)) (funcall (car (list (lambda (x) (* x r)))) 3))"],
"P21",
"(defvar *g* (let ((f1 (let ((f0 (let ((n 5)) (lambda () n)))) (lambda () (funcall f0))))) (lambda () (funcall f1))))",
"(funcall *g*)",
"P22",
"(defvar *fs* (let ((x 100)) (list (lambda (x) (* x 2)))))",
"(funcall (car *fs*) 3)",
]
}
/// What each row prints; CL is the closure's value in every row but the ones
/// that stay loud: P7 (CL 1, a counter the closure shares) and P15 (CL 5).
/// P19 is a closure over a closure only known at run time.
const TODAY: &[(&str, &[&str])] = &[
("P1", &["-", "6"]),
("P2", &["-", "5/1"]),
("P3", &["-", "twice", "8"]),
("P4", &["-", "(2 4 6)"]),
("P5", &["-", "#<closure:1>", "4"]),
(
"-",
"lp",
"err: execution trapped: error while executing at ",
"4",
("P7", &["-", LOST_C]),
("P8", &["-", "4"]),
("P9", &["-", "-", "1/5", "2/1"]),
("P10", &["-", "6", "8"]),
("P11", &["adder", "-", "6"]),
("P12", &["-", "6"]),
("P13", &["-", "3"]),
("P14", &["-", "\"hi\""]),
&["-", "err: Compile error: *M*: its closure value does n"],
("P16", &["-", "-", "6"]),
("P17", &["-", "t"]),
("P18", &["-", "8"]),
("P19", &["-", "1"]),
("P20", &["6"]),
("P21", &["-", "5"]),
("P22", &["-", "6"]),
];
const LOST_C: &str = "err: Compile error: *C*: its closure value does n";
#[test]
fn every_row_reads_as_common_lisp() {
assert_pinned(&rows(), TODAY);