1
Fork 0

Update rand in the stdlib tests, and remove the getrandom feature from it

This commit is contained in:
Thom Chiovoloni 2022-11-20 16:55:41 -08:00
parent 659e169d37
commit a4bf36e87b
No known key found for this signature in database
GPG key ID: D7733D1D7A775F0A
22 changed files with 508 additions and 480 deletions

View file

@ -33,7 +33,8 @@ default-features = false
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive']
[dev-dependencies]
rand = "0.7"
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
rand_xorshift = "0.3.0"
[target.'cfg(any(all(target_family = "wasm", not(target_os = "emscripten")), all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }

View file

@ -3,7 +3,8 @@ use super::HashMap;
use super::RandomState;
use crate::assert_matches::assert_matches;
use crate::cell::RefCell;
use rand::{thread_rng, Rng};
use crate::test_helpers::test_rng;
use rand::Rng;
use realstd::collections::TryReserveErrorKind::*;
// https://github.com/rust-lang/rust/issues/62301
@ -710,16 +711,16 @@ fn test_entry_take_doesnt_corrupt() {
}
let mut m = HashMap::new();
let mut rng = thread_rng();
let mut rng = test_rng();
// Populate the map with some items.
for _ in 0..50 {
let x = rng.gen_range(-10, 10);
let x = rng.gen_range(-10..10);
m.insert(x, ());
}
for _ in 0..1000 {
let x = rng.gen_range(-10, 10);
let x = rng.gen_range(-10..10);
match m.entry(x) {
Vacant(_) => {}
Occupied(e) => {

View file

@ -10,7 +10,7 @@ use crate::sys_common::io::test::{tmpdir, TempDir};
use crate::thread;
use crate::time::{Duration, Instant};
use rand::{rngs::StdRng, RngCore, SeedableRng};
use rand::RngCore;
#[cfg(unix)]
use crate::os::unix::fs::symlink as symlink_dir;
@ -1181,7 +1181,7 @@ fn _assert_send_sync() {
#[test]
fn binary_file() {
let mut bytes = [0; 1024];
StdRng::from_entropy().fill_bytes(&mut bytes);
crate::test_helpers::test_rng().fill_bytes(&mut bytes);
let tmpdir = tmpdir();
@ -1194,7 +1194,7 @@ fn binary_file() {
#[test]
fn write_then_read() {
let mut bytes = [0; 1024];
StdRng::from_entropy().fill_bytes(&mut bytes);
crate::test_helpers::test_rng().fill_bytes(&mut bytes);
let tmpdir = tmpdir();

View file

@ -652,3 +652,30 @@ mod sealed {
#[unstable(feature = "sealed", issue = "none")]
pub trait Sealed {}
}
#[cfg(test)]
#[allow(dead_code)] // Not used in all configurations.
pub(crate) mod test_helpers {
/// Test-only replacement for `rand::thread_rng()`, which is unusable for
/// us, as we want to allow running stdlib tests on tier-3 targets which may
/// not have `getrandom` support.
///
/// Does a bit of a song and dance to ensure that the seed is different on
/// each call (as some tests sadly rely on this), but doesn't try that hard.
///
/// This is duplicated in the `core`, `alloc` test suites (as well as
/// `std`'s integration tests), but figuring out a mechanism to share these
/// seems far more painful than copy-pasting a 7 line function a couple
/// times, given that even under a perma-unstable feature, I don't think we
/// want to expose types from `rand` from `std`.
#[track_caller]
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
use core::hash::{BuildHasher, Hash, Hasher};
let mut hasher = crate::collections::hash_map::RandomState::new().build_hasher();
core::panic::Location::caller().hash(&mut hasher);
let hc64 = hasher.finish();
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
rand::SeedableRng::from_seed(seed)
}
}

View file

@ -2,7 +2,7 @@ use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sync::mpsc::channel;
use crate::sync::{Arc, RwLock, RwLockReadGuard, TryLockError};
use crate::thread;
use rand::{self, Rng};
use rand::Rng;
#[derive(Eq, PartialEq, Debug)]
struct NonCopy(i32);
@ -28,7 +28,7 @@ fn frob() {
let tx = tx.clone();
let r = r.clone();
thread::spawn(move || {
let mut rng = rand::thread_rng();
let mut rng = crate::test_helpers::test_rng();
for _ in 0..M {
if rng.gen_bool(1.0 / (N as f64)) {
drop(r.write().unwrap());

View file

@ -39,9 +39,10 @@ pub mod test {
}
}
#[track_caller] // for `test_rng`
pub fn tmpdir() -> TempDir {
let p = env::temp_dir();
let mut r = rand::thread_rng();
let mut r = crate::test_helpers::test_rng();
let ret = p.join(&format!("rust-{}", r.next_u32()));
fs::create_dir(&ret).unwrap();
TempDir(ret)

View file

@ -1,12 +1,24 @@
use std::env::*;
use std::ffi::{OsStr, OsString};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use rand::distributions::{Alphanumeric, DistString};
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
/// seed not being the same for every RNG invocation too.
#[track_caller]
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
use core::hash::{BuildHasher, Hash, Hasher};
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
core::panic::Location::caller().hash(&mut hasher);
let hc64 = hasher.finish();
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
rand::SeedableRng::from_seed(seed)
}
#[track_caller]
fn make_rand_name() -> OsString {
let rng = thread_rng();
let n = format!("TEST{}", rng.sample_iter(&Alphanumeric).take(10).collect::<String>());
let n = format!("TEST{}", Alphanumeric.sample_string(&mut test_rng(), 10));
let n = OsString::from(n);
assert!(var_os(&n).is_none());
n