Skip to content

Commit 731567f

Browse files
committed
Cleanup common tests
Signed-off-by: Joe Richey <[email protected]>
1 parent 580f3a0 commit 731567f

File tree

5 files changed

+117
-133
lines changed

5 files changed

+117
-133
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,6 @@ pub fn getrandom_uninit(dest: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error
406406
// since it returned `Ok`.
407407
Ok(unsafe { slice_assume_init_mut(dest) })
408408
}
409+
410+
#[cfg(test)]
411+
pub(crate) mod tests;

src/tests.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//! Common tests and testing utilities
2+
extern crate std;
3+
4+
use crate::Error;
5+
use std::{sync::mpsc, thread, vec, vec::Vec};
6+
7+
#[cfg(feature = "test-in-browser")]
8+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
9+
10+
fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize {
11+
assert_eq!(s1.len(), s2.len());
12+
s1.iter()
13+
.zip(s2.iter())
14+
.map(|(a, b)| (a ^ b).count_ones() as usize)
15+
.sum()
16+
}
17+
18+
// Type of the functions we want to test
19+
type FillFn<B> = fn(&mut [B]) -> Result<(), Error>;
20+
21+
// Helper trait for testing different `FillFn`s.
22+
pub(crate) trait Byte: Sized + 'static {
23+
fn make_vec(len: usize, fill: FillFn<Self>) -> Vec<u8>;
24+
}
25+
impl Byte for u8 {
26+
fn make_vec(len: usize, fill: FillFn<u8>) -> Vec<u8> {
27+
let mut v = vec![0; len];
28+
fill(&mut v).unwrap();
29+
v
30+
}
31+
}
32+
33+
// For calls of size `len`, count the number of bits which differ between calls
34+
// and check that between 3 and 5 bits per byte differ. Probability of failure:
35+
// ~ 10^(-30) = 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
36+
pub(crate) fn check_bits<B: Byte>(len: usize, fill: FillFn<B>) {
37+
let mut num_bytes = 0;
38+
let mut diff_bits = 0;
39+
while num_bytes < 256 {
40+
let v1 = B::make_vec(len, fill);
41+
let v2 = B::make_vec(len, fill);
42+
43+
num_bytes += len;
44+
diff_bits += num_diff_bits(&v1, &v2);
45+
}
46+
47+
// When the custom feature is enabled, don't check RNG quality.
48+
assert!(diff_bits > 3 * num_bytes);
49+
assert!(diff_bits < 5 * num_bytes);
50+
}
51+
52+
pub(crate) fn check_multithreading<B: Byte>(fill: FillFn<B>) {
53+
let mut txs = vec![];
54+
for _ in 0..20 {
55+
let (tx, rx) = mpsc::channel();
56+
txs.push(tx);
57+
58+
thread::spawn(move || {
59+
// wait until all the tasks are ready to go.
60+
rx.recv().unwrap();
61+
for _ in 0..100 {
62+
check_bits(1000, fill);
63+
thread::yield_now();
64+
}
65+
});
66+
}
67+
68+
// start all the tasks
69+
for tx in txs.iter() {
70+
tx.send(()).unwrap();
71+
}
72+
}
73+
74+
macro_rules! define_tests {
75+
($fill:expr) => {
76+
#[cfg(all(
77+
any(target_arch = "wasm32", target_arch = "wasm64"),
78+
target_os = "unknown",
79+
))]
80+
use wasm_bindgen_test::wasm_bindgen_test as test;
81+
82+
#[test]
83+
fn fill_zero() {
84+
$fill(&mut []).unwrap();
85+
}
86+
// Don't check the RNG quality when using a custom implementation.
87+
#[test]
88+
#[cfg_attr(feature = "custom", ignore)]
89+
fn fill_small() {
90+
for len in 1..=64 {
91+
crate::tests::check_bits(len, $fill);
92+
}
93+
}
94+
#[test]
95+
#[cfg_attr(feature = "custom", ignore)]
96+
fn fill_large() {
97+
crate::tests::check_bits(1_000, $fill);
98+
}
99+
#[test]
100+
#[cfg_attr(feature = "custom", ignore)]
101+
fn fill_huge() {
102+
crate::tests::check_bits(1_000_000, $fill);
103+
}
104+
// On WASM, the thread API always fails/panics.
105+
#[test]
106+
#[cfg_attr(any(target_arch = "wasm32", target_arch = "wasm64"), ignore)]
107+
fn multithreading() {
108+
crate::tests::check_multithreading($fill)
109+
}
110+
};
111+
}
112+
pub(crate) use define_tests;
113+
114+
define_tests!(crate::getrandom);

tests/common/mod.rs

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/normal.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/rdrand.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)