@@ -749,3 +749,52 @@ fn cast_float_to_int_edge_cases() {
749
749
test_edge ! ( f64 -> isize i8 i16 i32 i64 ) ;
750
750
test_edge ! ( f64 -> usize u8 u16 u32 u64 ) ;
751
751
}
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