diff --git a/src/pcg64si.rs b/src/pcg64si.rs index 558d0dad..f71f4aa2 100644 --- a/src/pcg64si.rs +++ b/src/pcg64si.rs @@ -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 = 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]); + } +}