Skip to content

Commit 7c40fde

Browse files
bors[bot]cuviper
andauthored
Merge #194 #195
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>
3 parents 60637c5 + 96cf255 + 8f30944 commit 7c40fde

File tree

12 files changed

+61
-36
lines changed

12 files changed

+61
-36
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ jobs:
1414
matrix:
1515
rust: [
1616
1.31.0, # 2018!
17-
1.34.0, # quickcheck, has_try_from
17+
1.34.0, # has_try_from
1818
1.36.0, # alloc, rand
19+
1.40.0, # arbitrary
20+
1.46.0, # quickcheck
1921
stable,
2022
beta,
2123
nightly

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ default-features = false
6262

6363
[dependencies.quickcheck]
6464
optional = true
65-
version = "0.9"
65+
version = "1"
6666
default-features = false
6767

6868
[dependencies.arbitrary]
6969
optional = true
70-
version = "0.4"
70+
version = "1"
7171
default-features = false
7272

7373
[build-dependencies]

bors.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ status = [
22
"Test (1.31.0)",
33
"Test (1.34.0)",
44
"Test (1.36.0)",
5+
"Test (1.40.0)",
6+
"Test (1.46.0)",
57
"Test (stable)",
68
"Test (beta)",
79
"Test (nightly)",

ci/big_quickcheck/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ edition = "2018"
77
[dependencies]
88
num-integer = "0.1.42"
99
num-traits = "0.2.11"
10-
quickcheck_macros = "0.9"
10+
quickcheck_macros = "1"
1111

1212
[dependencies.quickcheck]
13-
version = "0.9"
13+
version = "1"
1414
default-features = false
1515

1616
[dependencies.num-bigint]

ci/big_quickcheck/src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use num_bigint::{BigInt, BigUint};
1010
use num_integer::Integer;
1111
use num_traits::{Num, One, Signed, Zero};
12-
use quickcheck::{QuickCheck, StdThreadGen, TestResult};
12+
use quickcheck::{Gen, QuickCheck, TestResult};
1313
use quickcheck_macros::quickcheck;
1414

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

4343
#[test]
4444
fn quickcheck_arith_primitive() {
45-
let gen = StdThreadGen::new(usize::max_value());
46-
let mut qc = QuickCheck::with_gen(gen);
45+
let gen = Gen::new(usize::max_value());
46+
let mut qc = QuickCheck::new().gen(gen);
4747

4848
fn test_unsigned_add_primitive(a: usize, b: usize) -> TestResult {
4949
let actual = BigUint::from(a) + BigUint::from(b);
@@ -79,11 +79,11 @@ fn quickcheck_arith_primitive() {
7979
}
8080
}
8181

82-
fn test_signed_sub_primitive(a: i128, b: i128) -> bool {
83-
if b < a {
84-
BigInt::from(a - b) == BigInt::from(a) - BigInt::from(b)
85-
} else {
86-
BigInt::from(b - a) == BigInt::from(b) - BigInt::from(a)
82+
fn test_signed_sub_primitive(a: i128, b: i128) -> TestResult {
83+
let actual = BigInt::from(a) - BigInt::from(b);
84+
match a.checked_sub(b) {
85+
None => TestResult::discard(),
86+
Some(expected) => TestResult::from_bool(BigInt::from(expected) == actual),
8787
}
8888
}
8989

@@ -96,7 +96,7 @@ fn quickcheck_arith_primitive() {
9696
}
9797

9898
fn test_signed_div_primitive(a: i128, b: i128) -> TestResult {
99-
if b == 0 {
99+
if b == 0 || (a == i128::MIN && b == -1) {
100100
TestResult::discard()
101101
} else {
102102
TestResult::from_bool(BigInt::from(a / b) == BigInt::from(a) / BigInt::from(b))
@@ -108,7 +108,7 @@ fn quickcheck_arith_primitive() {
108108
qc.quickcheck(test_unsigned_mul_primitive as fn(u64, u64) -> bool);
109109
qc.quickcheck(test_signed_mul_primitive as fn(i64, i64) -> bool);
110110
qc.quickcheck(test_unsigned_sub_primitive as fn(u128, u128) -> bool);
111-
qc.quickcheck(test_signed_sub_primitive as fn(i128, i128) -> bool);
111+
qc.quickcheck(test_signed_sub_primitive as fn(i128, i128) -> TestResult);
112112
qc.quickcheck(test_unsigned_div_primitive as fn(u128, u128) -> TestResult);
113113
qc.quickcheck(test_signed_div_primitive as fn(i128, i128) -> TestResult);
114114
}
@@ -280,8 +280,8 @@ fn quickcheck_signed_conversion(a: BigInt, radix: u8) -> TestResult {
280280

281281
#[test]
282282
fn quicktest_shift() {
283-
let gen = StdThreadGen::new(usize::max_value());
284-
let mut qc = QuickCheck::with_gen(gen);
283+
let gen = Gen::new(usize::max_value());
284+
let mut qc = QuickCheck::new().gen(gen);
285285

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

318318
#[test]
319319
fn quickcheck_modpow() {
320-
let gen = StdThreadGen::new(usize::max_value());
321-
let mut qc = QuickCheck::with_gen(gen);
320+
let gen = Gen::new(usize::max_value());
321+
let mut qc = QuickCheck::new().gen(gen);
322322

323323
fn simple_modpow(base: &BigInt, exponent: &BigInt, modulus: &BigInt) -> BigInt {
324324
assert!(!exponent.is_negative());

ci/rustup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
set -ex
66

77
ci=$(dirname $0)
8-
for version in 1.31.0 1.34.0 1.36.0 stable beta nightly; do
8+
for version in 1.31.0 1.34.0 1.36.0 1.40.0 1.46.0 stable beta nightly; do
99
rustup run "$version" "$ci/test_full.sh"
1010
done

ci/test_full.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ if ! check_version $MSRV ; then
2828
fi
2929

3030
STD_FEATURES=(serde)
31-
check_version 1.34 && STD_FEATURES+=(quickcheck)
3231
check_version 1.36 && STD_FEATURES+=(rand)
3332
check_version 1.36 && NO_STD_FEATURES=(serde rand)
3433
check_version 1.40 && STD_FEATURES+=(arbitrary)
34+
check_version 1.46 && STD_FEATURES+=(quickcheck)
3535
echo "Testing supported features: ${STD_FEATURES[*]}"
3636
if [ -n "${NO_STD_FEATURES[*]}" ]; then
3737
echo " no_std supported features: ${NO_STD_FEATURES[*]}"

src/bigint.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ impl Neg for Sign {
5959
}
6060

6161
/// A big signed integer type.
62-
#[derive(Debug)]
6362
pub struct BigInt {
6463
sign: Sign,
6564
data: BigUint,
@@ -137,6 +136,12 @@ impl Default for BigInt {
137136
}
138137
}
139138

139+
impl fmt::Debug for BigInt {
140+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141+
fmt::Display::fmt(self, f)
142+
}
143+
}
144+
140145
impl fmt::Display for BigInt {
141146
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142147
f.pad_integral(!self.is_negative(), "", &self.data.to_str_radix(10))

src/bigint/arbitrary.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::{BigInt, Sign};
22

3+
#[cfg(feature = "quickcheck")]
34
use crate::std_alloc::Box;
45
use crate::BigUint;
56

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

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

29-
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
30-
let sign = self.sign();
31-
let unsigned_shrink = self.data.shrink();
32-
Box::new(unsigned_shrink.map(move |x| BigInt::from_biguint(sign, x)))
30+
fn arbitrary_take_rest(mut u: arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
31+
let positive = bool::arbitrary(&mut u)?;
32+
let sign = if positive { Sign::Plus } else { Sign::Minus };
33+
Ok(Self::from_biguint(sign, BigUint::arbitrary_take_rest(u)?))
34+
}
35+
36+
fn size_hint(depth: usize) -> (usize, Option<usize>) {
37+
arbitrary::size_hint::and(bool::size_hint(depth), BigUint::size_hint(depth))
3338
}
3439
}

src/biguint.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub(crate) use self::convert::to_str_radix_reversed;
3535
pub use self::iter::{U32Digits, U64Digits};
3636

3737
/// A big unsigned integer type.
38-
#[derive(Debug)]
3938
pub struct BigUint {
4039
data: Vec<BigDigit>,
4140
}
@@ -106,6 +105,12 @@ impl Default for BigUint {
106105
}
107106
}
108107

108+
impl fmt::Debug for BigUint {
109+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110+
fmt::Display::fmt(self, f)
111+
}
112+
}
113+
109114
impl fmt::Display for BigUint {
110115
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111116
f.pad_integral(true, "", &self.to_str_radix(10))

0 commit comments

Comments
 (0)