From bee422fea2e30890059f1b9287e61522b53a4e2c Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 4 Jul 2023 13:00:26 -0700 Subject: [PATCH] Use a faster, deterministic RNG in the string escape tests This shaves about 9 seconds off of the runtime, and makes the test deterministic. We do not touch the test_convert test because there is a known failure and we need to track it down before making it deterministic. --- fish-rust/Cargo.lock | 10 ++++++++++ fish-rust/Cargo.toml | 1 + fish-rust/src/tests/string_escape.rs | 15 +++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) 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 {