Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,72 +328,3 @@ impl<W: Word, const N: usize, G: Generator<Output = [W; N]>> BlockRng<G> {
self.set_index(index);
}
}

#[cfg(test)]
mod test {
use crate::SeedableRng;
use crate::block::{BlockRng, Generator};

#[derive(Debug, Clone)]
struct DummyRng {
counter: u32,
}

impl Generator for DummyRng {
type Output = [u32; 16];

fn generate(&mut self, output: &mut Self::Output) {
for item in output {
*item = self.counter;
self.counter = self.counter.wrapping_add(3511615421);
}
}
}

impl SeedableRng for DummyRng {
type Seed = [u8; 4];

fn from_seed(seed: Self::Seed) -> Self {
DummyRng {
counter: u32::from_le_bytes(seed),
}
}
}

#[test]
fn blockrng_next_u32_vs_next_u64() {
let mut rng1 = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
let mut rng2 = rng1.clone();
let mut rng3 = rng1.clone();

let mut a = [0; 16];
a[..4].copy_from_slice(&rng1.next_word().to_le_bytes());
a[4..12].copy_from_slice(&rng1.next_u64_from_u32().to_le_bytes());
a[12..].copy_from_slice(&rng1.next_word().to_le_bytes());

let mut b = [0; 16];
b[..4].copy_from_slice(&rng2.next_word().to_le_bytes());
b[4..8].copy_from_slice(&rng2.next_word().to_le_bytes());
b[8..].copy_from_slice(&rng2.next_u64_from_u32().to_le_bytes());
assert_eq!(a, b);

let mut c = [0; 16];
c[..8].copy_from_slice(&rng3.next_u64_from_u32().to_le_bytes());
c[8..12].copy_from_slice(&rng3.next_word().to_le_bytes());
c[12..].copy_from_slice(&rng3.next_word().to_le_bytes());
assert_eq!(a, c);
}

#[test]
fn blockrng_next_u64() {
let mut rng = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
let result_size = rng.results.len();
for _i in 0..result_size / 2 - 1 {
rng.next_u64_from_u32();
}
rng.next_word();

let _ = rng.next_u64_from_u32();
assert_eq!(rng.index(), 1);
}
}
161 changes: 0 additions & 161 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,164 +526,3 @@ pub trait SeedableRng: Sized {
Self::try_from_rng(self)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_seed_from_u64() {
struct SeedableNum(u64);
impl SeedableRng for SeedableNum {
type Seed = [u8; 8];

fn from_seed(seed: Self::Seed) -> Self {
let x: [u64; 1] = utils::read_words(&seed);
SeedableNum(x[0])
}
}

const N: usize = 8;
const SEEDS: [u64; N] = [0u64, 1, 2, 3, 4, 8, 16, -1i64 as u64];
let mut results = [0u64; N];
for (i, seed) in SEEDS.iter().enumerate() {
let SeedableNum(x) = SeedableNum::seed_from_u64(*seed);
results[i] = x;
}

for (i1, r1) in results.iter().enumerate() {
let weight = r1.count_ones();
// This is the binomial distribution B(64, 0.5), so chance of
// weight < 20 is binocdf(19, 64, 0.5) = 7.8e-4, and same for
// weight > 44.
assert!((20..=44).contains(&weight));

for (i2, r2) in results.iter().enumerate() {
if i1 == i2 {
continue;
}
let diff_weight = (r1 ^ r2).count_ones();
assert!(diff_weight >= 20);
}
}

// value-breakage test:
assert_eq!(results[0], 5029875928683246316);
}

// A stub RNG.
struct SomeRng;

impl TryRng for SomeRng {
type Error = Infallible;

fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
unimplemented!()
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
unimplemented!()
}
fn try_fill_bytes(&mut self, _dst: &mut [u8]) -> Result<(), Self::Error> {
unimplemented!()
}
}

impl TryCryptoRng for SomeRng {}

#[test]
fn dyn_rng_to_tryrng() {
// Illustrates the need for `+ ?Sized` bound in `impl<R: Rng> TryRng for R`.

// A method in another crate taking a fallible RNG
fn third_party_api(_rng: &mut (impl TryRng + ?Sized)) -> bool {
true
}

// A method in our crate requiring an infallible RNG
fn my_api(rng: &mut dyn Rng) -> bool {
// We want to call the method above
third_party_api(rng)
}

assert!(my_api(&mut SomeRng));
}

#[test]
fn dyn_cryptorng_to_trycryptorng() {
// Illustrates the need for `+ ?Sized` bound in `impl<R: CryptoRng> TryCryptoRng for R`.

// A method in another crate taking a fallible RNG
fn third_party_api(_rng: &mut (impl TryCryptoRng + ?Sized)) -> bool {
true
}

// A method in our crate requiring an infallible RNG
fn my_api(rng: &mut dyn CryptoRng) -> bool {
// We want to call the method above
third_party_api(rng)
}

assert!(my_api(&mut SomeRng));
}

#[test]
fn dyn_unwrap_mut_tryrng() {
// Illustrates that UnwrapMut may be used over &mut R where R: TryRng

fn third_party_api(_rng: &mut impl Rng) -> bool {
true
}

fn my_api(rng: &mut (impl TryRng + ?Sized)) -> bool {
let mut infallible_rng = UnwrapErr(rng);
third_party_api(&mut infallible_rng)
}

assert!(my_api(&mut SomeRng));
}

#[test]
fn dyn_unwrap_mut_trycryptorng() {
// Crypto variant of the above

fn third_party_api(_rng: &mut impl CryptoRng) -> bool {
true
}

fn my_api(rng: &mut (impl TryCryptoRng + ?Sized)) -> bool {
let mut infallible_rng = UnwrapErr(rng);
third_party_api(&mut infallible_rng)
}

assert!(my_api(&mut SomeRng));
}

#[test]
fn reborrow_unwrap_mut() {
struct FourRng;

impl TryRng for FourRng {
type Error = Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(4)
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
unimplemented!()
}
fn try_fill_bytes(&mut self, _: &mut [u8]) -> Result<(), Self::Error> {
unimplemented!()
}
}

let mut rng = FourRng;
let mut rng = UnwrapErr(&mut rng);

assert_eq!(rng.next_u32(), 4);
{
let mut rng2 = rng.re();
assert_eq!(rng2.next_u32(), 4);
// Make sure rng2 is dropped.
}
assert_eq!(rng.next_u32(), 4);
}
}
25 changes: 0 additions & 25 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,3 @@ pub fn read_words<W: Word, const N: usize>(src: &[u8]) -> [W; N] {
}
dst
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_read() {
let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

let buf: [u32; 4] = read_words(&bytes);
assert_eq!(buf[0], 0x04030201);
assert_eq!(buf[3], 0x100F0E0D);

let buf: [u32; 3] = read_words(&bytes[1..13]); // unaligned
assert_eq!(buf[0], 0x05040302);
assert_eq!(buf[2], 0x0D0C0B0A);

let buf: [u64; 2] = read_words(&bytes);
assert_eq!(buf[0], 0x0807060504030201);
assert_eq!(buf[1], 0x100F0E0D0C0B0A09);

let buf: [u64; 1] = read_words(&bytes[7..15]); // unaligned
assert_eq!(buf[0], 0x0F0E0D0C0B0A0908);
}
}
69 changes: 69 additions & 0 deletions tests/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use rand_core::{
SeedableRng,
block::{BlockRng, Generator},
};

const RESULTS_LEN: usize = 16;

#[derive(Debug, Clone)]
struct DummyRng {
counter: u32,
}

impl Generator for DummyRng {
type Output = [u32; RESULTS_LEN];

fn generate(&mut self, output: &mut Self::Output) {
for item in output {
*item = self.counter;
self.counter = self.counter.wrapping_add(3511615421);
}
}
}

impl SeedableRng for DummyRng {
type Seed = [u8; 4];

fn from_seed(seed: Self::Seed) -> Self {
DummyRng {
counter: u32::from_le_bytes(seed),
}
}
}

#[test]
fn blockrng_next_u32_vs_next_u64() {
let mut rng1 = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
let mut rng2 = rng1.clone();
let mut rng3 = rng1.clone();

let mut a = [0; 16];
a[..4].copy_from_slice(&rng1.next_word().to_le_bytes());
a[4..12].copy_from_slice(&rng1.next_u64_from_u32().to_le_bytes());
a[12..].copy_from_slice(&rng1.next_word().to_le_bytes());

let mut b = [0; 16];
b[..4].copy_from_slice(&rng2.next_word().to_le_bytes());
b[4..8].copy_from_slice(&rng2.next_word().to_le_bytes());
b[8..].copy_from_slice(&rng2.next_u64_from_u32().to_le_bytes());
assert_eq!(a, b);

let mut c = [0; 16];
c[..8].copy_from_slice(&rng3.next_u64_from_u32().to_le_bytes());
c[8..12].copy_from_slice(&rng3.next_word().to_le_bytes());
c[12..].copy_from_slice(&rng3.next_word().to_le_bytes());
assert_eq!(a, c);
}

#[test]
fn blockrng_next_u64() {
let mut rng = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
let result_size = RESULTS_LEN;
for _i in 0..result_size / 2 - 1 {
rng.next_u64_from_u32();
}
rng.next_word();

let _ = rng.next_u64_from_u32();
assert_eq!(rng.word_offset(), 1);
}
Loading