Added trait bounds tests for Wrapping

This commit is contained in:
Yoan Lecoq 2017-05-01 13:42:30 +02:00
parent 3b7a775f38
commit fd50c9196b
6 changed files with 84 additions and 1 deletions

View File

@ -75,3 +75,25 @@ macro_rules! bounded_tuple {
for_each_tuple!(bounded_tuple); for_each_tuple!(bounded_tuple);
bounded_impl!(f64, f64::MIN, f64::MAX); bounded_impl!(f64, f64::MIN, f64::MAX);
macro_rules! test_wrapping_bounded {
($($t:ty)+) => {
$(
assert_eq!(Wrapping::<$t>::min_value().0, <$t>::min_value());
assert_eq!(Wrapping::<$t>::max_value().0, <$t>::max_value());
)+
};
}
#[test]
fn wrapping_bounded() {
test_wrapping_bounded!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
}
#[test]
fn wrapping_is_bounded() {
fn require_bounded<T: Bounded>(_: &T) {}
require_bounded(&Wrapping(42_u32));
require_bounded(&Wrapping(-42));
}

View File

@ -491,3 +491,21 @@ macro_rules! test_wrapping_to_primitive {
fn wrapping_to_primitive() { fn wrapping_to_primitive() {
test_wrapping_to_primitive!(usize u8 u16 u32 u64 isize i8 i16 i32 i64); test_wrapping_to_primitive!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
} }
#[test]
fn wrapping_is_toprimitive() {
fn require_toprimitive<T: ToPrimitive>(_: &T) {}
require_toprimitive(&Wrapping(42));
}
#[test]
fn wrapping_is_fromprimitive() {
fn require_fromprimitive<T: FromPrimitive>(_: &T) {}
require_fromprimitive(&Wrapping(42));
}
#[test]
fn wrapping_is_numcast() {
fn require_numcast<T: NumCast>(_: &T) {}
require_numcast(&Wrapping(42));
}

View File

@ -136,3 +136,13 @@ fn wrapping_identities() {
test_wrapping_identities!(isize i8 i16 i32 i64 usize u8 u16 u32 u64); test_wrapping_identities!(isize i8 i16 i32 i64 usize u8 u16 u32 u64);
} }
#[test]
fn wrapping_is_zero() {
fn require_zero<T: Zero>(_: &T) {}
require_zero(&Wrapping(42));
}
#[test]
fn wrapping_is_one() {
fn require_one<T: One>(_: &T) {}
require_one(&Wrapping(42));
}

View File

@ -305,7 +305,12 @@ macro_rules! test_wrapping_from_str_radix {
)+ )+
}; };
} }
#[test]
fn wrapping_is_num() {
fn require_num<T: Num>(_: &T) {}
require_num(&Wrapping(42_u32));
require_num(&Wrapping(-42));
}
#[test] #[test]
fn wrapping_from_str_radix() { fn wrapping_from_str_radix() {
test_wrapping_from_str_radix!(usize u8 u16 u32 u64 isize i8 i16 i32 i64); test_wrapping_from_str_radix!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);

View File

@ -107,3 +107,21 @@ fn test_wrapping_traits() {
assert_eq!(wrapping_sub(0, 1), (Wrapping(0u8) - 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); assert_eq!(wrapping_mul(255, 2), (Wrapping(255u8) * Wrapping(2u8)).0);
} }
#[test]
fn wrapping_is_wrappingadd() {
fn require_wrappingadd<T: WrappingAdd>(_: &T) {}
require_wrappingadd(&Wrapping(42));
}
#[test]
fn wrapping_is_wrappingsub() {
fn require_wrappingsub<T: WrappingSub>(_: &T) {}
require_wrappingsub(&Wrapping(42));
}
#[test]
fn wrapping_is_wrappingmul() {
fn require_wrappingmul<T: WrappingMul>(_: &T) {}
require_wrappingmul(&Wrapping(42));
}

View File

@ -187,3 +187,13 @@ empty_trait_impl!(Unsigned for usize u8 u16 u32 u64);
impl<T: Unsigned> Unsigned for Wrapping<T> where Wrapping<T>: Num {} impl<T: Unsigned> Unsigned for Wrapping<T> where Wrapping<T>: Num {}
#[test]
fn unsigned_wrapping_is_unsigned() {
fn require_unsigned<T: Unsigned>(_: &T) {}
require_unsigned(&Wrapping(42_u32));
}
#[test]
fn signed_wrapping_is_signed() {
fn require_signed<T: Signed>(_: &T) {}
require_signed(&Wrapping(-42));
}