diff --git a/fish-rust/Cargo.lock b/fish-rust/Cargo.lock index b13acfc0a..19ecc5c3a 100644 --- a/fish-rust/Cargo.lock +++ b/fish-rust/Cargo.lock @@ -347,6 +347,7 @@ dependencies = [ "pcre2", "printf-compat", "rand", + "rand_pcg", "rsconf", "unixstring", "widestring", @@ -789,6 +790,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_pcg" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e" +dependencies = [ + "rand_core", +] + [[package]] name = "redox_syscall" version = "0.3.5" diff --git a/fish-rust/Cargo.toml b/fish-rust/Cargo.toml index f7de98569..763e02cd3 100644 --- a/fish-rust/Cargo.toml +++ b/fish-rust/Cargo.toml @@ -28,6 +28,7 @@ once_cell = "1.17.0" rand = { version = "0.8.5", features = ["small_rng"] } unixstring = "0.2.7" widestring = "1.0.2" +rand_pcg = "0.3.1" [build-dependencies] autocxx-build = "0.23.1" diff --git a/fish-rust/src/tests/string_escape.rs b/fish-rust/src/tests/string_escape.rs index d44038212..96ce18858 100644 --- a/fish-rust/src/tests/string_escape.rs +++ b/fish-rust/src/tests/string_escape.rs @@ -5,7 +5,9 @@ use crate::common::{ }; use crate::wchar::{widestrs, wstr, WString}; use crate::wutil::encoding::{wcrtomb, zero_mbstate, AT_LEAST_MB_LEN_MAX}; -use rand::random; +use rand::SeedableRng; +use rand::{Rng, RngCore}; +use rand_pcg::Pcg64Mcg; /// wcs2string is locale-dependent, so ensure we have a multibyte locale /// before using it in a test. @@ -99,15 +101,19 @@ fn test_escape_var() { #[widestrs] #[test] -fn test_escape_crazy() { +fn test_escape_random() { setlocale(); + let seed: u128 = 92348567983274852905629743984572; + let mut rng = Pcg64Mcg::new(seed); + let mut random_string = WString::new(); let mut escaped_string; for _ in 0..(ESCAPE_TEST_COUNT as u32) { random_string.clear(); - while random::() % ESCAPE_TEST_LENGTH != 0 { + let length = rng.gen_range(0..=(2 * ESCAPE_TEST_LENGTH)); + for _ in 0..length { random_string - .push(char::from_u32((random::() % ESCAPE_TEST_CHAR as u32) + 1).unwrap()); + .push(char::from_u32((rng.next_u32() % ESCAPE_TEST_CHAR as u32) + 1).unwrap()); } for (escape_style, unescape_style) in [ @@ -157,6 +163,7 @@ fn str2hex(input: &[u8]) -> String { /// string comes back through double conversion. #[test] fn test_convert() { + use rand::random; for _ in 0..ESCAPE_TEST_COUNT { let mut origin: Vec = vec![]; while (random::() % ESCAPE_TEST_LENGTH) != 0 {