Add convenience macro for BigInt
timw4mail/rusty-numbers/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2020-04-07 13:46:51 -04:00
parent 322ccfe78a
commit bbfada5c0f
2 changed files with 22 additions and 2 deletions

View File

@ -15,6 +15,17 @@ pub struct BigInt {
sign: Sign, sign: Sign,
} }
/// Create a [BigInt](bigint/struct.BigInt.html) type with signed or unsigned number literals
#[macro_export]
macro_rules! big_int {
($w:literal) => {
$crate::bigint::BigInt::from($w)
};
(-$x:literal) => {
$crate::bigint::BigInt::from(-$x)
};
}
impl Default for BigInt { impl Default for BigInt {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -439,6 +450,17 @@ mod tests {
assert_eq!(int.inner[0], 45usize) assert_eq!(int.inner[0], 45usize)
} }
#[test]
fn test_macro() {
let a = big_int!(75);
let b = BigInt::from(75);
assert_eq!(a, b);
let a = big_int!(-75);
let b = BigInt::from(-75);
assert_eq!(a, b);
}
#[test] #[test]
fn test_trim_zeros() { fn test_trim_zeros() {
let mut lotsoftrailing = BigInt { let mut lotsoftrailing = BigInt {

View File

@ -160,7 +160,6 @@ macro_rules! impl_int {
self == 0 self == 0
} }
#[cfg_attr(tarpaulin, skip)]
fn max_value() -> $type { fn max_value() -> $type {
<$type>::max_value() <$type>::max_value()
} }
@ -253,7 +252,6 @@ macro_rules! impl_unsigned {
let (x, y) = (min(x, y), max(x, y)); let (x, y) = (min(x, y), max(x, y));
Self::stein_gcd((y - x) >> 1, x) Self::stein_gcd((y - x) >> 1, x)
} }
#[cfg_attr(tarpaulin, skip)]
_ => unreachable!(), _ => unreachable!(),
} }
} }