slightly improve spec and sanity check coverage

This commit is contained in:
Lokathor 2019-08-13 23:40:54 -06:00
parent c4a676d5cb
commit ff3dd206d1
8 changed files with 171 additions and 5 deletions

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}