Skip to content

Commit

Permalink
test(vrd): ♻️ new macros
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Dec 26, 2023
1 parent 9225694 commit b3b3b41
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 22 deletions.
26 changes: 23 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
};
}

Expand Down Expand Up @@ -153,3 +172,4 @@ macro_rules! rand_twist {
$rng.twist()
};
}

79 changes: 60 additions & 19 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,77 @@ 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]
fn test_rand_bytes_macro() {
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.");

Check failure on line 60 in tests/test_macros.rs

View workflow job for this annotation

GitHub Actions / Lint

this expression creates a reference which is immediately dereferenced by the compiler
}

#[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]
Expand All @@ -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);

Check failure on line 124 in tests/test_macros.rs

View workflow job for this annotation

GitHub Actions / Lint

manual `RangeInclusive::contains` implementation
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);

Check failure on line 132 in tests/test_macros.rs

View workflow job for this annotation

GitHub Actions / Lint

manual `RangeInclusive::contains` implementation
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.");
}

}

0 comments on commit b3b3b41

Please sign in to comment.