Skip to content

Commit bddeeab

Browse files
authored
Move module tests to the tests/ folder (#57)
This improves visibility of the tests and makes the modules less cluttered.
1 parent efcd06f commit bddeeab

File tree

6 files changed

+295
-255
lines changed

6 files changed

+295
-255
lines changed

src/block.rs

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -328,72 +328,3 @@ impl<W: Word, const N: usize, G: Generator<Output = [W; N]>> BlockRng<G> {
328328
self.set_index(index);
329329
}
330330
}
331-
332-
#[cfg(test)]
333-
mod test {
334-
use crate::SeedableRng;
335-
use crate::block::{BlockRng, Generator};
336-
337-
#[derive(Debug, Clone)]
338-
struct DummyRng {
339-
counter: u32,
340-
}
341-
342-
impl Generator for DummyRng {
343-
type Output = [u32; 16];
344-
345-
fn generate(&mut self, output: &mut Self::Output) {
346-
for item in output {
347-
*item = self.counter;
348-
self.counter = self.counter.wrapping_add(3511615421);
349-
}
350-
}
351-
}
352-
353-
impl SeedableRng for DummyRng {
354-
type Seed = [u8; 4];
355-
356-
fn from_seed(seed: Self::Seed) -> Self {
357-
DummyRng {
358-
counter: u32::from_le_bytes(seed),
359-
}
360-
}
361-
}
362-
363-
#[test]
364-
fn blockrng_next_u32_vs_next_u64() {
365-
let mut rng1 = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
366-
let mut rng2 = rng1.clone();
367-
let mut rng3 = rng1.clone();
368-
369-
let mut a = [0; 16];
370-
a[..4].copy_from_slice(&rng1.next_word().to_le_bytes());
371-
a[4..12].copy_from_slice(&rng1.next_u64_from_u32().to_le_bytes());
372-
a[12..].copy_from_slice(&rng1.next_word().to_le_bytes());
373-
374-
let mut b = [0; 16];
375-
b[..4].copy_from_slice(&rng2.next_word().to_le_bytes());
376-
b[4..8].copy_from_slice(&rng2.next_word().to_le_bytes());
377-
b[8..].copy_from_slice(&rng2.next_u64_from_u32().to_le_bytes());
378-
assert_eq!(a, b);
379-
380-
let mut c = [0; 16];
381-
c[..8].copy_from_slice(&rng3.next_u64_from_u32().to_le_bytes());
382-
c[8..12].copy_from_slice(&rng3.next_word().to_le_bytes());
383-
c[12..].copy_from_slice(&rng3.next_word().to_le_bytes());
384-
assert_eq!(a, c);
385-
}
386-
387-
#[test]
388-
fn blockrng_next_u64() {
389-
let mut rng = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
390-
let result_size = rng.results.len();
391-
for _i in 0..result_size / 2 - 1 {
392-
rng.next_u64_from_u32();
393-
}
394-
rng.next_word();
395-
396-
let _ = rng.next_u64_from_u32();
397-
assert_eq!(rng.index(), 1);
398-
}
399-
}

src/lib.rs

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -526,164 +526,3 @@ pub trait SeedableRng: Sized {
526526
Self::try_from_rng(self)
527527
}
528528
}
529-
530-
#[cfg(test)]
531-
mod test {
532-
use super::*;
533-
534-
#[test]
535-
fn test_seed_from_u64() {
536-
struct SeedableNum(u64);
537-
impl SeedableRng for SeedableNum {
538-
type Seed = [u8; 8];
539-
540-
fn from_seed(seed: Self::Seed) -> Self {
541-
let x: [u64; 1] = utils::read_words(&seed);
542-
SeedableNum(x[0])
543-
}
544-
}
545-
546-
const N: usize = 8;
547-
const SEEDS: [u64; N] = [0u64, 1, 2, 3, 4, 8, 16, -1i64 as u64];
548-
let mut results = [0u64; N];
549-
for (i, seed) in SEEDS.iter().enumerate() {
550-
let SeedableNum(x) = SeedableNum::seed_from_u64(*seed);
551-
results[i] = x;
552-
}
553-
554-
for (i1, r1) in results.iter().enumerate() {
555-
let weight = r1.count_ones();
556-
// This is the binomial distribution B(64, 0.5), so chance of
557-
// weight < 20 is binocdf(19, 64, 0.5) = 7.8e-4, and same for
558-
// weight > 44.
559-
assert!((20..=44).contains(&weight));
560-
561-
for (i2, r2) in results.iter().enumerate() {
562-
if i1 == i2 {
563-
continue;
564-
}
565-
let diff_weight = (r1 ^ r2).count_ones();
566-
assert!(diff_weight >= 20);
567-
}
568-
}
569-
570-
// value-breakage test:
571-
assert_eq!(results[0], 5029875928683246316);
572-
}
573-
574-
// A stub RNG.
575-
struct SomeRng;
576-
577-
impl TryRng for SomeRng {
578-
type Error = Infallible;
579-
580-
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
581-
unimplemented!()
582-
}
583-
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
584-
unimplemented!()
585-
}
586-
fn try_fill_bytes(&mut self, _dst: &mut [u8]) -> Result<(), Self::Error> {
587-
unimplemented!()
588-
}
589-
}
590-
591-
impl TryCryptoRng for SomeRng {}
592-
593-
#[test]
594-
fn dyn_rng_to_tryrng() {
595-
// Illustrates the need for `+ ?Sized` bound in `impl<R: Rng> TryRng for R`.
596-
597-
// A method in another crate taking a fallible RNG
598-
fn third_party_api(_rng: &mut (impl TryRng + ?Sized)) -> bool {
599-
true
600-
}
601-
602-
// A method in our crate requiring an infallible RNG
603-
fn my_api(rng: &mut dyn Rng) -> bool {
604-
// We want to call the method above
605-
third_party_api(rng)
606-
}
607-
608-
assert!(my_api(&mut SomeRng));
609-
}
610-
611-
#[test]
612-
fn dyn_cryptorng_to_trycryptorng() {
613-
// Illustrates the need for `+ ?Sized` bound in `impl<R: CryptoRng> TryCryptoRng for R`.
614-
615-
// A method in another crate taking a fallible RNG
616-
fn third_party_api(_rng: &mut (impl TryCryptoRng + ?Sized)) -> bool {
617-
true
618-
}
619-
620-
// A method in our crate requiring an infallible RNG
621-
fn my_api(rng: &mut dyn CryptoRng) -> bool {
622-
// We want to call the method above
623-
third_party_api(rng)
624-
}
625-
626-
assert!(my_api(&mut SomeRng));
627-
}
628-
629-
#[test]
630-
fn dyn_unwrap_mut_tryrng() {
631-
// Illustrates that UnwrapMut may be used over &mut R where R: TryRng
632-
633-
fn third_party_api(_rng: &mut impl Rng) -> bool {
634-
true
635-
}
636-
637-
fn my_api(rng: &mut (impl TryRng + ?Sized)) -> bool {
638-
let mut infallible_rng = UnwrapErr(rng);
639-
third_party_api(&mut infallible_rng)
640-
}
641-
642-
assert!(my_api(&mut SomeRng));
643-
}
644-
645-
#[test]
646-
fn dyn_unwrap_mut_trycryptorng() {
647-
// Crypto variant of the above
648-
649-
fn third_party_api(_rng: &mut impl CryptoRng) -> bool {
650-
true
651-
}
652-
653-
fn my_api(rng: &mut (impl TryCryptoRng + ?Sized)) -> bool {
654-
let mut infallible_rng = UnwrapErr(rng);
655-
third_party_api(&mut infallible_rng)
656-
}
657-
658-
assert!(my_api(&mut SomeRng));
659-
}
660-
661-
#[test]
662-
fn reborrow_unwrap_mut() {
663-
struct FourRng;
664-
665-
impl TryRng for FourRng {
666-
type Error = Infallible;
667-
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
668-
Ok(4)
669-
}
670-
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
671-
unimplemented!()
672-
}
673-
fn try_fill_bytes(&mut self, _: &mut [u8]) -> Result<(), Self::Error> {
674-
unimplemented!()
675-
}
676-
}
677-
678-
let mut rng = FourRng;
679-
let mut rng = UnwrapErr(&mut rng);
680-
681-
assert_eq!(rng.next_u32(), 4);
682-
{
683-
let mut rng2 = rng.re();
684-
assert_eq!(rng2.next_u32(), 4);
685-
// Make sure rng2 is dropped.
686-
}
687-
assert_eq!(rng.next_u32(), 4);
688-
}
689-
}

src/utils.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,3 @@ pub fn read_words<W: Word, const N: usize>(src: &[u8]) -> [W; N] {
145145
}
146146
dst
147147
}
148-
149-
#[cfg(test)]
150-
mod test {
151-
use super::*;
152-
153-
#[test]
154-
fn test_read() {
155-
let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
156-
157-
let buf: [u32; 4] = read_words(&bytes);
158-
assert_eq!(buf[0], 0x04030201);
159-
assert_eq!(buf[3], 0x100F0E0D);
160-
161-
let buf: [u32; 3] = read_words(&bytes[1..13]); // unaligned
162-
assert_eq!(buf[0], 0x05040302);
163-
assert_eq!(buf[2], 0x0D0C0B0A);
164-
165-
let buf: [u64; 2] = read_words(&bytes);
166-
assert_eq!(buf[0], 0x0807060504030201);
167-
assert_eq!(buf[1], 0x100F0E0D0C0B0A09);
168-
169-
let buf: [u64; 1] = read_words(&bytes[7..15]); // unaligned
170-
assert_eq!(buf[0], 0x0F0E0D0C0B0A0908);
171-
}
172-
}

tests/block.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use rand_core::{
2+
SeedableRng,
3+
block::{BlockRng, Generator},
4+
};
5+
6+
const RESULTS_LEN: usize = 16;
7+
8+
#[derive(Debug, Clone)]
9+
struct DummyRng {
10+
counter: u32,
11+
}
12+
13+
impl Generator for DummyRng {
14+
type Output = [u32; RESULTS_LEN];
15+
16+
fn generate(&mut self, output: &mut Self::Output) {
17+
for item in output {
18+
*item = self.counter;
19+
self.counter = self.counter.wrapping_add(3511615421);
20+
}
21+
}
22+
}
23+
24+
impl SeedableRng for DummyRng {
25+
type Seed = [u8; 4];
26+
27+
fn from_seed(seed: Self::Seed) -> Self {
28+
DummyRng {
29+
counter: u32::from_le_bytes(seed),
30+
}
31+
}
32+
}
33+
34+
#[test]
35+
fn blockrng_next_u32_vs_next_u64() {
36+
let mut rng1 = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
37+
let mut rng2 = rng1.clone();
38+
let mut rng3 = rng1.clone();
39+
40+
let mut a = [0; 16];
41+
a[..4].copy_from_slice(&rng1.next_word().to_le_bytes());
42+
a[4..12].copy_from_slice(&rng1.next_u64_from_u32().to_le_bytes());
43+
a[12..].copy_from_slice(&rng1.next_word().to_le_bytes());
44+
45+
let mut b = [0; 16];
46+
b[..4].copy_from_slice(&rng2.next_word().to_le_bytes());
47+
b[4..8].copy_from_slice(&rng2.next_word().to_le_bytes());
48+
b[8..].copy_from_slice(&rng2.next_u64_from_u32().to_le_bytes());
49+
assert_eq!(a, b);
50+
51+
let mut c = [0; 16];
52+
c[..8].copy_from_slice(&rng3.next_u64_from_u32().to_le_bytes());
53+
c[8..12].copy_from_slice(&rng3.next_word().to_le_bytes());
54+
c[12..].copy_from_slice(&rng3.next_word().to_le_bytes());
55+
assert_eq!(a, c);
56+
}
57+
58+
#[test]
59+
fn blockrng_next_u64() {
60+
let mut rng = BlockRng::new(DummyRng::from_seed([1, 2, 3, 4]));
61+
let result_size = RESULTS_LEN;
62+
for _i in 0..result_size / 2 - 1 {
63+
rng.next_u64_from_u32();
64+
}
65+
rng.next_word();
66+
67+
let _ = rng.next_u64_from_u32();
68+
assert_eq!(rng.word_offset(), 1);
69+
}

0 commit comments

Comments
 (0)