From e7399d5da47f5b72f2ff9646364c16e9ecb6b000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 7 Oct 2024 17:37:51 +0300 Subject: [PATCH] fix MSRV --- tests/mod.rs | 60 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/tests/mod.rs b/tests/mod.rs index e3238cad..951b65dc 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,5 +1,4 @@ -use std::mem::MaybeUninit; - +use core::mem::MaybeUninit; use getrandom::{getrandom, getrandom_uninit}; #[cfg(getrandom_browser_test)] @@ -24,8 +23,10 @@ fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize { .sum() } -fn uninit_array() -> [MaybeUninit; N] { - [const { MaybeUninit::uninit() }; N] +// TODO: use `[const { MaybeUninit::uninit() }; N]` after MSRV is bumped to 1.79+ +// or `MaybeUninit::uninit_array` +fn uninit_vec(n: usize) -> Vec> { + vec![MaybeUninit::uninit(); n] } // Tests the quality of calling getrandom on two large buffers @@ -37,8 +38,8 @@ fn test_diff() { getrandom(&mut v1).unwrap(); getrandom(&mut v2).unwrap(); - let mut t1 = uninit_array::(); - let mut t2 = uninit_array::(); + let mut t1 = uninit_vec(N); + let mut t2 = uninit_vec(N); let r1 = getrandom_uninit(&mut t1).unwrap(); let r2 = getrandom_uninit(&mut t2).unwrap(); assert_eq!(r1.len(), N); @@ -54,18 +55,19 @@ fn test_diff() { assert!(d2 < 4500); } -// Tests the quality of calling getrandom repeatedly on small buffers #[test] fn test_small() { - let mut buf1 = [0u8; 64]; - let mut buf2 = [0u8; 64]; + const N: usize = 64; // For each buffer size, get at least 256 bytes and check that between // 3 and 5 bits per byte differ. Probability of failure: // ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256] - for size in 1..=64 { + for size in 1..=N { let mut num_bytes = 0; let mut diff_bits = 0; while num_bytes < 256 { + let mut buf1 = [0u8; N]; + let mut buf2 = [0u8; N]; + let s1 = &mut buf1[..size]; let s2 = &mut buf2[..size]; @@ -80,12 +82,50 @@ fn test_small() { } } +// Tests the quality of calling getrandom repeatedly on small buffers +#[test] +fn test_small_uninit() { + const N: usize = 64; + // For each buffer size, get at least 256 bytes and check that between + // 3 and 5 bits per byte differ. Probability of failure: + // ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256] + for size in 1..=N { + let mut num_bytes = 0; + let mut diff_bits = 0; + while num_bytes < 256 { + let mut buf1 = uninit_vec(N); + let mut buf2 = uninit_vec(N); + + let s1 = &mut buf1[..size]; + let s2 = &mut buf2[..size]; + + let r1 = getrandom_uninit(s1).unwrap(); + let r2 = getrandom_uninit(s2).unwrap(); + assert_eq!(r1.len(), size); + assert_eq!(r2.len(), size); + + num_bytes += size; + diff_bits += num_diff_bits(r1, r2); + } + assert!(diff_bits > 3 * num_bytes); + assert!(diff_bits < 5 * num_bytes); + } +} + #[test] fn test_huge() { let mut huge = [0u8; 100_000]; getrandom(&mut huge).unwrap(); } +#[test] +fn test_huge_uninit() { + const N: usize = 100_000; + let mut huge = uninit_vec(N); + let res = getrandom_uninit(&mut huge).unwrap(); + assert_eq!(res.len(), N); +} + #[test] #[cfg_attr( target_arch = "wasm32",