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

Adds quickcheck::Arbitrary impls for BigInt, BigUint, Complex, and Ratio. #293

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ complex = ["num-complex"]
rational = ["num-rational"]
default = ["bigint", "complex", "rational", "rustc-serialize"]

quickcheck = [
"num-bigint/quickcheck",
"num-complex/quickcheck",
"num-rational/quickcheck"
]
serde = [
"num-bigint/serde",
"num-complex/serde",
Expand Down
4 changes: 4 additions & 0 deletions bigint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ version = "0.1.32"
path = "../traits"
version = "0.1.32"

[dependencies.quickcheck]
optional = true
version = "0.4.1"

[dependencies.rand]
optional = true
version = "0.3.14"
Expand Down
52 changes: 52 additions & 0 deletions bigint/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::cmp::Ordering::{self, Less, Greater, Equal};
use std::{i64, u64};
use std::ascii::AsciiExt;

#[cfg(feature = "quickcheck")]
use quickcheck;

#[cfg(feature = "serde")]
use serde;

Expand Down Expand Up @@ -751,6 +754,55 @@ impl From<BigUint> for BigInt {
}
}

#[cfg(feature = "quickcheck")]
impl quickcheck::Arbitrary for BigInt {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
use quickcheck::Arbitrary;

let sign = if g.gen() {
Sign::Plus
} else {
Sign::Minus
};

Self::new(sign, Arbitrary::arbitrary(g))
}

fn shrink(&self) -> Box<Iterator<Item = Self>> {
use quickcheck::empty_shrinker;

/// Based on the SignedShrinker for primitive types in quickcheck
/// itself.
struct Iter(BigInt, BigInt);
impl Iterator for Iter {
type Item = BigInt;

fn next(&mut self) -> Option<BigInt> {
if (self.0.clone() - &self.1).abs() < self.0.abs() {
let result = Some(self.0.clone() - &self.1);
// TODO This would benefit from in-place `/=`.
self.1 = self.1.clone() / BigInt::from_usize(2).unwrap();
result
} else {
None
}
}
}

if self.is_zero() {
empty_shrinker()
} else {
let two = BigInt::from_usize(2).unwrap();
let shrinker = Iter(self.clone(), self.clone() / two);
let mut items = vec![Self::zero()];
if shrinker.1.is_negative() {
items.push(shrinker.0.abs());
}
Box::new(items.into_iter().chain(shrinker))
}
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for BigInt {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
Expand Down
43 changes: 43 additions & 0 deletions bigint/src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::{f32, f64};
use std::{u8, u64};
use std::ascii::AsciiExt;

#[cfg(feature = "quickcheck")]
use quickcheck;

#[cfg(feature = "serde")]
use serde;

Expand Down Expand Up @@ -1071,6 +1074,46 @@ impl BigUint {
}
}

#[cfg(feature = "quickcheck")]
impl quickcheck::Arbitrary for BigUint {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
use quickcheck::Arbitrary;

Self::new(Arbitrary::arbitrary(g))
}

fn shrink(&self) -> Box<Iterator<Item = Self>> {
use quickcheck::empty_shrinker;
use std::iter::once;

/// Based on the UnsignedShrinker for primitive types in quickcheck
/// itself.
struct Iter(BigUint, BigUint);
impl Iterator for Iter {
type Item = BigUint;

fn next(&mut self) -> Option<BigUint> {
if (self.0.clone() - &self.1) < self.0 {
let result = Some(self.0.clone() - &self.1);
// TODO This would benefit from in-place `/=`.
self.1 = self.1.clone() / BigUint::from_usize(2).unwrap();
result
} else {
None
}
}
}

if self.is_zero() {
empty_shrinker()
} else {
let two = BigUint::from_usize(2).unwrap();
let shrinker = Iter(self.clone(), self.clone() / two);
Box::new(once(Self::zero()).chain(shrinker))
}
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for BigUint {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
Expand Down
2 changes: 2 additions & 0 deletions bigint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@

#[cfg(any(feature = "rand", test))]
extern crate rand;
#[cfg(feature = "quickcheck")]
extern crate quickcheck;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;
#[cfg(feature = "serde")]
Expand Down
4 changes: 4 additions & 0 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ cargo build --verbose --features "serde"

if [ "$TRAVIS_RUST_VERSION" = 1.8.0 ]; then exit; fi

# Build test for the quickcheck feature.
cargo build --verbose --features=quickcheck
cargo test --verbose --features=quickcheck

# num-derive should build on 1.15.0+
cargo build --verbose --manifest-path=derive/Cargo.toml

Expand Down
4 changes: 4 additions & 0 deletions complex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ optional = false
path = "../traits"
version = "0.1.32"

[dependencies.quickcheck]
optional = true
version = "0.4.1"

[dependencies.rustc-serialize]
optional = true
version = "0.3.19"
Expand Down
61 changes: 61 additions & 0 deletions complex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

extern crate num_traits as traits;

#[cfg(feature = "quickcheck")]
extern crate quickcheck;

#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;

Expand Down Expand Up @@ -740,6 +743,64 @@ impl<T> fmt::Binary for Complex<T> where
}
}

#[cfg(feature = "quickcheck")]
impl<T: quickcheck::Arbitrary + Num> quickcheck::Arbitrary for Complex<T> {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
Complex::new(T::arbitrary(g), T::arbitrary(g))
}

fn shrink(&self) -> Box<Iterator<Item = Self>> {
use quickcheck::Arbitrary;

struct Shrinker<T: Arbitrary + Num> {
mode: bool,
re: T,
im: T,
re_iter: Option<Box<Iterator<Item = T>>>,
im_iter: Option<Box<Iterator<Item = T>>>,
}

impl<T: Arbitrary + Num> Shrinker<T> {
fn new(re: T, im: T) -> Self {
Shrinker {
mode: true,
re: re.clone(),
im: im,
re_iter: Some(re.shrink()),
im_iter: None,
}
}
}

impl<T: Arbitrary + Num> Iterator for Shrinker<T> {
type Item = Complex<T>;

fn next(&mut self) -> Option<Complex<T>> {
match self.mode {
true => if let Some(re) = self.re_iter.as_mut().unwrap().next() {
Some(Complex::new(re, self.im.clone()))
} else {
self.mode = false;
self.re_iter = None;
self.im_iter = Some(self.im.clone().shrink());
self.next()
},
false => if let Some(im) = self.im_iter.as_mut().unwrap().next() {
Some(Complex::new(self.re.clone(), im))
} else if let Some(re) = self.re_iter.as_mut().unwrap().next() {
self.re = re;
self.next()
} else {
None
}
}
}
}

Box::new(Shrinker::new(self.re.clone(), self.im.clone()))
}
}

#[cfg(feature = "serde")]
impl<T> serde::Serialize for Complex<T>
where T: serde::Serialize
Expand Down
4 changes: 4 additions & 0 deletions rational/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ version = "0.1.32"
path = "../traits"
version = "0.1.32"

[dependencies.quickcheck]
optional = true
version = "0.4.1"

[dependencies.rustc-serialize]
optional = true
version = "0.3.19"
Expand Down
60 changes: 60 additions & 0 deletions rational/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
html_root_url = "https://rust-num.github.io/num/",
html_playground_url = "http://play.integer32.com/")]

#[cfg(feature = "quickcheck")]
extern crate quickcheck;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -608,6 +610,64 @@ impl<T> Into<(T, T)> for Ratio<T> {
}
}

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

fn shrink(&self) -> Box<Iterator<Item = Self>> {
use quickcheck::Arbitrary;

struct Shrinker<T: Arbitrary + Integer> {
mode: bool,
numer: T,
denom: T,
numer_iter: Option<Box<Iterator<Item = T>>>,
denom_iter: Option<Box<Iterator<Item = T>>>,
}

impl<T: Arbitrary + Integer> Shrinker<T> {
fn new(numer: T, denom: T) -> Self {
Shrinker {
mode: true,
numer: numer.clone(),
denom: denom,
numer_iter: Some(numer.shrink()),
denom_iter: None,
}
}
}

impl<T: Arbitrary + Integer> Iterator for Shrinker<T> {
type Item = Ratio<T>;

fn next(&mut self) -> Option<Ratio<T>> {
match self.mode {
true => if let Some(numer) = self.numer_iter.as_mut().unwrap().next() {
Some(Ratio::new(numer, self.denom.clone()))
} else {
self.mode = false;
self.numer_iter = None;
self.denom_iter = Some(self.denom.clone().shrink());
self.next()
},
false => if let Some(denom) = self.denom_iter.as_mut().unwrap().next() {
Some(Ratio::new(self.numer.clone(), denom))
} else if let Some(numer) = self.numer_iter.as_mut().unwrap().next() {
self.numer = numer;
self.next()
} else {
None
}
}
}
}

Box::new(Shrinker::new(self.numer.clone(), self.denom.clone()))
}
}

#[cfg(feature = "serde")]
impl<T> serde::Serialize for Ratio<T>
where T: serde::Serialize + Clone + Integer + PartialOrd
Expand Down