From 51d2378e0594e12f23ee640967fdb5aaded34c6d Mon Sep 17 00:00:00 2001 From: Max Blachman Date: Sat, 28 Sep 2019 14:17:07 -0700 Subject: [PATCH 1/6] add quickcheck support fixes #2. Requires underlying type to support quickcheck --- Cargo.toml | 7 ++++++- ci/test_full.sh | 5 ++++- src/lib.rs | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2cc1c40..7d6ebeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] @@ -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"] diff --git a/ci/test_full.sh b/ci/test_full.sh index 29ff03d..d94997d 100755 --- a/ci/test_full.sh +++ b/ci/test_full.sh @@ -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|1.31.0)$ ]]; then + FEATURES="$FEATURES quickcheck" +fi # num-rational should build and test everywhere. cargo build --verbose diff --git a/src/lib.rs b/src/lib.rs index 87380d2..46fa89a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,8 @@ extern crate num_bigint as bigint; #[cfg(feature = "serde")] extern crate serde; +#[cfg(feature = "quickcheck")] +extern crate quickcheck; extern crate num_integer as integer; extern crate num_traits as traits; @@ -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)] @@ -254,6 +259,17 @@ impl Ratio { } } +#[cfg(feature = "quickcheck")] +impl Arbitrary> { + fn arbitrary(g: &mut G) -> Ratio { + let denom = T::arbitrary(g: &mut G); + while denom.is_zero() { + denom = T::arbitrary(g: &mut G); + } + Ratio::new(T::arbitrary(), denom); + } +} + impl> Ratio { /// Raises the `Ratio` to the power of an exponent. #[inline] From 878a0a582c968bfb98e1674ba7d216fe29fec970 Mon Sep 17 00:00:00 2001 From: Max Blachman Date: Sun, 29 Sep 2019 08:01:25 -0700 Subject: [PATCH 2/6] implement shrinking --- src/lib.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 46fa89a..9974eb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ extern crate num_bigint as bigint; #[cfg(feature = "serde")] extern crate serde; #[cfg(feature = "quickcheck")] +#[cfg(feature = "std")] extern crate quickcheck; extern crate num_integer as integer; @@ -50,8 +51,13 @@ use traits::{ }; #[cfg(feature = "quickcheck")] +#[cfg(feature = "std")] use quickcheck::{Arbitrary, Gen}; +#[cfg(feature = "quickcheck")] +#[cfg(feature = "std")] +use std::boxed::Box; + /// Represents the ratio between two numbers. #[derive(Copy, Clone, Debug)] #[allow(missing_docs)] @@ -260,13 +266,24 @@ impl Ratio { } #[cfg(feature = "quickcheck")] -impl Arbitrary> { - fn arbitrary(g: &mut G) -> Ratio { - let denom = T::arbitrary(g: &mut G); +#[cfg(feature = "std")] +impl Arbitrary for Ratio { + fn arbitrary(g: &mut G) -> Ratio { + let mut denom = T::arbitrary(g); while denom.is_zero() { - denom = T::arbitrary(g: &mut G); + denom = T::arbitrary(g); } - Ratio::new(T::arbitrary(), denom); + Ratio::new(T::arbitrary(g), denom) + } + + fn shrink(&self) -> Box>> { + 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()) + }; + Box::new(std::iter::once(Ratio::new(numer, denom))) } } From 43036e4580dfc9b065d845bc17fb845005942097 Mon Sep 17 00:00:00 2001 From: Max Blachman Date: Tue, 1 Oct 2019 17:33:22 -0700 Subject: [PATCH 3/6] sipmlify cfg() conditional compilation --- src/lib.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9974eb6..5f870e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,6 @@ extern crate num_bigint as bigint; #[cfg(feature = "serde")] extern crate serde; #[cfg(feature = "quickcheck")] -#[cfg(feature = "std")] extern crate quickcheck; extern crate num_integer as integer; @@ -51,13 +50,8 @@ use traits::{ }; #[cfg(feature = "quickcheck")] -#[cfg(feature = "std")] use quickcheck::{Arbitrary, Gen}; -#[cfg(feature = "quickcheck")] -#[cfg(feature = "std")] -use std::boxed::Box; - /// Represents the ratio between two numbers. #[derive(Copy, Clone, Debug)] #[allow(missing_docs)] @@ -266,7 +260,6 @@ impl Ratio { } #[cfg(feature = "quickcheck")] -#[cfg(feature = "std")] impl Arbitrary for Ratio { fn arbitrary(g: &mut G) -> Ratio { let mut denom = T::arbitrary(g); @@ -276,14 +269,15 @@ impl Arbitrary for Ratio { Ratio::new(T::arbitrary(g), denom) } - fn shrink(&self) -> Box>> { + #[cfg(feature = "std")] + fn shrink(&self) -> std::boxed::Box>> { 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()) }; - Box::new(std::iter::once(Ratio::new(numer, denom))) + std::boxed::Box::new(std::iter::once(Ratio::new(numer, denom))) } } From a97e29eb4b119181a7f40468ec76ddbdaf73a839 Mon Sep 17 00:00:00 2001 From: Max Blachman Date: Tue, 1 Oct 2019 18:45:04 -0700 Subject: [PATCH 4/6] quickcheck requires rust 1.34 or greater --- ci/test_full.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/test_full.sh b/ci/test_full.sh index d94997d..ea33d2c 100755 --- a/ci/test_full.sh +++ b/ci/test_full.sh @@ -8,7 +8,7 @@ FEATURES="std bigint-std serde" 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|1.31.0)$ ]]; then +if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable)$ ]]; then FEATURES="$FEATURES quickcheck" fi From bd61ae06b42a650bc4fcc83b2558df8c2373c33f Mon Sep 17 00:00:00 2001 From: Max Blachman Date: Tue, 1 Oct 2019 18:45:24 -0700 Subject: [PATCH 5/6] remove dyn keyword (fixes rust 1.15 build) --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5f870e1..7a94b32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -270,7 +270,7 @@ impl Arbitrary for Ratio { } #[cfg(feature = "std")] - fn shrink(&self) -> std::boxed::Box>> { + fn shrink(&self) -> std::boxed::Box>> { let numer = self.numer.shrink().next().unwrap_or(T::zero()); let denom = if self.denom.is_one() { self.denom.clone() From 63c233f82eafc9094b3528227194e0d2c5e4ad91 Mon Sep 17 00:00:00 2001 From: Max Blachman Date: Tue, 1 Oct 2019 18:56:38 -0700 Subject: [PATCH 6/6] run rustfmt --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7a94b32..8ddc41b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,10 +19,10 @@ #[cfg(feature = "bigint")] extern crate num_bigint as bigint; -#[cfg(feature = "serde")] -extern crate serde; #[cfg(feature = "quickcheck")] extern crate quickcheck; +#[cfg(feature = "serde")] +extern crate serde; extern crate num_integer as integer; extern crate num_traits as traits; @@ -260,7 +260,7 @@ impl Ratio { } #[cfg(feature = "quickcheck")] -impl Arbitrary for Ratio { +impl Arbitrary for Ratio { fn arbitrary(g: &mut G) -> Ratio { let mut denom = T::arbitrary(g); while denom.is_zero() {