Skip to content

Commit

Permalink
Merge pull request #618 from wjhoward/pcg64si-unit-tests
Browse files Browse the repository at this point in the history
Pcg64Si unit tests
  • Loading branch information
hatoo authored Dec 1, 2024
2 parents 82a751d + bc7e4a5 commit eb815d6
Showing 1 changed file with 38 additions and 0 deletions.
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]);
}
}

0 comments on commit eb815d6

Please sign in to comment.