Skip to content

Commit

Permalink
Merge #99
Browse files Browse the repository at this point in the history
99: add quickcheck arbitrary and some quickcheck tests r=cuviper a=maxbla

Aims to fix #5. Still needs plenty of work, like supporting signed `BigInts`

Co-authored-by: Max Blachman <blachmanmax@gmail.com>
  • Loading branch information
bors[bot] and maxbla committed Aug 11, 2019
2 parents 4f6df3c + edc5428 commit 3e34876
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 3 deletions.
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 @@ -42,12 +42,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

0 comments on commit 3e34876

Please sign in to comment.