소스 검색

Added trait bounds tests for Wrapping

Yoan Lecoq 8 년 전
부모
커밋
fd50c9196b
6개의 변경된 파일84개의 추가작업 그리고 1개의 파일을 삭제
  1. 22 0
      traits/src/bounds.rs
  2. 18 0
      traits/src/cast.rs
  3. 10 0
      traits/src/identities.rs
  4. 6 1
      traits/src/lib.rs
  5. 18 0
      traits/src/ops/wrapping.rs
  6. 10 0
      traits/src/sign.rs

+ 22 - 0
traits/src/bounds.rs

@@ -75,3 +75,25 @@ macro_rules! bounded_tuple {
 
 for_each_tuple!(bounded_tuple);
 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));
+}

+ 18 - 0
traits/src/cast.rs

@@ -491,3 +491,21 @@ macro_rules! test_wrapping_to_primitive {
 fn wrapping_to_primitive() {
     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));
+}

+ 10 - 0
traits/src/identities.rs

@@ -136,3 +136,13 @@ fn wrapping_identities() {
     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));
+}

+ 6 - 1
traits/src/lib.rs

@@ -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]
 fn wrapping_from_str_radix() {
     test_wrapping_from_str_radix!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);

+ 18 - 0
traits/src/ops/wrapping.rs

@@ -107,3 +107,21 @@ fn test_wrapping_traits() {
     assert_eq!(wrapping_sub(0, 1), (Wrapping(0u8) - Wrapping(1u8)).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));
+}

+ 10 - 0
traits/src/sign.rs

@@ -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 {}
 
+#[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));
+}