mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
tests/common/random.rs: adapt to rand API changes
This commit is contained in:
parent
6235f1cbb9
commit
730b404b6e
1 changed files with 14 additions and 13 deletions
|
@ -4,17 +4,17 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
#![allow(clippy::naive_bytecount)]
|
#![allow(clippy::naive_bytecount)]
|
||||||
|
|
||||||
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`
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust,ignore
|
/// ```rust,ignore
|
||||||
/// use rand::{Rng, thread_rng};
|
/// use rand::{Rng, rng};
|
||||||
///
|
///
|
||||||
/// let vec = thread_rng()
|
/// let vec = rng()
|
||||||
/// .sample_iter(AlphanumericNewline)
|
/// .sample_iter(AlphanumericNewline)
|
||||||
/// .take(10)
|
/// .take(10)
|
||||||
/// .collect::<Vec<u8>>();
|
/// .collect::<Vec<u8>>();
|
||||||
|
@ -39,7 +39,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]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ impl RandomizedString {
|
||||||
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)
|
||||||
|
@ -133,15 +133,15 @@ impl RandomizedString {
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,9 +151,10 @@ impl RandomizedString {
|
||||||
num_delimiter
|
num_delimiter
|
||||||
};
|
};
|
||||||
|
|
||||||
let between = Uniform::new(0, samples);
|
// it's safe to unwrap because samples is always > 0, thus low < high
|
||||||
|
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;
|
||||||
|
@ -170,7 +171,7 @@ impl RandomizedString {
|
||||||
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()
|
||||||
|
@ -180,7 +181,7 @@ impl RandomizedString {
|
||||||
#[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() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue