expose feature gated quickcheck arbitrary impl for uints

This commit is contained in:
Maximilian Krüger 2018-02-16 09:34:03 +01:00
parent 08d458976c
commit 206529b7f9
3 changed files with 44 additions and 0 deletions

View File

@ -15,6 +15,7 @@ rustc_version = "0.2"
byteorder = { version = "1", default-features = false } byteorder = { version = "1", default-features = false }
heapsize = { version = "0.4.2", optional = true } heapsize = { version = "0.4.2", optional = true }
rustc-hex = { version = "1.0", optional = true } rustc-hex = { version = "1.0", optional = true }
quickcheck = { version = "0.6", optional = true }
[dev-dependencies] [dev-dependencies]
crunchy = "0.1.5" crunchy = "0.1.5"
@ -23,6 +24,7 @@ crunchy = "0.1.5"
std = ["rustc-hex", "byteorder/std"] std = ["rustc-hex", "byteorder/std"]
heapsizeof = ["heapsize"] heapsizeof = ["heapsize"]
use_asm = [] use_asm = []
impl_quickcheck_arbitrary = ["quickcheck"]
[[example]] [[example]]
name = "modular" name = "modular"

View File

@ -26,5 +26,9 @@ pub extern crate core;
#[doc(hidden)] #[doc(hidden)]
pub extern crate rustc_hex; pub extern crate rustc_hex;
#[cfg(feature="impl_quickcheck_arbitrary")]
#[doc(hidden)]
pub extern crate quickcheck;
mod uint; mod uint;
pub use uint::*; pub use uint::*;

View File

@ -1229,6 +1229,9 @@ macro_rules! construct_uint {
impl_std_for_uint!($name, $n_words); impl_std_for_uint!($name, $n_words);
impl_heapsize_for_uint!($name); impl_heapsize_for_uint!($name);
// `$n_words * 8` because macro expects bytes and
// uints use 64 bit (8 byte) words
impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8));
); );
} }
@ -1358,3 +1361,38 @@ macro_rules! impl_heapsize_for_uint {
macro_rules! impl_heapsize_for_uint { macro_rules! impl_heapsize_for_uint {
($name: ident) => {} ($name: ident) => {}
} }
#[cfg(feature="impl_quickcheck_arbitrary")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_quickcheck_arbitrary_for_uint {
($uint: ty, $n_bytes: tt) => {
impl $crate::quickcheck::Arbitrary for $uint {
fn arbitrary<G: $crate::quickcheck::Gen>(g: &mut G) -> Self {
let mut res = [0u8; $n_bytes];
let p = g.next_f64();
let range =
if p < 0.1 {
$n_bytes
} else if p < 0.2 {
$n_bytes / 2
} else {
$n_bytes / 5
};
let size = g.gen_range(0, range);
g.fill_bytes(&mut res[..size]);
res.as_ref().into()
}
}
}
}
#[cfg(not(feature="impl_quickcheck_arbitrary"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_quickcheck_arbitrary_for_uint {
($uint: ty, $n_bytes: tt) => {}
}