@@ -527,6 +527,37 @@ assume_usize_width! {
527
527
test_impl_try_from_signed_to_unsigned_err! { test_try_i128usize, i128 , usize }
528
528
}
529
529
530
+ /// Conversions to bool is lossy.
531
+ /// This checks that we only accept 1 for true and 0 for false.
532
+ macro_rules! test_impl_try_bool_from_integers {
533
+ ( $fn_name: ident, $source: ty) => {
534
+ #[ test]
535
+ fn $fn_name( ) {
536
+ let max = <$source>:: max_value( ) ;
537
+ let min = <$source>:: min_value( ) ;
538
+ let one: $source = 1 ;
539
+ let zero: $source = 0 ;
540
+ assert!( <bool as TryFrom <$source>>:: try_from( max) . is_err( ) ) ;
541
+ if min != zero {
542
+ assert!( <bool as TryFrom <$source>>:: try_from( min) . is_err( ) ) ;
543
+ }
544
+ assert_eq!( true , <bool as TryFrom <$source>>:: try_from( one) . unwrap( ) ) ;
545
+ assert_eq!( false , <bool as TryFrom <$source>>:: try_from( zero) . unwrap( ) ) ;
546
+ }
547
+ }
548
+ }
549
+
550
+ test_impl_try_bool_from_integers ! { test_try_bool_from_u8, u8 }
551
+ test_impl_try_bool_from_integers ! { test_try_bool_from_u16, u16 }
552
+ test_impl_try_bool_from_integers ! { test_try_bool_from_u32, u32 }
553
+ test_impl_try_bool_from_integers ! { test_try_bool_from_u64, u64 }
554
+ test_impl_try_bool_from_integers ! { test_try_bool_from_u128, u128 }
555
+ test_impl_try_bool_from_integers ! { test_try_bool_from_i8, i8 }
556
+ test_impl_try_bool_from_integers ! { test_try_bool_from_i16, i16 }
557
+ test_impl_try_bool_from_integers ! { test_try_bool_from_i32, i32 }
558
+ test_impl_try_bool_from_integers ! { test_try_bool_from_i64, i64 }
559
+ test_impl_try_bool_from_integers ! { test_try_bool_from_i128, i128 }
560
+
530
561
macro_rules! test_float {
531
562
( $modname: ident, $fty: ty, $inf: expr, $neginf: expr, $nan: expr) => { mod $modname {
532
563
// FIXME(nagisa): these tests should test for sign of -0.0
0 commit comments