Feat: lots of convenience stuff

This commit is contained in:
2026-02-24 00:52:40 +01:00
parent 8f131b46cc
commit 848d0e773f
15 changed files with 440 additions and 26 deletions

View File

@@ -83,3 +83,99 @@ fn cycle_zero_count_error() {
fn choose_zero_count_error() {
expect_error("1 2 3 0 choose", "choose count must be > 0");
}
// Bracket syntax tests
#[test]
fn bracket_cycle() {
let ctx = ctx_with(|c| c.runs = 0);
let f = run_ctx("[ 10 20 30 ] cycle", &ctx);
assert_eq!(stack_int(&f), 10);
let ctx = ctx_with(|c| c.runs = 1);
let f = run_ctx("[ 10 20 30 ] cycle", &ctx);
assert_eq!(stack_int(&f), 20);
let ctx = ctx_with(|c| c.runs = 2);
let f = run_ctx("[ 10 20 30 ] cycle", &ctx);
assert_eq!(stack_int(&f), 30);
let ctx = ctx_with(|c| c.runs = 3);
let f = run_ctx("[ 10 20 30 ] cycle", &ctx);
assert_eq!(stack_int(&f), 10);
}
#[test]
fn bracket_with_quotations() {
let ctx = ctx_with(|c| c.runs = 0);
let f = run_ctx("5 [ { 3 + } { 5 + } ] cycle", &ctx);
assert_eq!(stack_int(&f), 8);
let ctx = ctx_with(|c| c.runs = 1);
let f = run_ctx("5 [ { 3 + } { 5 + } ] cycle", &ctx);
assert_eq!(stack_int(&f), 10);
}
#[test]
fn bracket_nested() {
let ctx = ctx_with(|c| { c.runs = 0; c.iter = 0; });
let f = run_ctx("[ [ 10 20 ] cycle [ 30 40 ] cycle ] pcycle", &ctx);
assert_eq!(stack_int(&f), 10);
let ctx = ctx_with(|c| { c.runs = 0; c.iter = 1; });
let f = run_ctx("[ [ 10 20 ] cycle [ 30 40 ] cycle ] pcycle", &ctx);
assert_eq!(stack_int(&f), 30);
}
#[test]
fn bracket_with_generator() {
let ctx = ctx_with(|c| c.runs = 0);
let f = run_ctx("[ 1 4 .. ] cycle", &ctx);
assert_eq!(stack_int(&f), 1);
let ctx = ctx_with(|c| c.runs = 3);
let f = run_ctx("[ 1 4 .. ] cycle", &ctx);
assert_eq!(stack_int(&f), 4);
}
#[test]
fn stray_bracket_error() {
expect_error("10 ] cycle", "unexpected ]");
}
#[test]
fn unclosed_bracket_error() {
expect_error("[ 10 20", "missing ]");
}
// Index tests
#[test]
fn index_basic() {
expect_int("10 20 30 3 1 index", 20);
}
#[test]
fn index_with_brackets() {
expect_int("[ 10 20 30 ] 1 index", 20);
}
#[test]
fn index_modulo_wraps() {
expect_int("[ 10 20 30 ] 5 index", 30);
}
#[test]
fn index_negative_wraps() {
expect_int("[ 10 20 30 ] -1 index", 30);
}
#[test]
fn index_with_quotation() {
expect_int("5 [ { 3 + } { 5 + } ] 0 index", 8);
}
#[test]
fn index_zero_count_error() {
expect_error("0 0 index", "index count must be > 0");
}