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

Fix promotion of negative isize #133

Merged
merged 2 commits into from
Jan 27, 2020
Merged
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
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,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<i32> for $res, $method, i8, i16);
promote_scalars_assign!(impl $imp<UsizePromotion> for $res, $method, isize);
promote_scalars_assign!(impl $imp<IsizePromotion> for $res, $method, isize);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/bigint_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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() {
Expand All @@ -40,6 +41,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() {
Expand All @@ -65,6 +67,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() {
Expand Down Expand Up @@ -99,10 +102,14 @@ fn test_scalar_div_rem() {
let (a, ans_q, ans_r) = (a.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) {
Expand Down
7 changes: 7 additions & 0 deletions tests/biguint_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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() {
Expand All @@ -30,6 +31,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() {
Expand All @@ -48,6 +50,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() {
Expand All @@ -73,6 +76,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() {
Expand Down Expand Up @@ -101,6 +106,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);
}
}
}
27 changes: 27 additions & 0 deletions tests/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,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);
};
}