//! These tests cover the `Closest` trait for primitives to primitives. This is a hard one for //! which to provide random testing. use std::num::NonZeroI8; use cove::prelude::*; #[test] fn lossless() { assert_eq!(0u128.cast::().closest(), 0i8); assert_eq!((-99f64).cast::().closest(), -99i16); } #[test] fn saturating() { assert_eq!(u128::MAX.cast::().closest(), i32::MAX); assert_eq!((-300f32).cast::().closest(), 0usize); } #[test] fn rounding() { assert_eq!(5.5f32.cast::().closest(), 6u8); assert_eq!(5.49f32.cast::().closest(), 5u8); } #[test] #[allow(clippy::float_cmp)] fn infinity() { assert_eq!(f64::INFINITY.cast::().closest(), f64::INFINITY); assert_eq!(f64::INFINITY.cast::().closest(), f32::INFINITY); assert_eq!(f64::NEG_INFINITY.cast::().closest(), f64::NEG_INFINITY); assert_eq!(f64::NEG_INFINITY.cast::().closest(), f32::NEG_INFINITY); assert_eq!(f32::INFINITY.cast::().closest(), f64::INFINITY); assert_eq!(f32::INFINITY.cast::().closest(), f32::INFINITY); assert_eq!(f32::NEG_INFINITY.cast::().closest(), f64::NEG_INFINITY); assert_eq!(f32::NEG_INFINITY.cast::().closest(), f32::NEG_INFINITY); assert_eq!(f64::INFINITY.cast::().closest(), u32::MAX); assert_eq!(f32::NEG_INFINITY.cast::().closest(), i16::MIN); } #[test] #[allow(clippy::float_cmp)] fn overflow() { assert_eq!(u128::MAX.cast::().closest(), f32::MAX); assert_eq!(f64::MIN.cast::().closest(), f32::MIN); } #[test] fn nan() { assert!(f64::NAN.cast::().closest().is_nan()); assert!(f64::NAN.cast::().closest().is_nan()); assert!(f32::NAN.cast::().closest().is_nan()); assert!(f32::NAN.cast::().closest().is_nan()); assert_eq!(f32::NAN.cast::().closest(), 0i8); assert_eq!(f32::NAN.cast::().closest().get(), 1i8); assert_eq!((-f32::NAN).cast::().closest().get(), -1i8); }