From bfa8d319b1f489fee7c50a392f863b22a86987a3 Mon Sep 17 00:00:00 2001 From: Hactar <6060305+HactarCE@users.noreply.github.com> Date: Fri, 24 Jan 2020 20:35:21 -0500 Subject: [PATCH 1/5] Fix promotion of negative isize (cherry picked from commit 3b13d9a20e649c928282f88531af8a07f03ef092) --- src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 0ba6e48c..735cfcb9 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -344,7 +344,7 @@ macro_rules! promote_signed_scalars { macro_rules! promote_signed_scalars_assign { (impl $imp:ident for $res:ty, $method:ident) => { promote_scalars_assign!(impl $imp for $res, $method, i8, i16); - promote_scalars_assign!(impl $imp for $res, $method, isize); + promote_scalars_assign!(impl $imp for $res, $method, isize); } } From c6bf8ab2adf3166b34a12c666daa5bf3a528dd2d Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 9 Aug 2019 18:06:14 -0700 Subject: [PATCH 2/5] Fix recursion in Rem for BigInt (cherry picked from commit f7747e51da5252f7e8b3d475fd452b50afb48e80) --- tests/bigint_scalar.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/bigint_scalar.rs b/tests/bigint_scalar.rs index ae9a6d7a..8b199726 100644 --- a/tests/bigint_scalar.rs +++ b/tests/bigint_scalar.rs @@ -98,15 +98,14 @@ fn test_scalar_div_rem() { assert!(q == *ans_q); assert!(r == *ans_r); + let b = BigInt::from(b); let (a, b, ans_q, ans_r) = (a.clone(), b.clone(), ans_q.clone(), ans_r.clone()); - assert_op!(a / b == ans_q); - assert_op!(a % b == ans_r); + assert_signed_scalar_op!(a / b == ans_q); + assert_signed_scalar_op!(a % b == ans_r); - if b <= i32::max_value() as u32 { - let nb = -(b as i32); - assert_op!(a / nb == -ans_q.clone()); - assert_op!(a % nb == ans_r); - } + let nb = -b; + assert_signed_scalar_op!(a / nb == -ans_q.clone()); + assert_signed_scalar_op!(a % nb == ans_r); } fn check(a: &BigInt, b: u32, q: &BigInt, r: &BigInt) { From b752a307884f75a837e460bf41b6ec005e3cbea9 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 27 Jan 2020 10:48:35 -0800 Subject: [PATCH 3/5] Add scalar assign_op tests (cherry picked from commit aae704e845c5293f8d6457378a123455cf3bba09) --- tests/bigint_scalar.rs | 7 +++++++ tests/biguint_scalar.rs | 7 +++++++ tests/macros/mod.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/tests/bigint_scalar.rs b/tests/bigint_scalar.rs index 8b199726..6a1601ea 100644 --- a/tests/bigint_scalar.rs +++ b/tests/bigint_scalar.rs @@ -18,6 +18,7 @@ fn test_scalar_add() { fn check(x: &BigInt, y: &BigInt, z: &BigInt) { let (x, y, z) = (x.clone(), y.clone(), z.clone()); assert_signed_scalar_op!(x + y == z); + assert_signed_scalar_assign_op!(x += y == z); } for elm in SUM_TRIPLES.iter() { @@ -43,6 +44,7 @@ fn test_scalar_sub() { fn check(x: &BigInt, y: &BigInt, z: &BigInt) { let (x, y, z) = (x.clone(), y.clone(), z.clone()); assert_signed_scalar_op!(x - y == z); + assert_signed_scalar_assign_op!(x -= y == z); } for elm in SUM_TRIPLES.iter() { @@ -68,6 +70,7 @@ fn test_scalar_mul() { fn check(x: &BigInt, y: &BigInt, z: &BigInt) { let (x, y, z) = (x.clone(), y.clone(), z.clone()); assert_signed_scalar_op!(x * y == z); + assert_signed_scalar_assign_op!(x *= y == z); } for elm in MUL_TRIPLES.iter() { @@ -102,10 +105,14 @@ fn test_scalar_div_rem() { let (a, b, ans_q, ans_r) = (a.clone(), b.clone(), ans_q.clone(), ans_r.clone()); assert_signed_scalar_op!(a / b == ans_q); assert_signed_scalar_op!(a % b == ans_r); + assert_signed_scalar_assign_op!(a /= b == ans_q); + assert_signed_scalar_assign_op!(a %= b == ans_r); let nb = -b; assert_signed_scalar_op!(a / nb == -ans_q.clone()); assert_signed_scalar_op!(a % nb == ans_r); + assert_signed_scalar_assign_op!(a /= nb == -ans_q.clone()); + assert_signed_scalar_assign_op!(a %= nb == ans_r); } fn check(a: &BigInt, b: u32, q: &BigInt, r: &BigInt) { diff --git a/tests/biguint_scalar.rs b/tests/biguint_scalar.rs index fb8fbf03..45227735 100644 --- a/tests/biguint_scalar.rs +++ b/tests/biguint_scalar.rs @@ -15,6 +15,7 @@ fn test_scalar_add() { fn check(x: &BigUint, y: &BigUint, z: &BigUint) { let (x, y, z) = (x.clone(), y.clone(), z.clone()); assert_unsigned_scalar_op!(x + y == z); + assert_unsigned_scalar_assign_op!(x += y == z); } for elm in SUM_TRIPLES.iter() { @@ -33,6 +34,7 @@ fn test_scalar_sub() { fn check(x: &BigUint, y: &BigUint, z: &BigUint) { let (x, y, z) = (x.clone(), y.clone(), z.clone()); assert_unsigned_scalar_op!(x - y == z); + assert_unsigned_scalar_assign_op!(x -= y == z); } for elm in SUM_TRIPLES.iter() { @@ -51,6 +53,7 @@ fn test_scalar_mul() { fn check(x: &BigUint, y: &BigUint, z: &BigUint) { let (x, y, z) = (x.clone(), y.clone(), z.clone()); assert_unsigned_scalar_op!(x * y == z); + assert_unsigned_scalar_assign_op!(x *= y == z); } for elm in MUL_TRIPLES.iter() { @@ -76,6 +79,8 @@ fn test_scalar_div_rem() { let (x, y, z, r) = (x.clone(), y.clone(), z.clone(), r.clone()); assert_unsigned_scalar_op!(x / y == z); assert_unsigned_scalar_op!(x % y == r); + assert_unsigned_scalar_assign_op!(x /= y == z); + assert_unsigned_scalar_assign_op!(x %= y == r); } for elm in MUL_TRIPLES.iter() { @@ -104,6 +109,8 @@ fn test_scalar_div_rem() { check(&a, &b, &c, &d); assert_unsigned_scalar_op!(a / b == c); assert_unsigned_scalar_op!(a % b == d); + assert_unsigned_scalar_assign_op!(a /= b == c); + assert_unsigned_scalar_assign_op!(a %= b == d); } } } diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs index d848b29b..da210455 100644 --- a/tests/macros/mod.rs +++ b/tests/macros/mod.rs @@ -68,3 +68,30 @@ macro_rules! assert_signed_scalar_op { $left $op $right == $expected); }; } + +/// Assert that an op works for scalar right +macro_rules! assert_scalar_assign_op { + (($($to:ident),*) $left:ident $op:tt $right:ident == $expected:expr) => { + $( + if let Some(right) = $right.$to() { + let mut left = $left.clone(); + assert_eq!({ left $op right; left}, $expected); + } + )* + }; +} + +macro_rules! assert_unsigned_scalar_assign_op { + ($left:ident $op:tt $right:ident == $expected:expr) => { + assert_scalar_assign_op!((to_u8, to_u16, to_u32, to_u64, to_usize, to_u128) + $left $op $right == $expected); + }; +} + +macro_rules! assert_signed_scalar_assign_op { + ($left:ident $op:tt $right:ident == $expected:expr) => { + assert_scalar_assign_op!((to_u8, to_u16, to_u32, to_u64, to_usize, to_u128, + to_i8, to_i16, to_i32, to_i64, to_isize, to_i128) + $left $op $right == $expected); + }; +} From 2316a36698064156846251733db3f3219032970c Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 27 Jan 2020 11:09:03 -0800 Subject: [PATCH 4/5] Release 0.2.6 --- Cargo.toml | 2 +- RELEASES.md | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 78b6446a..7687a682 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ categories = [ "algorithms", "data-structures", "science" ] license = "MIT/Apache-2.0" name = "num-bigint" repository = "https://github.com/rust-num/num-bigint" -version = "0.2.5" +version = "0.2.6" readme = "README.md" build = "build.rs" diff --git a/RELEASES.md b/RELEASES.md index 51be5ece..358534ec 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,3 +1,11 @@ +# Release 0.2.6 (2020-01-27) + +- [Fix the promotion of negative `isize` in `BigInt` assign-ops][133]. + +**Contributors**: @cuviper, @HactarCE + +[133]: https://github.com/rust-num/num-bigint/pull/133 + # Release 0.2.5 (2020-01-09) - [Updated the `autocfg` build dependency to 1.0][126]. From 6d58f6d6aba1137ecdb22ded944d16d8aa228ef0 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 27 Jan 2020 11:41:28 -0800 Subject: [PATCH 5/5] Fix scalar_assign_op tests for not(has_i128) --- tests/macros/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs index da210455..946534a3 100644 --- a/tests/macros/mod.rs +++ b/tests/macros/mod.rs @@ -81,6 +81,15 @@ macro_rules! assert_scalar_assign_op { }; } +#[cfg(not(has_i128))] +macro_rules! assert_unsigned_scalar_assign_op { + ($left:ident $op:tt $right:ident == $expected:expr) => { + assert_scalar_assign_op!((to_u8, to_u16, to_u32, to_u64, to_usize) + $left $op $right == $expected); + }; +} + +#[cfg(has_i128)] macro_rules! assert_unsigned_scalar_assign_op { ($left:ident $op:tt $right:ident == $expected:expr) => { assert_scalar_assign_op!((to_u8, to_u16, to_u32, to_u64, to_usize, to_u128) @@ -88,6 +97,16 @@ macro_rules! assert_unsigned_scalar_assign_op { }; } +#[cfg(not(has_i128))] +macro_rules! assert_signed_scalar_assign_op { + ($left:ident $op:tt $right:ident == $expected:expr) => { + assert_scalar_assign_op!((to_u8, to_u16, to_u32, to_u64, to_usize, + to_i8, to_i16, to_i32, to_i64, to_isize) + $left $op $right == $expected); + }; +} + +#[cfg(has_i128)] macro_rules! assert_signed_scalar_assign_op { ($left:ident $op:tt $right:ident == $expected:expr) => { assert_scalar_assign_op!((to_u8, to_u16, to_u32, to_u64, to_usize, to_u128,