Skip to content

Commit

Permalink
Add scalar assign_op tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jan 27, 2020
1 parent 3b13d9a commit aae704e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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);
};
}

0 comments on commit aae704e

Please sign in to comment.