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.
This commit is contained in:
ridiculousfish 2023-07-04 13:00:26 -07:00
parent 0bfe83ce88
commit bee422fea2
3 changed files with 22 additions and 4 deletions

10
fish-rust/Cargo.lock generated
View File

@ -347,6 +347,7 @@ dependencies = [
"pcre2", "pcre2",
"printf-compat", "printf-compat",
"rand", "rand",
"rand_pcg",
"rsconf", "rsconf",
"unixstring", "unixstring",
"widestring", "widestring",
@ -789,6 +790,15 @@ dependencies = [
"getrandom", "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]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"

View File

@ -28,6 +28,7 @@ once_cell = "1.17.0"
rand = { version = "0.8.5", features = ["small_rng"] } rand = { version = "0.8.5", features = ["small_rng"] }
unixstring = "0.2.7" unixstring = "0.2.7"
widestring = "1.0.2" widestring = "1.0.2"
rand_pcg = "0.3.1"
[build-dependencies] [build-dependencies]
autocxx-build = "0.23.1" autocxx-build = "0.23.1"

View File

@ -5,7 +5,9 @@ use crate::common::{
}; };
use crate::wchar::{widestrs, wstr, WString}; use crate::wchar::{widestrs, wstr, WString};
use crate::wutil::encoding::{wcrtomb, zero_mbstate, AT_LEAST_MB_LEN_MAX}; 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 /// wcs2string is locale-dependent, so ensure we have a multibyte locale
/// before using it in a test. /// before using it in a test.
@ -99,15 +101,19 @@ fn test_escape_var() {
#[widestrs] #[widestrs]
#[test] #[test]
fn test_escape_crazy() { fn test_escape_random() {
setlocale(); setlocale();
let seed: u128 = 92348567983274852905629743984572;
let mut rng = Pcg64Mcg::new(seed);
let mut random_string = WString::new(); let mut random_string = WString::new();
let mut escaped_string; let mut escaped_string;
for _ in 0..(ESCAPE_TEST_COUNT as u32) { for _ in 0..(ESCAPE_TEST_COUNT as u32) {
random_string.clear(); random_string.clear();
while random::<usize>() % ESCAPE_TEST_LENGTH != 0 { let length = rng.gen_range(0..=(2 * ESCAPE_TEST_LENGTH));
for _ in 0..length {
random_string random_string
.push(char::from_u32((random::<u32>() % 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 [ for (escape_style, unescape_style) in [
@ -157,6 +163,7 @@ fn str2hex(input: &[u8]) -> String {
/// string comes back through double conversion. /// string comes back through double conversion.
#[test] #[test]
fn test_convert() { fn test_convert() {
use rand::random;
for _ in 0..ESCAPE_TEST_COUNT { for _ in 0..ESCAPE_TEST_COUNT {
let mut origin: Vec<u8> = vec![]; let mut origin: Vec<u8> = vec![];
while (random::<usize>() % ESCAPE_TEST_LENGTH) != 0 { while (random::<usize>() % ESCAPE_TEST_LENGTH) != 0 {