This commit is contained in:
2026-01-21 17:05:30 +01:00
commit 67322381c3
59 changed files with 10421 additions and 0 deletions

106
tests/forth/errors.rs Normal file
View File

@@ -0,0 +1,106 @@
use super::harness::*;
#[test]
fn empty_script() {
expect_error("", "empty script");
}
#[test]
fn whitespace_only() {
expect_error(" \n\t ", "empty script");
}
#[test]
fn unknown_word() {
expect_error("foobar", "unknown word");
}
#[test]
fn string_not_number() {
expect_error(r#""hello" neg"#, "expected number");
}
#[test]
fn get_expects_string() {
expect_error("42 get", "expected string");
}
#[test]
fn set_expects_string() {
expect_error("1 2 set", "expected string");
}
#[test]
fn comment_ignored() {
expect_int("1 (this is a comment) 2 +", 3);
}
#[test]
fn multiline_comment() {
expect_int("1 (multi\nline\ncomment) 2 +", 3);
}
#[test]
fn negative_literal() {
expect_int("-5", -5);
}
#[test]
fn float_literal() {
let f = run("3.14159");
let val = stack_float(&f);
assert!((val - 3.14159).abs() < 1e-9);
}
#[test]
fn string_with_spaces() {
let f = run(r#""hello world" "x" set "x" get"#);
match stack_top(&f) {
seq::model::forth::Value::Str(s, _) => assert_eq!(s, "hello world"),
other => panic!("expected string, got {:?}", other),
}
}
#[test]
fn list_count() {
// [ 1 2 3 ] => stack: 1 2 3 3 (items + count on top)
let f = run("[ 1 2 3 ]");
assert_eq!(stack_int(&f), 3);
}
#[test]
fn list_empty() {
expect_int("[ ]", 0);
}
#[test]
fn list_preserves_values() {
// [ 10 20 ] => stack: 10 20 2
// drop => 10 20
// + => 30
expect_int("[ 10 20 ] drop +", 30);
}
#[test]
fn conditional_based_on_step() {
let ctx0 = ctx_with(|c| c.step = 0);
let ctx1 = ctx_with(|c| c.step = 1);
let f0 = run_ctx("step 2 mod 0 = if 100 else 200 then", &ctx0);
let f1 = run_ctx("step 2 mod 0 = if 100 else 200 then", &ctx1);
assert_eq!(stack_int(&f0), 100);
assert_eq!(stack_int(&f1), 200);
}
#[test]
fn accumulator() {
let f = forth();
let ctx = default_ctx();
f.evaluate(r#"0 "acc" set"#, &ctx).unwrap();
for _ in 0..5 {
f.clear_stack();
f.evaluate(r#""acc" get 1 + dup "acc" set"#, &ctx).unwrap();
}
assert_eq!(stack_int(&f), 5);
}