Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump rand & adapt tests to its API changes #206

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ linux-raw-sys = { version = "0.7.0", features = ["ioctl"] }
nix = { version = "0.29", default-features = false }
phf = "0.11.2"
phf_codegen = "0.11.2"
rand = { version = "0.8", features = ["small_rng"] }
rand = { version = "0.9.0", features = ["small_rng"] }
regex = "1.10.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.122"
Expand Down
25 changes: 13 additions & 12 deletions tests/common/random.rs
Original file line number Diff line number Diff line change
@@ -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`
///
Expand Down Expand Up @@ -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]
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ impl RandomString {
where
D: Distribution<u8>,
{
thread_rng()
rng()
.sample_iter(dist)
.take(length)
.map(|b| b as char)
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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() {
Expand Down