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 rows: a struct accessor takes any operand that evaluates to a
//! struct, however it is written, and runs the operand's effects once.
#[path = "persist/rows.rs"]
mod persist;
use nms::interpreter::Interpreter;
use persist::{Row, assert_pinned, row};
use scripting::nomiscript::{Number, Value};
const PT: &str = "(defstruct pt x)";
const N: &str = "(defvar *n* 0)";
fn rows() -> Vec<Row> {
vec![
row("F1", &[PT, "(pt-x (let ((q (make-pt :x 6))) q))"]),
row(
"F2",
&[PT, "(let ((p (make-pt :x 1))) (pt-x (if (= 1 1) p p)))"],
),
row("F3", &[PT, "(let ((p (make-pt :x 1))) (pt-x (progn p)))"]),
"F4",
&[
PT,
"(let ((p (make-pt :x 1))) (pt-x (cond ((= 1 2) p) (t p))))",
],
row("F5", &[PT, "(let ((p (make-pt :x 1))) (pt-x (or p nil)))"]),
row("F6", &[PT, "(let ((p (make-pt :x 1))) (pt-x (and t p)))"]),
row("F7", &[PT, "(pt-x (let* ((a (make-pt :x 4)) (b a)) b))"]),
"F8",
&[PT, "(defmacro mkp (v) `(make-pt :x ,v))", "(pt-x (mkp 3))"],
"F9",
"(defmacro wrap (v) `(progn ,v))",
"(let ((p (make-pt :x 3))) (pt-x (wrap p)))",
"F10",
N,
"(let ((p (make-pt :x 1))) (pt-x (progn (setf *n* 5) p)) *n*)",
"F11",
"(let ((p (make-pt :x 1))) (if (pt-x (progn (setf *n* 9) p)) *n* 0))",
"F12",
"(let ((p (make-pt :x 1))) (when (pt-x (progn (setf *n* 9) p)) *n*))",
"F13",
"(defun g4 (instance) (if (pt-p instance) (pt-x instance) 0))",
"(g4 (make-pt :x 7))",
row("F14", &[PT, "(pt-x (make-pt :x (pt-x (make-pt :x 5))))"]),
"F15",
"(let ((p (make-pt :x (make-pt :x 5)))) (pt-x (pt-x p)))",
"F16",
"(let ((p (make-pt :x 1))) (when (> (entity-count) -1) (setf (pt-x p) 5)) (pt-x p))",
"F17",
"(let ((p (make-pt :x 1))) (when (< (entity-count) 0) (setf (pt-x p) 5)) (pt-x p))",
]
}
/// CL: 6 1 1 1 1 1 4 3 3, then *n* 5 and the test's effect 9 9, then 7 5 5, then 5 and 1
/// for a store under a runtime guard.
const CL: &[(&str, &[&str])] = &[
("F1", &["pt", "6"]),
("F2", &["pt", "1"]),
("F3", &["pt", "1"]),
("F4", &["pt", "1"]),
("F5", &["pt", "1"]),
("F6", &["pt", "1"]),
("F7", &["pt", "4"]),
("F8", &["pt", "mkp", "3"]),
("F9", &["pt", "wrap", "3"]),
("F10", &["pt", "-", "5"]),
("F11", &["pt", "-", "9"]),
("F12", &["pt", "-", "9"]),
("F13", &["pt", "g4", "7"]),
("F14", &["pt", "5"]),
("F15", &["pt", "5"]),
("F16", &["pt", "5"]),
("F17", &["pt", "1"]),
];
#[test]
fn every_row_reads_as_common_lisp() {
assert_pinned(&rows(), CL);
fn error_of(source: &str) -> String {
let mut interp = Interpreter::new(false).expect("interpreter");
interp.eval(PT).expect("defstruct");
interp.eval(source).expect_err("refused").to_string()
fn an_operand_that_fails_is_refused_by_its_own_error() {
assert_eq!(
error_of("(pt-x (car 5))"),
"Compile error: CAR expects a list, got 5"
);
error_of("(pt-x (make-pt :x (+ 1 \"a\")))"),
"Compile error: + expects numeric arguments, got a string value"
fn an_operand_that_may_be_a_struct_at_run_time_is_tested_there() {
let values = interp
.eval("(pt-x (if (> (entity-count) -1) (make-pt :x 1) 5))")
.expect("reads the field");
assert_eq!(values.last(), Some(&Value::Number(Number::Index(1))));