Files
Cagire/tests/forth/arithmetic.rs
2026-01-27 01:04:08 +01:00

203 lines
2.4 KiB
Rust

use super::harness::*;
#[test]
fn add_integers() {
expect_int("2 3 +", 5);
}
#[test]
fn add_floats() {
expect_int("2.5 3.5 +", 6);
}
#[test]
fn add_mixed() {
expect_float("2 3.5 +", 5.5);
}
#[test]
fn sub() {
expect_int("10 3 -", 7);
}
#[test]
fn sub_negative() {
expect_int("3 10 -", -7);
}
#[test]
fn mul() {
expect_int("4 5 *", 20);
}
#[test]
fn mul_floats() {
expect_int("2.5 4 *", 10);
}
#[test]
fn div() {
expect_int("10 2 /", 5);
}
#[test]
fn div_float_result() {
expect_float("7 2 /", 3.5);
}
#[test]
fn modulo() {
expect_int("7 3 mod", 1);
}
#[test]
fn modulo_exact() {
expect_int("9 3 mod", 0);
}
#[test]
fn neg_int() {
expect_int("5 neg", -5);
}
#[test]
fn neg_float() {
expect_float("3.5 neg", -3.5);
}
#[test]
fn neg_double() {
expect_int("-5 neg", 5);
}
#[test]
fn abs_positive() {
expect_int("5 abs", 5);
}
#[test]
fn abs_negative() {
expect_int("-5 abs", 5);
}
#[test]
fn abs_float() {
expect_float("-3.5 abs", 3.5);
}
#[test]
fn floor() {
expect_int("3.7 floor", 3);
}
#[test]
fn floor_negative() {
expect_int("-3.2 floor", -4);
}
#[test]
fn ceil() {
expect_int("3.2 ceil", 4);
}
#[test]
fn ceil_negative() {
expect_int("-3.7 ceil", -3);
}
#[test]
fn round_down() {
expect_int("3.4 round", 3);
}
#[test]
fn round_up() {
expect_int("3.6 round", 4);
}
#[test]
fn round_half() {
expect_int("3.5 round", 4);
}
#[test]
fn min() {
expect_int("3 5 min", 3);
}
#[test]
fn min_reverse() {
expect_int("5 3 min", 3);
}
#[test]
fn max() {
expect_int("3 5 max", 5);
}
#[test]
fn max_reverse() {
expect_int("5 3 max", 5);
}
#[test]
fn chain() {
// (2 + 3) * 4 - 1 = 19
expect_int("2 3 + 4 * 1 -", 19);
}
#[test]
fn underflow() {
expect_error("1 +", "stack underflow");
}
#[test]
fn pow_int() {
expect_int("2 3 pow", 8);
}
#[test]
fn pow_float() {
expect_float("2 0.5 pow", std::f64::consts::SQRT_2);
}
#[test]
fn sqrt() {
expect_int("16 sqrt", 4);
}
#[test]
fn sqrt_float() {
expect_float("2 sqrt", std::f64::consts::SQRT_2);
}
#[test]
fn sin_zero() {
expect_int("0 sin", 0);
}
#[test]
fn sin_pi_half() {
expect_float("3.14159265358979 2 / sin", 1.0);
}
#[test]
fn cos_zero() {
expect_int("0 cos", 1);
}
#[test]
fn cos_pi() {
expect_int("3.14159265358979 cos", -1);
}
#[test]
fn log_e() {
expect_float("2.718281828459045 log", 1.0);
}
#[test]
fn log_one() {
expect_int("1 log", 0);
}