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

Quickcheck #59

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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 @@ -14,7 +14,7 @@ build = "build.rs"
exclude = ["/ci/*", "/.travis.yml", "/bors.toml"]

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

[dependencies]

Expand All @@ -36,6 +36,11 @@ optional = true
version = "1.0.0"
default-features = false

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

[features]
default = ["bigint-std", "std"]
i128 = ["num-integer/i128", "num-traits/i128"]
Expand Down
5 changes: 4 additions & 1 deletion ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ set -ex
echo Testing num-rational on rustc ${TRAVIS_RUST_VERSION}

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

# num-rational should build and test everywhere.
cargo build --verbose
Expand Down
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#[cfg(feature = "bigint")]
extern crate num_bigint as bigint;
#[cfg(feature = "quickcheck")]
extern crate quickcheck;
#[cfg(feature = "serde")]
extern crate serde;

Expand Down Expand Up @@ -47,6 +49,9 @@ use traits::{
Pow, Signed, Zero,
};

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

/// Represents the ratio between two numbers.
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
Expand Down Expand Up @@ -254,6 +259,28 @@ impl<T: Clone + Integer> Ratio<T> {
}
}

#[cfg(feature = "quickcheck")]
impl<T: Arbitrary + Integer> Arbitrary for Ratio<T> {
fn arbitrary<G: Gen>(g: &mut G) -> Ratio<T> {
let mut denom = T::arbitrary(g);
while denom.is_zero() {
denom = T::arbitrary(g);
}
Ratio::new(T::arbitrary(g), denom)
}

#[cfg(feature = "std")]
fn shrink(&self) -> std::boxed::Box<Iterator<Item = Ratio<T>>> {
let numer = self.numer.shrink().next().unwrap_or(T::zero());
let denom = if self.denom.is_one() {
self.denom.clone()
} else {
self.denom.shrink().next().unwrap_or(T::one())
};
std::boxed::Box::new(std::iter::once(Ratio::new(numer, denom)))
}
}

impl<T: Clone + Integer + Pow<u32, Output = T>> Ratio<T> {
/// Raises the `Ratio` to the power of an exponent.
#[inline]
Expand Down