Skip to content

Commit

Permalink
Merge #194 #195
Browse files Browse the repository at this point in the history
194: Update to arbitrary-1 and quickcheck-1 r=cuviper a=cuviper



195: Change Debug to match Display r=cuviper a=cuviper

The derived `Debug` showed the raw digit data, which is sometimes useful
if you're debugging `num-bigint` itself, but annoying in many other
contexts. For something like a simple `dbg!` call in a program, you
probably just want to see the numeric value.

This is not _necessarily_ a breaking change, but we'll treat it so.

Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed Mar 5, 2021
3 parents 60637c5 + 96cf255 + 8f30944 commit 7c40fde
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 36 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Expand Up @@ -14,8 +14,10 @@ jobs:
matrix:
rust: [
1.31.0, # 2018!
1.34.0, # quickcheck, has_try_from
1.34.0, # has_try_from
1.36.0, # alloc, rand
1.40.0, # arbitrary
1.46.0, # quickcheck
stable,
beta,
nightly
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -62,12 +62,12 @@ default-features = false

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

[dependencies.arbitrary]
optional = true
version = "0.4"
version = "1"
default-features = false

[build-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions bors.toml
Expand Up @@ -2,6 +2,8 @@ status = [
"Test (1.31.0)",
"Test (1.34.0)",
"Test (1.36.0)",
"Test (1.40.0)",
"Test (1.46.0)",
"Test (stable)",
"Test (beta)",
"Test (nightly)",
Expand Down
4 changes: 2 additions & 2 deletions ci/big_quickcheck/Cargo.toml
Expand Up @@ -7,10 +7,10 @@ edition = "2018"
[dependencies]
num-integer = "0.1.42"
num-traits = "0.2.11"
quickcheck_macros = "0.9"
quickcheck_macros = "1"

[dependencies.quickcheck]
version = "0.9"
version = "1"
default-features = false

[dependencies.num-bigint]
Expand Down
28 changes: 14 additions & 14 deletions ci/big_quickcheck/src/lib.rs
Expand Up @@ -9,7 +9,7 @@
use num_bigint::{BigInt, BigUint};
use num_integer::Integer;
use num_traits::{Num, One, Signed, Zero};
use quickcheck::{QuickCheck, StdThreadGen, TestResult};
use quickcheck::{Gen, QuickCheck, TestResult};
use quickcheck_macros::quickcheck;

#[quickcheck]
Expand Down Expand Up @@ -42,8 +42,8 @@ fn quickcheck_signed_eq_symmetric(a: BigInt, b: BigInt) -> bool {

#[test]
fn quickcheck_arith_primitive() {
let gen = StdThreadGen::new(usize::max_value());
let mut qc = QuickCheck::with_gen(gen);
let gen = Gen::new(usize::max_value());
let mut qc = QuickCheck::new().gen(gen);

fn test_unsigned_add_primitive(a: usize, b: usize) -> TestResult {
let actual = BigUint::from(a) + BigUint::from(b);
Expand Down Expand Up @@ -79,11 +79,11 @@ fn quickcheck_arith_primitive() {
}
}

fn test_signed_sub_primitive(a: i128, b: i128) -> bool {
if b < a {
BigInt::from(a - b) == BigInt::from(a) - BigInt::from(b)
} else {
BigInt::from(b - a) == BigInt::from(b) - BigInt::from(a)
fn test_signed_sub_primitive(a: i128, b: i128) -> TestResult {
let actual = BigInt::from(a) - BigInt::from(b);
match a.checked_sub(b) {
None => TestResult::discard(),
Some(expected) => TestResult::from_bool(BigInt::from(expected) == actual),
}
}

Expand All @@ -96,7 +96,7 @@ fn quickcheck_arith_primitive() {
}

fn test_signed_div_primitive(a: i128, b: i128) -> TestResult {
if b == 0 {
if b == 0 || (a == i128::MIN && b == -1) {
TestResult::discard()
} else {
TestResult::from_bool(BigInt::from(a / b) == BigInt::from(a) / BigInt::from(b))
Expand All @@ -108,7 +108,7 @@ fn quickcheck_arith_primitive() {
qc.quickcheck(test_unsigned_mul_primitive as fn(u64, u64) -> bool);
qc.quickcheck(test_signed_mul_primitive as fn(i64, i64) -> bool);
qc.quickcheck(test_unsigned_sub_primitive as fn(u128, u128) -> bool);
qc.quickcheck(test_signed_sub_primitive as fn(i128, i128) -> bool);
qc.quickcheck(test_signed_sub_primitive as fn(i128, i128) -> TestResult);
qc.quickcheck(test_unsigned_div_primitive as fn(u128, u128) -> TestResult);
qc.quickcheck(test_signed_div_primitive as fn(i128, i128) -> TestResult);
}
Expand Down Expand Up @@ -280,8 +280,8 @@ fn quickcheck_signed_conversion(a: BigInt, radix: u8) -> TestResult {

#[test]
fn quicktest_shift() {
let gen = StdThreadGen::new(usize::max_value());
let mut qc = QuickCheck::with_gen(gen);
let gen = Gen::new(usize::max_value());
let mut qc = QuickCheck::new().gen(gen);

fn test_shr_unsigned(a: u64, shift: u8) -> TestResult {
let shift = (shift % 64) as usize; //shift at most 64 bits
Expand Down Expand Up @@ -317,8 +317,8 @@ fn quicktest_shift() {

#[test]
fn quickcheck_modpow() {
let gen = StdThreadGen::new(usize::max_value());
let mut qc = QuickCheck::with_gen(gen);
let gen = Gen::new(usize::max_value());
let mut qc = QuickCheck::new().gen(gen);

fn simple_modpow(base: &BigInt, exponent: &BigInt, modulus: &BigInt) -> BigInt {
assert!(!exponent.is_negative());
Expand Down
2 changes: 1 addition & 1 deletion ci/rustup.sh
Expand Up @@ -5,6 +5,6 @@
set -ex

ci=$(dirname $0)
for version in 1.31.0 1.34.0 1.36.0 stable beta nightly; do
for version in 1.31.0 1.34.0 1.36.0 1.40.0 1.46.0 stable beta nightly; do
rustup run "$version" "$ci/test_full.sh"
done
2 changes: 1 addition & 1 deletion ci/test_full.sh
Expand Up @@ -28,10 +28,10 @@ if ! check_version $MSRV ; then
fi

STD_FEATURES=(serde)
check_version 1.34 && STD_FEATURES+=(quickcheck)
check_version 1.36 && STD_FEATURES+=(rand)
check_version 1.36 && NO_STD_FEATURES=(serde rand)
check_version 1.40 && STD_FEATURES+=(arbitrary)
check_version 1.46 && STD_FEATURES+=(quickcheck)
echo "Testing supported features: ${STD_FEATURES[*]}"
if [ -n "${NO_STD_FEATURES[*]}" ]; then
echo " no_std supported features: ${NO_STD_FEATURES[*]}"
Expand Down
7 changes: 6 additions & 1 deletion src/bigint.rs
Expand Up @@ -59,7 +59,6 @@ impl Neg for Sign {
}

/// A big signed integer type.
#[derive(Debug)]
pub struct BigInt {
sign: Sign,
data: BigUint,
Expand Down Expand Up @@ -137,6 +136,12 @@ impl Default for BigInt {
}
}

impl fmt::Debug for BigInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for BigInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad_integral(!self.is_negative(), "", &self.data.to_str_radix(10))
Expand Down
17 changes: 11 additions & 6 deletions src/bigint/arbitrary.rs
@@ -1,11 +1,12 @@
use super::{BigInt, Sign};

#[cfg(feature = "quickcheck")]
use crate::std_alloc::Box;
use crate::BigUint;

#[cfg(feature = "quickcheck")]
impl quickcheck::Arbitrary for BigInt {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let positive = bool::arbitrary(g);
let sign = if positive { Sign::Plus } else { Sign::Minus };
Self::from_biguint(sign, BigUint::arbitrary(g))
Expand All @@ -19,16 +20,20 @@ impl quickcheck::Arbitrary for BigInt {
}

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for BigInt {
impl arbitrary::Arbitrary<'_> for BigInt {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let positive = bool::arbitrary(u)?;
let sign = if positive { Sign::Plus } else { Sign::Minus };
Ok(Self::from_biguint(sign, BigUint::arbitrary(u)?))
}

fn shrink(&self) -> Box<dyn 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)))
fn arbitrary_take_rest(mut u: arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let positive = bool::arbitrary(&mut u)?;
let sign = if positive { Sign::Plus } else { Sign::Minus };
Ok(Self::from_biguint(sign, BigUint::arbitrary_take_rest(u)?))
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(bool::size_hint(depth), BigUint::size_hint(depth))
}
}
7 changes: 6 additions & 1 deletion src/biguint.rs
Expand Up @@ -35,7 +35,6 @@ pub(crate) use self::convert::to_str_radix_reversed;
pub use self::iter::{U32Digits, U64Digits};

/// A big unsigned integer type.
#[derive(Debug)]
pub struct BigUint {
data: Vec<BigDigit>,
}
Expand Down Expand Up @@ -106,6 +105,12 @@ impl Default for BigUint {
}
}

impl fmt::Debug for BigUint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for BigUint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad_integral(true, "", &self.to_str_radix(10))
Expand Down
16 changes: 11 additions & 5 deletions src/biguint/arbitrary.rs
@@ -1,11 +1,13 @@
use super::{biguint_from_vec, BigUint};

use crate::big_digit::BigDigit;
use crate::std_alloc::{Box, Vec};
#[cfg(feature = "quickcheck")]
use crate::std_alloc::Box;
use crate::std_alloc::Vec;

#[cfg(feature = "quickcheck")]
impl quickcheck::Arbitrary for BigUint {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
// Use arbitrary from Vec
biguint_from_vec(Vec::<BigDigit>::arbitrary(g))
}
Expand All @@ -17,12 +19,16 @@ impl quickcheck::Arbitrary for BigUint {
}

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary for BigUint {
impl arbitrary::Arbitrary<'_> for BigUint {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(biguint_from_vec(Vec::<BigDigit>::arbitrary(u)?))
}

fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new(self.data.shrink().map(biguint_from_vec))
fn arbitrary_take_rest(u: arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(biguint_from_vec(Vec::<BigDigit>::arbitrary_take_rest(u)?))
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
Vec::<BigDigit>::size_hint(depth)
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -94,7 +94,7 @@ extern crate std;
#[cfg(feature = "std")]
mod std_alloc {
pub(crate) use std::borrow::Cow;
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
#[cfg(any(feature = "quickcheck"))]
pub(crate) use std::boxed::Box;
pub(crate) use std::string::String;
pub(crate) use std::vec::Vec;
Expand All @@ -107,7 +107,7 @@ extern crate alloc;
#[cfg(not(feature = "std"))]
mod std_alloc {
pub(crate) use alloc::borrow::Cow;
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
#[cfg(any(feature = "quickcheck"))]
pub(crate) use alloc::boxed::Box;
pub(crate) use alloc::string::String;
pub(crate) use alloc::vec::Vec;
Expand Down

0 comments on commit 7c40fde

Please sign in to comment.