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

add quickcheck arbitrary and some quickcheck tests #99

Merged
merged 23 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 6 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,11 @@ version = "1.0"
default-features = false
features = ["std"]

[dependencies.quickcheck]
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
cuviper marked this conversation as resolved.
Show resolved Hide resolved
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"
fi

# num-bigint should build and test everywhere.
cargo build --verbose
Expand Down
20 changes: 20 additions & 0 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,32 @@ 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 {
//Use arbitrary for Vec
maxbla marked this conversation as resolved.
Show resolved Hide resolved
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let num = Vec::<u32>::arbitrary(g);
Self::from_slice(&num)
maxbla marked this conversation as resolved.
Show resolved Hide resolved
}
//Use the shrinker for Vec
maxbla marked this conversation as resolved.
Show resolved Hide resolved
fn shrink(&self) -> Box<Iterator<Item = Self>> {
Box::new(
self.data
.shrink()
.map(|x| BigUint::from_slice(x.as_slice())),
maxbla marked this conversation as resolved.
Show resolved Hide resolved
)
}
}

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
154 changes: 154 additions & 0 deletions tests/quickcheck.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
extern crate num_bigint;
extern crate num_integer;
extern crate num_traits;

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

#[cfg(test)]
#[cfg(feature = "quickcheck")]
maxbla marked this conversation as resolved.
Show resolved Hide resolved
mod quickchecks {
use num_bigint::BigUint;
use num_traits::{Num, One, Pow, ToPrimitive};
use quickcheck::TestResult;

quickcheck! {
maxbla marked this conversation as resolved.
Show resolved Hide resolved
fn quickcheck_add_primitive(a: u128, b: u128) -> TestResult {
let a = a;
let b = b;
let a_big = BigUint::from(a);
let b_big = BigUint::from(b);
//maximum value of u64 means no overflow
match a.checked_add(b) {
None => TestResult::discard(),
Some(sum) => TestResult::from_bool(sum == (a_big + b_big).to_u128().unwrap())
}
}
}

quickcheck! {
fn quickcheck_add_commutative(a: BigUint, b: BigUint) -> bool {
a.clone() + b.clone() == b + a
}
}

quickcheck! {
fn quickcheck_add_zero(a: BigUint) -> bool {
a.clone() == a + BigUint::from(0_u32)
}
}

quickcheck! {
fn quickcheck_add_associative(a: BigUint, b: BigUint, c: BigUint) -> bool {
(a.clone() + b.clone()) + c.clone() == a + (b + c)
}
}

quickcheck! {
fn quickcheck_mul_primitive(a: u64, b: u64) -> bool {
let a = a as u128;
let b = b as u128;
let a_big = BigUint::from(a);
let b_big = BigUint::from(b);
//maximum value of u64 means no overflow
a * b == (a_big * b_big).to_u128().unwrap()
}
}

quickcheck! {
fn quickcheck_mul_commutative(a: BigUint, b: BigUint) -> bool {
a.clone() * b.clone() == b * a
}
}

quickcheck! {
fn quickcheck_mul_associative(a: BigUint, b: BigUint, c: BigUint) -> bool {
(a.clone() * b.clone()) * c.clone() == a * (b * c)
}
}

quickcheck! {
fn quickcheck_distributive(a: BigUint, b: BigUint, c: BigUint) -> bool {
a.clone() * (b.clone() + c.clone()) == a.clone() * b + a * c
}
}

quickcheck! {
///Tests that exactly one of a<b a>b a=b is true
fn quickcheck_ge_le_eq_mut_exclusive(a: BigUint, b: BigUint) -> bool {
let gt_lt_eq = vec![a > b, a < b, a == b];
gt_lt_eq
.iter()
.fold(0, |acc, e| if *e { acc + 1 } else { acc })
== 1
}
}

quickcheck! {
/// Tests correctness of subtraction assuming addition is correct
fn quickcheck_sub(a: BigUint, b: BigUint) -> bool {
if b < a {
a.clone() - b.clone() + b == a
} else {
b.clone() - a.clone() + a == b
}
}
}

quickcheck! {
fn quickcheck_pow_zero(a: BigUint) -> bool {
a.pow(0_u32) == BigUint::one()
}
}

quickcheck! {
fn quickcheck_pow_one(a: BigUint) -> bool {
a.pow(1_u32) == a
}
}

quickcheck! {
fn quickcheck_sqrt(a: BigUint) -> bool {
(a.clone() * a.clone()).sqrt() == a
}
}

quickcheck! {
fn quickcheck_cbrt(a: BigUint) -> bool {
(a.clone() * a.clone() * a.clone()).cbrt() == a
}
}

quickcheck! {
fn quickcheck_conversion(a:BigUint) -> bool {
let mut success = true;
for radix in 2..=36 {
let string = a.to_str_radix(radix);
success &= a == BigUint::from_str_radix(&string, radix).unwrap()
maxbla marked this conversation as resolved.
Show resolved Hide resolved
}
success
}
}

//this test takes too long (no surprise)
//quickcheck automaticly stops if a test takes more than 60s
// fn quickcheck_pow_and_root(a: BigUint, n: u8) -> TestResult {
// match n {
// 0 => TestResult::discard(),
// n => TestResult::from_bool(a.clone().pow(n).nth_root(n as u32) == a),
// }
// }

quickcheck! {
fn quickcheck_modpow(a:BigUint, exp:u8, modulus:u64) -> TestResult {
if modulus == 0 {
return TestResult::discard()
}
let expected = a.clone().pow(exp) % modulus;
let result = a.modpow(&BigUint::from(exp), &BigUint::from(modulus));
TestResult::from_bool(expected == result)
}
}
}