Skip to content

add quickcheck arbitrary and some quickcheck tests #99

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

Merged
merged 23 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
build = "build.rs"

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

[[bench]]
name = "bigint"
Expand Down Expand Up @@ -53,6 +53,16 @@ version = "1.0"
default-features = false
features = ["std"]

[dependencies.quickcheck]
optional = true
version = "0.8"
default-features = false

[dependencies.quickcheck_macros]
optional = true
version = "0.8"
default-features = false

[dev-dependencies.serde_test]
version = "1.0"

Expand Down
7 changes: 5 additions & 2 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ set -ex
echo Testing num-bigint on rustc ${TRAVIS_RUST_VERSION}

FEATURES="serde"
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.26.0|1.22.0)$ ]]; then
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.31.0|1.26.0|1.22.0)$ ]]; then
FEATURES="$FEATURES rand"
fi
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.26.0)$ ]]; then
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.31.0|1.26.0)$ ]]; then
FEATURES="$FEATURES i128"
fi
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.31.0)$ ]]; then
FEATURES="$FEATURES quickcheck quickcheck_macros"
fi

# num-bigint should build and test everywhere.
cargo build --verbose
Expand Down
18 changes: 18 additions & 0 deletions src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use biguint::{BigUint, IntDigits};
use IsizePromotion;
use 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 @@ -114,6 +117,21 @@ pub struct BigInt {
data: BigUint,
}

#[cfg(feature = "quickcheck")]
impl Arbitrary for BigInt {
fn arbitrary<G: 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))
}

fn shrink(&self) -> Box<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)))
}
}

/// Return the magnitude of a `BigInt`.
///
/// This is in a private module, pseudo pub(crate)
Expand Down
16 changes: 16 additions & 0 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ use UsizePromotion;

use ParseBigIntError;

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

/// A big unsigned integer type.
#[derive(Clone, Debug, Hash)]
pub struct BigUint {
data: Vec<BigDigit>,
}

#[cfg(feature = "quickcheck")]
impl Arbitrary for BigUint {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
// Use arbitrary from Vec
Self::new(Vec::<u32>::arbitrary(g))
}

fn shrink(&self) -> Box<Iterator<Item = Self>> {
// Use shrinker from Vec
Box::new(self.data.shrink().map(|x| BigUint::new(x)))
}
}

impl PartialEq for BigUint {
#[inline]
fn eq(&self, other: &BigUint) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ extern crate serde;

extern crate num_integer as integer;
extern crate num_traits as traits;
#[cfg(feature = "quickcheck")]
extern crate quickcheck;

use std::error::Error;
use std::fmt;
Expand Down
Loading