diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2019-08-14 09:03:41 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-14 09:03:41 -0500 |
| commit | 3d428bf111e7cc1bec71cf1c74c152469a952e69 (patch) | |
| tree | 9060fb3ce15a8c4e0efa53047e797ed810010312 | |
| parent | c4a676d5cb522906a8950c00b825ea133a956e5c (diff) | |
| parent | 3dd4991eab8456dee638bcc9fb935c4d6978eea9 (diff) | |
| download | rust-3d428bf111e7cc1bec71cf1c74c152469a952e69.tar.gz rust-3d428bf111e7cc1bec71cf1c74c152469a952e69.zip | |
Merge pull request rust-lang/libm#221 from Lokathor/tests
slightly improve spec and sanity check coverage
| -rw-r--r-- | library/compiler-builtins/libm/src/math/ceil.rs | 17 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/ceilf.rs | 22 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/fabs.rs | 24 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/fabsf.rs | 24 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/floor.rs | 22 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/floorf.rs | 19 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/sqrt.rs | 25 | ||||
| -rw-r--r-- | library/compiler-builtins/libm/src/math/sqrtf.rs | 23 |
8 files changed, 171 insertions, 5 deletions
diff --git a/library/compiler-builtins/libm/src/math/ceil.rs b/library/compiler-builtins/libm/src/math/ceil.rs index 63c1121c6d3..eda28b9a0f2 100644 --- a/library/compiler-builtins/libm/src/math/ceil.rs +++ b/library/compiler-builtins/libm/src/math/ceil.rs @@ -42,9 +42,22 @@ pub fn ceil(x: f64) -> f64 { #[cfg(test)] mod tests { + use super::*; + use core::f64::*; + #[test] fn sanity_check() { - assert_eq!(super::ceil(1.1), 2.0); - assert_eq!(super::ceil(2.9), 3.0); + assert_eq!(ceil(1.1), 2.0); + assert_eq!(ceil(2.9), 3.0); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil + #[test] + fn spec_tests() { + // Not Asserted: that the current rounding mode has no effect. + assert!(ceil(NAN).is_nan()); + for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() { + assert_eq!(ceil(f), f); + } } } diff --git a/library/compiler-builtins/libm/src/math/ceilf.rs b/library/compiler-builtins/libm/src/math/ceilf.rs index 87d96982aab..f1edbd061bf 100644 --- a/library/compiler-builtins/libm/src/math/ceilf.rs +++ b/library/compiler-builtins/libm/src/math/ceilf.rs @@ -39,3 +39,25 @@ pub fn ceilf(x: f32) -> f32 { } f32::from_bits(ui) } + +#[cfg(test)] +mod tests { + use super::*; + use core::f32::*; + + #[test] + fn sanity_check() { + assert_eq!(ceilf(1.1), 2.0); + assert_eq!(ceilf(2.9), 3.0); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil + #[test] + fn spec_tests() { + // Not Asserted: that the current rounding mode has no effect. + assert!(ceilf(NAN).is_nan()); + for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() { + assert_eq!(ceilf(f), f); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/fabs.rs b/library/compiler-builtins/libm/src/math/fabs.rs index 5a7f795f6a3..b2255ad32cf 100644 --- a/library/compiler-builtins/libm/src/math/fabs.rs +++ b/library/compiler-builtins/libm/src/math/fabs.rs @@ -15,3 +15,27 @@ pub fn fabs(x: f64) -> f64 { } f64::from_bits(x.to_bits() & (u64::MAX / 2)) } + +#[cfg(test)] +mod tests { + use super::*; + use core::f64::*; + + #[test] + fn sanity_check() { + assert_eq!(fabs(-1.0), 1.0); + assert_eq!(fabs(2.8), 2.8); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs + #[test] + fn spec_tests() { + assert!(fabs(NAN).is_nan()); + for f in [0.0, -0.0].iter().copied() { + assert_eq!(fabs(f), 0.0); + } + for f in [INFINITY, NEG_INFINITY].iter().copied() { + assert_eq!(fabs(f), INFINITY); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/fabsf.rs b/library/compiler-builtins/libm/src/math/fabsf.rs index 495512584d2..6655c4c3ccc 100644 --- a/library/compiler-builtins/libm/src/math/fabsf.rs +++ b/library/compiler-builtins/libm/src/math/fabsf.rs @@ -13,3 +13,27 @@ pub fn fabsf(x: f32) -> f32 { } f32::from_bits(x.to_bits() & 0x7fffffff) } + +#[cfg(test)] +mod tests { + use super::*; + use core::f32::*; + + #[test] + fn sanity_check() { + assert_eq!(fabsf(-1.0), 1.0); + assert_eq!(fabsf(2.8), 2.8); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs + #[test] + fn spec_tests() { + assert!(fabsf(NAN).is_nan()); + for f in [0.0, -0.0].iter().copied() { + assert_eq!(fabsf(f), 0.0); + } + for f in [INFINITY, NEG_INFINITY].iter().copied() { + assert_eq!(fabsf(f), INFINITY); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/floor.rs b/library/compiler-builtins/libm/src/math/floor.rs index 91825e3c88f..b2b760570de 100644 --- a/library/compiler-builtins/libm/src/math/floor.rs +++ b/library/compiler-builtins/libm/src/math/floor.rs @@ -38,3 +38,25 @@ pub fn floor(x: f64) -> f64 { x + y } } + +#[cfg(test)] +mod tests { + use super::*; + use core::f64::*; + + #[test] + fn sanity_check() { + assert_eq!(floor(1.1), 1.0); + assert_eq!(floor(2.9), 2.0); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/floor + #[test] + fn spec_tests() { + // Not Asserted: that the current rounding mode has no effect. + assert!(floor(NAN).is_nan()); + for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() { + assert_eq!(floor(f), f); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/floorf.rs b/library/compiler-builtins/libm/src/math/floorf.rs index 6d751b077ef..287f0864277 100644 --- a/library/compiler-builtins/libm/src/math/floorf.rs +++ b/library/compiler-builtins/libm/src/math/floorf.rs @@ -42,8 +42,23 @@ pub fn floorf(x: f32) -> f32 { #[cfg(test)] mod tests { + use super::*; + use core::f32::*; + #[test] - fn no_overflow() { - assert_eq!(super::floorf(0.5), 0.0); + fn sanity_check() { + assert_eq!(floorf(0.5), 0.0); + assert_eq!(floorf(1.1), 1.0); + assert_eq!(floorf(2.9), 2.0); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/floor + #[test] + fn spec_tests() { + // Not Asserted: that the current rounding mode has no effect. + assert!(floorf(NAN).is_nan()); + for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() { + assert_eq!(floorf(f), f); + } } } diff --git a/library/compiler-builtins/libm/src/math/sqrt.rs b/library/compiler-builtins/libm/src/math/sqrt.rs index 31afe33566b..f06b209a49f 100644 --- a/library/compiler-builtins/libm/src/math/sqrt.rs +++ b/library/compiler-builtins/libm/src/math/sqrt.rs @@ -37,7 +37,7 @@ * If (2) is false, then q = q ; otherwise q = q + 2 . * i+1 i i+1 i * - * With some algebric manipulation, it is not difficult to see + * With some algebraic manipulation, it is not difficult to see * that (2) is equivalent to * -(i+1) * s + 2 <= y (3) @@ -239,3 +239,26 @@ pub fn sqrt(x: f64) -> f64 { f64::from_bits((ix0 as u64) << 32 | ix1.0 as u64) } } + +#[cfg(test)] +mod tests { + use super::*; + use core::f64::*; + + #[test] + fn sanity_check() { + assert_eq!(sqrt(100.0), 10.0); + assert_eq!(sqrt(4.0), 2.0); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/sqrt + #[test] + fn spec_tests() { + // Not Asserted: FE_INVALID exception is raised if argument is negative. + assert!(sqrt(-1.0).is_nan()); + assert!(sqrt(NAN).is_nan()); + for f in [0.0, -0.0, INFINITY].iter().copied() { + assert_eq!(sqrt(f), f); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/sqrtf.rs b/library/compiler-builtins/libm/src/math/sqrtf.rs index 1d5b78e84c6..ee868c8c811 100644 --- a/library/compiler-builtins/libm/src/math/sqrtf.rs +++ b/library/compiler-builtins/libm/src/math/sqrtf.rs @@ -127,3 +127,26 @@ pub fn sqrtf(x: f32) -> f32 { f32::from_bits(ix as u32) } } + +#[cfg(test)] +mod tests { + use super::*; + use core::f32::*; + + #[test] + fn sanity_check() { + assert_eq!(sqrtf(100.0), 10.0); + assert_eq!(sqrtf(4.0), 2.0); + } + + /// The spec: https://en.cppreference.com/w/cpp/numeric/math/sqrt + #[test] + fn spec_tests() { + // Not Asserted: FE_INVALID exception is raised if argument is negative. + assert!(sqrtf(-1.0).is_nan()); + assert!(sqrtf(NAN).is_nan()); + for f in [0.0, -0.0, INFINITY].iter().copied() { + assert_eq!(sqrtf(f), f); + } + } +} |
