| 1 |
use num_bigint::BigUint; |
| 2 |
use num_traits::{One, ToPrimitive, Zero}; |
| 3 |
|
| 4 |
use std::panic::catch_unwind; |
| 5 |
|
| 6 |
mod consts; |
| 7 |
use crate::consts::*; |
| 8 |
|
| 9 |
#[macro_use] |
| 10 |
mod macros; |
| 11 |
|
| 12 |
#[test] |
| 13 |
fn test_scalar_add() { |
| 14 |
fn check(x: &BigUint, y: &BigUint, z: &BigUint) { |
| 15 |
let (x, y, z) = (x.clone(), y.clone(), z.clone()); |
| 16 |
assert_unsigned_scalar_op!(x + y == z); |
| 17 |
assert_unsigned_scalar_assign_op!(x += y == z); |
| 18 |
} |
| 19 |
|
| 20 |
for elm in SUM_TRIPLES.iter() { |
| 21 |
let (a_vec, b_vec, c_vec) = *elm; |
| 22 |
let a = BigUint::from_slice(a_vec); |
| 23 |
let b = BigUint::from_slice(b_vec); |
| 24 |
let c = BigUint::from_slice(c_vec); |
| 25 |
|
| 26 |
check(&a, &b, &c); |
| 27 |
check(&b, &a, &c); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn test_scalar_sub() { |
| 33 |
fn check(x: &BigUint, y: &BigUint, z: &BigUint) { |
| 34 |
let (x, y, z) = (x.clone(), y.clone(), z.clone()); |
| 35 |
assert_unsigned_scalar_op!(x - y == z); |
| 36 |
assert_unsigned_scalar_assign_op!(x -= y == z); |
| 37 |
} |
| 38 |
|
| 39 |
for elm in SUM_TRIPLES.iter() { |
| 40 |
let (a_vec, b_vec, c_vec) = *elm; |
| 41 |
let a = BigUint::from_slice(a_vec); |
| 42 |
let b = BigUint::from_slice(b_vec); |
| 43 |
let c = BigUint::from_slice(c_vec); |
| 44 |
|
| 45 |
check(&c, &a, &b); |
| 46 |
check(&c, &b, &a); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_scalar_mul() { |
| 52 |
fn check(x: &BigUint, y: &BigUint, z: &BigUint) { |
| 53 |
let (x, y, z) = (x.clone(), y.clone(), z.clone()); |
| 54 |
assert_unsigned_scalar_op!(x * y == z); |
| 55 |
assert_unsigned_scalar_assign_op!(x *= y == z); |
| 56 |
} |
| 57 |
|
| 58 |
for elm in MUL_TRIPLES.iter() { |
| 59 |
let (a_vec, b_vec, c_vec) = *elm; |
| 60 |
let a = BigUint::from_slice(a_vec); |
| 61 |
let b = BigUint::from_slice(b_vec); |
| 62 |
let c = BigUint::from_slice(c_vec); |
| 63 |
|
| 64 |
check(&a, &b, &c); |
| 65 |
check(&b, &a, &c); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn test_scalar_rem_noncommutative() { |
| 71 |
assert_eq!(5u8 % BigUint::from(7u8), BigUint::from(5u8)); |
| 72 |
assert_eq!(BigUint::from(5u8) % 7u8, BigUint::from(5u8)); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn test_scalar_div_rem() { |
| 77 |
fn check(x: &BigUint, y: &BigUint, z: &BigUint, r: &BigUint) { |
| 78 |
let (x, y, z, r) = (x.clone(), y.clone(), z.clone(), r.clone()); |
| 79 |
assert_unsigned_scalar_op!(x / y == z); |
| 80 |
assert_unsigned_scalar_op!(x % y == r); |
| 81 |
assert_unsigned_scalar_assign_op!(x /= y == z); |
| 82 |
assert_unsigned_scalar_assign_op!(x %= y == r); |
| 83 |
} |
| 84 |
|
| 85 |
for elm in MUL_TRIPLES.iter() { |
| 86 |
let (a_vec, b_vec, c_vec) = *elm; |
| 87 |
let a = BigUint::from_slice(a_vec); |
| 88 |
let b = BigUint::from_slice(b_vec); |
| 89 |
let c = BigUint::from_slice(c_vec); |
| 90 |
|
| 91 |
if !a.is_zero() { |
| 92 |
check(&c, &a, &b, &Zero::zero()); |
| 93 |
} |
| 94 |
|
| 95 |
if !b.is_zero() { |
| 96 |
check(&c, &b, &a, &Zero::zero()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
for elm in DIV_REM_QUADRUPLES.iter() { |
| 101 |
let (a_vec, b_vec, c_vec, d_vec) = *elm; |
| 102 |
let a = BigUint::from_slice(a_vec); |
| 103 |
let b = BigUint::from_slice(b_vec); |
| 104 |
let c = BigUint::from_slice(c_vec); |
| 105 |
let d = BigUint::from_slice(d_vec); |
| 106 |
|
| 107 |
if !b.is_zero() { |
| 108 |
check(&a, &b, &c, &d); |
| 109 |
assert_unsigned_scalar_op!(a / b == c); |
| 110 |
assert_unsigned_scalar_op!(a % b == d); |
| 111 |
assert_unsigned_scalar_assign_op!(a /= b == c); |
| 112 |
assert_unsigned_scalar_assign_op!(a %= b == d); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn test_scalar_div_rem_zero() { |
| 119 |
catch_unwind(|| BigUint::zero() / 0u32).unwrap_err(); |
| 120 |
catch_unwind(|| BigUint::zero() % 0u32).unwrap_err(); |
| 121 |
catch_unwind(|| BigUint::one() / 0u32).unwrap_err(); |
| 122 |
catch_unwind(|| BigUint::one() % 0u32).unwrap_err(); |
| 123 |
} |