Lines
99.31 %
Functions
100 %
Branches
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Fraction, Number, Pair, Value, vec_to_list};
fn int(n: i64) -> Value {
Value::Number(Number::Index(n))
}
fn ints(items: &[i64]) -> Value {
vec_to_list(items.iter().map(|n| int(*n)).collect())
// LIST
#[test]
fn test_eval_list_empty() {
let mut interp = Interpreter::new(false).unwrap();
let results = interp.eval("(list)").unwrap();
assert_eq!(results, vec![Value::Nil]);
fn test_eval_list_numbers() {
let results = interp.eval("(list 1 2 3)").unwrap();
assert_eq!(results, vec![ints(&[1, 2, 3])]);
fn test_eval_list_mixed() {
let results = interp.eval(r#"(list 1 "hello" t)"#).unwrap();
assert_eq!(
results,
vec![vec_to_list(vec![
int(1),
Value::String("hello".to_string()),
Value::T
])]
);
// CONS
fn test_eval_cons_onto_list() {
let results = interp.eval("(cons 1 (list 2 3))").unwrap();
fn test_eval_cons_onto_nil() {
let results = interp.eval("(cons 1 nil)").unwrap();
assert_eq!(results, vec![ints(&[1])]);
fn test_eval_cons_arity_error() {
assert!(interp.eval("(cons 1)").is_err());
assert!(interp.eval("(cons 1 2 3)").is_err());
fn test_eval_numeric_literal_in_list_stays_ratio() {
// A numeric literal is a dimensionless Ratio in EVERY context — including a
// pair cell — never an i32 count. Reclassifying an integer literal to I32
// when it rides a list cell was an illegal implicit Ratio→count conversion:
// the `5` came back from CAR as a count and arithmetic refused it. These
// exercise the RUNTIME pair path (a runtime element forces the chain
// runtime), so the literal `5` must survive as a usable Ratio.
// CAR of a runtime list holding `5` then arithmetic on it.
interp
.eval("(let* ((p (cons 5 (cons (transaction-post-date 0) nil)))) (+ (car p) 1/1))")
.unwrap(),
vec![Value::Number(Number::Scalar(Fraction::new(6, 1)))]
// CL 6: a cell holds a scalar, so the bare integer beside it is a mix.
let refused = interp
.eval("(let* ((p (cons 5 (cons (transaction-post-date 0) nil)))) (+ (car p) 1))")
.expect_err("a mix");
assert!(
refused
.to_string()
.contains("+ cannot mix an index (count) with a scalar"),
"{refused}"
// a mixed literal+runtime list no longer miscompiles to Bool; CAR is 5.
.eval("(car (list 5 (transaction-post-date 0)))")
vec![Value::Number(Number::Scalar(Fraction::new(5, 1)))]
// CAR over a list whose element is a RUNTIME value (#57). `(list <runtime>)`
// used to embed the runtime placeholder inside a quoted compile-time list, so
// CAR extracted a bare placeholder with no stack value -> invalid wasm. Now
// `list` desugars a runtime element to the CONS chain, so every element type
// round-trips through the $pair stack path.
fn test_eval_car_of_list_with_runtime_i32() {
// tag-count is 0 on the minimal input; the i32 COUNT element keeps Number.
.eval("(car (list (transaction-tag-count 0)))")
vec![Value::Number(Number::Index(0))]
fn test_eval_car_of_list_with_runtime_ratio() {
.eval("(car (list (transaction-post-date 0)))")
vec![Value::Number(Number::Scalar(Fraction::new(0, 1)))]
fn test_eval_car_of_list_with_runtime_bool() {
// A bool element rides its own PairElement::Bool slot, so CAR recovers a
// truth value: (= 0 0) -> T, (< 0 0) -> Nil — NOT Number(1)/(0).
.eval("(car (list (= (transaction-tag-count 0) 0)))")
vec![Value::T]
.eval("(car (list (< 0 (transaction-tag-count 0))))")
vec![Value::Nil]
fn test_eval_cdr_then_car_of_multi_runtime_list() {
// Two runtime bool elements; CDR then CAR reaches the second cell.
.eval(
"(car (cdr (list (= (transaction-tag-count 0) 0) \
(< 0 (transaction-tag-count 0)))))"
)
fn test_eval_do_accumulator_of_runtime_cons_car() {
// A `do` whose accumulator step is `(cons <host-fn-call> acc)` must size the
// accumulator local from the (eval-resolved) cons car's element type. The
// car is a not-yet-reduced `(transaction-… i)` List form; the pair-element
// walker now evals it on a clone instead of falling to the scalar default
// (which mis-sized `acc` and failed CONS cdr typing). Pre-existing bug
// surfaced while completing the PairElement::Bool work.
// i32 car: CAR of the accumulated list keeps Number.
"(car (do ((i 0 (+ i 1)) \
(acc nil (cons (transaction-tag-count 0) acc))) \
((= i 3) acc)))"
// bool car: CAR recovers a truth value (Bool), not Number.
(acc nil (cons (= (transaction-tag-count 0) 0) acc))) \
fn test_eval_runtime_list_applies_element_side_effects_once() {
// A runtime `(list …)` desugars to a CONS chain built from the
// already-resolved elements, NOT a re-eval of the source args — so a SETF
// side effect in an element runs exactly once. `n` ends at 1, not 2.
.eval("(let* ((n 0)) (list (transaction-tag-count 0) (setf n (+ n 1))) n)")
vec![Value::Number(Number::Index(1))]
fn test_eval_apply_spreads_the_builtin_list() {
.eval("(defun f (a) (+ a 100)) (apply (function f) (list 5))")
vec![Value::Number(Number::Index(105))]
// QUOTE
fn test_eval_quote_number() {
let results = interp.eval("(quote 42)").unwrap();
assert_eq!(results, vec![Value::Number(Number::Index(42))]);
fn test_eval_quote_reader_sugar() {
let results = interp.eval("'42").unwrap();
fn test_eval_quote_string() {
let results = interp.eval("(quote \"hello\")").unwrap();
assert_eq!(results, vec![Value::String("hello".to_string())]);
// DESCRIBE
fn test_eval_describe_function() {
let results = interp
.eval(r#"(defun sum (a b) "Adds two numbers" (+ a b)) (describe sum)"#)
.unwrap();
match &results[0] {
Value::String(s) => {
assert!(s.contains("SUM is a function"), "got: {s}");
assert!(s.contains("Lambda:"), "got: {s}");
assert!(s.contains("Adds two numbers"), "got: {s}");
_ => panic!("expected string, got {:?}", results[0]),
fn test_eval_describe_variable() {
.eval(r#"(defvar *x* 42 "The answer") (describe *x*)"#)
assert!(s.contains("*X* is a variable"), "got: {s}");
assert!(s.contains("Value: 42"), "got: {s}");
assert!(s.contains("The answer"), "got: {s}");
// Nested calls
fn test_nested_comparison_with_subtraction() {
assert_eq!(interp.eval("(= 1 (- 3 2))").unwrap(), vec![Value::T]);
fn test_nested_addition_with_multiplication() {
interp.eval("(+ 1 (* 2 3))").unwrap(),
vec![Value::Number(Number::Index(7))]
fn test_nested_subtraction_multiple_args() {
interp.eval("(- 10 (+ 1 2) (- 5 2))").unwrap(),
vec![Value::Number(Number::Index(4))]
fn test_nested_addition_both_sides() {
interp.eval("(+ (+ 1 2) (+ 3 4))").unwrap(),
vec![Value::Number(Number::Index(10))]
fn test_deeply_nested_addition() {
interp.eval("(+ 1 (+ 2 (+ 3 4)))").unwrap(),
fn test_nested_calls_with_defun() {
.eval("(defun double (x) (* x 2)) (+ (double 3) (double 4))")
vec![Value::Number(Number::Index(14))]
fn test_nested_equal_with_arithmetic() {
interp.eval("(equal (+ 1 1) (- 4 2))").unwrap(),
fn test_nested_funcall_with_arithmetic() {
interp.eval("(funcall #'+ (- 10 3) (* 2 3))").unwrap(),
vec![Value::Number(Number::Index(13))]
/// A dotted datum onto a list or `nil` tail is the proper list it spells
/// (one `$pair` representation, plan 0014 Stage 1, a symbol member riding a
/// symbol cell); a non-list tail has no cell representation and is the
/// structured rejection, never a printed string.
fn quoted_dotted_lists_are_the_proper_lists_they_spell() {
assert_eq!(interp.eval("'(1 . (2 3))").unwrap(), vec![ints(&[1, 2, 3])]);
assert_eq!(interp.eval("'(1 . nil)").unwrap(), vec![ints(&[1])]);
interp.eval("'(a . nil)").unwrap(),
vec![vec_to_list(vec![Value::Symbol("A".into())])]
let err = interp
.eval("'(1 . 2)")
.expect_err("a dotted pair")
.to_string();
err.contains("'(1 . 2) has no list cell representation"),
"{err}"
// ADR-0033 Phase 3: an index widened into an AnyRef (a missing-else beside a
// count) must decode as its Number, not the pre-Phase-3 "non-null marker"
// boolean.
fn test_count_widened_to_anyref_decodes_as_its_number() {
.eval("(if (>= (entity-count) 0) (entity-count))")
assert_eq!(results, vec![Value::Number(Number::Index(0))]);
// A `t` literal riding a widened `AnyRef` result must decode by identity
// against the `$t` singleton, not the pre-Phase-3 non-null-marker fallback.
fn test_t_widened_to_anyref_decodes_as_t() {
.eval("(if (>= (entity-count) 0) t (entity-count))")
assert_eq!(results, vec![Value::T]);
// ADR-0033 Phase 6: a Ratio riding a widened `AnyRef` result (here, `or`'s
// Bool-vs-Ratio arm mismatch) must decode as its own Number, not the
// pre-Phase-6 non-null-marker fallback (which wrote the `T` tag for any
// struct/array car it didn't recognise).
fn test_ratio_widened_to_anyref_decodes_as_its_number_in_the_debug_serializer() {
.eval("(or (> (entity-count) 5) (index->scalar (entity-count)))")
// A String riding a widened `AnyRef` result must decode as its own String,
// not the pre-Phase-6 non-null-marker fallback.
fn test_string_widened_to_anyref_decodes_as_its_string_in_the_debug_serializer() {
let results = interp.eval(r#"(or (> (entity-count) 5) "x")"#).unwrap();
assert_eq!(results, vec![Value::String("x".to_string())]);
// A list in a dynamic cell is neither the boolean true nor a placeholder: the
// debug serializer walks it like any other list.
fn test_pair_widened_to_anyref_decodes_as_its_list_in_the_debug_serializer() {
.eval("(or (> (transaction-tag-count 0) 0) (list 1))")
vec![Pair::cons(Value::Number(Number::Index(1)), Value::Nil)]
// A flat COND with 25 genuinely-runtime clauses (past the shared recursion
// ceiling of 24) compiles and selects the matching clause; `entity-count` is
// 0, so only the 25th test matches.
fn test_cond_25_genuinely_runtime_clauses_selects_the_matching_clause() {
let leading: String = (1..=24)
.map(|i| format!("((= (entity-count) {i}) {i}) "))
.collect();
let src = format!("(cond {leading}((= (entity-count) 0) 100) (t 999))");
let results = interp.eval(&src).unwrap();
assert_eq!(results, vec![Value::Number(Number::Index(100))]);
// Same chain with no matching clause falls through to the `t` clause.
fn test_cond_25_genuinely_runtime_clauses_falls_through_to_else() {
let clauses: String = (1..=25)
let src = format!("(cond {clauses}(t 999))");
assert_eq!(results, vec![Value::Number(Number::Index(999))]);