Skip to content

Commit

Permalink
Reseeding test: replace custom RNGs with standard ones
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Feb 25, 2018
1 parent 6dd358a commit a03677c
Showing 1 changed file with 15 additions and 48 deletions.
63 changes: 15 additions & 48 deletions src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,57 +182,24 @@ impl<R: RngCore + SeedableRng, Rsdr: RngCore> RngCore for ReseedingRng<R, Rsdr>

#[cfg(test)]
mod test {
use {impls, le};
use super::{ReseedingRng};
use {SeedableRng, RngCore, Error};

struct Counter {
i: u32
}

impl RngCore for Counter {
fn next_u32(&mut self) -> u32 {
self.i += 1;
// very random
self.i - 1
}
fn next_u64(&mut self) -> u64 {
impls::next_u64_via_u32(self)
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_u64(self, dest)
}
}
impl SeedableRng for Counter {
type Seed = [u8; 4];
fn from_seed(seed: Self::Seed) -> Self {
let mut seed_u32 = [0u32; 1];
le::read_u32_into(&seed, &mut seed_u32);
Counter { i: seed_u32[0] }
}
}

#[derive(Debug, Clone)]
struct ResetCounter;
impl RngCore for ResetCounter {
fn next_u32(&mut self) -> u32 { unimplemented!() }
fn next_u64(&mut self) -> u64 { unimplemented!() }
fn fill_bytes(&mut self, _dest: &mut [u8]) { unimplemented!() }
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
for i in dest.iter_mut() { *i = 0; }
Ok(())
}
}
use {Rng, SeedableRng, StdRng};
use mock::StepRng;
use super::ReseedingRng;

#[test]
fn test_reseeding() {
let mut rs = ReseedingRng::new(Counter {i:0}, 400, ResetCounter);

let mut i = 0;
for _ in 0..1000 {
assert_eq!(rs.next_u32(), i % 100);
i += 1;
let mut zero = StepRng::new(0, 0);
let rng = StdRng::from_rng(&mut zero).unwrap();
let mut reseeding = ReseedingRng::new(rng, 32, 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 = [0u8; 32];
reseeding.fill(&mut buf);
let seq = buf;
for _ in 0..10 {
reseeding.fill(&mut buf);
assert_eq!(buf, seq);
}
}
}

0 comments on commit a03677c

Please sign in to comment.