Skip to content

Commit

Permalink
Fix/update value-stability tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed May 7, 2019
1 parent e32a474 commit f209854
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/distributions/unit_circle.rs
Expand Up @@ -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]);
}
}
6 changes: 3 additions & 3 deletions src/distributions/unit_sphere.rs
Expand Up @@ -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]);
}
}
6 changes: 4 additions & 2 deletions src/lib.rs
Expand Up @@ -556,12 +556,14 @@ where Standard: Distribution<T> {
#[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]
Expand Down
18 changes: 10 additions & 8 deletions src/rngs/adapter/reseeding.rs
Expand Up @@ -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);
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/rngs/std.rs
Expand Up @@ -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);
}
}

0 comments on commit f209854

Please sign in to comment.