tests: adapt to API changes in rand

This commit is contained in:
Daniel Hofstetter
2025-01-29 09:43:43 +01:00
parent b7f80b6b90
commit 1ddd589e5e

View File

@@ -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 // For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code. // file that was distributed with this source code.
use rand::distributions::{Distribution, Uniform}; use rand::distr::{Distribution, Uniform};
use rand::{thread_rng, Rng}; use rand::{rng, Rng};
/// Samples alphanumeric characters `[A-Za-z0-9]` including newline `\n` /// Samples alphanumeric characters `[A-Za-z0-9]` including newline `\n`
/// ///
@@ -38,7 +38,7 @@ impl AlphanumericNewline {
where where
R: Rng + ?Sized, R: Rng + ?Sized,
{ {
let idx = rng.gen_range(0..Self::CHARSET.len()); let idx = rng.random_range(0..Self::CHARSET.len());
Self::CHARSET[idx] Self::CHARSET[idx]
} }
} }
@@ -80,7 +80,7 @@ impl RandomString {
where where
D: Distribution<u8>, D: Distribution<u8>,
{ {
thread_rng() rng()
.sample_iter(dist) .sample_iter(dist)
.take(length) .take(length)
.map(|b| b as char) .map(|b| b as char)
@@ -132,15 +132,15 @@ impl RandomString {
return if num_delimiter > 0 { return if num_delimiter > 0 {
String::from(delimiter as char) String::from(delimiter as char)
} else { } else {
String::from(thread_rng().sample(&dist) as char) String::from(rng().sample(&dist) as char)
}; };
} }
let samples = length - 1; let samples = length - 1;
let mut result: Vec<u8> = thread_rng().sample_iter(&dist).take(samples).collect(); let mut result: Vec<u8> = rng().sample_iter(&dist).take(samples).collect();
if num_delimiter == 0 { if num_delimiter == 0 {
result.push(thread_rng().sample(&dist)); result.push(rng().sample(&dist));
return String::from_utf8(result).unwrap(); return String::from_utf8(result).unwrap();
} }
@@ -150,9 +150,10 @@ impl RandomString {
num_delimiter 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 { for _ in 0..num_delimiter {
let mut pos = between.sample(&mut thread_rng()); let mut pos = between.sample(&mut rng());
let turn = pos; let turn = pos;
while result[pos] == delimiter { while result[pos] == delimiter {
pos += 1; pos += 1;
@@ -169,7 +170,7 @@ impl RandomString {
if end_with_delimiter { if end_with_delimiter {
result.push(delimiter); result.push(delimiter);
} else { } else {
result.push(thread_rng().sample(&dist)); result.push(rng().sample(&dist));
} }
String::from_utf8(result).unwrap() String::from_utf8(result).unwrap()
@@ -179,7 +180,7 @@ impl RandomString {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use rand::distributions::Alphanumeric; use rand::distr::Alphanumeric;
#[test] #[test]
fn test_random_string_generate() { fn test_random_string_generate() {