From ac35a621a9e7ec8f9caf650910ef3bb25a2343bb Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Thu, 1 Sep 2022 16:09:21 +0300 Subject: [PATCH 1/6] Upgrade uint:quickcheck --- uint/Cargo.toml | 5 ++--- uint/src/lib.rs | 4 ---- uint/src/uint.rs | 18 +++++++++++------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/uint/Cargo.toml b/uint/Cargo.toml index 1de346d84..91a1e031b 100644 --- a/uint/Cargo.toml +++ b/uint/Cargo.toml @@ -13,8 +13,7 @@ rust-version = "1.56.1" [dependencies] byteorder = { version = "1.4.2", default-features = false } crunchy = { version = "0.2.2", default-features = false } -qc = { package = "quickcheck", version = "0.9.0", optional = true } -rand07 = { package = "rand", version = "0.7", default-features = false, optional = true } +qc = { package = "quickcheck", version = "1", optional = true } hex = { version = "0.4", default-features = false } static_assertions = "1.0.0" arbitrary = { version = "1.0", optional = true } @@ -22,7 +21,7 @@ arbitrary = { version = "1.0", optional = true } [features] default = ["std"] std = ["byteorder/std", "crunchy/std", "hex/std"] -quickcheck = ["qc", "rand07"] +quickcheck = ["qc"] [[example]] name = "modular" diff --git a/uint/src/lib.rs b/uint/src/lib.rs index 83ab957a2..85460db79 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -25,10 +25,6 @@ pub use hex; #[doc(hidden)] pub use qc; -#[cfg(feature = "quickcheck")] -#[doc(hidden)] -pub use rand07; - #[cfg(feature = "arbitrary")] #[doc(hidden)] pub use arbitrary; diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 1b633fc79..cd340fc4e 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1742,11 +1742,8 @@ macro_rules! construct_uint { macro_rules! impl_quickcheck_arbitrary_for_uint { ($uint: ty, $n_bytes: tt) => { impl $crate::qc::Arbitrary for $uint { - fn arbitrary(g: &mut G) -> Self { - let mut res = [0u8; $n_bytes]; - - use $crate::rand07::Rng; - let p: f64 = $crate::rand07::rngs::OsRng.gen(); + fn arbitrary(g: &mut $crate::qc::Gen) -> Self { + let p = f64::arbitrary(g) % 1.; // make it more likely to generate smaller numbers that // don't use up the full $n_bytes let range = @@ -1761,8 +1758,15 @@ macro_rules! impl_quickcheck_arbitrary_for_uint { $n_bytes / 5 }; - let size = g.gen_range(0, range); - g.fill_bytes(&mut res[..size]); + let size: usize = usize::arbitrary(g) % range; + + let res: [u8; $n_bytes] = $crate::core_::array::from_fn(|i| { + if i > range { + 0 + } else { + u8::arbitrary(g) + } + }); res.as_ref().into() } From 6e8f55ad7847ddf96b8e439ded3f7b5db15c53a4 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Thu, 1 Sep 2022 16:10:21 +0300 Subject: [PATCH 2/6] Upgrade fixed-hash:quickcheck --- fixed-hash/Cargo.toml | 2 +- fixed-hash/src/hash.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/fixed-hash/Cargo.toml b/fixed-hash/Cargo.toml index 27ad7d217..d66dad70e 100644 --- a/fixed-hash/Cargo.toml +++ b/fixed-hash/Cargo.toml @@ -16,7 +16,7 @@ features = ["quickcheck", "api-dummy"] [dependencies] byteorder = { version = "1.4.2", optional = true, default-features = false } -quickcheck = { version = "0.9.0", optional = true } +quickcheck = { version = "1", optional = true } rand = { version = "0.8.0", optional = true, default-features = false } rustc-hex = { version = "2.0.1", optional = true, default-features = false } static_assertions = "1.0.0" diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index 232245868..5a6229095 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -629,9 +629,10 @@ macro_rules! impl_quickcheck_for_fixed_hash { macro_rules! impl_quickcheck_for_fixed_hash { ( $name:ident ) => { impl $crate::quickcheck::Arbitrary for $name { - fn arbitrary(g: &mut G) -> Self { - let mut res = [0u8; $crate::core_::mem::size_of::()]; - g.fill_bytes(&mut res[..Self::len_bytes()]); + fn arbitrary(g: &mut $crate::quickcheck::Gen) -> Self { + let res: [u8; Self::len_bytes()] = $crate::core_::array::from_fn(|_| { + u8::arbitrary(g) + }); Self::from(res) } } From 28d11c95df68dc113414491318d698c967451e31 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Thu, 1 Sep 2022 16:13:46 +0300 Subject: [PATCH 3/6] cargo fmt --- fixed-hash/src/hash.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index 5a6229095..9dc356cdc 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -630,9 +630,7 @@ macro_rules! impl_quickcheck_for_fixed_hash { ( $name:ident ) => { impl $crate::quickcheck::Arbitrary for $name { fn arbitrary(g: &mut $crate::quickcheck::Gen) -> Self { - let res: [u8; Self::len_bytes()] = $crate::core_::array::from_fn(|_| { - u8::arbitrary(g) - }); + let res: [u8; Self::len_bytes()] = $crate::core_::array::from_fn(|_| u8::arbitrary(g)); Self::from(res) } } From 777a855ab2e1243eec78b4e8de1502d2ee039a29 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Thu, 1 Sep 2022 16:32:06 +0300 Subject: [PATCH 4/6] Generate a probability for len of an Arbitrary in a more fancy way --- uint/src/uint.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index cd340fc4e..608ef4155 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1743,15 +1743,15 @@ macro_rules! impl_quickcheck_arbitrary_for_uint { ($uint: ty, $n_bytes: tt) => { impl $crate::qc::Arbitrary for $uint { fn arbitrary(g: &mut $crate::qc::Gen) -> Self { - let p = f64::arbitrary(g) % 1.; + let p = usize::arbitrary(g) % 100; // make it more likely to generate smaller numbers that // don't use up the full $n_bytes let range = // 10% chance to generate number that uses up to $n_bytes - if p < 0.1 { + if p < 10 { $n_bytes // 10% chance to generate number that uses up to $n_bytes / 2 - } else if p < 0.2 { + } else if p < 20 { $n_bytes / 2 // 80% chance to generate number that uses up to $n_bytes / 5 } else { @@ -1761,7 +1761,7 @@ macro_rules! impl_quickcheck_arbitrary_for_uint { let size: usize = usize::arbitrary(g) % range; let res: [u8; $n_bytes] = $crate::core_::array::from_fn(|i| { - if i > range { + if i > size { 0 } else { u8::arbitrary(g) From a8d9a22012ebb5a459fab3462b437a3e628c6744 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Thu, 1 Sep 2022 16:39:24 +0300 Subject: [PATCH 5/6] Get rid of quickcheck->qc renaming --- uint/Cargo.toml | 3 +-- uint/src/lib.rs | 2 +- uint/src/uint.rs | 4 ++-- uint/tests/uint_tests.rs | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/uint/Cargo.toml b/uint/Cargo.toml index 91a1e031b..7be3e29b1 100644 --- a/uint/Cargo.toml +++ b/uint/Cargo.toml @@ -13,7 +13,7 @@ rust-version = "1.56.1" [dependencies] byteorder = { version = "1.4.2", default-features = false } crunchy = { version = "0.2.2", default-features = false } -qc = { package = "quickcheck", version = "1", optional = true } +quickcheck = { version = "1", optional = true } hex = { version = "0.4", default-features = false } static_assertions = "1.0.0" arbitrary = { version = "1.0", optional = true } @@ -21,7 +21,6 @@ arbitrary = { version = "1.0", optional = true } [features] default = ["std"] std = ["byteorder/std", "crunchy/std", "hex/std"] -quickcheck = ["qc"] [[example]] name = "modular" diff --git a/uint/src/lib.rs b/uint/src/lib.rs index 85460db79..e259c79d7 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -23,7 +23,7 @@ pub use hex; #[cfg(feature = "quickcheck")] #[doc(hidden)] -pub use qc; +pub use quickcheck; #[cfg(feature = "arbitrary")] #[doc(hidden)] diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 608ef4155..874e58c82 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1741,8 +1741,8 @@ macro_rules! construct_uint { #[doc(hidden)] macro_rules! impl_quickcheck_arbitrary_for_uint { ($uint: ty, $n_bytes: tt) => { - impl $crate::qc::Arbitrary for $uint { - fn arbitrary(g: &mut $crate::qc::Gen) -> Self { + impl $crate::quickcheck::Arbitrary for $uint { + fn arbitrary(g: &mut $crate::quickcheck::Gen) -> Self { let p = usize::arbitrary(g) % 100; // make it more likely to generate smaller numbers that // don't use up the full $n_bytes diff --git a/uint/tests/uint_tests.rs b/uint/tests/uint_tests.rs index f378423f1..9ef7f2e19 100644 --- a/uint/tests/uint_tests.rs +++ b/uint/tests/uint_tests.rs @@ -1157,7 +1157,7 @@ pub mod laws { macro_rules! uint_laws { ($mod_name:ident, $uint_ty:ident) => { mod $mod_name { - use qc::{TestResult, quickcheck}; + use quickcheck::{TestResult, quickcheck}; use super::$uint_ty; quickcheck! { From 8626c9fbbb67fd211ba1bd8fc38b6fde45fe77ef Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 1 Sep 2022 16:51:50 +0300 Subject: [PATCH 6/6] Fix %0 in arbitrary for $uint Co-authored-by: Andronik --- uint/src/uint.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 874e58c82..728957dcc 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1758,6 +1758,7 @@ macro_rules! impl_quickcheck_arbitrary_for_uint { $n_bytes / 5 }; + let range = $crate::core_::cmp::max(range, 1); let size: usize = usize::arbitrary(g) % range; let res: [u8; $n_bytes] = $crate::core_::array::from_fn(|i| {