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))]
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Number, Value};
#[test]
fn test_eval_num_eq() {
let mut interp = Interpreter::new(false).unwrap();
assert_eq!(interp.eval("(= 1 1 1)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(= 1 2)").unwrap(), vec![Value::Nil]);
}
fn test_eval_num_eq_single() {
assert_eq!(interp.eval("(= 42)").unwrap(), vec![Value::T]);
fn test_eval_num_neq() {
assert_eq!(interp.eval("(/= 1 2 3)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(/= 1 2 1)").unwrap(), vec![Value::Nil]);
fn test_eval_num_lt() {
assert_eq!(interp.eval("(< 1 2 3)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(< 1 2 2)").unwrap(), vec![Value::Nil]);
fn test_eval_num_gt() {
assert_eq!(interp.eval("(> 3 2 1)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(> 3 3 1)").unwrap(), vec![Value::Nil]);
fn test_eval_num_le() {
assert_eq!(interp.eval("(<= 1 2 2 3)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(<= 1 3 2)").unwrap(), vec![Value::Nil]);
fn test_eval_num_ge() {
assert_eq!(interp.eval("(>= 3 2 2 1)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(>= 1 2 3)").unwrap(), vec![Value::Nil]);
fn test_eval_comparison_type_error() {
assert!(interp.eval("(= 1 \"one\")").is_err());
assert!(interp.eval("(< \"a\" \"b\")").is_err());
fn test_eval_eql_numbers() {
assert_eq!(interp.eval("(eql 1 1)").unwrap(), vec![Value::T]);
assert_eq!(interp.eval("(eql 1 2)").unwrap(), vec![Value::Nil]);
fn test_eval_eql_strings() {
assert_eq!(interp.eval(r#"(eql "abc" "abc")"#).unwrap(), vec![Value::T]);
assert_eq!(
interp.eval(r#"(eql "abc" "def")"#).unwrap(),
vec![Value::Nil]
);
fn test_eval_eql_mixed_types() {
assert_eq!(interp.eval(r#"(eql 1 "1")"#).unwrap(), vec![Value::Nil]);
fn test_eval_eql_nil() {
assert_eq!(interp.eval("(eql nil nil)").unwrap(), vec![Value::T]);
fn test_eval_eql_arity_error() {
assert!(interp.eval("(eql 1)").is_err());
assert!(interp.eval("(eql 1 2 3)").is_err());
fn test_eval_equal_strings() {
interp.eval(r#"(equal "hello" "hello")"#).unwrap(),
vec![Value::T]
interp.eval(r#"(equal "hello" "world")"#).unwrap(),
fn test_eval_equal_numbers() {
assert_eq!(interp.eval("(equal 0.5 0.5)").unwrap(), vec![Value::T]);
fn test_eval_equal_arity_error() {
assert!(interp.eval("(equal 1)").is_err());
// --- Runtime bool serialization (WasmType::Bool) ---
// `(transaction-tag-count 0)` is a RUNTIME i32 (0 on the minimal input), so
// these comparisons/predicates can't const-fold — they exercise the runtime
// producers. The result must serialize as Bool/Nil (the falsy/truthy pair),
// not Number, mirroring the const-fold path above.
fn test_runtime_comparison_true_is_bool() {
interp.eval("(= (transaction-tag-count 0) 0)").unwrap(),
fn test_runtime_comparison_false_is_nil() {
interp.eval("(< 0 (transaction-tag-count 0))").unwrap(),
fn test_runtime_not_is_bool() {
// (not <runtime-false>) → true; tag-count is 0 (falsy) so not → Bool(true).
interp
.eval("(not (< 0 (transaction-tag-count 0)))")
.unwrap(),
fn test_runtime_and_or_are_bool() {
// and of two runtime comparisons: tag-count 0 == 0 (true) and 0 >= 0
// (true) → Bool(true).
.eval("(and (= (transaction-tag-count 0) 0) (>= (transaction-tag-count 0) 0))")
// or short-circuits on the first truthy runtime comparison. This case
// (true FIRST arg) is the regression: the old effect-only short-circuit
// helper produced no output entity on the early-exit branch, so a
// top-level `(or <true> …)` returned "no output entities". The
// value-producing stack path yields a result on every branch.
.eval("(or (= (transaction-tag-count 0) 0) (< (transaction-tag-count 0) 0))")
// false first, true second — exercises the short-circuit fall-through too.
.eval("(or (< (transaction-tag-count 0) 0) (= (transaction-tag-count 0) 0))")
fn test_unary_runtime_and_or_over_a_count_is_the_count_itself() {
// ADR-0033: `and`/`or` return values, as in Common Lisp. `(and a)` / `(or
// a)` (a single operand) is just `a` itself, not a collapsed boolean —
// `(transaction-tag-count 0)` is 0 on the minimal input.
interp.eval("(and (transaction-tag-count 0))").unwrap(),
vec![Value::Number(Number::Index(0))]
interp.eval("(or (transaction-tag-count 0))").unwrap(),
fn test_unary_and_or_over_a_bound_count_local_is_the_count_itself() {
// A bound runtime local (`WasmLocal(_, I32)`) must get the same result as a
// fresh placeholder.
.eval("(let* ((n (transaction-tag-count 0)) (x (and n))) x)")
.eval("(let* ((n (transaction-tag-count 0)) (x (or n))) x)")
fn test_effect_position_and_or_accepts_a_ref_runtime_operand() {
// ADR-0033: any value is a valid test — `reject_non_boolean_runtime_test`
// is gone. A ref-typed runtime operand (`(transaction-post-date 0)` is a
// runtime Ratio) in effect position now compiles instead of erroring.
.eval(r#"(and (transaction-post-date 0) 1) 7"#)
.expect("a ref-typed AND operand in effect position must compile");
.eval(r#"(or (transaction-post-date 0) 1) 7"#)
.expect("a ref-typed OR operand in effect position must compile");
fn test_runtime_and_or_accepts_a_ref_typed_operand() {
// ADR-0033: `and`/`or` rewrite into an `IF` chain (arm reconciliation),
// not an i32-only short-circuit block, so a ref-typed (StringRef/…)
// operand rides it like any other value.
.eval(r#"(and (= (transaction-tag-count 0) 0) "x")"#)
.expect("a ref-typed AND operand must compile");
fn test_runtime_nullable_ref_in_and_or_is_a_valid_test() {
// #58 / ADR-0033: a let-bound runtime PairRef that is nil at runtime is a
// valid `or`/`and` test now (any value is a valid test) — nil beside the
// literal `7` widens the result to `AnyRef`, it is not rejected.
let nullable = "(let* ((x (cdr (cons (transaction-tag-count 0) nil)))) ";
.eval(&format!("{nullable} (or x 7))"))
.expect("a nullable PairRef test in OR must compile");
.eval(&format!("{nullable} (and x 7))"))
.expect("a nullable PairRef test in AND must compile");
// The `(null? …)` escape hatch yields a Bool, so the form compiles and the
// null ref reads as truthy-null: `(or (null? x) …)` → Bool(true).
.eval(&format!(
"{nullable} (or (null? x) (= (transaction-tag-count 0) 5)))"
))
// --- Phase 7 (0010-common-lisp-booleans.org): identity against a type that
// can never be `t`/`nil` (`nil_repr` is `Never` — Index, Ratio, Commodity). A
// runtime Index (`transaction-tag-count`) and Ratio (`transaction-post-date`)
// producer stand in for the never-nil types this doesn't require a database
// for; the Commodity case lives in
// `tests-integration/tests/eq_identity_never_nil.rs`, since a runtime
// Commodity only exists behind the DB-backed `account-balance` host fn.
// `t`/`nil` are unique objects, so no value of such a type is ever one of
// them — the comparison is `nil`, not the old "= expects numeric arguments,
// got nil" compile error.
fn test_eq_nil_against_runtime_index_is_nil() {
interp.eval("(eq? nil (transaction-tag-count 0))").unwrap(),
fn test_eq_runtime_index_against_nil_is_nil_reversed_order() {
interp.eval("(eq? (transaction-tag-count 0) nil)").unwrap(),
fn test_eq_t_against_runtime_index_is_nil() {
interp.eval("(eq? t (transaction-tag-count 0))").unwrap(),
fn test_eq_nil_against_runtime_ratio_is_nil() {
interp.eval("(eq? nil (transaction-post-date 0))").unwrap(),
fn test_eq_runtime_ratio_against_t_is_nil_reversed_order() {
interp.eval("(eq? (transaction-post-date 0) t)").unwrap(),
/// A never-nil operand's own effects (a `setf` inside a `progn`) must still
/// run — exactly once, not zero and not twice — even though `eq?` never
/// needs the operand's VALUE (the result is statically `nil`). `n` ends at
/// `1`: dropped effects would leave it `0`, a doubled compile-time-probe-plus-
/// real-emission bug would leave it `2`.
fn test_eq_nil_against_never_nil_operand_runs_effect_exactly_once() {
.eval(
"(let* ((n 0)) \
(eq? nil (progn (setf n (+ n 1)) (transaction-tag-count 0))) \
n)"
)
vec![Value::Number(Number::Index(1))]