-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joe Richey <[email protected]>
- Loading branch information
Showing
8 changed files
with
150 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
//! Common tests and testing utilities | ||
extern crate std; | ||
|
||
use crate::Error; | ||
use std::{mem::MaybeUninit, sync::mpsc, thread, vec, vec::Vec}; | ||
|
||
#[cfg(feature = "test-in-browser")] | ||
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize { | ||
assert_eq!(s1.len(), s2.len()); | ||
s1.iter() | ||
.zip(s2.iter()) | ||
.map(|(a, b)| (a ^ b).count_ones() as usize) | ||
.sum() | ||
} | ||
|
||
pub(crate) trait FillFn: Copy + Send + 'static { | ||
fn make_vec(self, len: usize) -> Vec<u8>; | ||
} | ||
impl FillFn for fn(&mut [u8]) -> Result<(), Error> { | ||
fn make_vec(self, len: usize) -> Vec<u8> { | ||
let mut v = vec![0; len]; | ||
self(&mut v).unwrap(); | ||
v | ||
} | ||
} | ||
impl FillFn for fn(&mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
fn make_vec(self, len: usize) -> Vec<u8> { | ||
let mut v = Vec::with_capacity(len); | ||
self(v.spare_capacity_mut()).unwrap(); | ||
unsafe { v.set_len(len) }; | ||
v | ||
} | ||
} | ||
impl FillFn for fn(&mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error> { | ||
fn make_vec(self, len: usize) -> Vec<u8> { | ||
let mut v = Vec::with_capacity(len); | ||
let ret = self(v.spare_capacity_mut()).unwrap(); | ||
assert_eq!(ret.len(), len); | ||
assert_eq!(ret.as_ptr(), v.as_ptr()); | ||
unsafe { v.set_len(len) }; | ||
v | ||
} | ||
} | ||
|
||
// For calls of size `len`, count the number of bits which differ between calls | ||
// and check that between 3 and 5 bits per byte differ. Probability of failure: | ||
// ~ 10^(-30) = 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256] | ||
pub(crate) fn check_bits(len: usize, fill: impl FillFn) { | ||
let mut num_bytes = 0; | ||
let mut diff_bits = 0; | ||
while num_bytes < 256 { | ||
let v1 = fill.make_vec(len); | ||
let v2 = fill.make_vec(len); | ||
|
||
num_bytes += len; | ||
diff_bits += num_diff_bits(&v1, &v2); | ||
} | ||
|
||
// When the custom feature is enabled, don't check RNG quality. | ||
assert!(diff_bits > 3 * num_bytes); | ||
assert!(diff_bits < 5 * num_bytes); | ||
} | ||
|
||
pub(crate) fn check_multithreading(fill: impl FillFn) { | ||
let mut txs = vec![]; | ||
for _ in 0..20 { | ||
let (tx, rx) = mpsc::channel(); | ||
txs.push(tx); | ||
|
||
thread::spawn(move || { | ||
// wait until all the tasks are ready to go. | ||
rx.recv().unwrap(); | ||
for _ in 0..100 { | ||
check_bits(1000, fill); | ||
thread::yield_now(); | ||
} | ||
}); | ||
} | ||
|
||
// start all the tasks | ||
for tx in txs.iter() { | ||
tx.send(()).unwrap(); | ||
} | ||
} | ||
|
||
macro_rules! define_tests { | ||
($fill:expr) => { | ||
#[cfg(all(target_family = "wasm", target_os = "unknown"))] | ||
use wasm_bindgen_test::wasm_bindgen_test as test; | ||
|
||
#[test] | ||
fn fill_zero() { | ||
crate::tests::FillFn::make_vec($fill, 0); | ||
} | ||
#[test] | ||
fn fill_small() { | ||
for len in 1..=64 { | ||
crate::tests::check_bits(len, $fill); | ||
} | ||
} | ||
#[test] | ||
fn fill_large() { | ||
crate::tests::check_bits(1_000, $fill); | ||
} | ||
#[test] | ||
fn fill_huge() { | ||
crate::tests::check_bits(1_000_000, $fill); | ||
} | ||
// On WASM, the thread API always fails/panics. | ||
#[test] | ||
#[cfg_attr(target_family = "wasm", ignore)] | ||
fn multithreading() { | ||
crate::tests::check_multithreading($fill) | ||
} | ||
}; | ||
} | ||
pub(crate) use define_tests; | ||
|
||
mod init { | ||
super::define_tests!(crate::getrandom as fn(&mut _) -> _); | ||
} | ||
mod uninit { | ||
super::define_tests!(crate::getrandom_uninit as fn(&mut _) -> Result<&mut _, _>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.