Skip to content

Commit

Permalink
Rename MockAddRng → StepRng and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Feb 25, 2018
1 parent ff1b538 commit f4420c9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
20 changes: 10 additions & 10 deletions src/distributions/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,57 +99,57 @@ float_impls! { f32_rand_impls, f32, 23, next_f32 }
#[cfg(test)]
mod tests {
use Rng;
use mock::MockAddRng;
use mock::StepRng;
use distributions::{Open01, Closed01};

const EPSILON32: f32 = ::core::f32::EPSILON;
const EPSILON64: f64 = ::core::f64::EPSILON;

#[test]
fn floating_point_edge_cases() {
let mut zeros = MockAddRng::new(0, 0);
let mut zeros = StepRng::new(0, 0);
assert_eq!(zeros.gen::<f32>(), 0.0);
assert_eq!(zeros.gen::<f64>(), 0.0);

let mut one = MockAddRng::new(1, 0);
let mut one = StepRng::new(1, 0);
assert_eq!(one.gen::<f32>(), EPSILON32);
assert_eq!(one.gen::<f64>(), EPSILON64);

let mut max = MockAddRng::new(!0, 0);
let mut max = StepRng::new(!0, 0);
assert_eq!(max.gen::<f32>(), 1.0 - EPSILON32);
assert_eq!(max.gen::<f64>(), 1.0 - EPSILON64);
}

#[test]
fn fp_closed_edge_cases() {
let mut zeros = MockAddRng::new(0, 0);
let mut zeros = StepRng::new(0, 0);
assert_eq!(zeros.sample::<f32, _>(Closed01), 0.0);
assert_eq!(zeros.sample::<f64, _>(Closed01), 0.0);

let mut one = MockAddRng::new(1, 0);
let mut one = StepRng::new(1, 0);
let one32 = one.sample::<f32, _>(Closed01);
let one64 = one.sample::<f64, _>(Closed01);
assert!(EPSILON32 < one32 && one32 < EPSILON32 * 1.01);
assert!(EPSILON64 < one64 && one64 < EPSILON64 * 1.01);

let mut max = MockAddRng::new(!0, 0);
let mut max = StepRng::new(!0, 0);
assert_eq!(max.sample::<f32, _>(Closed01), 1.0);
assert_eq!(max.sample::<f64, _>(Closed01), 1.0);
}

#[test]
fn fp_open_edge_cases() {
let mut zeros = MockAddRng::new(0, 0);
let mut zeros = StepRng::new(0, 0);
assert_eq!(zeros.sample::<f32, _>(Open01), 0.0 + EPSILON32 / 2.0);
assert_eq!(zeros.sample::<f64, _>(Open01), 0.0 + EPSILON64 / 2.0);

let mut one = MockAddRng::new(1, 0);
let mut one = StepRng::new(1, 0);
let one32 = one.sample::<f32, _>(Open01);
let one64 = one.sample::<f64, _>(Open01);
assert!(EPSILON32 < one32 && one32 < EPSILON32 * 2.0);
assert!(EPSILON64 < one64 && one64 < EPSILON64 * 2.0);

let mut max = MockAddRng::new(!0, 0);
let mut max = StepRng::new(!0, 0);
assert_eq!(max.sample::<f32, _>(Open01), 1.0 - EPSILON32 / 2.0);
assert_eq!(max.sample::<f64, _>(Open01), 1.0 - EPSILON64 / 2.0);
}
Expand Down
4 changes: 2 additions & 2 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn ziggurat<R: Rng + ?Sized, P, Z>(
#[cfg(test)]
mod tests {
use Rng;
use mock::MockAddRng;
use mock::StepRng;
use super::{WeightedChoice, Weighted, Distribution};

#[test]
Expand All @@ -403,7 +403,7 @@ mod tests {
let wc = WeightedChoice::new(&mut items);
let expected = $expected;

let mut rng = MockAddRng::new(0, 1);
let mut rng = StepRng::new(0, 1);

for &val in expected.iter() {
assert_eq!(wc.sample(&mut rng), val)
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>

#[cfg(test)]
mod test {
use mock::MockAddRng;
use mock::StepRng;
#[cfg(feature="std")]
use super::{random, thread_rng, EntropyRng};
use super::{RngCore, Rng, SeedableRng, StdRng};
Expand Down Expand Up @@ -1429,7 +1429,7 @@ mod test {

#[test]
fn test_fill_bytes_default() {
let mut r = MockAddRng::new(0x11_22_33_44_55_66_77_88, 0);
let mut r = StepRng::new(0x11_22_33_44_55_66_77_88, 0);

// check every remainder mod 8, both in small and big vectors.
let lengths = [0, 1, 2, 3, 4, 5, 6, 7,
Expand All @@ -1451,7 +1451,7 @@ mod test {
#[test]
fn test_fill() {
let x = 9041086907909331047; // a random u64
let mut rng = MockAddRng::new(x, 0);
let mut rng = StepRng::new(x, 0);

// Convert to byte sequence and back to u64; byte-swap twice if BE.
let mut array = [0u64; 2];
Expand Down
14 changes: 7 additions & 7 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ use {RngCore, Error, impls};
///
/// ```rust
/// use rand::Rng;
/// use rand::mock::MockAddRng;
/// use rand::mock::StepRng;
///
/// let mut my_rng = MockAddRng::new(2, 1);
/// let mut my_rng = StepRng::new(2, 1);
/// let sample: [u64; 3] = my_rng.gen();
/// assert_eq!(sample, [2, 3, 4]);
/// ```
#[derive(Debug, Clone)]
pub struct MockAddRng {
pub struct StepRng {
v: u64,
a: u64,
}

impl MockAddRng {
/// Create a `MockAddRng`, yielding an arithmetic sequence starting with
impl StepRng {
/// Create a `StepRng`, yielding an arithmetic sequence starting with
/// `initial` and incremented by `increment` each time.
pub fn new(initial: u64, increment: u64) -> Self {
MockAddRng { v: initial, a: increment }
StepRng { v: initial, a: increment }
}
}

impl RngCore for MockAddRng {
impl RngCore for StepRng {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
Expand Down
5 changes: 2 additions & 3 deletions src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ impl<R: RngCore + SeedableRng, Rsdr: RngCore> RngCore for ReseedingRng<R, Rsdr>
#[cfg(test)]
mod test {
use {Rng, SeedableRng, StdRng};
use mock::MockAddRng;
use mock::StepRng;
use super::ReseedingRng;

#[test]
fn test_reseeding() {
let mut zero = MockAddRng::new(0, 0);
let mut zero = StepRng::new(0, 0);
let rng = StdRng::from_rng(&mut zero).unwrap();
let mut reseeding = ReseedingRng::new(rng, 32, zero);

Expand All @@ -198,7 +198,6 @@ mod test {
let mut buf = [0u8; 32];
reseeding.fill(&mut buf);
let seq = buf;
println!("buf: {:?}", buf);
for _ in 0..10 {
reseeding.fill(&mut buf);
assert_eq!(buf, seq);
Expand Down

0 comments on commit f4420c9

Please sign in to comment.