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

Upgrade quickcheck in uint, fixed-hash #674

Merged
merged 6 commits into from Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fixed-hash/Cargo.toml
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions fixed-hash/src/hash.rs
Expand Up @@ -629,9 +629,8 @@ 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: $crate::quickcheck::Gen>(g: &mut G) -> Self {
let mut res = [0u8; $crate::core_::mem::size_of::<Self>()];
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)
}
}
Expand Down
5 changes: 2 additions & 3 deletions uint/Cargo.toml
Expand Up @@ -13,16 +13,15 @@ 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 }
kpp marked this conversation as resolved.
Show resolved Hide resolved
hex = { version = "0.4", default-features = false }
static_assertions = "1.0.0"
arbitrary = { version = "1.0", optional = true }

[features]
default = ["std"]
std = ["byteorder/std", "crunchy/std", "hex/std"]
quickcheck = ["qc", "rand07"]
quickcheck = ["qc"]
kpp marked this conversation as resolved.
Show resolved Hide resolved

[[example]]
name = "modular"
Expand Down
4 changes: 0 additions & 4 deletions uint/src/lib.rs
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions uint/src/uint.rs
Expand Up @@ -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: $crate::qc::Gen>(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 =
Expand All @@ -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;
kpp marked this conversation as resolved.
Show resolved Hide resolved

let res: [u8; $n_bytes] = $crate::core_::array::from_fn(|i| {
if i > range {
kpp marked this conversation as resolved.
Show resolved Hide resolved
0
} else {
u8::arbitrary(g)
}
});

res.as_ref().into()
}
Expand Down