Skip to content

Commit 8b238f4

Browse files
committed
Consolidate is_normal tests
1 parent a70f55c commit 8b238f4

File tree

5 files changed

+50
-69
lines changed

5 files changed

+50
-69
lines changed

library/coretests/tests/floats/f128.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@ const NAN_MASK2: u128 = 0x00005555555555555555555555555555;
4040
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
4141
// the intrinsics.
4242

43-
#[test]
44-
fn test_is_normal() {
45-
let nan: f128 = f128::NAN;
46-
let inf: f128 = f128::INFINITY;
47-
let neg_inf: f128 = f128::NEG_INFINITY;
48-
let zero: f128 = 0.0f128;
49-
let neg_zero: f128 = -0.0;
50-
assert!(!nan.is_normal());
51-
assert!(!inf.is_normal());
52-
assert!(!neg_inf.is_normal());
53-
assert!(!zero.is_normal());
54-
assert!(!neg_zero.is_normal());
55-
assert!(1f128.is_normal());
56-
assert!(1e-4931f128.is_normal());
57-
assert!(!1e-4932f128.is_normal());
58-
}
59-
6043
#[test]
6144
fn test_classify() {
6245
let nan: f128 = f128::NAN;

library/coretests/tests/floats/f16.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,6 @@ const NAN_MASK2: u16 = 0x0155;
4646
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
4747
// the intrinsics.
4848

49-
#[test]
50-
fn test_is_normal() {
51-
let nan: f16 = f16::NAN;
52-
let inf: f16 = f16::INFINITY;
53-
let neg_inf: f16 = f16::NEG_INFINITY;
54-
let zero: f16 = 0.0f16;
55-
let neg_zero: f16 = -0.0;
56-
assert!(!nan.is_normal());
57-
assert!(!inf.is_normal());
58-
assert!(!neg_inf.is_normal());
59-
assert!(!zero.is_normal());
60-
assert!(!neg_zero.is_normal());
61-
assert!(1f16.is_normal());
62-
assert!(1e-4f16.is_normal());
63-
assert!(!1e-5f16.is_normal());
64-
}
65-
6649
#[test]
6750
fn test_classify() {
6851
let nan: f16 = f16::NAN;

library/coretests/tests/floats/f32.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@ const NAN_MASK2: u32 = 0x0055_5555;
3030
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
3131
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
3232

33-
#[test]
34-
fn test_is_normal() {
35-
let nan: f32 = f32::NAN;
36-
let inf: f32 = f32::INFINITY;
37-
let neg_inf: f32 = f32::NEG_INFINITY;
38-
let zero: f32 = 0.0f32;
39-
let neg_zero: f32 = -0.0;
40-
assert!(!nan.is_normal());
41-
assert!(!inf.is_normal());
42-
assert!(!neg_inf.is_normal());
43-
assert!(!zero.is_normal());
44-
assert!(!neg_zero.is_normal());
45-
assert!(1f32.is_normal());
46-
assert!(1e-37f32.is_normal());
47-
assert!(!1e-38f32.is_normal());
48-
}
49-
5033
#[test]
5134
fn test_classify() {
5235
let nan: f32 = f32::NAN;

library/coretests/tests/floats/f64.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,6 @@ const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa;
2525
/// Second pattern over the mantissa
2626
const NAN_MASK2: u64 = 0x0005_5555_5555_5555;
2727

28-
#[test]
29-
fn test_is_normal() {
30-
let nan: f64 = f64::NAN;
31-
let inf: f64 = f64::INFINITY;
32-
let neg_inf: f64 = f64::NEG_INFINITY;
33-
let zero: f64 = 0.0f64;
34-
let neg_zero: f64 = -0.0;
35-
assert!(!nan.is_normal());
36-
assert!(!inf.is_normal());
37-
assert!(!neg_inf.is_normal());
38-
assert!(!zero.is_normal());
39-
assert!(!neg_zero.is_normal());
40-
assert!(1f64.is_normal());
41-
assert!(1e-307f64.is_normal());
42-
assert!(!1e-308f64.is_normal());
43-
}
44-
4528
#[test]
4629
fn test_classify() {
4730
let nan: f64 = f64::NAN;

library/coretests/tests/floats/mod.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ impl Approx for f128 {
1919
const LIM: Self = 1e-9;
2020
}
2121

22+
trait TestableFloat {
23+
const SMALL_NORMAL: Self;
24+
const SUBNORMAL: Self;
25+
}
26+
27+
impl TestableFloat for f16 {
28+
const SMALL_NORMAL: Self = 1e-4;
29+
const SUBNORMAL: Self = 1e-5;
30+
}
31+
32+
impl TestableFloat for f32 {
33+
const SMALL_NORMAL: Self = 1e-37;
34+
const SUBNORMAL: Self = 1e-38;
35+
}
36+
37+
impl TestableFloat for f64 {
38+
const SMALL_NORMAL: Self = 1e-307;
39+
const SUBNORMAL: Self = 1e-308;
40+
}
41+
42+
impl TestableFloat for f128 {
43+
const SMALL_NORMAL: Self = 1e-4931;
44+
const SUBNORMAL: Self = 1e-4932;
45+
}
46+
2247
/// Determine the tolerance for values of the argument type.
2348
const fn lim_for_ty<T: Approx + Copy>(_x: T) -> T {
2449
T::LIM
@@ -186,7 +211,7 @@ macro_rules! float_test {
186211
$( $( #[$const_meta] )+ )?
187212
mod const_ {
188213
#[allow(unused)]
189-
use super::Approx;
214+
use super::{Approx, TestableFloat};
190215
#[allow(unused)]
191216
use std::num::FpCategory as Fp;
192217
#[allow(unused)]
@@ -443,6 +468,30 @@ float_test! {
443468
}
444469
}
445470

471+
float_test! {
472+
name: is_normal,
473+
attrs: {
474+
f16: #[cfg(any(miri, target_has_reliable_f16))],
475+
f128: #[cfg(any(miri, target_has_reliable_f128))],
476+
},
477+
test<Float> {
478+
let nan: Float = Float::NAN;
479+
let inf: Float = Float::INFINITY;
480+
let neg_inf: Float = Float::NEG_INFINITY;
481+
let zero: Float = 0.0;
482+
let neg_zero: Float = -0.0;
483+
let one : Float = 1.0;
484+
assert!(!nan.is_normal());
485+
assert!(!inf.is_normal());
486+
assert!(!neg_inf.is_normal());
487+
assert!(!zero.is_normal());
488+
assert!(!neg_zero.is_normal());
489+
assert!(one.is_normal());
490+
assert!(Float::SMALL_NORMAL.is_normal());
491+
assert!(!Float::SUBNORMAL.is_normal());
492+
}
493+
}
494+
446495
float_test! {
447496
name: min,
448497
attrs: {

0 commit comments

Comments
 (0)