Skip to content

Commit

Permalink
Remove quickcheck dev-dependency
Browse files Browse the repository at this point in the history
Remove `quickcheck` dependency for `recover`, `temper`, and `untemper`
tests. Replace with loops and `getrandom::getrandom` for generating
integers to test.

This removes all dependencies that pull in `rand` and `rand_core`, which
will make updating `rand` in this crate easier moving forward.
  • Loading branch information
lopopolo committed Dec 27, 2020
1 parent ecb00a7 commit 0ec7626
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ std = []
rand_core = { version = "0.5", default-features = false, optional = true }

[dev-dependencies]
quickcheck = { version = "0.9", default-features = false }
quickcheck_macros = "0.9"
getrandom = { version = "0.2", default-features = false }
version-sync = "0.9"

[package.metadata.docs.rs]
Expand Down
63 changes: 40 additions & 23 deletions src/mt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ mod tests {
use core::convert::TryFrom;
use core::iter;
use core::num::Wrapping;
use quickcheck_macros::quickcheck;

use super::{Mt19937GenRand32, N};
use crate::vectors::mt::{STATE_SEEDED_BY_SLICE, STATE_SEEDED_BY_U32, TEST_OUTPUT};
Expand Down Expand Up @@ -513,34 +512,52 @@ mod tests {
}
}

#[quickcheck]
fn temper_untemper_is_identity(x: u32) -> bool {
x == super::untemper(super::temper(x))
#[test]
fn temper_untemper_is_identity() {
let mut buf = [0; 4];
for _ in 0..10_000 {
getrandom::getrandom(&mut buf).unwrap();
let x = u32::from_le_bytes(buf);
assert_eq!(x, super::untemper(super::temper(x)));
let x = u32::from_be_bytes(buf);
assert_eq!(x, super::untemper(super::temper(x)));
}
}

#[quickcheck]
fn untemper_temper_is_identity(x: u32) -> bool {
x == super::temper(super::untemper(x))
#[test]
fn untemper_temper_is_identity() {
let mut buf = [0; 4];
for _ in 0..10_000 {
getrandom::getrandom(&mut buf).unwrap();
let x = u32::from_le_bytes(buf);
assert_eq!(x, super::temper(super::untemper(x)));
let x = u32::from_be_bytes(buf);
assert_eq!(x, super::temper(super::untemper(x)));
}
}

#[quickcheck]
fn recovery(seed: u32, skip: u8) -> bool {
let mut orig_mt = Mt19937GenRand32::new(seed);
// skip some samples so the RNG is in an intermediate state
for _ in 0..skip {
orig_mt.next_u32();
}
let mut samples = [0; 624];
for sample in samples.iter_mut() {
*sample = orig_mt.next_u32();
}
let mut recovered_mt = Mt19937GenRand32::from(samples);
for _ in 0..624 * 2 {
if orig_mt.next_u32() != recovered_mt.next_u32() {
return false;
#[test]
fn recovery() {
let mut buf = [0; 8];
for _ in 0..100 {
getrandom::getrandom(&mut buf).unwrap();
let seed = u64::from_le_bytes(buf);
for skip in 0..256 {
let mut orig_mt = Mt19937GenRand32::new(seed);
// skip some samples so the RNG is in an intermediate state
for _ in 0..skip {
orig_mt.next_u32();
}
let mut samples = [0; 624];
for sample in samples.iter_mut() {
*sample = orig_mt.next_u32();
}
let mut recovered_mt = Mt19937GenRand32::from(samples);
for _ in 0..624 * 2 {
assert_eq!(orig_mt.next_u32(), recovered_mt.next_u32());
}
}
}
true
}

#[test]
Expand Down
63 changes: 40 additions & 23 deletions src/mt64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ fn fill_next_state(rng: &mut Mt19937GenRand64) {
mod tests {
use core::convert::TryFrom;
use core::num::Wrapping;
use quickcheck_macros::quickcheck;

use super::{Mt19937GenRand64, NN};
use crate::vectors::mt64::{STATE_SEEDED_BY_SLICE, STATE_SEEDED_BY_U64, TEST_OUTPUT};
Expand Down Expand Up @@ -494,34 +493,52 @@ mod tests {
}
}

#[quickcheck]
fn temper_untemper_is_identity(x: u64) -> bool {
x == super::untemper(super::temper(x))
#[test]
fn temper_untemper_is_identity() {
let mut buf = [0; 8];
for _ in 0..10_000 {
getrandom::getrandom(&mut buf).unwrap();
let x = u64::from_le_bytes(buf);
assert_eq!(x, super::untemper(super::temper(x)));
let x = u64::from_be_bytes(buf);
assert_eq!(x, super::untemper(super::temper(x)));
}
}

#[quickcheck]
fn untemper_temper_is_identity(x: u64) -> bool {
x == super::temper(super::untemper(x))
#[test]
fn untemper_temper_is_identity() {
let mut buf = [0; 8];
for _ in 0..10_000 {
getrandom::getrandom(&mut buf).unwrap();
let x = u64::from_le_bytes(buf);
assert_eq!(x, super::temper(super::untemper(x)));
let x = u64::from_be_bytes(buf);
assert_eq!(x, super::temper(super::untemper(x)));
}
}

#[quickcheck]
fn recovery(seed: u64, skip: u8) -> bool {
let mut orig_mt = Mt19937GenRand64::new(seed);
// skip some samples so the RNG is in an intermediate state
for _ in 0..skip {
orig_mt.next_u64();
}
let mut samples = [0; 312];
for sample in samples.iter_mut() {
*sample = orig_mt.next_u64();
}
let mut recovered_mt = Mt19937GenRand64::from(samples);
for _ in 0..312 * 2 {
if orig_mt.next_u64() != recovered_mt.next_u64() {
return false;
#[test]
fn recovery() {
let mut buf = [0; 8];
for _ in 0..100 {
getrandom::getrandom(&mut buf).unwrap();
let seed = u64::from_le_bytes(buf);
for skip in 0..256 {
let mut orig_mt = Mt19937GenRand64::new(seed);
// skip some samples so the RNG is in an intermediate state
for _ in 0..skip {
orig_mt.next_u64();
}
let mut samples = [0; 312];
for sample in samples.iter_mut() {
*sample = orig_mt.next_u64();
}
let mut recovered_mt = Mt19937GenRand64::from(samples);
for _ in 0..312 * 2 {
assert_eq!(orig_mt.next_u64(), recovered_mt.next_u64());
}
}
}
true
}

#[test]
Expand Down

0 comments on commit 0ec7626

Please sign in to comment.