From f2098548214c1bc617dbb358a5d64ef6d2621fbc Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 7 May 2019 16:23:45 +0100 Subject: [PATCH] Fix/update value-stability tests --- src/distributions/unit_circle.rs | 6 +++--- src/distributions/unit_sphere.rs | 6 +++--- src/lib.rs | 6 ++++-- src/rngs/adapter/reseeding.rs | 18 ++++++++++-------- src/rngs/std.rs | 18 ++++++++++++++---- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/distributions/unit_circle.rs b/src/distributions/unit_circle.rs index 74a7ea727dc..d078f182d6f 100644 --- a/src/distributions/unit_circle.rs +++ b/src/distributions/unit_circle.rs @@ -86,8 +86,8 @@ mod tests { fn value_stability() { let mut rng = ::test::rng(2); let dist = UnitCircle::new(); - assert_eq!(dist.sample(&mut rng), [-0.8032118336637037, 0.5956935036263119]); - assert_eq!(dist.sample(&mut rng), [-0.4742919588505423, -0.880367615130018]); - assert_eq!(dist.sample(&mut rng), [0.9297328981467168, 0.368234623716601]); + assert_eq!(dist.sample(&mut rng), [0.968253135419177, 0.2499717299034683]); + assert_eq!(dist.sample(&mut rng), [-0.2083028196300198, 0.9780643819985387]); + assert_eq!(dist.sample(&mut rng), [0.4284767927057159, -0.9035527865667964]); } } diff --git a/src/distributions/unit_sphere.rs b/src/distributions/unit_sphere.rs index af53cdc97b3..4d895ed2359 100644 --- a/src/distributions/unit_sphere.rs +++ b/src/distributions/unit_sphere.rs @@ -82,10 +82,10 @@ mod tests { let mut rng = ::test::rng(2); let dist = UnitSphereSurface::new(); assert_eq!(dist.sample(&mut rng), - [-0.24950027180862533, -0.7552572587896719, 0.6060825747478084]); + [0.9754391834492048, 0.12388255134190126, -0.18213048307455182]); assert_eq!(dist.sample(&mut rng), - [0.47604534507233487, -0.797200864987207, -0.3712837328763685]); + [0.45487349593184284, 0.5619516852114866, -0.6908693119445342]); assert_eq!(dist.sample(&mut rng), - [0.9795722330927367, 0.18692349236651176, 0.07414747571708524]); + [-0.8374011645099787, 0.5296803977011814, 0.13489983679919715]); } } diff --git a/src/lib.rs b/src/lib.rs index ec2753fda3d..0d398f8d75a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -556,12 +556,14 @@ where Standard: Distribution { #[cfg(test)] mod test { use rngs::mock::StepRng; - use rngs::StdRng; + use super::*; #[cfg(all(not(feature="std"), feature="alloc"))] use alloc::boxed::Box; pub fn rng(seed: u64) -> impl RngCore { - StdRng::seed_from_u64(seed) + // For tests, we want a statistically good, fast, reproducible RNG. We + // do not need cryptographic strength. ChaCha8 is a good choice. + rand_chacha::ChaCha8Rng::seed_from_u64(seed) } #[test] diff --git a/src/rngs/adapter/reseeding.rs b/src/rngs/adapter/reseeding.rs index 2ba35f486ac..29f01e031ad 100644 --- a/src/rngs/adapter/reseeding.rs +++ b/src/rngs/adapter/reseeding.rs @@ -340,16 +340,18 @@ mod test { fn test_reseeding() { let mut zero = StepRng::new(0, 0); let rng = ChaCha8Core::from_rng(&mut zero).unwrap(); - let mut reseeding = ReseedingRng::new(rng, 32*4, zero); - - // Currently we only support for arrays up to length 32. - // TODO: cannot generate seq via Rng::gen because it uses different alg - let mut buf = [0u32; 32]; // Needs to be a multiple of the RNGs result - // size to test exactly. - reseeding.fill(&mut buf); + let thresh = 1; // reseed every time the buffer is exhausted + let mut reseeding = ReseedingRng::new(rng, thresh, zero); + + // RNG buffer size is [u32; 64] + // Debug is only implemented up to length 32 so use two arrays + let mut buf = ([0u32; 32], [0u32; 32]); + reseeding.fill(&mut buf.0); + reseeding.fill(&mut buf.1); let seq = buf; for _ in 0..10 { - reseeding.fill(&mut buf); + reseeding.fill(&mut buf.0); + reseeding.fill(&mut buf.1); assert_eq!(buf, seq); } } diff --git a/src/rngs/std.rs b/src/rngs/std.rs index 4cdde1d4115..9a6a7ef65c7 100644 --- a/src/rngs/std.rs +++ b/src/rngs/std.rs @@ -80,12 +80,22 @@ mod test { #[test] fn test_stdrng_construction() { + // Test value-stability of StdRng. This is expected to break any time + // the algorithm is changed. let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; - let mut rng1 = StdRng::from_seed(seed); - assert_eq!(rng1.next_u64(), 15759097995037006553); - let mut rng2 = StdRng::from_rng(rng1).unwrap(); - assert_eq!(rng2.next_u64(), 6766915756997287454); + #[cfg(any(feature="stdrng_strong", not(feature="stdrng_fast")))] + let target = [3950704604716924505, 5573172343717151650]; + #[cfg(all(not(feature="stdrng_strong"), feature="stdrng_fast"))] + let target = [10719222850664546238, 14064965282130556830]; + + let mut rng0 = StdRng::from_seed(seed); + let x0 = rng0.next_u64(); + + let mut rng1 = StdRng::from_rng(rng0).unwrap(); + let x1 = rng1.next_u64(); + + assert_eq!([x0, x1], target); } }