Skip to content

Commit 8b1d101

Browse files
committed
dedup to_degrees float test
1 parent 258a80d commit 8b1d101

File tree

5 files changed

+30
-60
lines changed

5 files changed

+30
-60
lines changed

library/coretests/tests/floats/f128.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,6 @@ fn test_max_recip() {
5252
);
5353
}
5454

55-
#[test]
56-
fn test_to_degrees() {
57-
let pi: f128 = consts::PI;
58-
let nan: f128 = f128::NAN;
59-
let inf: f128 = f128::INFINITY;
60-
let neg_inf: f128 = f128::NEG_INFINITY;
61-
assert_biteq!(0.0f128.to_degrees(), 0.0);
62-
assert_approx_eq!((-5.8f128).to_degrees(), -332.31552117587745090765431723855668471, TOL);
63-
assert_approx_eq!(pi.to_degrees(), 180.0, TOL);
64-
assert!(nan.to_degrees().is_nan());
65-
assert_biteq!(inf.to_degrees(), inf);
66-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
67-
assert_biteq!(1_f128.to_degrees(), 57.2957795130823208767981548141051703);
68-
}
69-
7055
#[test]
7156
fn test_to_radians() {
7257
let pi: f128 = consts::PI;

library/coretests/tests/floats/f16.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,6 @@ fn test_max_recip() {
5454
assert_approx_eq!(f16::MAX.recip(), 1.526624e-5f16, 1e-4);
5555
}
5656

57-
#[test]
58-
fn test_to_degrees() {
59-
let pi: f16 = consts::PI;
60-
let nan: f16 = f16::NAN;
61-
let inf: f16 = f16::INFINITY;
62-
let neg_inf: f16 = f16::NEG_INFINITY;
63-
assert_biteq!(0.0f16.to_degrees(), 0.0);
64-
assert_approx_eq!((-5.8f16).to_degrees(), -332.315521, TOL_P2);
65-
assert_approx_eq!(pi.to_degrees(), 180.0, TOL_P2);
66-
assert!(nan.to_degrees().is_nan());
67-
assert_biteq!(inf.to_degrees(), inf);
68-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
69-
assert_biteq!(1_f16.to_degrees(), 57.2957795130823208767981548141051703);
70-
}
71-
7257
#[test]
7358
fn test_to_radians() {
7459
let pi: f16 = consts::PI;

library/coretests/tests/floats/f32.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ fn test_mul_add() {
2727
assert_biteq!(f32::math::mul_add(-3.2f32, 2.4, neg_inf), neg_inf);
2828
}
2929

30-
#[test]
31-
fn test_to_degrees() {
32-
let pi: f32 = consts::PI;
33-
let nan: f32 = f32::NAN;
34-
let inf: f32 = f32::INFINITY;
35-
let neg_inf: f32 = f32::NEG_INFINITY;
36-
assert_biteq!(0.0f32.to_degrees(), 0.0);
37-
assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
38-
assert_biteq!(pi.to_degrees(), 180.0);
39-
assert!(nan.to_degrees().is_nan());
40-
assert_biteq!(inf.to_degrees(), inf);
41-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
42-
assert_biteq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
43-
}
44-
4530
#[test]
4631
fn test_to_radians() {
4732
let pi: f32 = consts::PI;

library/coretests/tests/floats/f64.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ fn test_mul_add() {
2727
assert_biteq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
2828
}
2929

30-
#[test]
31-
fn test_to_degrees() {
32-
let pi: f64 = consts::PI;
33-
let nan: f64 = f64::NAN;
34-
let inf: f64 = f64::INFINITY;
35-
let neg_inf: f64 = f64::NEG_INFINITY;
36-
assert_biteq!(0.0f64.to_degrees(), 0.0);
37-
assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
38-
assert_biteq!(pi.to_degrees(), 180.0);
39-
assert!(nan.to_degrees().is_nan());
40-
assert_biteq!(inf.to_degrees(), inf);
41-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
42-
}
43-
4430
#[test]
4531
fn test_to_radians() {
4632
let pi: f64 = consts::PI;

library/coretests/tests/floats/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use std::num::FpCategory as Fp;
22
use std::ops::{Add, Div, Mul, Rem, Sub};
33

4-
trait TestableFloat {
4+
trait TestableFloat: Sized {
55
/// Unsigned int with the same size, for converting to/from bits.
66
type Int;
77
/// Set the default tolerance for float comparison based on the type.
88
const APPROX: Self;
9+
// Needed for looser tolerance for f16
10+
const PI_TO_DEGREES_APPROX: Self = Self::APPROX;
911
const ZERO: Self;
1012
const ONE: Self;
13+
const PI: Self;
1114
const MIN_POSITIVE_NORMAL: Self;
1215
const MAX_SUBNORMAL: Self;
1316
/// Smallest number
@@ -25,8 +28,10 @@ trait TestableFloat {
2528
impl TestableFloat for f16 {
2629
type Int = u16;
2730
const APPROX: Self = 1e-3;
31+
const PI_TO_DEGREES_APPROX: Self = 0.125;
2832
const ZERO: Self = 0.0;
2933
const ONE: Self = 1.0;
34+
const PI: Self = std::f16::consts::PI;
3035
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
3136
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
3237
const TINY: Self = Self::from_bits(0x1);
@@ -41,6 +46,7 @@ impl TestableFloat for f32 {
4146
const APPROX: Self = 1e-6;
4247
const ZERO: Self = 0.0;
4348
const ONE: Self = 1.0;
49+
const PI: Self = std::f32::consts::PI;
4450
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
4551
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
4652
const TINY: Self = Self::from_bits(0x1);
@@ -55,6 +61,7 @@ impl TestableFloat for f64 {
5561
const APPROX: Self = 1e-6;
5662
const ZERO: Self = 0.0;
5763
const ONE: Self = 1.0;
64+
const PI: Self = std::f64::consts::PI;
5865
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
5966
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
6067
const TINY: Self = Self::from_bits(0x1);
@@ -69,6 +76,7 @@ impl TestableFloat for f128 {
6976
const APPROX: Self = 1e-9;
7077
const ZERO: Self = 0.0;
7178
const ONE: Self = 1.0;
79+
const PI: Self = std::f128::consts::PI;
7280
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
7381
const MAX_SUBNORMAL: Self = Self::MIN_POSITIVE.next_down();
7482
const TINY: Self = Self::from_bits(0x1);
@@ -1381,3 +1389,24 @@ float_test! {
13811389
assert_biteq!(neg_inf.powi(2), inf);
13821390
}
13831391
}
1392+
1393+
float_test! {
1394+
name: to_degrees,
1395+
attrs: {
1396+
f16: #[cfg(target_has_reliable_f16)],
1397+
f128: #[cfg(target_has_reliable_f128)],
1398+
},
1399+
test<Float> {
1400+
let pi: Float = Float::PI;
1401+
let nan: Float = Float::NAN;
1402+
let inf: Float = Float::INFINITY;
1403+
let neg_inf: Float = Float::NEG_INFINITY;
1404+
assert_biteq!((0.0 as Float).to_degrees(), 0.0);
1405+
assert_approx_eq!((-5.8 as Float).to_degrees(), -332.31552117587745090765431723855668471);
1406+
assert_approx_eq!(pi.to_degrees(), 180.0, Float::PI_TO_DEGREES_APPROX);
1407+
assert!(nan.to_degrees().is_nan());
1408+
assert_biteq!(inf.to_degrees(), inf);
1409+
assert_biteq!(neg_inf.to_degrees(), neg_inf);
1410+
assert_biteq!((1.0 as Float).to_degrees(), 57.2957795130823208767981548141051703);
1411+
}
1412+
}

0 commit comments

Comments
 (0)