Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move unit tests into its own file #204

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
/math/src
/math/target
/target
/tests
Cargo.lock
39 changes: 0 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,4 @@
)]

mod math;

use core::{f32, f64};

pub use self::math::*;

/// Approximate equality with 1 ULP of tolerance
#[doc(hidden)]
#[inline]
pub fn _eqf(a: f32, b: f32) -> Result<(), u32> {
if a.is_nan() && b.is_nan() {
Ok(())
} else {
let err = (a.to_bits() as i32).wrapping_sub(b.to_bits() as i32).abs();

if err <= 1 {
Ok(())
} else {
Err(err as u32)
}
}
}

#[doc(hidden)]
#[inline]
pub fn _eq(a: f64, b: f64) -> Result<(), u64> {
if a.is_nan() && b.is_nan() {
Ok(())
} else {
let err = (a.to_bits() as i64).wrapping_sub(b.to_bits() as i64).abs();

if err <= 1 {
Ok(())
} else {
Err(err as u64)
}
}
}

#[cfg(all(test, feature = "musl-reference-tests"))]
include!(concat!(env!("OUT_DIR"), "/musl-tests.rs"));
48 changes: 0 additions & 48 deletions src/math/atan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,51 +135,3 @@ pub fn atan(x: f64) -> f64 {
z
}
}

#[cfg(test)]
mod tests {
use super::atan;
use core::f64;

#[test]
fn sanity_check() {
for (input, answer) in [
(3.0_f64.sqrt() / 3.0, f64::consts::FRAC_PI_6),
(1.0, f64::consts::FRAC_PI_4),
(3.0_f64.sqrt(), f64::consts::FRAC_PI_3),
(-3.0_f64.sqrt() / 3.0, -f64::consts::FRAC_PI_6),
(-1.0, -f64::consts::FRAC_PI_4),
(-3.0_f64.sqrt(), -f64::consts::FRAC_PI_3),
]
.iter()
{
assert!(
(atan(*input) - answer) / answer < 1e-5,
"\natan({:.4}/16) = {:.4}, actual: {}",
input * 16.0,
answer,
atan(*input)
);
}
}

#[test]
fn zero() {
assert_eq!(atan(0.0), 0.0);
}

#[test]
fn infinity() {
assert_eq!(atan(f64::INFINITY), f64::consts::FRAC_PI_2);
}

#[test]
fn minus_infinity() {
assert_eq!(atan(f64::NEG_INFINITY), -f64::consts::FRAC_PI_2);
}

#[test]
fn nan() {
assert!(atan(f64::NAN).is_nan());
}
}
10 changes: 0 additions & 10 deletions src/math/atan2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,3 @@ pub fn atan2(y: f64, x: f64) -> f64 {
_ => (z - PI_LO) - PI, /* atan(-,-) */
}
}

#[test]
fn sanity_check() {
assert_eq!(atan2(0.0, 1.0), 0.0);
assert_eq!(atan2(0.0, -1.0), PI);
assert_eq!(atan2(-0.0, -1.0), -PI);
assert_eq!(atan2(3.0, 2.0), atan(3.0 / 2.0));
assert_eq!(atan2(2.0, -1.0), atan(2.0 / -1.0) + PI);
assert_eq!(atan2(-2.0, -1.0), atan(-2.0 / -1.0) - PI);
}
9 changes: 0 additions & 9 deletions src/math/ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,3 @@ pub fn ceil(x: f64) -> f64 {
x + y
}
}

#[cfg(test)]
mod tests {
#[test]
fn sanity_check() {
assert_eq!(super::ceil(1.1), 2.0);
assert_eq!(super::ceil(2.9), 3.0);
}
}
8 changes: 1 addition & 7 deletions src/math/exp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use super::scalbn;

const TBLSIZE: usize = 256;

#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
static TBL: [u64; TBLSIZE * 2] = [
// exp2(z + eps) eps
0x3fe6a09e667f3d5d, 0x3d39880000000000,
Expand Down Expand Up @@ -387,9 +387,3 @@ pub fn exp2(mut x: f64) -> f64 {

scalbn(r, ki)
}

#[test]
fn i0_wrap_test() {
let x = -3.0 / 256.0;
assert_eq!(exp2(x), f64::from_bits(0x3fefbdba3692d514));
}
8 changes: 0 additions & 8 deletions src/math/expm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,3 @@ pub fn expm1(mut x: f64) -> f64 {
}
y
}

#[cfg(test)]
mod tests {
#[test]
fn sanity_check() {
assert_eq!(super::expm1(1.1), 2.0041660239464334);
}
}
8 changes: 0 additions & 8 deletions src/math/floorf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,3 @@ pub fn floorf(x: f32) -> f32 {
}
f32::from_bits(ui)
}

#[cfg(test)]
mod tests {
#[test]
fn no_overflow() {
assert_eq!(super::floorf(0.5), 0.0);
}
}
14 changes: 0 additions & 14 deletions src/math/j1f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,3 @@ fn qonef(x: f32) -> f32 {
s = 1.0 + z * (q[0] + z * (q[1] + z * (q[2] + z * (q[3] + z * (q[4] + z * q[5])))));
return (0.375 + r / s) / x;
}

#[cfg(test)]
mod tests {
use super::{j1f, y1f};
#[test]
fn test_j1f_2488() {
// 0x401F3E49
assert_eq!(j1f(2.4881766_f32), 0.49999475_f32);
}
#[test]
fn test_y1f_2002() {
assert_eq!(y1f(2.0000002_f32), -0.10703229_f32);
}
}
Loading