-
Notifications
You must be signed in to change notification settings - Fork 187
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
5 changed files
with
126 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//! 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() | ||
} | ||
|
||
// Type of the functions we want to test | ||
type FillFn<B> = fn(&mut [B]) -> Result<(), Error>; | ||
|
||
// Helper trait for testing different `FillFn`s. | ||
pub(crate) trait Byte: Sized + 'static { | ||
fn make_vec(len: usize, fill: FillFn<Self>) -> Vec<u8>; | ||
} | ||
impl Byte for u8 { | ||
fn make_vec(len: usize, fill: FillFn<u8>) -> Vec<u8> { | ||
let mut v = vec![0; len]; | ||
fill(&mut v).unwrap(); | ||
v | ||
} | ||
} | ||
impl Byte for MaybeUninit<u8> { | ||
fn make_vec(len: usize, fill: FillFn<MaybeUninit<u8>>) -> Vec<u8> { | ||
// Ensure that we always get uninitialized memory. | ||
let mut v = Vec::with_capacity(len); | ||
fill(v.spare_capacity_mut()).unwrap(); | ||
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<B: Byte>(len: usize, fill: FillFn<B>) { | ||
let mut num_bytes = 0; | ||
let mut diff_bits = 0; | ||
while num_bytes < 256 { | ||
let v1 = B::make_vec(len, fill); | ||
let v2 = B::make_vec(len, fill); | ||
|
||
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<B: Byte>(fill: FillFn<B>) { | ||
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( | ||
any(target_arch = "wasm32", target_arch = "wasm64"), | ||
target_os = "unknown", | ||
))] | ||
use wasm_bindgen_test::wasm_bindgen_test as test; | ||
|
||
#[test] | ||
fn fill_zero() { | ||
$fill(&mut []).unwrap(); | ||
} | ||
// Don't check the RNG quality when using a custom implementation. | ||
#[test] | ||
#[cfg_attr(feature = "custom", ignore)] | ||
fn fill_small() { | ||
for len in 1..=64 { | ||
crate::tests::check_bits(len, $fill); | ||
} | ||
} | ||
#[test] | ||
#[cfg_attr(feature = "custom", ignore)] | ||
fn fill_large() { | ||
crate::tests::check_bits(1_000, $fill); | ||
} | ||
#[test] | ||
#[cfg_attr(feature = "custom", ignore)] | ||
fn fill_huge() { | ||
crate::tests::check_bits(1_000_000, $fill); | ||
} | ||
// On WASM, the thread API always fails/panics. | ||
#[test] | ||
#[cfg_attr(any(target_arch = "wasm32", target_arch = "wasm64"), ignore)] | ||
fn multithreading() { | ||
crate::tests::check_multithreading($fill) | ||
} | ||
}; | ||
} | ||
pub(crate) use define_tests; | ||
|
||
define_tests!(crate::getrandom); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.