Make test_convert seedable, but generate the seed

This commit is contained in:
Henrik Hørlück Berg 2023-07-05 01:24:55 +02:00 committed by ridiculousfish
parent ac2810e9ef
commit 47f1dbe56c

View file

@ -164,12 +164,14 @@ fn str2hex(input: &[u8]) -> String {
#[test] #[test]
fn test_convert() { fn test_convert() {
use rand::random; use rand::random;
let seed: u128 = random::<u128>();
let mut rng = Pcg64Mcg::new(seed);
for _ in 0..ESCAPE_TEST_COUNT { for _ in 0..ESCAPE_TEST_COUNT {
let mut origin: Vec<u8> = vec![]; let length = rng.gen_range(0..=(2 * ESCAPE_TEST_LENGTH));
while (random::<usize>() % ESCAPE_TEST_LENGTH) != 0 { let mut origin: Vec<u8> = vec![0; length];
let byte = random(); rng.fill_bytes(&mut origin);
origin.push(byte);
}
let w = str2wcstring(&origin[..]); let w = str2wcstring(&origin[..]);
let n = wcs2string(&w); let n = wcs2string(&w);
@ -178,11 +180,13 @@ fn test_convert() {
n, n,
"Conversion cycle of string:\n{:4} chars: {}\n\ "Conversion cycle of string:\n{:4} chars: {}\n\
produced different string:\n\ produced different string:\n\
{:4} chars: {}", {:4} chars: {}\n
Use this seed to reproduce: {}",
origin.len(), origin.len(),
&str2hex(&origin), &str2hex(&origin),
n.len(), n.len(),
&str2hex(&n) &str2hex(&n),
seed,
); );
} }
} }