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

99
tests/forth/context.rs Normal file
View File

@@ -0,0 +1,99 @@
use super::harness::*;
#[test]
fn step() {
let ctx = ctx_with(|c| c.step = 7);
let f = run_ctx("step", &ctx);
assert_eq!(stack_int(&f), 7);
}
#[test]
fn beat() {
let ctx = ctx_with(|c| c.beat = 4.5);
let f = run_ctx("beat", &ctx);
assert!((stack_float(&f) - 4.5).abs() < 1e-9);
}
#[test]
fn pattern() {
let ctx = ctx_with(|c| c.pattern = 3);
let f = run_ctx("pattern", &ctx);
assert_eq!(stack_int(&f), 3);
}
#[test]
fn tempo() {
let ctx = ctx_with(|c| c.tempo = 140.0);
let f = run_ctx("tempo", &ctx);
assert!((stack_float(&f) - 140.0).abs() < 1e-9);
}
#[test]
fn phase() {
let ctx = ctx_with(|c| c.phase = 0.25);
let f = run_ctx("phase", &ctx);
assert!((stack_float(&f) - 0.25).abs() < 1e-9);
}
#[test]
fn slot() {
let ctx = ctx_with(|c| c.slot = 5);
let f = run_ctx("slot", &ctx);
assert_eq!(stack_int(&f), 5);
}
#[test]
fn runs() {
let ctx = ctx_with(|c| c.runs = 10);
let f = run_ctx("runs", &ctx);
assert_eq!(stack_int(&f), 10);
}
#[test]
fn iter() {
let ctx = ctx_with(|c| c.iter = 5);
let f = run_ctx("iter", &ctx);
assert_eq!(stack_int(&f), 5);
}
#[test]
fn every_true_on_zero() {
let ctx = ctx_with(|c| c.iter = 0);
let f = run_ctx("4 every", &ctx);
assert_eq!(stack_int(&f), 1);
}
#[test]
fn every_true_on_multiple() {
let ctx = ctx_with(|c| c.iter = 8);
let f = run_ctx("4 every", &ctx);
assert_eq!(stack_int(&f), 1);
}
#[test]
fn every_false_between() {
for i in 1..4 {
let ctx = ctx_with(|c| c.iter = i);
let f = run_ctx("4 every", &ctx);
assert_eq!(stack_int(&f), 0, "iter={} should be false", i);
}
}
#[test]
fn every_zero_count() {
expect_error("0 every", "every count must be > 0");
}
#[test]
fn stepdur() {
// stepdur = 60.0 / tempo / 4.0 / speed = 60 / 120 / 4 / 1 = 0.125
let f = run("stepdur");
assert!((stack_float(&f) - 0.125).abs() < 1e-9);
}
#[test]
fn context_in_computation() {
let ctx = ctx_with(|c| c.step = 3);
let f = run_ctx("60 step +", &ctx);
assert_eq!(stack_int(&f), 63);
}