From 6060f1a8ae5ade1c4cbf0106ce6c0c7b18c9533b Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Fri, 13 Apr 2018 14:54:58 +0200 Subject: [PATCH] Test reseeding on clone --- src/reseeding.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/reseeding.rs b/src/reseeding.rs index e34e5acc434..0f7f049429a 100644 --- a/src/reseeding.rs +++ b/src/reseeding.rs @@ -19,6 +19,8 @@ use rand_core::impls::BlockRng; /// A wrapper around any PRNG which reseeds the underlying PRNG after it has /// generated a certain number of random bytes. /// +/// When the RNG gets cloned, the clone is reseeded on first use. +/// /// Reseeding is never strictly *necessary*. Cryptographic PRNGs don't have a /// limited number of bytes they can output, or at least not a limit reachable /// in any practical way. There is no such thing as 'running out of entropy'. @@ -242,4 +244,17 @@ mod test { assert_eq!(buf, seq); } } + + #[test] + fn test_clone_reseeding() { + let mut zero = StepRng::new(0, 0); + let rng = ChaChaCore::from_rng(&mut zero).unwrap(); + let mut rng1 = ReseedingRng::new(rng, 32*4, zero); + + let first: u32 = rng1.gen(); + for _ in 0..10 { let _ = rng1.gen::(); } + + let mut rng2 = rng1.clone(); + assert_eq!(first, rng2.gen::()); + } }