refactor tests/rand.rs

This commit is contained in:
Josh Stone 2018-05-22 14:38:59 -07:00
parent 9fdec42818
commit 39104f5732

View File

@ -5,9 +5,9 @@ extern crate num_traits;
extern crate rand;
mod biguint {
use rand::thread_rng;
use num_bigint::{BigUint, RandBigInt};
use num_traits::{FromPrimitive, Zero};
use num_traits::Zero;
use rand::thread_rng;
#[test]
fn test_rand() {
@ -21,13 +21,14 @@ mod biguint {
let mut rng = thread_rng();
for _ in 0..10 {
assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_usize(236).unwrap(),
&FromPrimitive::from_usize(237).unwrap()),
FromPrimitive::from_usize(236).unwrap());
assert_eq!(
rng.gen_biguint_range(&BigUint::from(236u32), &BigUint::from(237u32)),
BigUint::from(236u32)
);
}
let l = FromPrimitive::from_usize(403469000 + 2352).unwrap();
let u = FromPrimitive::from_usize(403469000 + 3513).unwrap();
let l = BigUint::from(403469000u32 + 2352);
let u = BigUint::from(403469000u32 + 3513);
for _ in 0..1000 {
let n: BigUint = rng.gen_biguint_below(&u);
assert!(n < u);
@ -41,25 +42,24 @@ mod biguint {
#[test]
#[should_panic]
fn test_zero_rand_range() {
thread_rng().gen_biguint_range(&FromPrimitive::from_usize(54).unwrap(),
&FromPrimitive::from_usize(54).unwrap());
thread_rng().gen_biguint_range(&BigUint::from(54u32), &BigUint::from(54u32));
}
#[test]
#[should_panic]
fn test_negative_rand_range() {
let mut rng = thread_rng();
let l = FromPrimitive::from_usize(2352).unwrap();
let u = FromPrimitive::from_usize(3513).unwrap();
let l = BigUint::from(2352u32);
let u = BigUint::from(3513u32);
// Switching u and l should fail:
let _n: BigUint = rng.gen_biguint_range(&u, &l);
}
}
mod bigint {
use rand::thread_rng;
use num_bigint::{BigInt, RandBigInt};
use num_traits::{FromPrimitive, Zero};
use num_traits::Zero;
use rand::thread_rng;
#[test]
fn test_rand() {
@ -73,9 +73,10 @@ mod bigint {
let mut rng = thread_rng();
for _ in 0..10 {
assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_usize(236).unwrap(),
&FromPrimitive::from_usize(237).unwrap()),
FromPrimitive::from_usize(236).unwrap());
assert_eq!(
rng.gen_bigint_range(&BigInt::from(236), &BigInt::from(237)),
BigInt::from(236)
);
}
fn check(l: BigInt, u: BigInt) {
@ -86,8 +87,8 @@ mod bigint {
assert!(n < u);
}
}
let l: BigInt = FromPrimitive::from_usize(403469000 + 2352).unwrap();
let u: BigInt = FromPrimitive::from_usize(403469000 + 3513).unwrap();
let l: BigInt = BigInt::from(403469000 + 2352);
let u: BigInt = BigInt::from(403469000 + 3513);
check(l.clone(), u.clone());
check(-l.clone(), u.clone());
check(-u.clone(), -l.clone());
@ -96,16 +97,15 @@ mod bigint {
#[test]
#[should_panic]
fn test_zero_rand_range() {
thread_rng().gen_bigint_range(&FromPrimitive::from_isize(54).unwrap(),
&FromPrimitive::from_isize(54).unwrap());
thread_rng().gen_bigint_range(&BigInt::from(54), &BigInt::from(54));
}
#[test]
#[should_panic]
fn test_negative_rand_range() {
let mut rng = thread_rng();
let l = FromPrimitive::from_usize(2352).unwrap();
let u = FromPrimitive::from_usize(3513).unwrap();
let l = BigInt::from(2352);
let u = BigInt::from(3513);
// Switching u and l should fail:
let _n: BigInt = rng.gen_bigint_range(&u, &l);
}