Skip to content

Commit

Permalink
Upgrade quickcheck in uint, fixed-hash (#674)
Browse files Browse the repository at this point in the history
* Upgrade uint:quickcheck

* Upgrade fixed-hash:quickcheck

* cargo fmt

* Generate a probability for len of an Arbitrary  in a more fancy way

* Get rid of quickcheck->qc renaming

* Fix %0 in arbitrary for $uint

Co-authored-by: Andronik <write@reusable.software>

Co-authored-by: Andronik <write@reusable.software>
  • Loading branch information
kpp and ordian committed Sep 2, 2022
1 parent f16979b commit 9642a0e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
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
4 changes: 1 addition & 3 deletions uint/Cargo.toml
Expand Up @@ -13,16 +13,14 @@ 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 }
quickcheck = { version = "1", optional = true }
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"]

[[example]]
name = "modular"
Expand Down
6 changes: 1 addition & 5 deletions uint/src/lib.rs
Expand Up @@ -23,11 +23,7 @@ pub use hex;

#[cfg(feature = "quickcheck")]
#[doc(hidden)]
pub use qc;

#[cfg(feature = "quickcheck")]
#[doc(hidden)]
pub use rand07;
pub use quickcheck;

#[cfg(feature = "arbitrary")]
#[doc(hidden)]
Expand Down
25 changes: 15 additions & 10 deletions uint/src/uint.rs
Expand Up @@ -1741,28 +1741,33 @@ 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: $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();
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
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 {
$n_bytes / 5
};

let size = g.gen_range(0, range);
g.fill_bytes(&mut res[..size]);
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| {
if i > size {
0
} else {
u8::arbitrary(g)
}
});

res.as_ref().into()
}
Expand Down
2 changes: 1 addition & 1 deletion uint/tests/uint_tests.rs
Expand Up @@ -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! {
Expand Down

0 comments on commit 9642a0e

Please sign in to comment.