File tree 1 file changed +17
-7
lines changed
1 file changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,18 @@ fn test_zero() {
12
12
getrandom_impl ( & mut [ 0u8 ; 0 ] ) . unwrap ( ) ;
13
13
}
14
14
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
15
27
#[ test]
16
28
#[ cfg( not( feature = "custom" ) ) ]
17
29
fn test_diff ( ) {
@@ -21,13 +33,11 @@ fn test_diff() {
21
33
let mut v2 = [ 0u8 ; 1000 ] ;
22
34
getrandom_impl ( & mut v2) . unwrap ( ) ;
23
35
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 ) ;
31
41
}
32
42
33
43
#[ test]
You can’t perform that action at this time.
0 commit comments