From cae6bc5e8264e3013d9d2a2ff160180051371a0e Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Mon, 17 Jan 2022 20:07:28 -0500 Subject: [PATCH 01/12] deps: update rand to 0.8 fix: #2888 --- Cargo.lock | 66 ++++----------------------------- Cargo.toml | 2 +- src/uu/factor/Cargo.toml | 2 +- src/uu/mktemp/Cargo.toml | 2 +- src/uu/shred/Cargo.toml | 2 +- src/uu/shuf/Cargo.toml | 2 +- src/uu/sort/Cargo.toml | 2 +- tests/benches/factor/Cargo.toml | 2 +- 8 files changed, 14 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e618372bd..64f440ccb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -267,15 +267,6 @@ dependencies = [ "clap 3.0.10", ] -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", -] - [[package]] name = "compare" version = "0.1.0" @@ -312,7 +303,7 @@ dependencies = [ "libc", "nix 0.23.1", "pretty_assertions", - "rand 0.7.3", + "rand 0.8.4", "regex", "rlimit", "selinux", @@ -792,12 +783,6 @@ dependencies = [ "libc", ] -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - [[package]] name = "gcd" version = "2.1.0" @@ -1495,19 +1480,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9" -dependencies = [ - "cloudabi", - "fuchsia-cprng", - "libc", - "rand_core 0.3.1", - "winapi 0.3.9", -] - [[package]] name = "rand" version = "0.7.3" @@ -1519,7 +1491,6 @@ dependencies = [ "rand_chacha 0.2.2", "rand_core 0.5.1", "rand_hc 0.2.0", - "rand_pcg", ] [[package]] @@ -1554,21 +1525,6 @@ dependencies = [ "rand_core 0.6.3", ] -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.2", -] - -[[package]] -name = "rand_core" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" - [[package]] name = "rand_core" version = "0.5.1" @@ -1605,15 +1561,6 @@ dependencies = [ "rand_core 0.6.3", ] -[[package]] -name = "rand_pcg" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" -dependencies = [ - "rand_core 0.5.1", -] - [[package]] name = "rayon" version = "1.5.1" @@ -2429,7 +2376,7 @@ dependencies = [ "num-traits", "paste 0.1.18", "quickcheck", - "rand 0.7.3", + "rand 0.8.4", "smallvec", "uucore", "uucore_procs", @@ -2652,7 +2599,7 @@ name = "uu_mktemp" version = "0.0.12" dependencies = [ "clap 3.0.10", - "rand 0.5.6", + "rand 0.8.4", "tempfile", "uucore", "uucore_procs", @@ -2917,7 +2864,7 @@ version = "0.0.12" dependencies = [ "clap 3.0.10", "libc", - "rand 0.7.3", + "rand 0.8.4", "uucore", "uucore_procs", ] @@ -2927,7 +2874,8 @@ name = "uu_shuf" version = "0.0.12" dependencies = [ "clap 3.0.10", - "rand 0.5.6", + "rand", + "rand_core", "uucore", "uucore_procs", ] @@ -2953,7 +2901,7 @@ dependencies = [ "itertools 0.10.3", "memchr 2.4.1", "ouroboros", - "rand 0.7.3", + "rand 0.8.4", "rayon", "tempfile", "unicode-width", diff --git a/Cargo.toml b/Cargo.toml index 14265524f..cff38933a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -368,7 +368,7 @@ filetime = "0.2" glob = "0.3.0" libc = "0.2" pretty_assertions = "1" -rand = "0.7" +rand = "0.8" regex = "1.0" sha1 = { version="0.6", features=["std"] } tempfile = "3.2.0" diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index d38f82f8e..083b24999 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -18,7 +18,7 @@ num-traits = "0.2.13" # used in src/numerics.rs, which is included by build.rs clap = { version = "3.0", features = ["wrap_help", "cargo"] } coz = { version = "0.1.3", optional = true } num-traits = "0.2.13" # Needs at least version 0.2.13 for "OverflowingAdd" -rand = { version = "0.7", features = ["small_rng"] } +rand = { version = "0.8", features = ["small_rng"] } smallvec = "1.7" # TODO(nicoo): Use `union` feature, requires Rust 1.49 or later. uucore = { version = ">=0.0.8", package = "uucore", path = "../../uucore" } uucore_procs = { version=">=0.0.8", package = "uucore_procs", path = "../../uucore_procs" } diff --git a/src/uu/mktemp/Cargo.toml b/src/uu/mktemp/Cargo.toml index c183b14a9..dc4b28329 100644 --- a/src/uu/mktemp/Cargo.toml +++ b/src/uu/mktemp/Cargo.toml @@ -16,7 +16,7 @@ path = "src/mktemp.rs" [dependencies] clap = { version = "3.0", features = ["wrap_help", "cargo"] } -rand = "0.5" +rand = "0.8" tempfile = "3.1" uucore = { version=">=0.0.11", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/shred/Cargo.toml b/src/uu/shred/Cargo.toml index 6b28f1242..46608dd86 100644 --- a/src/uu/shred/Cargo.toml +++ b/src/uu/shred/Cargo.toml @@ -17,7 +17,7 @@ path = "src/shred.rs" [dependencies] clap = { version = "3.0", features = ["wrap_help", "cargo"] } libc = "0.2.42" -rand = "0.7" +rand = "0.8" uucore = { version=">=0.0.11", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/shuf/Cargo.toml b/src/uu/shuf/Cargo.toml index d750e9c8e..b269e47c1 100644 --- a/src/uu/shuf/Cargo.toml +++ b/src/uu/shuf/Cargo.toml @@ -16,7 +16,7 @@ path = "src/shuf.rs" [dependencies] clap = { version = "3.0", features = ["wrap_help", "cargo"] } -rand = "0.5" +rand = "0.8" uucore = { version=">=0.0.11", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/sort/Cargo.toml b/src/uu/sort/Cargo.toml index b21c76e82..3d827193d 100644 --- a/src/uu/sort/Cargo.toml +++ b/src/uu/sort/Cargo.toml @@ -23,7 +23,7 @@ fnv = "1.0.7" itertools = "0.10.0" memchr = "2.4.0" ouroboros = "0.10.1" -rand = "0.7" +rand = "0.8" rayon = "1.5" tempfile = "3" unicode-width = "0.1.8" diff --git a/tests/benches/factor/Cargo.toml b/tests/benches/factor/Cargo.toml index b3b718477..5d9f39620 100644 --- a/tests/benches/factor/Cargo.toml +++ b/tests/benches/factor/Cargo.toml @@ -13,7 +13,7 @@ uu_factor = { path = "../../../src/uu/factor" } [dev-dependencies] array-init = "2.0.0" criterion = "0.3" -rand = "0.7" +rand = "0.8" rand_chacha = "0.2.2" From 771c9f5d9c8e4b313827306984736fb07c4081f3 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Mon, 17 Jan 2022 20:23:51 -0500 Subject: [PATCH 02/12] tests: update random_chars generator to map u8 to char Fix 'value of type `char` cannot be built from `std::iter::Iterator`' for split test. refs: https://docs.rs/rand/0.8.4/rand/distributions/struct.Alphanumeric.html#example --- tests/by-util/test_split.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index ebcc0926d..d55e13644 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -22,6 +22,7 @@ use std::{ fn random_chars(n: usize) -> String { thread_rng() .sample_iter(&rand::distributions::Alphanumeric) + .map(char::from) .take(n) .collect::() } From 1e0dc6c278a56b0fcbc3e953039abf9808b4a043 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Mon, 17 Jan 2022 21:23:55 -0500 Subject: [PATCH 03/12] tests: update test_random to take range Fix 'error[E0061]: this function takes 1 argument but 2 arguments were supplied'. --- tests/by-util/test_factor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/by-util/test_factor.rs b/tests/by-util/test_factor.rs index bcb1238bf..bd265f4ce 100644 --- a/tests/by-util/test_factor.rs +++ b/tests/by-util/test_factor.rs @@ -115,7 +115,7 @@ fn test_random() { // log distribution---higher probability for lower numbers let factor; loop { - let next = rng.gen_range(0_f64, log_num_primes).exp2().floor() as usize; + let next = rng.gen_range(0_f64..log_num_primes).exp2().floor() as usize; if next < NUM_PRIMES { factor = primes[next]; break; From a342df03f0a2e1e3aba7ea3687efa2da9591ab0e Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Tue, 18 Jan 2022 08:47:39 -0500 Subject: [PATCH 04/12] tests: update factor Distribution sample to take range --- src/uu/factor/src/factor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index 54ad0bdd4..e32b36db6 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -258,7 +258,7 @@ impl Distribution for Standard { // See Generating Random Factored Numbers, Easily, J. Cryptology (2003) 'attempt: loop { while n > 1 { - n = rng.gen_range(1, n); + n = rng.gen_range(1..n); if miller_rabin::is_prime(n) { if let Some(h) = g.checked_mul(n) { f.push(n); From 6bcca01e832d285ac7fcaf66491dd18a3177d5c9 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Wed, 19 Jan 2022 21:13:41 -0500 Subject: [PATCH 05/12] shuf: add deprecated rand crate ReadRng adapter It is deprecated pending future removal. This version copied from: https://github.com/rust-random/rand/blob/0.8.4/src/rngs/adapter/read.rs --- src/uu/shuf/Cargo.toml | 1 + src/uu/shuf/src/rand_read_adapter.rs | 150 +++++++++++++++++++++++++++ src/uu/shuf/src/shuf.rs | 6 +- 3 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 src/uu/shuf/src/rand_read_adapter.rs diff --git a/src/uu/shuf/Cargo.toml b/src/uu/shuf/Cargo.toml index b269e47c1..722d9722b 100644 --- a/src/uu/shuf/Cargo.toml +++ b/src/uu/shuf/Cargo.toml @@ -17,6 +17,7 @@ path = "src/shuf.rs" [dependencies] clap = { version = "3.0", features = ["wrap_help", "cargo"] } rand = "0.8" +rand_core = "0.6" uucore = { version=">=0.0.11", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.8", package="uucore_procs", path="../../uucore_procs" } diff --git a/src/uu/shuf/src/rand_read_adapter.rs b/src/uu/shuf/src/rand_read_adapter.rs new file mode 100644 index 000000000..25a9ca7fc --- /dev/null +++ b/src/uu/shuf/src/rand_read_adapter.rs @@ -0,0 +1,150 @@ +// Copyright 2018 Developers of the Rand project. +// Copyright 2013 The Rust Project Developers. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! A wrapper around any Read to treat it as an RNG. + +#![allow(deprecated)] + +use std::fmt; +use std::io::Read; + +use rand_core::{impls, Error, RngCore}; + + +/// An RNG that reads random bytes straight from any type supporting +/// [`std::io::Read`], for example files. +/// +/// This will work best with an infinite reader, but that is not required. +/// +/// This can be used with `/dev/urandom` on Unix but it is recommended to use +/// [`OsRng`] instead. +/// +/// # Panics +/// +/// `ReadRng` uses [`std::io::Read::read_exact`], which retries on interrupts. +/// All other errors from the underlying reader, including when it does not +/// have enough data, will only be reported through [`try_fill_bytes`]. +/// The other [`RngCore`] methods will panic in case of an error. +/// +/// [`OsRng`]: crate::rngs::OsRng +/// [`try_fill_bytes`]: RngCore::try_fill_bytes +#[derive(Debug)] +#[deprecated(since="0.8.4", note="removal due to lack of usage")] +pub struct ReadRng { + reader: R, +} + +impl ReadRng { + /// Create a new `ReadRng` from a `Read`. + pub fn new(r: R) -> ReadRng { + ReadRng { reader: r } + } +} + +impl RngCore for ReadRng { + fn next_u32(&mut self) -> u32 { + impls::next_u32_via_fill(self) + } + + fn next_u64(&mut self) -> u64 { + impls::next_u64_via_fill(self) + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + self.try_fill_bytes(dest).unwrap_or_else(|err| { + panic!( + "reading random bytes from Read implementation failed; error: {}", + err + ) + }); + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { + if dest.is_empty() { + return Ok(()); + } + // Use `std::io::read_exact`, which retries on `ErrorKind::Interrupted`. + self.reader + .read_exact(dest) + .map_err(|e| Error::new(ReadError(e))) + } +} + +/// `ReadRng` error type +#[derive(Debug)] +#[deprecated(since="0.8.4")] +pub struct ReadError(std::io::Error); + +impl fmt::Display for ReadError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ReadError: {}", self.0) + } +} + +impl std::error::Error for ReadError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(&self.0) + } +} + + +#[cfg(test)] +mod test { + use std::println; + + use super::ReadRng; + use crate::RngCore; + + #[test] + fn test_reader_rng_u64() { + // transmute from the target to avoid endianness concerns. + #[rustfmt::skip] + let v = [0u8, 0, 0, 0, 0, 0, 0, 1, + 0, 4, 0, 0, 3, 0, 0, 2, + 5, 0, 0, 0, 0, 0, 0, 0]; + let mut rng = ReadRng::new(&v[..]); + + assert_eq!(rng.next_u64(), 1 << 56); + assert_eq!(rng.next_u64(), (2 << 56) + (3 << 32) + (4 << 8)); + assert_eq!(rng.next_u64(), 5); + } + + #[test] + fn test_reader_rng_u32() { + let v = [0u8, 0, 0, 1, 0, 0, 2, 0, 3, 0, 0, 0]; + let mut rng = ReadRng::new(&v[..]); + + assert_eq!(rng.next_u32(), 1 << 24); + assert_eq!(rng.next_u32(), 2 << 16); + assert_eq!(rng.next_u32(), 3); + } + + #[test] + fn test_reader_rng_fill_bytes() { + let v = [1u8, 2, 3, 4, 5, 6, 7, 8]; + let mut w = [0u8; 8]; + + let mut rng = ReadRng::new(&v[..]); + rng.fill_bytes(&mut w); + + assert!(v == w); + } + + #[test] + fn test_reader_rng_insufficient_bytes() { + let v = [1u8, 2, 3, 4, 5, 6, 7, 8]; + let mut w = [0u8; 9]; + + let mut rng = ReadRng::new(&v[..]); + + let result = rng.try_fill_bytes(&mut w); + assert!(result.is_err()); + println!("Error: {}", result.unwrap_err()); + } +} diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index c1090e5ff..596953d3d 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -15,6 +15,8 @@ use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; use uucore::InvalidEncodingHandling; +mod rand_read_adapter; + enum Mode { Default(String), Echo(Vec), @@ -244,7 +246,7 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) -> UResult<()> { Some(r) => { let file = File::open(&r[..]) .map_err_context(|| format!("failed to open random source {}", r.quote()))?; - WrappedRng::RngFile(rand::rngs::adapter::ReadRng::new(file)) + WrappedRng::RngFile(rand_read_adapter::ReadRng::new(file)) } None => WrappedRng::RngDefault(rand::thread_rng()), }; @@ -302,7 +304,7 @@ fn parse_range(input_range: &str) -> Result<(usize, usize), String> { } enum WrappedRng { - RngFile(rand::rngs::adapter::ReadRng), + RngFile(rand_read_adapter::ReadRng), RngDefault(rand::rngs::ThreadRng), } From 26308946587bd75728adc8161d3f4c60dcfb30f9 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Wed, 19 Jan 2022 21:41:05 -0500 Subject: [PATCH 06/12] shuf: remove ReadRng deprecation notices --- src/uu/shuf/src/rand_read_adapter.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/uu/shuf/src/rand_read_adapter.rs b/src/uu/shuf/src/rand_read_adapter.rs index 25a9ca7fc..1b5860056 100644 --- a/src/uu/shuf/src/rand_read_adapter.rs +++ b/src/uu/shuf/src/rand_read_adapter.rs @@ -9,14 +9,11 @@ //! A wrapper around any Read to treat it as an RNG. -#![allow(deprecated)] - use std::fmt; use std::io::Read; use rand_core::{impls, Error, RngCore}; - /// An RNG that reads random bytes straight from any type supporting /// [`std::io::Read`], for example files. /// @@ -35,7 +32,6 @@ use rand_core::{impls, Error, RngCore}; /// [`OsRng`]: crate::rngs::OsRng /// [`try_fill_bytes`]: RngCore::try_fill_bytes #[derive(Debug)] -#[deprecated(since="0.8.4", note="removal due to lack of usage")] pub struct ReadRng { reader: R, } @@ -78,7 +74,6 @@ impl RngCore for ReadRng { /// `ReadRng` error type #[derive(Debug)] -#[deprecated(since="0.8.4")] pub struct ReadError(std::io::Error); impl fmt::Display for ReadError { @@ -93,7 +88,6 @@ impl std::error::Error for ReadError { } } - #[cfg(test)] mod test { use std::println; From a6f8d1d9fd1c09c506644856eee7118aa250a37c Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Sat, 22 Jan 2022 11:24:33 -0500 Subject: [PATCH 07/12] shuf: fix crate relative import for vendored rand read adapter --- src/uu/shuf/src/rand_read_adapter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/shuf/src/rand_read_adapter.rs b/src/uu/shuf/src/rand_read_adapter.rs index 1b5860056..bde03a928 100644 --- a/src/uu/shuf/src/rand_read_adapter.rs +++ b/src/uu/shuf/src/rand_read_adapter.rs @@ -93,7 +93,7 @@ mod test { use std::println; use super::ReadRng; - use crate::RngCore; + use rand::RngCore; #[test] fn test_reader_rng_u64() { From a950c98bcf78f9ea9a69adc523a502d10faa4881 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Sat, 22 Jan 2022 11:31:09 -0500 Subject: [PATCH 08/12] cspell: add endianness to jargon list --- .vscode/cspell.dictionaries/jargon.wordlist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/cspell.dictionaries/jargon.wordlist.txt b/.vscode/cspell.dictionaries/jargon.wordlist.txt index 69c72d17d..4e5f11e8d 100644 --- a/.vscode/cspell.dictionaries/jargon.wordlist.txt +++ b/.vscode/cspell.dictionaries/jargon.wordlist.txt @@ -28,6 +28,7 @@ devs discoverability duplicative dsync +endianness enqueue errored executable From 2b19dd20ae4629c66d060ce28c5e47c8442ab4e0 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Sat, 22 Jan 2022 11:32:22 -0500 Subject: [PATCH 09/12] cspell: add impls to abbrevs in acronyms+names list --- .vscode/cspell.dictionaries/acronyms+names.wordlist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt b/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt index a46448a32..53307bf35 100644 --- a/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt +++ b/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt @@ -35,6 +35,7 @@ WASM XFS aarch flac +impls lzma # * names From c037382df7a79cd670610e5640c380624a2ed561 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Sat, 22 Jan 2022 12:54:24 -0500 Subject: [PATCH 10/12] factor: update quickcheck dev dep to 1.0.3 quickcheck <1 uses rand 0.6.x which results in E0599 errors. Upgrading resolves that error and lets us remove the older rand version from our deps. refs: https://stackoverflow.com/questions/56901973/errore0599-no-method-named-gen-found-for-type-mut-g-in-the-current-scope/56902740#56902740 --- Cargo.lock | 97 ++++++++-------------------------------- src/uu/factor/Cargo.toml | 2 +- 2 files changed, 20 insertions(+), 79 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64f440ccb..aba7791e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,7 +303,7 @@ dependencies = [ "libc", "nix 0.23.1", "pretty_assertions", - "rand 0.8.4", + "rand", "regex", "rlimit", "selinux", @@ -663,7 +663,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68df3f2b690c1b86e65ef7830956aededf3cb0a16f898f79b9a6f421a7b6211b" dependencies = [ - "rand 0.8.4", + "rand", ] [[package]] @@ -692,9 +692,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "env_logger" -version = "0.7.1" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" dependencies = [ "log", "regex", @@ -811,17 +811,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if 1.0.0", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] - [[package]] name = "getrandom" version = "0.2.4" @@ -830,7 +819,7 @@ checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" dependencies = [ "cfg-if 1.0.0", "libc", - "wasi 0.10.2+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -1455,14 +1444,13 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" [[package]] name = "quickcheck" -version = "0.9.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f" +checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" dependencies = [ - "env_logger 0.7.1", + "env_logger 0.8.4", "log", - "rand 0.7.3", - "rand_core 0.5.1", + "rand", ] [[package]] @@ -1480,19 +1468,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc 0.2.0", -] - [[package]] name = "rand" version = "0.8.4" @@ -1500,19 +1475,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ "libc", - "rand_chacha 0.3.1", - "rand_core 0.6.3", - "rand_hc 0.3.1", -] - -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", + "rand_chacha", + "rand_core", + "rand_hc", ] [[package]] @@ -1522,16 +1487,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core 0.6.3", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", + "rand_core", ] [[package]] @@ -1540,16 +1496,7 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ - "getrandom 0.2.4", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", + "getrandom", ] [[package]] @@ -1558,7 +1505,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" dependencies = [ - "rand_core 0.6.3", + "rand_core", ] [[package]] @@ -2376,7 +2323,7 @@ dependencies = [ "num-traits", "paste 0.1.18", "quickcheck", - "rand 0.8.4", + "rand", "smallvec", "uucore", "uucore_procs", @@ -2599,7 +2546,7 @@ name = "uu_mktemp" version = "0.0.12" dependencies = [ "clap 3.0.10", - "rand 0.8.4", + "rand", "tempfile", "uucore", "uucore_procs", @@ -2864,7 +2811,7 @@ version = "0.0.12" dependencies = [ "clap 3.0.10", "libc", - "rand 0.8.4", + "rand", "uucore", "uucore_procs", ] @@ -2901,7 +2848,7 @@ dependencies = [ "itertools 0.10.3", "memchr 2.4.1", "ouroboros", - "rand 0.8.4", + "rand", "rayon", "tempfile", "unicode-width", @@ -3251,12 +3198,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index 083b24999..9d53cd52a 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -25,7 +25,7 @@ uucore_procs = { version=">=0.0.8", package = "uucore_procs", path = "../../uuco [dev-dependencies] paste = "0.1.18" -quickcheck = "0.9.2" +quickcheck = "1.0.3" [[bin]] From e24ecea1dad1dc4b6b05b4ecba5566d30ea89f72 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Sat, 22 Jan 2022 13:21:52 -0500 Subject: [PATCH 11/12] factor: tests: update Arbitrary impl for Factors Upstream removed the Gen trait and made the gen method private in https://github.com/BurntSushi/quickcheck/commit/d286e4db208535692dd5fb6a580077e5ecbb747b --- src/uu/factor/src/factor.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index e32b36db6..d5dbf2491 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -277,8 +277,8 @@ impl Distribution for Standard { #[cfg(test)] impl quickcheck::Arbitrary for Factors { - fn arbitrary(g: &mut G) -> Self { - g.gen() + fn arbitrary(g: &mut quickcheck::Gen) -> Self { + factor(u64::arbitrary(g)) } } From e6fdf0761ff3773f580a26950a930dd17b136be5 Mon Sep 17 00:00:00 2001 From: Greg Guthe <226605+g-k@users.noreply.github.com> Date: Sat, 22 Jan 2022 15:17:37 -0500 Subject: [PATCH 12/12] factor: ignore quickcheck tests using unhandled large vals refs: #1559 --- src/uu/factor/src/miller_rabin.rs | 6 +++- src/uu/factor/src/numeric/gcd.rs | 29 +++++++++++++++++--- src/uu/factor/src/numeric/modular_inverse.rs | 12 +++++--- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/uu/factor/src/miller_rabin.rs b/src/uu/factor/src/miller_rabin.rs index ec6d81c0f..d336188a5 100644 --- a/src/uu/factor/src/miller_rabin.rs +++ b/src/uu/factor/src/miller_rabin.rs @@ -200,7 +200,11 @@ mod tests { quickcheck! { fn composites(i: u64, j: u64) -> bool { - i < 2 || j < 2 || !is_prime(i*j) + // TODO: #1559 factor n > 2^64 - 1 + match i.checked_mul(j) { + Some(n) => i < 2 || j < 2 || !is_prime(n), + _ => true, + } } } } diff --git a/src/uu/factor/src/numeric/gcd.rs b/src/uu/factor/src/numeric/gcd.rs index 78197c722..089318f48 100644 --- a/src/uu/factor/src/numeric/gcd.rs +++ b/src/uu/factor/src/numeric/gcd.rs @@ -6,6 +6,8 @@ // * For the full copyright and license information, please view the LICENSE file // * that was distributed with this source code. +// spell-checker:ignore (vars) kgcdab gcdac gcdbc + use std::cmp::min; use std::mem::swap; @@ -93,16 +95,35 @@ mod tests { } fn scalar_multiplication(a: u64, b: u64, k: u64) -> bool { - gcd(k * a, k * b) == k * gcd(a, b) + // TODO: #1559 factor n > 2^64 - 1 + match (k.checked_mul(a), k.checked_mul(b), k.checked_mul(gcd(a, b))) { + (Some(ka), Some(kb), Some(kgcdab)) => gcd(ka, kb) == kgcdab, + _ => true + } } fn multiplicative(a: u64, b: u64, c: u64) -> bool { - // gcd(ab, c) = gcd(a, c) gcd(b, c) when a and b coprime - gcd(a, b) != 1 || gcd(a * b, c) == gcd(a, c) * gcd(b, c) + // TODO: #1559 factor n > 2^64 - 1 + match (a.checked_mul(b), gcd(a, c).checked_mul(gcd(b, c))) { + (Some(ab), Some(gcdac_gcdbc)) => { + // gcd(ab, c) = gcd(a, c) gcd(b, c) when a and b coprime + gcd(a, b) != 1 || gcd(ab, c) == gcdac_gcdbc + }, + _ => true, + } } fn linearity(a: u64, b: u64, k: u64) -> bool { - gcd(a + k * b, b) == gcd(a, b) + // TODO: #1559 factor n > 2^64 - 1 + match k.checked_mul(b) { + Some(kb) => { + match a.checked_add(kb) { + Some(a_plus_kb) => gcd(a_plus_kb, b) == gcd(a, b), + _ => true, + } + } + _ => true, + } } } } diff --git a/src/uu/factor/src/numeric/modular_inverse.rs b/src/uu/factor/src/numeric/modular_inverse.rs index e4827a3ee..5b37a9782 100644 --- a/src/uu/factor/src/numeric/modular_inverse.rs +++ b/src/uu/factor/src/numeric/modular_inverse.rs @@ -63,13 +63,17 @@ mod tests { quickcheck! { fn random_values_u32(n: u32) -> bool { - let n = 2 * n + 1; - modular_inverse(n).wrapping_mul(n) == 1 + match 2_u32.checked_mul(n) { + Some(n) => modular_inverse(n + 1).wrapping_mul(n + 1) == 1, + _ => true, + } } fn random_values_u64(n: u64) -> bool { - let n = 2 * n + 1; - modular_inverse(n).wrapping_mul(n) == 1 + match 2_u64.checked_mul(n) { + Some(n) => modular_inverse(n + 1).wrapping_mul(n + 1) == 1, + _ => true, + } } } }