From 0e2242f6d62d6d49bf8a2d1860a41273c1fdfa0d Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 6 May 2013 21:51:48 +1000 Subject: [PATCH] Add assert_approx_eq! macro --- src/libcore/num/f32.rs | 139 ++++++++--------- src/libcore/num/f64.rs | 140 ++++++++---------- src/libcore/num/float.rs | 139 ++++++++--------- src/libsyntax/ext/expand.rs | 36 +++++ .../assert-approx-eq-eps-macro-fail.rs | 14 ++ .../run-fail/assert-approx-eq-macro-fail.rs | 14 ++ .../assert-approx-eq-macro-success.rs | 16 ++ 7 files changed, 275 insertions(+), 223 deletions(-) create mode 100644 src/test/run-fail/assert-approx-eq-eps-macro-fail.rs create mode 100644 src/test/run-fail/assert-approx-eq-macro-fail.rs create mode 100644 src/test/run-pass/assert-approx-eq-macro-success.rs diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index feeb2c3f83605..7c13f136a80f2 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -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::(), 2f32 * Real::pi::()); - assert_fuzzy_eq!(Real::frac_pi_2::(), Real::pi::() / 2f32); - assert_fuzzy_eq!(Real::frac_pi_3::(), Real::pi::() / 3f32); - assert_fuzzy_eq!(Real::frac_pi_4::(), Real::pi::() / 4f32); - assert_fuzzy_eq!(Real::frac_pi_6::(), Real::pi::() / 6f32); - assert_fuzzy_eq!(Real::frac_pi_8::(), Real::pi::() / 8f32); - assert_fuzzy_eq!(Real::frac_1_pi::(), 1f32 / Real::pi::()); - assert_fuzzy_eq!(Real::frac_2_pi::(), 2f32 / Real::pi::()); - assert_fuzzy_eq!(Real::frac_2_sqrtpi::(), 2f32 / Real::pi::().sqrt()); - assert_fuzzy_eq!(Real::sqrt2::(), 2f32.sqrt()); - assert_fuzzy_eq!(Real::frac_1_sqrt2::(), 1f32 / 2f32.sqrt()); - assert_fuzzy_eq!(Real::log2_e::(), Real::e::().log2()); - assert_fuzzy_eq!(Real::log10_e::(), Real::e::().log10()); - assert_fuzzy_eq!(Real::log_2::(), 2f32.log()); - assert_fuzzy_eq!(Real::log_10::(), 10f32.log()); + assert_approx_eq!(Real::two_pi::(), 2f32 * Real::pi::()); + assert_approx_eq!(Real::frac_pi_2::(), Real::pi::() / 2f32); + assert_approx_eq!(Real::frac_pi_3::(), Real::pi::() / 3f32); + assert_approx_eq!(Real::frac_pi_4::(), Real::pi::() / 4f32); + assert_approx_eq!(Real::frac_pi_6::(), Real::pi::() / 6f32); + assert_approx_eq!(Real::frac_pi_8::(), Real::pi::() / 8f32); + assert_approx_eq!(Real::frac_1_pi::(), 1f32 / Real::pi::()); + assert_approx_eq!(Real::frac_2_pi::(), 2f32 / Real::pi::()); + assert_approx_eq!(Real::frac_2_sqrtpi::(), 2f32 / Real::pi::().sqrt()); + assert_approx_eq!(Real::sqrt2::(), 2f32.sqrt()); + assert_approx_eq!(Real::frac_1_sqrt2::(), 1f32 / 2f32.sqrt()); + assert_approx_eq!(Real::log2_e::(), Real::e::().log2()); + assert_approx_eq!(Real::log10_e::(), Real::e::().log10()); + assert_approx_eq!(Real::log_2::(), 2f32.log()); + assert_approx_eq!(Real::log_10::(), 10f32.log()); } #[test] diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 08f9c341188ad..e5f10c23ecd87 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -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::(), 2.0 * Real::pi::()); - assert_fuzzy_eq!(Real::frac_pi_2::(), Real::pi::() / 2f64); - assert_fuzzy_eq!(Real::frac_pi_3::(), Real::pi::() / 3f64); - assert_fuzzy_eq!(Real::frac_pi_4::(), Real::pi::() / 4f64); - assert_fuzzy_eq!(Real::frac_pi_6::(), Real::pi::() / 6f64); - assert_fuzzy_eq!(Real::frac_pi_8::(), Real::pi::() / 8f64); - assert_fuzzy_eq!(Real::frac_1_pi::(), 1f64 / Real::pi::()); - assert_fuzzy_eq!(Real::frac_2_pi::(), 2f64 / Real::pi::()); - assert_fuzzy_eq!(Real::frac_2_sqrtpi::(), 2f64 / Real::pi::().sqrt()); - assert_fuzzy_eq!(Real::sqrt2::(), 2f64.sqrt()); - assert_fuzzy_eq!(Real::frac_1_sqrt2::(), 1f64 / 2f64.sqrt()); - assert_fuzzy_eq!(Real::log2_e::(), Real::e::().log2()); - assert_fuzzy_eq!(Real::log10_e::(), Real::e::().log10()); - assert_fuzzy_eq!(Real::log_2::(), 2f64.log()); - assert_fuzzy_eq!(Real::log_10::(), 10f64.log()); + assert_approx_eq!(Real::two_pi::(), 2.0 * Real::pi::()); + assert_approx_eq!(Real::frac_pi_2::(), Real::pi::() / 2f64); + assert_approx_eq!(Real::frac_pi_3::(), Real::pi::() / 3f64); + assert_approx_eq!(Real::frac_pi_4::(), Real::pi::() / 4f64); + assert_approx_eq!(Real::frac_pi_6::(), Real::pi::() / 6f64); + assert_approx_eq!(Real::frac_pi_8::(), Real::pi::() / 8f64); + assert_approx_eq!(Real::frac_1_pi::(), 1f64 / Real::pi::()); + assert_approx_eq!(Real::frac_2_pi::(), 2f64 / Real::pi::()); + assert_approx_eq!(Real::frac_2_sqrtpi::(), 2f64 / Real::pi::().sqrt()); + assert_approx_eq!(Real::sqrt2::(), 2f64.sqrt()); + assert_approx_eq!(Real::frac_1_sqrt2::(), 1f64 / 2f64.sqrt()); + assert_approx_eq!(Real::log2_e::(), Real::e::().log2()); + assert_approx_eq!(Real::log10_e::(), Real::e::().log10()); + assert_approx_eq!(Real::log_2::(), 2f64.log()); + assert_approx_eq!(Real::log_10::(), 10f64.log()); } #[test] diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index 61dd741b671b5..a548165326396 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -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::(), 2f * Real::pi::()); - assert_fuzzy_eq!(Real::frac_pi_2::(), Real::pi::() / 2f); - assert_fuzzy_eq!(Real::frac_pi_3::(), Real::pi::() / 3f); - assert_fuzzy_eq!(Real::frac_pi_4::(), Real::pi::() / 4f); - assert_fuzzy_eq!(Real::frac_pi_6::(), Real::pi::() / 6f); - assert_fuzzy_eq!(Real::frac_pi_8::(), Real::pi::() / 8f); - assert_fuzzy_eq!(Real::frac_1_pi::(), 1f / Real::pi::()); - assert_fuzzy_eq!(Real::frac_2_pi::(), 2f / Real::pi::()); - assert_fuzzy_eq!(Real::frac_2_sqrtpi::(), 2f / Real::pi::().sqrt()); - assert_fuzzy_eq!(Real::sqrt2::(), 2f.sqrt()); - assert_fuzzy_eq!(Real::frac_1_sqrt2::(), 1f / 2f.sqrt()); - assert_fuzzy_eq!(Real::log2_e::(), Real::e::().log2()); - assert_fuzzy_eq!(Real::log10_e::(), Real::e::().log10()); - assert_fuzzy_eq!(Real::log_2::(), 2f.log()); - assert_fuzzy_eq!(Real::log_10::(), 10f.log()); + assert_approx_eq!(Real::two_pi::(), 2f * Real::pi::()); + assert_approx_eq!(Real::frac_pi_2::(), Real::pi::() / 2f); + assert_approx_eq!(Real::frac_pi_3::(), Real::pi::() / 3f); + assert_approx_eq!(Real::frac_pi_4::(), Real::pi::() / 4f); + assert_approx_eq!(Real::frac_pi_6::(), Real::pi::() / 6f); + assert_approx_eq!(Real::frac_pi_8::(), Real::pi::() / 8f); + assert_approx_eq!(Real::frac_1_pi::(), 1f / Real::pi::()); + assert_approx_eq!(Real::frac_2_pi::(), 2f / Real::pi::()); + assert_approx_eq!(Real::frac_2_sqrtpi::(), 2f / Real::pi::().sqrt()); + assert_approx_eq!(Real::sqrt2::(), 2f.sqrt()); + assert_approx_eq!(Real::frac_1_sqrt2::(), 1f / 2f.sqrt()); + assert_approx_eq!(Real::log2_e::(), Real::e::().log2()); + assert_approx_eq!(Real::log10_e::(), Real::e::().log10()); + assert_approx_eq!(Real::log_2::(), 2f.log()); + assert_approx_eq!(Real::log_10::(), 10f.log()); } #[test] diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 02721a02fbd0c..9363807ab9b9e 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -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; } => { diff --git a/src/test/run-fail/assert-approx-eq-eps-macro-fail.rs b/src/test/run-fail/assert-approx-eq-eps-macro-fail.rs new file mode 100644 index 0000000000000..c0c20f7af4351 --- /dev/null +++ b/src/test/run-fail/assert-approx-eq-eps-macro-fail.rs @@ -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 or the MIT license +// , 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); +} diff --git a/src/test/run-fail/assert-approx-eq-macro-fail.rs b/src/test/run-fail/assert-approx-eq-macro-fail.rs new file mode 100644 index 0000000000000..43de4f92b63b1 --- /dev/null +++ b/src/test/run-fail/assert-approx-eq-macro-fail.rs @@ -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 or the MIT license +// , 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); +} diff --git a/src/test/run-pass/assert-approx-eq-macro-success.rs b/src/test/run-pass/assert-approx-eq-macro-success.rs new file mode 100644 index 0000000000000..5c7c11ef50343 --- /dev/null +++ b/src/test/run-pass/assert-approx-eq-macro-success.rs @@ -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 or the MIT license +// , 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); +}