From f0ed42b3bc45cdbb4323ad2ece36fe164a9bd066 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Sun, 11 Mar 2018 01:37:27 -0800 Subject: [PATCH] Test edge cases of ToPrimitive with ints --- src/cast.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/cast.rs b/src/cast.rs index 694b0e5..8a87008 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -749,3 +749,52 @@ fn cast_float_to_int_edge_cases() { test_edge!(f64 -> isize i8 i16 i32 i64); test_edge!(f64 -> usize u8 u16 u32 u64); } + +#[test] +fn cast_int_to_int_edge_cases() { + use core::cmp::Ordering::*; + + macro_rules! test_edge { + ($f:ident -> $($t:ident)+) => { $({ + fn test_edge() { + dbg!("testing cast edge cases for {} -> {}", stringify!($f), stringify!($t)); + + match ($f::MIN as i64).cmp(&($t::MIN as i64)) { + Greater => { + assert_eq!(Some($f::MIN as $t), cast::<$f, $t>($f::MIN)); + } + Equal => { + assert_eq!(Some($t::MIN), cast::<$f, $t>($f::MIN)); + } + Less => { + let min = $t::MIN as $f; + assert_eq!(Some($t::MIN), cast::<$f, $t>(min)); + assert_eq!(None, cast::<$f, $t>(min - 1)); + } + } + + match ($f::MAX as u64).cmp(&($t::MAX as u64)) { + Greater => { + let max = $t::MAX as $f; + assert_eq!(Some($t::MAX), cast::<$f, $t>(max)); + assert_eq!(None, cast::<$f, $t>(max + 1)); + } + Equal => { + assert_eq!(Some($t::MAX), cast::<$f, $t>($f::MAX)); + } + Less => { + assert_eq!(Some($f::MAX as $t), cast::<$f, $t>($f::MAX)); + } + } + } + test_edge(); + })+}; + ($( $from:ident )+) => { $({ + test_edge!($from -> isize i8 i16 i32 i64); + test_edge!($from -> usize u8 u16 u32 u64); + })+} + } + + test_edge!(isize i8 i16 i32 i64); + test_edge!(usize u8 u16 u32 u64); +}