Added some tests for Wrapping<T>'s impls

This commit is contained in:
Yoan Lecoq 2017-04-30 10:17:06 +02:00
parent b90cfaaa7f
commit 807584688c
5 changed files with 88 additions and 0 deletions

View File

@ -465,3 +465,29 @@ fn to_primitive_float() {
assert_eq!((f64::NEG_INFINITY).to_f32(), Some(f32::NEG_INFINITY));
assert!((f64::NAN).to_f32().map_or(false, |f| f.is_nan()));
}
macro_rules! test_wrapping_to_primitive {
($($t:ty)+) => {
$(
let i: $t = 0;
let w = Wrapping(i);
assert_eq!(i.to_u8(), w.to_u8());
assert_eq!(i.to_u16(), w.to_u16());
assert_eq!(i.to_u32(), w.to_u32());
assert_eq!(i.to_u64(), w.to_u64());
assert_eq!(i.to_usize(), w.to_usize());
assert_eq!(i.to_i8(), w.to_i8());
assert_eq!(i.to_i16(), w.to_i16());
assert_eq!(i.to_i32(), w.to_i32());
assert_eq!(i.to_i64(), w.to_i64());
assert_eq!(i.to_isize(), w.to_isize());
assert_eq!(i.to_f32(), w.to_f32());
assert_eq!(i.to_f64(), w.to_f64());
)+
};
}
#[test]
fn wrapping_to_primitive() {
test_wrapping_to_primitive!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
}

View File

@ -118,3 +118,21 @@ impl<T: One> One for Wrapping<T> where Wrapping<T>: Mul<Output=Wrapping<T>> {
/// Returns the multiplicative identity, `1`.
#[inline(always)] pub fn one<T: One>() -> T { One::one() }
macro_rules! test_wrapping_identities {
($($t:ty)+) => {
$(
assert_eq!(zero::<$t>(), zero::<Wrapping<$t>>().0);
assert_eq!(one::<$t>(), one::<Wrapping<$t>>().0);
assert_eq!((0 as $t).is_zero(), Wrapping(0 as $t).is_zero());
assert_eq!((1 as $t).is_zero(), Wrapping(1 as $t).is_zero());
)+
};
}
#[test]
fn wrapping_identities() {
test_wrapping_identities!(isize i8 i16 i32 i64 usize u8 u16 u32 u64);
}

View File

@ -294,3 +294,19 @@ fn from_str_radix_unwrap() {
let f: f32 = Num::from_str_radix("0.0", 10).unwrap();
assert_eq!(f, 0.0);
}
macro_rules! test_wrapping_from_str_radix {
($($t:ty)+) => {
$(
for &(s, r) in &[("42", 10), ("42", 2), ("-13.0", 10), ("foo", 10)] {
let w = Wrapping::<$t>::from_str_radix(s, r).map(|w| w.0);
assert_eq!(w, <$t as Num>::from_str_radix(s, r));
}
)+
};
}
#[test]
fn wrapping_from_str_radix() {
test_wrapping_from_str_radix!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
}

View File

@ -103,4 +103,7 @@ fn test_wrapping_traits() {
assert_eq!(wrapping_add(255, 1), 0u8);
assert_eq!(wrapping_sub(0, 1), 255u8);
assert_eq!(wrapping_mul(255, 2), 254u8);
assert_eq!(wrapping_add(255, 1), (Wrapping(255u8) + Wrapping(1u8)).0);
assert_eq!(wrapping_sub(0, 1), (Wrapping(0u8) - Wrapping(1u8)).0);
assert_eq!(wrapping_mul(255, 2), (Wrapping(255u8) * Wrapping(2u8)).0);
}

View File

@ -186,3 +186,28 @@ macro_rules! empty_trait_impl {
empty_trait_impl!(Unsigned for usize u8 u16 u32 u64);
impl<T: Unsigned> Unsigned for Wrapping<T> where Wrapping<T>: Num {}
macro_rules! test_signed_wrapping {
($($t:ty)+) => {
$(
let range = -1 as $t .. 2 as $t;
for i in range.clone() {
let w = Wrapping(i);
assert_eq!(signum(i), signum(w).0);
assert_eq!(abs(i), abs(w).0);
for j in range.clone() {
assert_eq!(abs_sub(i, j), abs_sub(w, Wrapping(j)).0);
}
assert_eq!(i.is_positive(), w.is_positive());
assert_eq!(i.is_negative(), w.is_negative());
}
)+
};
}
#[test]
fn signed_wrapping() {
test_signed_wrapping!(isize i8 i16 i32 i64);
}