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",
"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"

View File

@ -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"

View File

@ -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::<usize>() % 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::<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 [
@ -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<u8> = vec![];
while (random::<usize>() % ESCAPE_TEST_LENGTH) != 0 {