Merge pull request #206 from cakebaker/bump_rand

Bump `rand` & adapt tests to its API changes
This commit is contained in:
Sylvestre Ledru
2025-01-31 15:47:19 +01:00
committed by GitHub
3 changed files with 62 additions and 42 deletions

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
// 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<u8>,
{
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<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 {
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() {