use monty::MontyRun;
use monty_types::{CompileOptions, MontyObject};
fn run_expr(code: &str) -> MontyObject {
let ex = MontyRun::new(code.to_owned(), "test.py", vec![], CompileOptions::default()).unwrap();
ex.run_no_limits(vec![]).unwrap()
}
fn run_expect_error(code: &str) -> String {
let ex = MontyRun::new(code.to_owned(), "test.py", vec![], CompileOptions::default()).unwrap();
let err = ex.run_no_limits(vec![]).unwrap_err();
err.to_string()
}
#[test]
fn factorial_i64_overflow() {
let msg = run_expect_error("import math\nmath.factorial(21)");
assert!(
msg.contains("OverflowError"),
"Expected OverflowError for factorial(21), got: {msg}"
);
}
#[test]
fn comb_large_but_fits_i64() {
let result = run_expr("import math\nmath.comb(66, 33)");
let v: i64 = (&result).try_into().unwrap();
assert_eq!(v, 7_219_428_434_016_265_740);
}
#[test]
fn comb_i64_overflow() {
let msg = run_expect_error("import math\nmath.comb(68, 34)");
assert!(
msg.contains("OverflowError"),
"Expected OverflowError for comb(68, 34), got: {msg}"
);
}
#[test]
fn perm_i64_overflow() {
let msg = run_expect_error("import math\nmath.perm(21, 21)");
assert!(
msg.contains("OverflowError"),
"Expected OverflowError for perm(21, 21), got: {msg}"
);
}
#[test]
fn ldexp_large_negative_exponent_loop() {
let result = run_expr("import math\nmath.ldexp(1.0, -1050)");
let f: f64 = (&result).try_into().unwrap();
assert!(f > 0.0, "ldexp(1.0, -1050) should be positive, got: {f}");
assert!(f < 1e-300, "ldexp(1.0, -1050) should be tiny, got: {f}");
}
#[test]
fn ldexp_minimum_subnormal() {
let result = run_expr("import math\nmath.ldexp(1.0, -1074)");
let f: f64 = (&result).try_into().unwrap();
assert_eq!(
f.to_bits(),
5e-324_f64.to_bits(),
"ldexp(1.0, -1074) should equal 5e-324"
);
}
#[test]
fn isqrt_large_values_newton_refinement() {
let result = run_expr("import math\nmath.isqrt(9223372036854775807)");
let v: i64 = (&result).try_into().unwrap();
assert_eq!(v, 3_037_000_499);
let result = run_expr("import math\nmath.isqrt(9223372030926249001)");
let v: i64 = (&result).try_into().unwrap();
assert_eq!(v, 3_037_000_499);
let result = run_expr("import math\nmath.isqrt(9223372030926249000)");
let v: i64 = (&result).try_into().unwrap();
assert_eq!(v, 3_037_000_498);
}