use cove::prelude::*; use core::num::{ NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize }; #[test] fn nonzero_assumed_lossless() { // Narrowing: NonZero -> NonZero assert_eq!(NonZeroI32::new(7).unwrap().cast::().assumed_lossless().get(), 7u16); // Narrowing: NonZero -> primitive assert_eq!( NonZeroU32::new(3001).unwrap().cast::().assumed_lossless().get(), 3001i16 ); } #[test] #[allow( clippy::cast_sign_loss, clippy::float_cmp, clippy::cast_precision_loss, clippy::cast_possible_wrap )] fn nonzero_bitwise() { // NonZero -> primitive assert_eq!(NonZeroU8::new(7).unwrap().cast::().bitwise(), 7u8); assert_eq!(NonZeroI8::new(-7).unwrap().cast::().bitwise(), -7i8 as u8); assert_eq!( NonZeroI64::new(i64::MIN).unwrap().cast::().bitwise(), f64::from_ne_bytes(i64::MIN.to_ne_bytes()) ); assert_ne!(NonZeroI64::new(i64::MIN).unwrap().cast::().bitwise(), i64::MIN as f64); // NonZero -> NonZero assert_eq!( NonZeroU16::new(u16::MAX).unwrap().cast::().bitwise(), NonZeroI16::new(u16::MAX as i16).unwrap() ); } #[test] #[cfg(target_pointer_width = "16")] #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] fn nonzero_bitwise_16() { assert_eq!(NonZeroUsize::new(4500).unwrap().cast::().bitwise(), 4500i16); assert_eq!(NonZeroIsize::new(isize::MIN).unwrap().cast::().bitwise(), isize::MIN as u16); assert_eq!(NonZeroU16::new(8080).unwrap().cast::().bitwise(), 8080usize); assert_eq!( NonZeroI16::new(-301).unwrap().cast::().bitwise().get(), -301i16 as usize ); } #[test] #[cfg(target_pointer_width = "32")] #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] fn nonzero_bitwise_32() { assert_eq!(NonZeroUsize::new(45000).unwrap().cast::().bitwise(), 45000i32); assert_eq!(NonZeroIsize::new(isize::MIN).unwrap().cast::().bitwise(), isize::MIN as u32); assert_eq!(NonZeroU32::new(8080).unwrap().cast::().bitwise(), 8080usize); assert_eq!( NonZeroI32::new(-301).unwrap().cast::().bitwise().get(), -301i32 as usize ); } #[test] #[cfg(target_pointer_width = "64")] #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] fn nonzero_bitwise_64() { assert_eq!(NonZeroUsize::new(45000).unwrap().cast::().bitwise(), 45000i64); assert_eq!(NonZeroIsize::new(isize::MIN).unwrap().cast::().bitwise(), isize::MIN as u64); assert_eq!(NonZeroU64::new(8080).unwrap().cast::().bitwise(), 8080usize); assert_eq!( NonZeroI64::new(-301).unwrap().cast::().bitwise().get(), -301i64 as usize ); } #[test] #[cfg(target_pointer_width = "128")] #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] fn nonzero_bitwise_128() { assert_eq!(NonZeroUsize::new(45000).unwrap().cast::().bitwise(), 45000i128); assert_eq!(NonZeroIsize::new(isize::MIN).unwrap().cast::().bitwise(), isize::MIN as u128); assert_eq!(NonZeroU128::new(8080).unwrap().cast::().bitwise(), 8080usize); assert_eq!( NonZeroI128::new(-301).unwrap().cast::().bitwise().get(), -301i128 as usize ); } #[test] fn nonzero_closest() { // Narrowing: NonZero -> NonZero assert_eq!(NonZeroU16::new(3).unwrap().cast::().closest().get(), 3u8); assert_eq!(NonZeroU16::new(261).unwrap().cast::().closest().get(), 255u8); // Narrowing: NonZero -> primitive assert_eq!(NonZeroU16::new(3).unwrap().cast::().closest(), 3u8); assert_eq!(NonZeroU16::new(261).unwrap().cast::().closest(), 255u8); // Narrowing: primitive -> NonZero assert_eq!(3u16.cast::().closest(), NonZeroU8::new(3).unwrap()); assert_eq!(261u16.cast::().closest(), NonZeroU8::new(255).unwrap()); assert_eq!(0u16.cast::().closest(), NonZeroU8::new(1).unwrap()); // Floating point assert_eq!(8.0f32.cast::().closest(), NonZeroI32::new(8).unwrap()); } #[test] fn nonzero_lossy() { // Narrowing: NonZero -> NonZero assert_eq!(NonZeroU16::new(3).unwrap().cast::().lossy().get(), 3u8); assert_eq!(NonZeroU16::new(261).unwrap().cast::().lossy().get(), 5u8); // Narrowing: NonZero -> primitive assert_eq!(NonZeroU16::new(3).unwrap().cast::().lossy(), 3u8); assert_eq!(NonZeroU16::new(261).unwrap().cast::().lossy(), 5u8); } #[test] #[allow(clippy::cast_lossless)] fn lossless() { assert_eq!( NonZeroU64::new(u64::MAX).unwrap().cast::().lossless().get(), u64::MAX as u128 ); assert_eq!(NonZeroI8::new(-3i8).unwrap().cast::().lossless().get(), -3isize); assert_eq!(NonZeroUsize::new(1).unwrap().cast::().lossless(), 1usize); assert_eq!( NonZeroI64::new(i64::MIN).unwrap().cast::().lossless().get(), i64::MIN as i128 ); // Casts that should not compile //let _failure = 0u64.cast::().lossless(); }