Skip to content

Commit d2c1900

Browse files
committed
Consolidate is_normal tests
1 parent 7dd2811 commit d2c1900

File tree

5 files changed

+51
-82
lines changed

5 files changed

+51
-82
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: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
use std::num::FpCategory as Fp;
22
use std::ops::{Add, Div, Mul, Rem, Sub};
33

4-
/// Set the default tolerance for float comparison based on the type.
5-
trait Approx {
6-
const LIM: Self;
4+
trait TestableFloat {
5+
/// Set the default tolerance for float comparison based on the type.
6+
const APPROX: Self;
7+
const MIN_POSITIVE_NORMAL: Self;
8+
const MAX_SUBNORMAL: Self;
79
}
810

9-
impl Approx for f16 {
10-
const LIM: Self = 1e-3;
11+
impl TestableFloat for f16 {
12+
const APPROX: Self = 1e-3;
13+
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
14+
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
1115
}
12-
impl Approx for f32 {
13-
const LIM: Self = 1e-6;
16+
17+
impl TestableFloat for f32 {
18+
const APPROX: Self = 1e-6;
19+
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
20+
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
1421
}
15-
impl Approx for f64 {
16-
const LIM: Self = 1e-6;
22+
23+
impl TestableFloat for f64 {
24+
const APPROX: Self = 1e-6;
25+
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
26+
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
1727
}
18-
impl Approx for f128 {
19-
const LIM: Self = 1e-9;
28+
29+
impl TestableFloat for f128 {
30+
const APPROX: Self = 1e-9;
31+
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
32+
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
2033
}
2134

2235
/// Determine the tolerance for values of the argument type.
23-
const fn lim_for_ty<T: Approx + Copy>(_x: T) -> T {
24-
T::LIM
36+
const fn lim_for_ty<T: TestableFloat + Copy>(_x: T) -> T {
37+
T::APPROX
2538
}
2639

2740
// We have runtime ("rt") and const versions of these macros.
@@ -186,7 +199,7 @@ macro_rules! float_test {
186199
$( $( #[$const_meta] )+ )?
187200
mod const_ {
188201
#[allow(unused)]
189-
use super::Approx;
202+
use super::TestableFloat;
190203
#[allow(unused)]
191204
use std::num::FpCategory as Fp;
192205
#[allow(unused)]
@@ -446,6 +459,30 @@ float_test! {
446459
}
447460
}
448461

462+
float_test! {
463+
name: is_normal,
464+
attrs: {
465+
f16: #[cfg(any(miri, target_has_reliable_f16))],
466+
f128: #[cfg(any(miri, target_has_reliable_f128))],
467+
},
468+
test<Float> {
469+
let nan: Float = Float::NAN;
470+
let inf: Float = Float::INFINITY;
471+
let neg_inf: Float = Float::NEG_INFINITY;
472+
let zero: Float = 0.0;
473+
let neg_zero: Float = -0.0;
474+
let one : Float = 1.0;
475+
assert!(!nan.is_normal());
476+
assert!(!inf.is_normal());
477+
assert!(!neg_inf.is_normal());
478+
assert!(!zero.is_normal());
479+
assert!(!neg_zero.is_normal());
480+
assert!(one.is_normal());
481+
assert!(Float::MIN_POSITIVE_NORMAL.is_normal());
482+
assert!(!Float::MAX_SUBNORMAL.is_normal());
483+
}
484+
}
485+
449486
float_test! {
450487
name: min,
451488
attrs: {

0 commit comments

Comments
 (0)