Refactor nan tests

This commit is contained in:
Roger Curley 2025-07-03 15:18:57 -04:00
parent 607fbd8d3e
commit bc2001f158
5 changed files with 23 additions and 56 deletions

View file

@ -55,20 +55,6 @@ fn test_num_f128() {
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
// the intrinsics.
#[test]
fn test_nan() {
let nan: f128 = f128::NAN;
assert!(nan.is_nan());
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert!(!nan.is_normal());
assert_eq!(Fp::Nan, nan.classify());
// Ensure the quiet bit is set.
assert!(nan.to_bits() & (1 << (f128::MANTISSA_DIGITS - 2)) != 0);
}
#[test]
fn test_infinity() {
let inf: f128 = f128::INFINITY;

View file

@ -51,20 +51,6 @@ fn test_num_f16() {
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
// the intrinsics.
#[test]
fn test_nan() {
let nan: f16 = f16::NAN;
assert!(nan.is_nan());
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert!(!nan.is_normal());
assert_eq!(Fp::Nan, nan.classify());
// Ensure the quiet bit is set.
assert!(nan.to_bits() & (1 << (f16::MANTISSA_DIGITS - 2)) != 0);
}
#[test]
fn test_infinity() {
let inf: f16 = f16::INFINITY;

View file

@ -35,20 +35,6 @@ fn test_num_f32() {
super::test_num(10f32, 2f32);
}
#[test]
fn test_nan() {
let nan: f32 = f32::NAN;
assert!(nan.is_nan());
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(!nan.is_normal());
assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert_eq!(Fp::Nan, nan.classify());
// Ensure the quiet bit is set.
assert!(nan.to_bits() & (1 << (f32::MANTISSA_DIGITS - 2)) != 0);
}
#[test]
fn test_infinity() {
let inf: f32 = f32::INFINITY;

View file

@ -30,20 +30,6 @@ fn test_num_f64() {
super::test_num(10f64, 2f64);
}
#[test]
fn test_nan() {
let nan: f64 = f64::NAN;
assert!(nan.is_nan());
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(!nan.is_normal());
assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert_eq!(Fp::Nan, nan.classify());
// Ensure the quiet bit is set.
assert!(nan.to_bits() & (1 << (f64::MANTISSA_DIGITS - 2)) != 0);
}
#[test]
fn test_infinity() {
let inf: f64 = f64::INFINITY;

View file

@ -1,4 +1,5 @@
use std::fmt;
use std::num::FpCategory as Fp;
use std::ops::{Add, Div, Mul, Rem, Sub};
/// Set the default tolerance for float comparison based on the type.
@ -187,6 +188,8 @@ macro_rules! float_test {
mod const_ {
#[allow(unused)]
use super::Approx;
#[allow(unused)]
use std::num::FpCategory as Fp;
// Shadow the runtime versions of the macro with const-compatible versions.
#[allow(unused)]
use $crate::floats::{
@ -250,6 +253,26 @@ mod f16;
mod f32;
mod f64;
float_test! {
name: nan,
attrs: {
f16: #[cfg(any(miri, target_has_reliable_f16))],
f128: #[cfg(any(miri, target_has_reliable_f128))],
},
test<Float> {
let nan: Float = Float::NAN;
assert!(nan.is_nan());
assert!(!nan.is_infinite());
assert!(!nan.is_finite());
assert!(!nan.is_normal());
assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative());
assert!(matches!(nan.classify(), Fp::Nan));
// Ensure the quiet bit is set.
assert!(nan.to_bits() & (1 << (Float::MANTISSA_DIGITS - 2)) != 0);
}
}
float_test! {
name: min,
attrs: {