diff --git a/src/macros.rs b/src/macros.rs index 9474486..ab66838 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -42,12 +42,31 @@ macro_rules! random_range { }; } -/// Generate a random boolean with the provided probability using the -/// provided `Random (VRD)` struct +/// Generate a random boolean with a provided probability. +/// +/// # Examples +/// +/// ``` +/// #[macro_use] extern crate vrd; +/// # use vrd::Random; +/// # use vrd::rand_bool; +/// # fn main() { +/// # let mut rng = Random::new(); +/// // Generates a boolean with 50% probability of being true +/// # let value = rand_bool!(rng, 0.5); +/// # } +/// ``` +/// +/// # Panics +/// +/// Panics if probability is not between 0.0 and 1.0. #[macro_export] macro_rules! rand_bool { ($rng:expr, $probability:expr) => { - $rng.bool($probability) + { + assert!($probability >= 0.0 && $probability <= 1.0, "Probability must be between 0.0 and 1.0"); + $rng.bool($probability) + } }; } @@ -153,3 +172,4 @@ macro_rules! rand_twist { $rng.twist() }; } + diff --git a/tests/test_macros.rs b/tests/test_macros.rs index 28996da..0d751dd 100644 --- a/tests/test_macros.rs +++ b/tests/test_macros.rs @@ -10,21 +10,28 @@ mod tests { use vrd::Random; use vrd::*; + #[test] - fn test_random_range_macro() { + fn test_random_range_macro_within_bounds() { let mut rng = Random::new(); let min = 10; let max = 20; let num = random_range!(rng, min, max); - assert!(num >= min && num < max); + assert!(num >= min && num <= max, "Number should be within the given range."); } #[test] - fn test_rand_bool_macro() { + fn test_rand_bool_macro_always_true() { let mut rng = Random::new(); - let p = 1.0; // Set p to 1.0 to always generate true - let b = rand_bool!(rng, p); - assert!(b); + let b = rand_bool!(rng, 1.0); + assert!(b, "rand_bool should always return true with probability 1.0."); + } + + #[test] + fn test_rand_bool_macro_always_false() { + let mut rng = Random::new(); + let b = rand_bool!(rng, 0.0); + assert!(!b, "rand_bool should always return false with probability 0.0."); } #[test] @@ -32,49 +39,48 @@ mod tests { let mut rng = Random::new(); let len = 10; let bytes = rand_bytes!(rng, len); - assert_eq!(bytes.len(), len); + assert_eq!(bytes.len(), len, "Length of bytes should be equal to the specified length."); } #[test] - fn test_rand_char_macro() { + fn test_rand_char_macro_ascii_check() { let mut rng = Random::new(); let c = rand_char!(rng); assert!( - c.is_ascii_lowercase() - || c.is_ascii_uppercase() - || c.is_ascii_digit() + c.is_ascii_lowercase() || c.is_ascii_uppercase() || c.is_ascii_digit(), + "Generated character should be ASCII lowercase, uppercase, or digit." ); } #[test] - fn test_rand_choose_macro() { + fn test_rand_choose_macro_value_in_slice() { let mut rng = Random::new(); let values = vec![1, 2, 3, 4, 5]; let chosen = rand_choose!(rng, &values).unwrap(); - assert!(values.contains(chosen)); + assert!(values.contains(&chosen), "Chosen value should be in the provided slice."); } #[test] - fn test_rand_float_macro() { + fn test_rand_float_macro_within_bounds() { let mut rng = Random::new(); let f = rand_float!(rng); - assert!((0.0..1.0).contains(&f)); + assert!((0.0..1.0).contains(&f), "Generated float should be within 0.0 and 1.0."); } #[test] - fn test_rand_int_macro() { + fn test_rand_int_macro_within_range() { let mut rng = Random::new(); let min = 10; let max = 20; let num = rand_int!(rng, min, max); - assert!(num >= min && num <= max); + assert!(num >= min && num <= max, "Generated integer should be within the specified range."); } #[test] - fn test_rand_pseudo_macro() { + fn test_rand_pseudo_macro_upper_bound() { let mut rng = Random::new(); let p = rand_pseudo!(rng); - assert!(p < 4294967295); + assert!(p < 4294967295, "Generated pseudo random number should be less than 4294967295."); } #[test] @@ -101,4 +107,39 @@ mod tests { let num = rng.rand(); assert!(num < 4294967295); } + + #[test] + fn test_random_range_macro_valid_range() { + let mut rng = Random::new(); + let min = 10; + let max = 20; + let num = random_range!(rng, min, max); + assert!(num >= min && num < max, "Number should be within the given range."); + } + + #[test] + fn test_rand_bool_macro_true() { + let mut rng = Random::new(); + let p = 1.0; // Set p to 1.0 to always generate true + let b = rand_bool!(rng, p); + assert!(b, "The probability of 1.0 should always return true."); + } + + #[test] + fn test_rand_bool_macro_false() { + let mut rng = Random::new(); + let p = 0.0; // Set p to 0.0 to always generate false + let b = rand_bool!(rng, p); + assert!(!b, "The probability of 0.0 should always return false."); + } + + #[test] + fn test_rand_int_macro_valid_range() { + let mut rng = Random::new(); + let min = 10; + let max = 20; + let num = rand_int!(rng, min, max); + assert!(num >= min && num <= max, "Number should be within the given range."); + } + }