Skip to content

Commit c12f70c

Browse files
committed
Sharpen bounds in test_diff
We can be much stricter without risking inadvertant failure. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 97c1789 commit c12f70c

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

tests/common/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ fn test_zero() {
1212
getrandom_impl(&mut [0u8; 0]).unwrap();
1313
}
1414

15+
// Return the number of bits in which s1 and s2 differ
16+
#[cfg(not(feature = "custom"))]
17+
fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize {
18+
assert_eq!(s1.len(), s2.len());
19+
let mut n = 0;
20+
for i in 0..s1.len() {
21+
n += (s1[i] ^ s2[i]).count_ones() as usize;
22+
}
23+
n
24+
}
25+
26+
// Tests the quality of calling getrandom on two large buffers
1527
#[test]
1628
#[cfg(not(feature = "custom"))]
1729
fn test_diff() {
@@ -21,13 +33,11 @@ fn test_diff() {
2133
let mut v2 = [0u8; 1000];
2234
getrandom_impl(&mut v2).unwrap();
2335

24-
let mut n_diff_bits = 0;
25-
for i in 0..v1.len() {
26-
n_diff_bits += (v1[i] ^ v2[i]).count_ones();
27-
}
28-
29-
// Check at least 1 bit per byte differs. p(failure) < 1e-1000 with random input.
30-
assert!(n_diff_bits >= v1.len() as u32);
36+
// Between 3.5 and 4.5 bits per byte should differ. Probability of failure:
37+
// ~ 2^(-94) = 2 * CDF[BinomialDistribution[8000, 0.5], 3500]
38+
let d = num_diff_bits(&v1, &v2);
39+
assert!(d > 3500);
40+
assert!(d < 4500);
3141
}
3242

3343
#[test]

0 commit comments

Comments
 (0)