about summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-02-05 16:57:29 +0000
committerbors <bors@rust-lang.org>2017-02-05 16:57:29 +0000
commitfc02736d59252fe408dd6c2f7e2c4b6f229e4443 (patch)
treea5b61777e069dac44c179cba88a836166639a8c9 /src/libcoretest
parent9c8cdb2923a69177017165a4cdb0e1ea673fc49f (diff)
parenta2de6e22858a02cfcf6bfc18ff40ebb163ebb07c (diff)
downloadrust-fc02736d59252fe408dd6c2f7e2c4b6f229e4443.tar.gz
rust-fc02736d59252fe408dd6c2f7e2c4b6f229e4443.zip
Auto merge of #39408 - ollie27:i128_try_from, r=alexcrichton
Fix TryFrom for i128/u128

Another case of `as` cast silent truncation being error prone.

This also adds a few missing TryFrom tests to libcoretest.

cc #33417
cc #35118
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/num/mod.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs
index 4834c0e072c..51737c9c3b4 100644
--- a/src/libcoretest/num/mod.rs
+++ b/src/libcoretest/num/mod.rs
@@ -366,3 +366,35 @@ test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
 test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
 test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
 test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }
+
+macro_rules! test_impl_try_from_signed_to_unsigned_err {
+    ($fn_name:ident, $source:ty, $target:ty) => {
+        #[test]
+        fn $fn_name() {
+            let max = <$source>::max_value();
+            let min = <$source>::min_value();
+            let zero: $source = 0;
+            let t_max = <$target>::max_value();
+            let t_min = <$target>::min_value();
+            assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
+            assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
+            assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
+                       zero as $target);
+            assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
+                            .unwrap(),
+                       t_max as $target);
+            assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
+                            .unwrap(),
+                       t_min as $target);
+        }
+    }
+}
+
+test_impl_try_from_signed_to_unsigned_err! { test_try_i16u8, i16, u8 }
+
+test_impl_try_from_signed_to_unsigned_err! { test_try_i32u8, i32, u8 }
+test_impl_try_from_signed_to_unsigned_err! { test_try_i32u16, i32, u16 }
+
+test_impl_try_from_signed_to_unsigned_err! { test_try_i64u8, i64, u8 }
+test_impl_try_from_signed_to_unsigned_err! { test_try_i64u16, i64, u16 }
+test_impl_try_from_signed_to_unsigned_err! { test_try_i64u32, i64, u32 }