Skip to content

Commit f0ed42b

Browse files
committed
Test edge cases of ToPrimitive with ints
1 parent 50868c6 commit f0ed42b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/cast.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,52 @@ fn cast_float_to_int_edge_cases() {
749749
test_edge!(f64 -> isize i8 i16 i32 i64);
750750
test_edge!(f64 -> usize u8 u16 u32 u64);
751751
}
752+
753+
#[test]
754+
fn cast_int_to_int_edge_cases() {
755+
use core::cmp::Ordering::*;
756+
757+
macro_rules! test_edge {
758+
($f:ident -> $($t:ident)+) => { $({
759+
fn test_edge() {
760+
dbg!("testing cast edge cases for {} -> {}", stringify!($f), stringify!($t));
761+
762+
match ($f::MIN as i64).cmp(&($t::MIN as i64)) {
763+
Greater => {
764+
assert_eq!(Some($f::MIN as $t), cast::<$f, $t>($f::MIN));
765+
}
766+
Equal => {
767+
assert_eq!(Some($t::MIN), cast::<$f, $t>($f::MIN));
768+
}
769+
Less => {
770+
let min = $t::MIN as $f;
771+
assert_eq!(Some($t::MIN), cast::<$f, $t>(min));
772+
assert_eq!(None, cast::<$f, $t>(min - 1));
773+
}
774+
}
775+
776+
match ($f::MAX as u64).cmp(&($t::MAX as u64)) {
777+
Greater => {
778+
let max = $t::MAX as $f;
779+
assert_eq!(Some($t::MAX), cast::<$f, $t>(max));
780+
assert_eq!(None, cast::<$f, $t>(max + 1));
781+
}
782+
Equal => {
783+
assert_eq!(Some($t::MAX), cast::<$f, $t>($f::MAX));
784+
}
785+
Less => {
786+
assert_eq!(Some($f::MAX as $t), cast::<$f, $t>($f::MAX));
787+
}
788+
}
789+
}
790+
test_edge();
791+
})+};
792+
($( $from:ident )+) => { $({
793+
test_edge!($from -> isize i8 i16 i32 i64);
794+
test_edge!($from -> usize u8 u16 u32 u64);
795+
})+}
796+
}
797+
798+
test_edge!(isize i8 i16 i32 i64);
799+
test_edge!(usize u8 u16 u32 u64);
800+
}

0 commit comments

Comments
 (0)