Skip to content

Commit 9c7c995

Browse files
committed
bigint: add tests for From implementations
1 parent 8e1c6a3 commit 9c7c995

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

src/bigint.rs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,7 @@ mod biguint_tests {
24122412
use std::i64;
24132413
use std::iter::repeat;
24142414
use std::str::FromStr;
2415-
use std::u64;
2415+
use std::{u8, u16, u32, u64, usize};
24162416

24172417
use rand::thread_rng;
24182418
use {Num, Zero, One, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
@@ -2907,6 +2907,24 @@ mod biguint_tests {
29072907
BigInt::from_biguint(Plus, BigUint::new(vec!(1,2,3))));
29082908
}
29092909

2910+
#[test]
2911+
fn test_convert_from_uint() {
2912+
macro_rules! check {
2913+
($ty:ident, $max:expr) => {
2914+
assert_eq!(BigUint::from($ty::zero()), BigUint::zero());
2915+
assert_eq!(BigUint::from($ty::one()), BigUint::one());
2916+
assert_eq!(BigUint::from($ty::MAX - $ty::one()), $max - BigUint::one());
2917+
assert_eq!(BigUint::from($ty::MAX), $max);
2918+
}
2919+
}
2920+
2921+
check!(u8, BigUint::from_slice(&[u8::MAX as BigDigit]));
2922+
check!(u16, BigUint::from_slice(&[u16::MAX as BigDigit]));
2923+
check!(u32, BigUint::from_slice(&[u32::MAX]));
2924+
check!(u64, BigUint::from_slice(&[u32::MAX, u32::MAX]));
2925+
check!(usize, BigUint::from(usize::MAX as u64));
2926+
}
2927+
29102928
const SUM_TRIPLES: &'static [(&'static [BigDigit],
29112929
&'static [BigDigit],
29122930
&'static [BigDigit])] = &[
@@ -3411,9 +3429,9 @@ mod bigint_tests {
34113429
use super::Sign::{Minus, NoSign, Plus};
34123430

34133431
use std::cmp::Ordering::{Less, Equal, Greater};
3414-
use std::i64;
3432+
use std::{i8, i16, i32, i64, isize};
34153433
use std::iter::repeat;
3416-
use std::u64;
3434+
use std::{u8, u16, u32, u64, usize};
34173435
use std::ops::{Neg};
34183436

34193437
use rand::thread_rng;
@@ -3637,6 +3655,57 @@ mod bigint_tests {
36373655
assert_eq!(negative.to_biguint(), None);
36383656
}
36393657

3658+
#[test]
3659+
fn test_convert_from_uint() {
3660+
macro_rules! check {
3661+
($ty:ident, $max:expr) => {
3662+
assert_eq!(BigInt::from($ty::zero()), BigInt::zero());
3663+
assert_eq!(BigInt::from($ty::one()), BigInt::one());
3664+
assert_eq!(BigInt::from($ty::MAX - $ty::one()), $max - BigInt::one());
3665+
assert_eq!(BigInt::from($ty::MAX), $max);
3666+
}
3667+
}
3668+
3669+
check!(u8, BigInt::from_slice(Plus, &[u8::MAX as BigDigit]));
3670+
check!(u16, BigInt::from_slice(Plus, &[u16::MAX as BigDigit]));
3671+
check!(u32, BigInt::from_slice(Plus, &[u32::MAX as BigDigit]));
3672+
check!(u64, BigInt::from_slice(Plus, &[u32::MAX as BigDigit, u32::MAX as BigDigit]));
3673+
check!(usize, BigInt::from(usize::MAX as u64));
3674+
}
3675+
3676+
#[test]
3677+
fn test_convert_from_int() {
3678+
macro_rules! check {
3679+
($ty:ident, $min:expr, $max:expr) => {
3680+
assert_eq!(BigInt::from($ty::MIN), $min);
3681+
assert_eq!(BigInt::from($ty::MIN + $ty::one()), $min + BigInt::one());
3682+
assert_eq!(BigInt::from(-$ty::one()), -BigInt::one());
3683+
assert_eq!(BigInt::from($ty::zero()), BigInt::zero());
3684+
assert_eq!(BigInt::from($ty::one()), BigInt::one());
3685+
assert_eq!(BigInt::from($ty::MAX - $ty::one()), $max - BigInt::one());
3686+
assert_eq!(BigInt::from($ty::MAX), $max);
3687+
}
3688+
}
3689+
3690+
check!(i8, BigInt::from_slice(Minus, &[1 << 7]),
3691+
BigInt::from_slice(Plus, &[i8::MAX as BigDigit]));
3692+
check!(i16, BigInt::from_slice(Minus, &[1 << 15]),
3693+
BigInt::from_slice(Plus, &[i16::MAX as BigDigit]));
3694+
check!(i32, BigInt::from_slice(Minus, &[1 << 31]),
3695+
BigInt::from_slice(Plus, &[i32::MAX as BigDigit]));
3696+
check!(i64, BigInt::from_slice(Minus, &[0, 1 << 31]),
3697+
BigInt::from_slice(Plus, &[u32::MAX as BigDigit, i32::MAX as BigDigit]));
3698+
check!(isize, BigInt::from(isize::MIN as i64),
3699+
BigInt::from(isize::MAX as i64));
3700+
}
3701+
3702+
#[test]
3703+
fn test_convert_from_biguint() {
3704+
assert_eq!(BigInt::from(BigUint::zero()), BigInt::zero());
3705+
assert_eq!(BigInt::from(BigUint::one()), BigInt::one());
3706+
assert_eq!(BigInt::from(BigUint::from_slice(&[1, 2, 3])), BigInt::from_slice(Plus, &[1, 2, 3]));
3707+
}
3708+
36403709
const N1: BigDigit = -1i32 as BigDigit;
36413710
const N2: BigDigit = -2i32 as BigDigit;
36423711

0 commit comments

Comments
 (0)