From 6f3e84dbe9434b020da73d66275c3609a2b7bb1b Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 15 Oct 2015 05:23:44 +0300 Subject: Implement conversion traits for primitive integer types --- src/libcoretest/num/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/libcoretest') diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index 2a3ff88fe6d..5e2ec66baba 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -137,4 +137,44 @@ mod tests { assert_eq!("+".parse::().ok(), None); assert_eq!("".parse::().ok(), None); } + + macro_rules! test_impl_from { + ($fn_name: ident, $Small: ty, $Large: ty) => { + #[test] + fn $fn_name() { + let small_max = <$Small>::max_value(); + let small_min = <$Small>::min_value(); + let large_max: $Large = small_max.into(); + let large_min: $Large = small_min.into(); + assert_eq!(large_max as $Small, small_max); + assert_eq!(large_min as $Small, small_min); + } + } + } + + // Unsigned -> Unsigned + test_impl_from! { test_u8u16, u8, u16 } + test_impl_from! { test_u8u32, u8, u32 } + test_impl_from! { test_u8u64, u8, u64 } + test_impl_from! { test_u8usize, u8, usize } + test_impl_from! { test_u16u32, u16, u32 } + test_impl_from! { test_u16u64, u16, u64 } + test_impl_from! { test_u32u64, u32, u64 } + + // Signed -> Signed + test_impl_from! { test_i8i16, i8, i16 } + test_impl_from! { test_i8i32, i8, i32 } + test_impl_from! { test_i8i64, i8, i64 } + test_impl_from! { test_i8isize, i8, isize } + test_impl_from! { test_i16i32, i16, i32 } + test_impl_from! { test_i16i64, i16, i64 } + test_impl_from! { test_i32i64, i32, i64 } + + // Unsigned -> Signed + test_impl_from! { test_u8i16, u8, i16 } + test_impl_from! { test_u8i32, u8, i32 } + test_impl_from! { test_u8i64, u8, i64 } + test_impl_from! { test_u16i32, u16, i32 } + test_impl_from! { test_u16i64, u16, i64 } + test_impl_from! { test_u32i64, u32, i64 } } -- cgit 1.4.1-3-g733a5