use super::harness::*;

#[test]
fn fetch_store() {
    expect_int(r#"42 !x @x"#, 42);
}

#[test]
fn fetch_nonexistent() {
    expect_int(r#"@novar"#, 0);
}

#[test]
fn persistence_across_evals() {
    let f = forth();
    let ctx = default_ctx();
    f.evaluate(r#"10 !counter"#, &ctx).unwrap();
    f.clear_stack();
    f.evaluate(r#"@counter 1 +"#, &ctx).unwrap();
    assert_eq!(stack_int(&f), 11);
}

#[test]
fn overwrite() {
    expect_int(r#"1 !x 99 !x @x"#, 99);
}

#[test]
fn multiple_vars() {
    let f = run(r#"10 !a 20 !b @a @b +"#);
    assert_eq!(stack_int(&f), 30);
}

#[test]
fn float_var() {
    let f = run(r#"3.14 !pi @pi"#);
    let val = stack_float(&f);
    assert!((val - 3.14).abs() < 1e-9);
}

#[test]
fn increment_pattern() {
    expect_int(r#"0 !n @n 1 + !n @n 1 + !n @n"#, 2);
}

#[test]
fn set_keep() {
    expect_int(r#"42 ,x"#, 42);
}

#[test]
fn set_keep_stores() {
    let f = forth();
    let ctx = default_ctx();
    f.evaluate(r#"42 ,x"#, &ctx).unwrap();
    f.clear_stack();
    f.evaluate(r#"@x"#, &ctx).unwrap();
    assert_eq!(stack_int(&f), 42);
}

#[test]
fn set_keep_chain() {
    let f = forth();
    let ctx = default_ctx();
    f.evaluate(r#"10 ,a ,b"#, &ctx).unwrap();
    f.clear_stack();
    f.evaluate(r#"@a @b +"#, &ctx).unwrap();
    assert_eq!(stack_int(&f), 20);
}
