Skip to content

Commit

Permalink
Auto merge of #104658 - thomcc:rand-update-and-usable-no_std, r=Mark-…
Browse files Browse the repository at this point in the history
…Simulacrum

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

The main goal is actually removing `getrandom`, so that eventually we can allow running the stdlib test suite on tier3 targets which don't have `getrandom` support. Currently those targets can only run the subset of stdlib tests that exist in uitests, and (generally speaking), we prefer not to test libstd functionality in uitests, which came up recently in rust-lang/rust#104095 and rust-lang/rust#104185. Additionally, the fact that we can't update `rand`/`getrandom` means we're stuck with the old set of tier3 targets, so can't test new ones.

~~Anyway, I haven't checked that this actually does allow use on tier3 targets (I think it does not, as some work is needed in stdlib submodules) but it moves us slightly closer to this, and seems to allow at least finally updating our `rand` dep, which definitely improves the status quo.~~ Checked and works now.

For the most part, our tests and benchmarks are fine using hard-coded seeds. A couple tests seem to fail with this (stuff manipulating the environment expecting no collisions, for example), or become pointless (all inputs to a function become equivalent). In these cases I've done a (gross) dance (ab)using `RandomState` and `Location::caller()` for some extra "entropy".

Trying to share that code seems *way* more painful than it's worth given that the duplication is a 7-line function, even if the lines are quite gross. (Keeping in mind that sharing it would require adding `rand` as a non-dev dep to std, and exposing a type from it publicly, all of which sounds truly awful, even if done behind a perma-unstable feature).

See also some previous attempts:
- rust-lang/rust#86963 (in particular rust-lang/rust#86963 (comment) which explains why this is non-trivial)
- rust-lang/rust#89131
- rust-lang/rust#96626 (comment) (I tried in that PR at the same time, but settled for just removing the usage of `thread_rng()` from the benchmarks, since that was the main goal).
- rust-lang/rust#104185
- Probably more. It's very tempting of a thing to "just update".

r? `@Mark-Simulacrum`
  • Loading branch information
bors committed Jan 8, 2023
2 parents b055008 + f0685e3 commit 22e6883
Show file tree
Hide file tree
Showing 20 changed files with 474 additions and 388 deletions.
4 changes: 2 additions & 2 deletions alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ core = { path = "../core" }
compiler_builtins = { version = "0.1.40", features = ['rustc-dep-of-std'] }

[dev-dependencies]
rand = "0.7"
rand_xorshift = "0.2"
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
rand_xorshift = "0.3.0"

[[test]]
name = "collectionstests"
Expand Down
4 changes: 2 additions & 2 deletions alloc/benches/slice.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{mem, ptr};

use rand::distributions::{Alphanumeric, Standard};
use rand::distributions::{Alphanumeric, DistString, Standard};
use rand::Rng;
use test::{black_box, Bencher};

Expand Down Expand Up @@ -218,7 +218,7 @@ fn gen_strings(len: usize) -> Vec<String> {
let mut v = vec![];
for _ in 0..len {
let n = rng.gen::<usize>() % 20 + 1;
v.push((&mut rng).sample_iter(&Alphanumeric).take(n).collect());
v.push(Alphanumeric.sample_string(&mut rng, n));
}
v
}
Expand Down
4 changes: 2 additions & 2 deletions alloc/src/collections/binary_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ fn test_retain() {
#[test]
#[cfg(not(target_os = "emscripten"))]
fn panic_safe() {
use rand::{seq::SliceRandom, thread_rng};
use rand::seq::SliceRandom;
use std::cmp;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand All @@ -490,7 +490,7 @@ fn panic_safe() {
self.0.partial_cmp(&other.0)
}
}
let mut rng = thread_rng();
let mut rng = crate::test_helpers::test_rng();
const DATASZ: usize = 32;
// Miri is too slow
let ntest = if cfg!(miri) { 1 } else { 10 };
Expand Down
13 changes: 7 additions & 6 deletions alloc/src/collections/linked_list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::vec::Vec;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::thread;

use rand::{thread_rng, RngCore};
use rand::RngCore;

#[test]
fn test_basic() {
Expand Down Expand Up @@ -481,12 +481,12 @@ fn test_split_off_2() {
}
}

fn fuzz_test(sz: i32) {
fn fuzz_test(sz: i32, rng: &mut impl RngCore) {
let mut m: LinkedList<_> = LinkedList::new();
let mut v = vec![];
for i in 0..sz {
check_links(&m);
let r: u8 = thread_rng().next_u32() as u8;
let r: u8 = rng.next_u32() as u8;
match r % 6 {
0 => {
m.pop_back();
Expand Down Expand Up @@ -521,11 +521,12 @@ fn fuzz_test(sz: i32) {

#[test]
fn test_fuzz() {
let mut rng = crate::test_helpers::test_rng();
for _ in 0..25 {
fuzz_test(3);
fuzz_test(16);
fuzz_test(3, &mut rng);
fuzz_test(16, &mut rng);
#[cfg(not(miri))] // Miri is too slow
fuzz_test(189);
fuzz_test(189, &mut rng);
}
}

Expand Down
18 changes: 18 additions & 0 deletions alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
#![feature(unsized_fn_params)]
#![feature(c_unwind)]
#![feature(with_negative_coherence)]
#![cfg_attr(test, feature(panic_update_hook))]
//
// Rustdoc features:
#![feature(doc_cfg)]
Expand Down Expand Up @@ -255,3 +256,20 @@ pub mod vec;
pub mod __export {
pub use core::format_args;
}

#[cfg(test)]
#[allow(dead_code)] // Not used in all configurations
pub(crate) mod test_helpers {
/// Copied from `std::test_helpers::test_rng`, since these tests rely on the
/// seed not being the same for every RNG invocation too.
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
use std::hash::{BuildHasher, Hash, Hasher};
let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
std::panic::Location::caller().hash(&mut hasher);
let hc64 = hasher.finish();
let seed_vec =
hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
rand::SeedableRng::from_seed(seed)
}
}
3 changes: 3 additions & 0 deletions alloc/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use crate::borrow::ToOwned;
use crate::boxed::Box;
use crate::vec::Vec;

#[cfg(test)]
mod tests;

#[unstable(feature = "slice_range", issue = "76393")]
pub use core::slice::range;
#[unstable(feature = "array_chunks", issue = "74985")]
Expand Down
Loading

0 comments on commit 22e6883

Please sign in to comment.