//! Tests for the RNG functionality. use claims::*; use expy::{Context, Error, EvalError, eval, eval_in}; const TRIES: usize = 256; #[test] fn rand_no_args() { for _ in 0..TRIES { let x = eval("rand()").unwrap().unwrap_float(); assert_ge!(x, 0.); assert_lt!(x, 1.); } } #[test] fn rand_one_arg_integer() { let mut ctx = Context::new(); // rand(x) draws from 0..x, so rand(0) doesn't make sense (it'd be an empty range). assert_eq!( eval("rand(0)").unwrap_err(), Error::Eval(EvalError::argument("positive bound", 0))); for _ in 0..TRIES { let max = fastrand::i64(1..65536); ctx.set("max", max); let x = eval_in(&mut ctx, "rand(max)").unwrap().unwrap_integer(); assert_ge!(x, 0); assert_lt!(x, max); } } #[test] fn rand_one_arg_float() { let mut ctx = Context::new(); for _ in 0..TRIES { let max = fastrand::f32() * 65536.; ctx.set("max", max); let x = eval_in(&mut ctx, "rand(max)").unwrap().unwrap_float(); assert_ge!(x, 0.); assert_lt!(x, max); } } #[test] fn rand_two_args_integers() { let mut ctx = Context::new(); for _ in 0..TRIES { let min = fastrand::i64(0..65535); let max = fastrand::i64(min..65536); ctx.set("min", min); ctx.set("max", max); let x = eval_in(&mut ctx, "rand(min, max)").unwrap().unwrap_integer(); assert_ge!(x, min); assert_le!(x, max); } } #[test] fn rand_two_args_floats() { let mut ctx = Context::new(); for _ in 0..TRIES { let min = fastrand::f32() * 65535.; let max = min + fastrand::f32() * (65536. - min); ctx.set("min", min); ctx.set("max", max); let x = eval_in(&mut ctx, "rand(min, max)").unwrap().unwrap_float(); assert_ge!(x, min); assert_lt!(x, max); } }