Add assert_approx_eq! macro

This commit is contained in:
Brendan Zabarauskas 2013-05-06 21:51:48 +10:00
parent 0211833008
commit 0e2242f6d6
7 changed files with 275 additions and 223 deletions

View File

@ -826,15 +826,6 @@ mod tests {
use super::*;
use prelude::*;
macro_rules! assert_fuzzy_eq(
($a:expr, $b:expr) => ({
let a = $a, b = $b;
if !((a - b).abs() < 1.0e-6) {
fail!(fmt!("The values were not approximately equal. Found: %? and %?", a, b));
}
})
)
#[test]
fn test_num() {
num::test_num(10f32, 2f32);
@ -864,91 +855,91 @@ mod tests {
#[test]
fn test_floor() {
assert_fuzzy_eq!(1.0f32.floor(), 1.0f32);
assert_fuzzy_eq!(1.3f32.floor(), 1.0f32);
assert_fuzzy_eq!(1.5f32.floor(), 1.0f32);
assert_fuzzy_eq!(1.7f32.floor(), 1.0f32);
assert_fuzzy_eq!(0.0f32.floor(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).floor(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).floor(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).floor(), -2.0f32);
assert_fuzzy_eq!((-1.5f32).floor(), -2.0f32);
assert_fuzzy_eq!((-1.7f32).floor(), -2.0f32);
assert_approx_eq!(1.0f32.floor(), 1.0f32);
assert_approx_eq!(1.3f32.floor(), 1.0f32);
assert_approx_eq!(1.5f32.floor(), 1.0f32);
assert_approx_eq!(1.7f32.floor(), 1.0f32);
assert_approx_eq!(0.0f32.floor(), 0.0f32);
assert_approx_eq!((-0.0f32).floor(), -0.0f32);
assert_approx_eq!((-1.0f32).floor(), -1.0f32);
assert_approx_eq!((-1.3f32).floor(), -2.0f32);
assert_approx_eq!((-1.5f32).floor(), -2.0f32);
assert_approx_eq!((-1.7f32).floor(), -2.0f32);
}
#[test]
fn test_ceil() {
assert_fuzzy_eq!(1.0f32.ceil(), 1.0f32);
assert_fuzzy_eq!(1.3f32.ceil(), 2.0f32);
assert_fuzzy_eq!(1.5f32.ceil(), 2.0f32);
assert_fuzzy_eq!(1.7f32.ceil(), 2.0f32);
assert_fuzzy_eq!(0.0f32.ceil(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).ceil(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).ceil(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).ceil(), -1.0f32);
assert_fuzzy_eq!((-1.5f32).ceil(), -1.0f32);
assert_fuzzy_eq!((-1.7f32).ceil(), -1.0f32);
assert_approx_eq!(1.0f32.ceil(), 1.0f32);
assert_approx_eq!(1.3f32.ceil(), 2.0f32);
assert_approx_eq!(1.5f32.ceil(), 2.0f32);
assert_approx_eq!(1.7f32.ceil(), 2.0f32);
assert_approx_eq!(0.0f32.ceil(), 0.0f32);
assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
}
#[test]
fn test_round() {
assert_fuzzy_eq!(1.0f32.round(), 1.0f32);
assert_fuzzy_eq!(1.3f32.round(), 1.0f32);
assert_fuzzy_eq!(1.5f32.round(), 2.0f32);
assert_fuzzy_eq!(1.7f32.round(), 2.0f32);
assert_fuzzy_eq!(0.0f32.round(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).round(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).round(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).round(), -1.0f32);
assert_fuzzy_eq!((-1.5f32).round(), -2.0f32);
assert_fuzzy_eq!((-1.7f32).round(), -2.0f32);
assert_approx_eq!(1.0f32.round(), 1.0f32);
assert_approx_eq!(1.3f32.round(), 1.0f32);
assert_approx_eq!(1.5f32.round(), 2.0f32);
assert_approx_eq!(1.7f32.round(), 2.0f32);
assert_approx_eq!(0.0f32.round(), 0.0f32);
assert_approx_eq!((-0.0f32).round(), -0.0f32);
assert_approx_eq!((-1.0f32).round(), -1.0f32);
assert_approx_eq!((-1.3f32).round(), -1.0f32);
assert_approx_eq!((-1.5f32).round(), -2.0f32);
assert_approx_eq!((-1.7f32).round(), -2.0f32);
}
#[test]
fn test_trunc() {
assert_fuzzy_eq!(1.0f32.trunc(), 1.0f32);
assert_fuzzy_eq!(1.3f32.trunc(), 1.0f32);
assert_fuzzy_eq!(1.5f32.trunc(), 1.0f32);
assert_fuzzy_eq!(1.7f32.trunc(), 1.0f32);
assert_fuzzy_eq!(0.0f32.trunc(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).trunc(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).trunc(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).trunc(), -1.0f32);
assert_fuzzy_eq!((-1.5f32).trunc(), -1.0f32);
assert_fuzzy_eq!((-1.7f32).trunc(), -1.0f32);
assert_approx_eq!(1.0f32.trunc(), 1.0f32);
assert_approx_eq!(1.3f32.trunc(), 1.0f32);
assert_approx_eq!(1.5f32.trunc(), 1.0f32);
assert_approx_eq!(1.7f32.trunc(), 1.0f32);
assert_approx_eq!(0.0f32.trunc(), 0.0f32);
assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
}
#[test]
fn test_fract() {
assert_fuzzy_eq!(1.0f32.fract(), 0.0f32);
assert_fuzzy_eq!(1.3f32.fract(), 0.3f32);
assert_fuzzy_eq!(1.5f32.fract(), 0.5f32);
assert_fuzzy_eq!(1.7f32.fract(), 0.7f32);
assert_fuzzy_eq!(0.0f32.fract(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).fract(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).fract(), -0.0f32);
assert_fuzzy_eq!((-1.3f32).fract(), -0.3f32);
assert_fuzzy_eq!((-1.5f32).fract(), -0.5f32);
assert_fuzzy_eq!((-1.7f32).fract(), -0.7f32);
assert_approx_eq!(1.0f32.fract(), 0.0f32);
assert_approx_eq!(1.3f32.fract(), 0.3f32);
assert_approx_eq!(1.5f32.fract(), 0.5f32);
assert_approx_eq!(1.7f32.fract(), 0.7f32);
assert_approx_eq!(0.0f32.fract(), 0.0f32);
assert_approx_eq!((-0.0f32).fract(), -0.0f32);
assert_approx_eq!((-1.0f32).fract(), -0.0f32);
assert_approx_eq!((-1.3f32).fract(), -0.3f32);
assert_approx_eq!((-1.5f32).fract(), -0.5f32);
assert_approx_eq!((-1.7f32).fract(), -0.7f32);
}
#[test]
fn test_real_consts() {
assert_fuzzy_eq!(Real::two_pi::<f32>(), 2f32 * Real::pi::<f32>());
assert_fuzzy_eq!(Real::frac_pi_2::<f32>(), Real::pi::<f32>() / 2f32);
assert_fuzzy_eq!(Real::frac_pi_3::<f32>(), Real::pi::<f32>() / 3f32);
assert_fuzzy_eq!(Real::frac_pi_4::<f32>(), Real::pi::<f32>() / 4f32);
assert_fuzzy_eq!(Real::frac_pi_6::<f32>(), Real::pi::<f32>() / 6f32);
assert_fuzzy_eq!(Real::frac_pi_8::<f32>(), Real::pi::<f32>() / 8f32);
assert_fuzzy_eq!(Real::frac_1_pi::<f32>(), 1f32 / Real::pi::<f32>());
assert_fuzzy_eq!(Real::frac_2_pi::<f32>(), 2f32 / Real::pi::<f32>());
assert_fuzzy_eq!(Real::frac_2_sqrtpi::<f32>(), 2f32 / Real::pi::<f32>().sqrt());
assert_fuzzy_eq!(Real::sqrt2::<f32>(), 2f32.sqrt());
assert_fuzzy_eq!(Real::frac_1_sqrt2::<f32>(), 1f32 / 2f32.sqrt());
assert_fuzzy_eq!(Real::log2_e::<f32>(), Real::e::<f32>().log2());
assert_fuzzy_eq!(Real::log10_e::<f32>(), Real::e::<f32>().log10());
assert_fuzzy_eq!(Real::log_2::<f32>(), 2f32.log());
assert_fuzzy_eq!(Real::log_10::<f32>(), 10f32.log());
assert_approx_eq!(Real::two_pi::<f32>(), 2f32 * Real::pi::<f32>());
assert_approx_eq!(Real::frac_pi_2::<f32>(), Real::pi::<f32>() / 2f32);
assert_approx_eq!(Real::frac_pi_3::<f32>(), Real::pi::<f32>() / 3f32);
assert_approx_eq!(Real::frac_pi_4::<f32>(), Real::pi::<f32>() / 4f32);
assert_approx_eq!(Real::frac_pi_6::<f32>(), Real::pi::<f32>() / 6f32);
assert_approx_eq!(Real::frac_pi_8::<f32>(), Real::pi::<f32>() / 8f32);
assert_approx_eq!(Real::frac_1_pi::<f32>(), 1f32 / Real::pi::<f32>());
assert_approx_eq!(Real::frac_2_pi::<f32>(), 2f32 / Real::pi::<f32>());
assert_approx_eq!(Real::frac_2_sqrtpi::<f32>(), 2f32 / Real::pi::<f32>().sqrt());
assert_approx_eq!(Real::sqrt2::<f32>(), 2f32.sqrt());
assert_approx_eq!(Real::frac_1_sqrt2::<f32>(), 1f32 / 2f32.sqrt());
assert_approx_eq!(Real::log2_e::<f32>(), Real::e::<f32>().log2());
assert_approx_eq!(Real::log10_e::<f32>(), Real::e::<f32>().log10());
assert_approx_eq!(Real::log_2::<f32>(), 2f32.log());
assert_approx_eq!(Real::log_10::<f32>(), 10f32.log());
}
#[test]

View File

@ -869,16 +869,6 @@ mod tests {
use super::*;
use prelude::*;
macro_rules! assert_fuzzy_eq(
($a:expr, $b:expr) => ({
let a = $a, b = $b;
if !((a - b).abs() < 1.0e-6) {
fail!(fmt!("The values were not approximately equal. \
Found: %? and expected %?", a, b));
}
})
)
#[test]
fn test_num() {
num::test_num(10f64, 2f64);
@ -912,91 +902,91 @@ mod tests {
#[test]
fn test_floor() {
assert_fuzzy_eq!(1.0f64.floor(), 1.0f64);
assert_fuzzy_eq!(1.3f64.floor(), 1.0f64);
assert_fuzzy_eq!(1.5f64.floor(), 1.0f64);
assert_fuzzy_eq!(1.7f64.floor(), 1.0f64);
assert_fuzzy_eq!(0.0f64.floor(), 0.0f64);
assert_fuzzy_eq!((-0.0f64).floor(), -0.0f64);
assert_fuzzy_eq!((-1.0f64).floor(), -1.0f64);
assert_fuzzy_eq!((-1.3f64).floor(), -2.0f64);
assert_fuzzy_eq!((-1.5f64).floor(), -2.0f64);
assert_fuzzy_eq!((-1.7f64).floor(), -2.0f64);
assert_approx_eq!(1.0f64.floor(), 1.0f64);
assert_approx_eq!(1.3f64.floor(), 1.0f64);
assert_approx_eq!(1.5f64.floor(), 1.0f64);
assert_approx_eq!(1.7f64.floor(), 1.0f64);
assert_approx_eq!(0.0f64.floor(), 0.0f64);
assert_approx_eq!((-0.0f64).floor(), -0.0f64);
assert_approx_eq!((-1.0f64).floor(), -1.0f64);
assert_approx_eq!((-1.3f64).floor(), -2.0f64);
assert_approx_eq!((-1.5f64).floor(), -2.0f64);
assert_approx_eq!((-1.7f64).floor(), -2.0f64);
}
#[test]
fn test_ceil() {
assert_fuzzy_eq!(1.0f64.ceil(), 1.0f64);
assert_fuzzy_eq!(1.3f64.ceil(), 2.0f64);
assert_fuzzy_eq!(1.5f64.ceil(), 2.0f64);
assert_fuzzy_eq!(1.7f64.ceil(), 2.0f64);
assert_fuzzy_eq!(0.0f64.ceil(), 0.0f64);
assert_fuzzy_eq!((-0.0f64).ceil(), -0.0f64);
assert_fuzzy_eq!((-1.0f64).ceil(), -1.0f64);
assert_fuzzy_eq!((-1.3f64).ceil(), -1.0f64);
assert_fuzzy_eq!((-1.5f64).ceil(), -1.0f64);
assert_fuzzy_eq!((-1.7f64).ceil(), -1.0f64);
assert_approx_eq!(1.0f64.ceil(), 1.0f64);
assert_approx_eq!(1.3f64.ceil(), 2.0f64);
assert_approx_eq!(1.5f64.ceil(), 2.0f64);
assert_approx_eq!(1.7f64.ceil(), 2.0f64);
assert_approx_eq!(0.0f64.ceil(), 0.0f64);
assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
}
#[test]
fn test_round() {
assert_fuzzy_eq!(1.0f64.round(), 1.0f64);
assert_fuzzy_eq!(1.3f64.round(), 1.0f64);
assert_fuzzy_eq!(1.5f64.round(), 2.0f64);
assert_fuzzy_eq!(1.7f64.round(), 2.0f64);
assert_fuzzy_eq!(0.0f64.round(), 0.0f64);
assert_fuzzy_eq!((-0.0f64).round(), -0.0f64);
assert_fuzzy_eq!((-1.0f64).round(), -1.0f64);
assert_fuzzy_eq!((-1.3f64).round(), -1.0f64);
assert_fuzzy_eq!((-1.5f64).round(), -2.0f64);
assert_fuzzy_eq!((-1.7f64).round(), -2.0f64);
assert_approx_eq!(1.0f64.round(), 1.0f64);
assert_approx_eq!(1.3f64.round(), 1.0f64);
assert_approx_eq!(1.5f64.round(), 2.0f64);
assert_approx_eq!(1.7f64.round(), 2.0f64);
assert_approx_eq!(0.0f64.round(), 0.0f64);
assert_approx_eq!((-0.0f64).round(), -0.0f64);
assert_approx_eq!((-1.0f64).round(), -1.0f64);
assert_approx_eq!((-1.3f64).round(), -1.0f64);
assert_approx_eq!((-1.5f64).round(), -2.0f64);
assert_approx_eq!((-1.7f64).round(), -2.0f64);
}
#[test]
fn test_trunc() {
assert_fuzzy_eq!(1.0f64.trunc(), 1.0f64);
assert_fuzzy_eq!(1.3f64.trunc(), 1.0f64);
assert_fuzzy_eq!(1.5f64.trunc(), 1.0f64);
assert_fuzzy_eq!(1.7f64.trunc(), 1.0f64);
assert_fuzzy_eq!(0.0f64.trunc(), 0.0f64);
assert_fuzzy_eq!((-0.0f64).trunc(), -0.0f64);
assert_fuzzy_eq!((-1.0f64).trunc(), -1.0f64);
assert_fuzzy_eq!((-1.3f64).trunc(), -1.0f64);
assert_fuzzy_eq!((-1.5f64).trunc(), -1.0f64);
assert_fuzzy_eq!((-1.7f64).trunc(), -1.0f64);
assert_approx_eq!(1.0f64.trunc(), 1.0f64);
assert_approx_eq!(1.3f64.trunc(), 1.0f64);
assert_approx_eq!(1.5f64.trunc(), 1.0f64);
assert_approx_eq!(1.7f64.trunc(), 1.0f64);
assert_approx_eq!(0.0f64.trunc(), 0.0f64);
assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
}
#[test]
fn test_fract() {
assert_fuzzy_eq!(1.0f64.fract(), 0.0f64);
assert_fuzzy_eq!(1.3f64.fract(), 0.3f64);
assert_fuzzy_eq!(1.5f64.fract(), 0.5f64);
assert_fuzzy_eq!(1.7f64.fract(), 0.7f64);
assert_fuzzy_eq!(0.0f64.fract(), 0.0f64);
assert_fuzzy_eq!((-0.0f64).fract(), -0.0f64);
assert_fuzzy_eq!((-1.0f64).fract(), -0.0f64);
assert_fuzzy_eq!((-1.3f64).fract(), -0.3f64);
assert_fuzzy_eq!((-1.5f64).fract(), -0.5f64);
assert_fuzzy_eq!((-1.7f64).fract(), -0.7f64);
assert_approx_eq!(1.0f64.fract(), 0.0f64);
assert_approx_eq!(1.3f64.fract(), 0.3f64);
assert_approx_eq!(1.5f64.fract(), 0.5f64);
assert_approx_eq!(1.7f64.fract(), 0.7f64);
assert_approx_eq!(0.0f64.fract(), 0.0f64);
assert_approx_eq!((-0.0f64).fract(), -0.0f64);
assert_approx_eq!((-1.0f64).fract(), -0.0f64);
assert_approx_eq!((-1.3f64).fract(), -0.3f64);
assert_approx_eq!((-1.5f64).fract(), -0.5f64);
assert_approx_eq!((-1.7f64).fract(), -0.7f64);
}
#[test]
fn test_real_consts() {
assert_fuzzy_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());
assert_fuzzy_eq!(Real::frac_pi_2::<f64>(), Real::pi::<f64>() / 2f64);
assert_fuzzy_eq!(Real::frac_pi_3::<f64>(), Real::pi::<f64>() / 3f64);
assert_fuzzy_eq!(Real::frac_pi_4::<f64>(), Real::pi::<f64>() / 4f64);
assert_fuzzy_eq!(Real::frac_pi_6::<f64>(), Real::pi::<f64>() / 6f64);
assert_fuzzy_eq!(Real::frac_pi_8::<f64>(), Real::pi::<f64>() / 8f64);
assert_fuzzy_eq!(Real::frac_1_pi::<f64>(), 1f64 / Real::pi::<f64>());
assert_fuzzy_eq!(Real::frac_2_pi::<f64>(), 2f64 / Real::pi::<f64>());
assert_fuzzy_eq!(Real::frac_2_sqrtpi::<f64>(), 2f64 / Real::pi::<f64>().sqrt());
assert_fuzzy_eq!(Real::sqrt2::<f64>(), 2f64.sqrt());
assert_fuzzy_eq!(Real::frac_1_sqrt2::<f64>(), 1f64 / 2f64.sqrt());
assert_fuzzy_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
assert_fuzzy_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
assert_fuzzy_eq!(Real::log_2::<f64>(), 2f64.log());
assert_fuzzy_eq!(Real::log_10::<f64>(), 10f64.log());
assert_approx_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());
assert_approx_eq!(Real::frac_pi_2::<f64>(), Real::pi::<f64>() / 2f64);
assert_approx_eq!(Real::frac_pi_3::<f64>(), Real::pi::<f64>() / 3f64);
assert_approx_eq!(Real::frac_pi_4::<f64>(), Real::pi::<f64>() / 4f64);
assert_approx_eq!(Real::frac_pi_6::<f64>(), Real::pi::<f64>() / 6f64);
assert_approx_eq!(Real::frac_pi_8::<f64>(), Real::pi::<f64>() / 8f64);
assert_approx_eq!(Real::frac_1_pi::<f64>(), 1f64 / Real::pi::<f64>());
assert_approx_eq!(Real::frac_2_pi::<f64>(), 2f64 / Real::pi::<f64>());
assert_approx_eq!(Real::frac_2_sqrtpi::<f64>(), 2f64 / Real::pi::<f64>().sqrt());
assert_approx_eq!(Real::sqrt2::<f64>(), 2f64.sqrt());
assert_approx_eq!(Real::frac_1_sqrt2::<f64>(), 1f64 / 2f64.sqrt());
assert_approx_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
assert_approx_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
assert_approx_eq!(Real::log_2::<f64>(), 2f64.log());
assert_approx_eq!(Real::log_10::<f64>(), 10f64.log());
}
#[test]

View File

@ -837,15 +837,6 @@ mod tests {
use super::*;
use prelude::*;
macro_rules! assert_fuzzy_eq(
($a:expr, $b:expr) => ({
let a = $a, b = $b;
if !((a - b).abs() < 1.0e-6) {
fail!(fmt!("The values were not approximately equal. Found: %? and %?", a, b));
}
})
)
#[test]
fn test_num() {
num::test_num(10f, 2f);
@ -875,91 +866,91 @@ mod tests {
#[test]
fn test_floor() {
assert_fuzzy_eq!(1.0f.floor(), 1.0f);
assert_fuzzy_eq!(1.3f.floor(), 1.0f);
assert_fuzzy_eq!(1.5f.floor(), 1.0f);
assert_fuzzy_eq!(1.7f.floor(), 1.0f);
assert_fuzzy_eq!(0.0f.floor(), 0.0f);
assert_fuzzy_eq!((-0.0f).floor(), -0.0f);
assert_fuzzy_eq!((-1.0f).floor(), -1.0f);
assert_fuzzy_eq!((-1.3f).floor(), -2.0f);
assert_fuzzy_eq!((-1.5f).floor(), -2.0f);
assert_fuzzy_eq!((-1.7f).floor(), -2.0f);
assert_approx_eq!(1.0f.floor(), 1.0f);
assert_approx_eq!(1.3f.floor(), 1.0f);
assert_approx_eq!(1.5f.floor(), 1.0f);
assert_approx_eq!(1.7f.floor(), 1.0f);
assert_approx_eq!(0.0f.floor(), 0.0f);
assert_approx_eq!((-0.0f).floor(), -0.0f);
assert_approx_eq!((-1.0f).floor(), -1.0f);
assert_approx_eq!((-1.3f).floor(), -2.0f);
assert_approx_eq!((-1.5f).floor(), -2.0f);
assert_approx_eq!((-1.7f).floor(), -2.0f);
}
#[test]
fn test_ceil() {
assert_fuzzy_eq!(1.0f.ceil(), 1.0f);
assert_fuzzy_eq!(1.3f.ceil(), 2.0f);
assert_fuzzy_eq!(1.5f.ceil(), 2.0f);
assert_fuzzy_eq!(1.7f.ceil(), 2.0f);
assert_fuzzy_eq!(0.0f.ceil(), 0.0f);
assert_fuzzy_eq!((-0.0f).ceil(), -0.0f);
assert_fuzzy_eq!((-1.0f).ceil(), -1.0f);
assert_fuzzy_eq!((-1.3f).ceil(), -1.0f);
assert_fuzzy_eq!((-1.5f).ceil(), -1.0f);
assert_fuzzy_eq!((-1.7f).ceil(), -1.0f);
assert_approx_eq!(1.0f.ceil(), 1.0f);
assert_approx_eq!(1.3f.ceil(), 2.0f);
assert_approx_eq!(1.5f.ceil(), 2.0f);
assert_approx_eq!(1.7f.ceil(), 2.0f);
assert_approx_eq!(0.0f.ceil(), 0.0f);
assert_approx_eq!((-0.0f).ceil(), -0.0f);
assert_approx_eq!((-1.0f).ceil(), -1.0f);
assert_approx_eq!((-1.3f).ceil(), -1.0f);
assert_approx_eq!((-1.5f).ceil(), -1.0f);
assert_approx_eq!((-1.7f).ceil(), -1.0f);
}
#[test]
fn test_round() {
assert_fuzzy_eq!(1.0f.round(), 1.0f);
assert_fuzzy_eq!(1.3f.round(), 1.0f);
assert_fuzzy_eq!(1.5f.round(), 2.0f);
assert_fuzzy_eq!(1.7f.round(), 2.0f);
assert_fuzzy_eq!(0.0f.round(), 0.0f);
assert_fuzzy_eq!((-0.0f).round(), -0.0f);
assert_fuzzy_eq!((-1.0f).round(), -1.0f);
assert_fuzzy_eq!((-1.3f).round(), -1.0f);
assert_fuzzy_eq!((-1.5f).round(), -2.0f);
assert_fuzzy_eq!((-1.7f).round(), -2.0f);
assert_approx_eq!(1.0f.round(), 1.0f);
assert_approx_eq!(1.3f.round(), 1.0f);
assert_approx_eq!(1.5f.round(), 2.0f);
assert_approx_eq!(1.7f.round(), 2.0f);
assert_approx_eq!(0.0f.round(), 0.0f);
assert_approx_eq!((-0.0f).round(), -0.0f);
assert_approx_eq!((-1.0f).round(), -1.0f);
assert_approx_eq!((-1.3f).round(), -1.0f);
assert_approx_eq!((-1.5f).round(), -2.0f);
assert_approx_eq!((-1.7f).round(), -2.0f);
}
#[test]
fn test_trunc() {
assert_fuzzy_eq!(1.0f.trunc(), 1.0f);
assert_fuzzy_eq!(1.3f.trunc(), 1.0f);
assert_fuzzy_eq!(1.5f.trunc(), 1.0f);
assert_fuzzy_eq!(1.7f.trunc(), 1.0f);
assert_fuzzy_eq!(0.0f.trunc(), 0.0f);
assert_fuzzy_eq!((-0.0f).trunc(), -0.0f);
assert_fuzzy_eq!((-1.0f).trunc(), -1.0f);
assert_fuzzy_eq!((-1.3f).trunc(), -1.0f);
assert_fuzzy_eq!((-1.5f).trunc(), -1.0f);
assert_fuzzy_eq!((-1.7f).trunc(), -1.0f);
assert_approx_eq!(1.0f.trunc(), 1.0f);
assert_approx_eq!(1.3f.trunc(), 1.0f);
assert_approx_eq!(1.5f.trunc(), 1.0f);
assert_approx_eq!(1.7f.trunc(), 1.0f);
assert_approx_eq!(0.0f.trunc(), 0.0f);
assert_approx_eq!((-0.0f).trunc(), -0.0f);
assert_approx_eq!((-1.0f).trunc(), -1.0f);
assert_approx_eq!((-1.3f).trunc(), -1.0f);
assert_approx_eq!((-1.5f).trunc(), -1.0f);
assert_approx_eq!((-1.7f).trunc(), -1.0f);
}
#[test]
fn test_fract() {
assert_fuzzy_eq!(1.0f.fract(), 0.0f);
assert_fuzzy_eq!(1.3f.fract(), 0.3f);
assert_fuzzy_eq!(1.5f.fract(), 0.5f);
assert_fuzzy_eq!(1.7f.fract(), 0.7f);
assert_fuzzy_eq!(0.0f.fract(), 0.0f);
assert_fuzzy_eq!((-0.0f).fract(), -0.0f);
assert_fuzzy_eq!((-1.0f).fract(), -0.0f);
assert_fuzzy_eq!((-1.3f).fract(), -0.3f);
assert_fuzzy_eq!((-1.5f).fract(), -0.5f);
assert_fuzzy_eq!((-1.7f).fract(), -0.7f);
assert_approx_eq!(1.0f.fract(), 0.0f);
assert_approx_eq!(1.3f.fract(), 0.3f);
assert_approx_eq!(1.5f.fract(), 0.5f);
assert_approx_eq!(1.7f.fract(), 0.7f);
assert_approx_eq!(0.0f.fract(), 0.0f);
assert_approx_eq!((-0.0f).fract(), -0.0f);
assert_approx_eq!((-1.0f).fract(), -0.0f);
assert_approx_eq!((-1.3f).fract(), -0.3f);
assert_approx_eq!((-1.5f).fract(), -0.5f);
assert_approx_eq!((-1.7f).fract(), -0.7f);
}
#[test]
fn test_real_consts() {
assert_fuzzy_eq!(Real::two_pi::<float>(), 2f * Real::pi::<float>());
assert_fuzzy_eq!(Real::frac_pi_2::<float>(), Real::pi::<float>() / 2f);
assert_fuzzy_eq!(Real::frac_pi_3::<float>(), Real::pi::<float>() / 3f);
assert_fuzzy_eq!(Real::frac_pi_4::<float>(), Real::pi::<float>() / 4f);
assert_fuzzy_eq!(Real::frac_pi_6::<float>(), Real::pi::<float>() / 6f);
assert_fuzzy_eq!(Real::frac_pi_8::<float>(), Real::pi::<float>() / 8f);
assert_fuzzy_eq!(Real::frac_1_pi::<float>(), 1f / Real::pi::<float>());
assert_fuzzy_eq!(Real::frac_2_pi::<float>(), 2f / Real::pi::<float>());
assert_fuzzy_eq!(Real::frac_2_sqrtpi::<float>(), 2f / Real::pi::<float>().sqrt());
assert_fuzzy_eq!(Real::sqrt2::<float>(), 2f.sqrt());
assert_fuzzy_eq!(Real::frac_1_sqrt2::<float>(), 1f / 2f.sqrt());
assert_fuzzy_eq!(Real::log2_e::<float>(), Real::e::<float>().log2());
assert_fuzzy_eq!(Real::log10_e::<float>(), Real::e::<float>().log10());
assert_fuzzy_eq!(Real::log_2::<float>(), 2f.log());
assert_fuzzy_eq!(Real::log_10::<float>(), 10f.log());
assert_approx_eq!(Real::two_pi::<float>(), 2f * Real::pi::<float>());
assert_approx_eq!(Real::frac_pi_2::<float>(), Real::pi::<float>() / 2f);
assert_approx_eq!(Real::frac_pi_3::<float>(), Real::pi::<float>() / 3f);
assert_approx_eq!(Real::frac_pi_4::<float>(), Real::pi::<float>() / 4f);
assert_approx_eq!(Real::frac_pi_6::<float>(), Real::pi::<float>() / 6f);
assert_approx_eq!(Real::frac_pi_8::<float>(), Real::pi::<float>() / 8f);
assert_approx_eq!(Real::frac_1_pi::<float>(), 1f / Real::pi::<float>());
assert_approx_eq!(Real::frac_2_pi::<float>(), 2f / Real::pi::<float>());
assert_approx_eq!(Real::frac_2_sqrtpi::<float>(), 2f / Real::pi::<float>().sqrt());
assert_approx_eq!(Real::sqrt2::<float>(), 2f.sqrt());
assert_approx_eq!(Real::frac_1_sqrt2::<float>(), 1f / 2f.sqrt());
assert_approx_eq!(Real::log2_e::<float>(), Real::e::<float>().log2());
assert_approx_eq!(Real::log10_e::<float>(), Real::e::<float>().log10());
assert_approx_eq!(Real::log_2::<float>(), 2f.log());
assert_approx_eq!(Real::log_10::<float>(), 10f.log());
}
#[test]

View File

@ -483,6 +483,42 @@ pub fn core_macros() -> ~str {
)
)
macro_rules! assert_approx_eq (
($given:expr , $expected:expr) => (
{
use core::cmp::ApproxEq;
let given_val = $given;
let expected_val = $expected;
// check both directions of equality....
if !(
given_val.approx_eq(&expected_val) &&
expected_val.approx_eq(&given_val)
) {
fail!(\"left: %? does not approximately equal right: %?\",
given_val, expected_val);
}
}
);
($given:expr , $expected:expr , $epsilon:expr) => (
{
use core::cmp::ApproxEq;
let given_val = $given;
let expected_val = $expected;
let epsilon_val = $epsilon;
// check both directions of equality....
if !(
given_val.approx_eq_eps(&expected_val, &epsilon_val) &&
expected_val.approx_eq_eps(&given_val, &epsilon_val)
) {
fail!(\"left: %? does not approximately equal right: %? with epsilon: %?\",
given_val, expected_val, epsilon_val);
}
}
)
)
macro_rules! condition (
{ $c:ident: $in:ty -> $out:ty; } => {

View File

@ -0,0 +1,14 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// error-pattern:left: 1.0000001 does not approximately equal right: 1 with epsilon: 0.0000001
pub fn main() {
assert_approx_eq!(1.0000001f, 1.0f, 1.0e-7);
}

View File

@ -0,0 +1,14 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// error-pattern:left: 1.00001 does not approximately equal right: 1
pub fn main() {
assert_approx_eq!(1.00001f, 1.0f);
}

View File

@ -0,0 +1,16 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
pub fn main() {
assert_approx_eq!(1.0f, 1.0f);
assert_approx_eq!(1.0000001f, 1.0f);
assert_approx_eq!(1.0000001f, 1.0f, 1.0e-6);
assert_approx_eq!(1.000001f, 1.0f, 1.0e-5);
}