From 1ddd589e5e86e538d0ef47b1cb1c55cd41409816 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Wed, 29 Jan 2025 09:43:43 +0100 Subject: [PATCH] tests: adapt to API changes in rand --- tests/common/random.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/common/random.rs b/tests/common/random.rs index 42b6eaa..72a2b07 100644 --- a/tests/common/random.rs +++ b/tests/common/random.rs @@ -1,10 +1,10 @@ -// This file is part of the uutils coreutils package. +// This file is part of the uutils util-linux package. // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -use rand::distributions::{Distribution, Uniform}; -use rand::{thread_rng, Rng}; +use rand::distr::{Distribution, Uniform}; +use rand::{rng, Rng}; /// Samples alphanumeric characters `[A-Za-z0-9]` including newline `\n` /// @@ -38,7 +38,7 @@ impl AlphanumericNewline { where R: Rng + ?Sized, { - let idx = rng.gen_range(0..Self::CHARSET.len()); + let idx = rng.random_range(0..Self::CHARSET.len()); Self::CHARSET[idx] } } @@ -80,7 +80,7 @@ impl RandomString { where D: Distribution, { - thread_rng() + rng() .sample_iter(dist) .take(length) .map(|b| b as char) @@ -132,15 +132,15 @@ impl RandomString { return if num_delimiter > 0 { String::from(delimiter as char) } else { - String::from(thread_rng().sample(&dist) as char) + String::from(rng().sample(&dist) as char) }; } let samples = length - 1; - let mut result: Vec = thread_rng().sample_iter(&dist).take(samples).collect(); + let mut result: Vec = rng().sample_iter(&dist).take(samples).collect(); if num_delimiter == 0 { - result.push(thread_rng().sample(&dist)); + result.push(rng().sample(&dist)); return String::from_utf8(result).unwrap(); } @@ -150,9 +150,10 @@ impl RandomString { num_delimiter }; - let between = Uniform::new(0, samples); + // safe to unwrap because samples is at least 1, thus high > low + let between = Uniform::new(0, samples).unwrap(); for _ in 0..num_delimiter { - let mut pos = between.sample(&mut thread_rng()); + let mut pos = between.sample(&mut rng()); let turn = pos; while result[pos] == delimiter { pos += 1; @@ -169,7 +170,7 @@ impl RandomString { if end_with_delimiter { result.push(delimiter); } else { - result.push(thread_rng().sample(&dist)); + result.push(rng().sample(&dist)); } String::from_utf8(result).unwrap() @@ -179,7 +180,7 @@ impl RandomString { #[cfg(test)] mod tests { use super::*; - use rand::distributions::Alphanumeric; + use rand::distr::Alphanumeric; #[test] fn test_random_string_generate() {