Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pcg64Si unit tests #618

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/pcg64si.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ impl SeedableRng for Pcg64Si {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;

// For a given seed the RNG is deterministic
// thus we can perform some basic tests consistently
#[test]
fn test_rng_next() {
let mut rng = Pcg64Si::from_seed([1, 2, 3, 4, 5, 6, 7, 8]);
let mut values_set: HashSet<u32> = HashSet::new();
// Generate 1000 values modulus 100 (so each value is between 0 and 99)
for _ in 0..1000 {
values_set.insert(rng.next_u32() % 100);
}
// Expect to generate every number between 0 and 99 (the generated values are somewhat evenly distributed)
assert_eq!(values_set.len(), 100);
}

#[test]
fn test_rng_from_seed() {
// Different seeds should result in a different RNG state
let rng1 = Pcg64Si::from_seed([1, 2, 3, 4, 5, 6, 7, 8]);
let rng2 = Pcg64Si::from_seed([1, 2, 3, 4, 5, 6, 7, 7]);
assert_ne!(rng1.state, rng2.state);
}

#[test]
fn test_rng_fill_bytes() {
// This uses the next_u64/u32 functions underneath, so don't need to test the pseudo randomness again
let mut array: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
let mut rng = Pcg64Si::from_seed([1, 2, 3, 4, 5, 6, 7, 8]);
let result = rng.try_fill_bytes(&mut array);
assert!(result.is_ok());
assert_ne!(array, [0, 0, 0, 0, 0, 0, 0, 0]);
}
}