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

Support arbitrary::Arbitrary #166

Merged
merged 3 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude = ["/bors.toml", "/ci/*", "/.github/*"]
edition = "2018"

[package.metadata.docs.rs]
features = ["std", "serde", "rand", "quickcheck"]
features = ["std", "serde", "rand", "quickcheck", "arbitrary"]

[[bench]]
name = "bigint"
Expand Down Expand Up @@ -60,6 +60,11 @@ optional = true
version = "0.9"
default-features = false

[dependencies.arbitrary]
optional = true
version = "0.4"
default-features = false

[features]
default = ["std"]
std = ["num-integer/std", "num-traits/std"]
Expand Down
1 change: 1 addition & 0 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ STD_FEATURES=(serde)
check_version 1.32 && STD_FEATURES+=(rand)
check_version 1.34 && STD_FEATURES+=(quickcheck)
check_version 1.36 && NO_STD_FEATURES=(serde rand)
check_version 1.40 && STD_FEATURES+=(arbitrary)
echo "Testing supported features: ${STD_FEATURES[*]}"
if [ -n "${NO_STD_FEATURES[*]}" ]; then
echo " no_std supported features: ${NO_STD_FEATURES[*]}"
Expand Down
29 changes: 23 additions & 6 deletions src/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// `Add`/`Sub` ops may flip from `BigInt` to its `BigUint` magnitude
#![allow(clippy::suspicious_arithmetic_impl)]

#[cfg(feature = "quickcheck")]
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
use crate::std_alloc::Box;
use crate::std_alloc::{String, Vec};
use core::cmp::Ordering::{self, Equal, Greater, Less};
Expand Down Expand Up @@ -39,9 +39,6 @@ use crate::TryFromBigIntError;
use crate::IsizePromotion;
use crate::UsizePromotion;

#[cfg(feature = "quickcheck")]
use quickcheck::{Arbitrary, Gen};

/// A Sign is a `BigInt`'s composing element.
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, Hash)]
pub enum Sign {
Expand Down Expand Up @@ -141,8 +138,8 @@ impl Clone for BigInt {
}

#[cfg(feature = "quickcheck")]
impl Arbitrary for BigInt {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
impl quickcheck::Arbitrary for BigInt {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
let positive = bool::arbitrary(g);
let sign = if positive { Sign::Plus } else { Sign::Minus };
Self::from_biguint(sign, BigUint::arbitrary(g))
Expand All @@ -155,6 +152,26 @@ impl Arbitrary for BigInt {
}
}

#[cfg(feature = "arbitrary")]
mod abitrary_impl {
use super::*;
use arbitrary::{Arbitrary, Result, Unstructured};

impl Arbitrary for BigInt {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
let positive = bool::arbitrary(u)?;
let sign = if positive { Sign::Plus } else { Sign::Minus };
Ok(Self::from_biguint(sign, BigUint::arbitrary(u)?))
}

fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
let sign = self.sign();
let unsigned_shrink = self.data.shrink();
Box::new(unsigned_shrink.map(move |x| BigInt::from_biguint(sign, x)))
}
}
}

impl hash::Hash for BigInt {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
Expand Down
25 changes: 19 additions & 6 deletions src/biguint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "quickcheck")]
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
use crate::std_alloc::Box;
use crate::std_alloc::{Cow, String, Vec};
use core::cmp;
Expand Down Expand Up @@ -45,9 +45,6 @@ use crate::ParseBigIntError;
#[cfg(has_try_from)]
use crate::TryFromBigIntError;

#[cfg(feature = "quickcheck")]
use quickcheck::{Arbitrary, Gen};

/// A big unsigned integer type.
#[derive(Debug)]
pub struct BigUint {
Expand All @@ -71,8 +68,8 @@ impl Clone for BigUint {
}

#[cfg(feature = "quickcheck")]
impl Arbitrary for BigUint {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
impl quickcheck::Arbitrary for BigUint {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
// Use arbitrary from Vec
biguint_from_vec(Vec::<BigDigit>::arbitrary(g))
}
Expand All @@ -83,6 +80,22 @@ impl Arbitrary for BigUint {
}
}

#[cfg(feature = "arbitrary")]
mod abitrary_impl {
use super::*;
use arbitrary::{Arbitrary, Result, Unstructured};

impl Arbitrary for BigUint {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
Ok(biguint_from_vec(Vec::<BigDigit>::arbitrary(u)?))
}

fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new(self.data.shrink().map(biguint_from_vec))
}
}
}

impl hash::Hash for BigUint {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extern crate std;
#[cfg(feature = "std")]
mod std_alloc {
pub(crate) use std::borrow::Cow;
#[cfg(feature = "quickcheck")]
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
pub(crate) use std::boxed::Box;
pub(crate) use std::string::String;
pub(crate) use std::vec::Vec;
Expand All @@ -107,7 +107,7 @@ extern crate alloc;
#[cfg(not(feature = "std"))]
mod std_alloc {
pub(crate) use alloc::borrow::Cow;
#[cfg(feature = "quickcheck")]
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
pub(crate) use alloc::boxed::Box;
pub(crate) use alloc::string::String;
pub(crate) use alloc::vec::Vec;
Expand Down