Skip to content

Remove ApproxEq and assert_approx_eq! #11402

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

Merged
merged 1 commit into from
Jan 9, 2014
Merged
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
3 changes: 1 addition & 2 deletions doc/guide-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ value. To run the tests in a crate, it must be compiled with the
the resulting executable will run all the tests in the crate. A test
is considered successful if its function returns; if the task running
the test fails, through a call to `fail!`, a failed `check` or
`assert`, or some other (`assert_eq`, `assert_approx_eq`, ...) means,
then the test fails.
`assert`, or some other (`assert_eq`, ...) means, then the test fails.

When compiling a crate with the '--test' flag '--cfg test' is also
implied, so that tests can be conditionally compiled.
Expand Down
2 changes: 1 addition & 1 deletion src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ syn keyword rustTrait Bool
syn keyword rustTrait ToCStr
syn keyword rustTrait Char
syn keyword rustTrait Clone DeepClone
syn keyword rustTrait Eq ApproxEq Ord TotalEq TotalOrd Ordering Equiv
syn keyword rustTrait Eq Ord TotalEq TotalOrd Ordering Equiv
syn keyword rustEnumVariant Less Equal Greater
syn keyword rustTrait Container Mutable Map MutableMap Set MutableSet
syn keyword rustTrait Default
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/num/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ mod test {
#[test]
fn test_arg() {
fn test(c: Complex64, arg: f64) {
assert!(c.arg().approx_eq(&arg))
assert!((c.arg() - arg).abs() < 1.0e-6)
}
test(_1_0i, 0.0);
test(_1_1i, 0.25 * Real::pi());
Expand Down
8 changes: 8 additions & 0 deletions src/libextra/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,14 @@ mod tests {
use std::io;
use std::str;

macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);
})
)

fn check(samples: &[f64], summ: &Summary) {

let summ2 = Summary::new(samples);
Expand Down
7 changes: 0 additions & 7 deletions src/libstd/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ totaleq_impl!(uint)

totaleq_impl!(char)

/// Trait for testing approximate equality
pub trait ApproxEq<Eps> {
fn approx_epsilon() -> Eps;
fn approx_eq(&self, other: &Self) -> bool;
fn approx_eq_eps(&self, other: &Self, approx_epsilon: &Eps) -> bool;
}

#[deriving(Clone, Eq)]
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }

Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub mod prelude;

/* Primitive types */

#[path = "num/float_macros.rs"] mod float_macros;
#[path = "num/int_macros.rs"] mod int_macros;
#[path = "num/uint_macros.rs"] mod uint_macros;

Expand Down
25 changes: 0 additions & 25 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,6 @@ impl Eq for f32 {
fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
}

#[cfg(not(test))]
impl ApproxEq<f32> for f32 {
#[inline]
fn approx_epsilon() -> f32 { 1.0e-6 }

#[inline]
fn approx_eq(&self, other: &f32) -> bool {
self.approx_eq_eps(other, &1.0e-6)
}

#[inline]
fn approx_eq_eps(&self, other: &f32, approx_epsilon: &f32) -> bool {
(*self - *other).abs() < *approx_epsilon
}
}

#[cfg(not(test))]
impl Ord for f32 {
#[inline]
Expand Down Expand Up @@ -1195,15 +1179,6 @@ mod tests {
assert!(!NAN.is_negative());
}

#[test]
fn test_approx_eq() {
assert!(1.0f32.approx_eq(&1f32));
assert!(0.9999999f32.approx_eq(&1f32));
assert!(1.000001f32.approx_eq_eps(&1f32, &1.0e-5));
assert!(1.0000001f32.approx_eq_eps(&1f32, &1.0e-6));
assert!(!1.0000001f32.approx_eq_eps(&1f32, &1.0e-7));
}

#[test]
fn test_primitive() {
let none: Option<f32> = None;
Expand Down
25 changes: 0 additions & 25 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,6 @@ impl Eq for f64 {
fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
}

#[cfg(not(test))]
impl ApproxEq<f64> for f64 {
#[inline]
fn approx_epsilon() -> f64 { 1.0e-6 }

#[inline]
fn approx_eq(&self, other: &f64) -> bool {
self.approx_eq_eps(other, &1.0e-6)
}

#[inline]
fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
(*self - *other).abs() < *approx_epsilon
}
}

#[cfg(not(test))]
impl Ord for f64 {
#[inline]
Expand Down Expand Up @@ -1246,15 +1230,6 @@ mod tests {
assert!(!NAN.is_negative());
}

#[test]
fn test_approx_eq() {
assert!(1.0f64.approx_eq(&1f64));
assert!(0.9999999f64.approx_eq(&1f64));
assert!(1.000001f64.approx_eq_eps(&1f64, &1.0e-5));
assert!(1.0000001f64.approx_eq_eps(&1f64, &1.0e-6));
assert!(!1.0000001f64.approx_eq_eps(&1f64, &1.0e-7));
}

#[test]
fn test_primitive() {
let none: Option<f64> = None;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -8,7 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern:left: 1.0000001f64 does not approximately equal right: 1f64 with epsilon: 0.0000001f64
pub fn main() {
assert_approx_eq!(1.0000001f64, 1.0f64, 1.0e-7);
}
#[macro_escape];
#[doc(hidden)];

macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);
})
)
59 changes: 3 additions & 56 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#[allow(missing_doc)];

use clone::{Clone, DeepClone};
use cmp::{Eq, ApproxEq, Ord};
use cmp::{Eq, Ord};
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
use option::{Option, Some, None};
Expand Down Expand Up @@ -138,60 +138,19 @@ pub trait Integer: Num
/// A collection of rounding operations.
pub trait Round {
/// Return the largest integer less than or equal to a number.
///
/// # Example
///
/// ```rust
/// assert_approx_eq!(1.3f32.floor(), 1.0);
/// assert_approx_eq!((-1.3f32).floor(), -2.0);
/// ```
fn floor(&self) -> Self;

/// Return the smallest integer greater than or equal to a number.
///
/// # Example
///
/// ```rust
/// assert_approx_eq!(1.3f32.ceil(), 2.0);
/// assert_approx_eq!((-1.3f32).ceil(), -1.0);
/// ```
fn ceil(&self) -> Self;

/// Return the nearest integer to a number. Round half-way cases away from
/// `0.0`.
///
/// # Example
///
/// ```rust
/// assert_approx_eq!(1.3f32.round(), 1.0);
/// assert_approx_eq!((-1.3f32).round(), -1.0);
/// assert_approx_eq!(1.5f32.round(), 2.0);
/// assert_approx_eq!((-1.5f32).round(), -2.0);
/// ```
fn round(&self) -> Self;

/// Return the integer part of a number.
///
/// # Example
///
/// ```rust
/// assert_approx_eq!(1.3f32.trunc(), 1.0);
/// assert_approx_eq!((-1.3f32).trunc(), -1.0);
/// assert_approx_eq!(1.5f32.trunc(), 1.0);
/// assert_approx_eq!((-1.5f32).trunc(), -1.0);
/// ```
fn trunc(&self) -> Self;

/// Return the fractional part of a number.
///
/// # Example
///
/// ```rust
/// assert_approx_eq!(1.3f32.fract(), 0.3);
/// assert_approx_eq!((-1.3f32).fract(), -0.3);
/// assert_approx_eq!(1.5f32.fract(), 0.5);
/// assert_approx_eq!((-1.5f32).fract(), -0.5);
/// ```
fn fract(&self) -> Self;
}

Expand Down Expand Up @@ -262,18 +221,7 @@ pub trait Trigonometric {
fn atan(&self) -> Self;

/// Computes the four quadrant arctangent of a number, `y`, and another
/// number `x`. Return value is in radians in the range [-pi, pi];
///
/// # Example
///
/// ```rust
/// use std::f32;
///
/// let y = 3f32.sqrt();
/// let x = 1f32;
/// assert_approx_eq!(y.atan2(&x), f32::consts::PI / 3f32);
/// assert_approx_eq!((-y).atan2(&(-x)), - 2f32 * f32::consts::PI / 3f32);
/// ```
/// number `x`. Return value is in radians in the range [-pi, pi].
fn atan2(&self, other: &Self) -> Self;

/// Simultaneously computes the sine and cosine of the number, `x`. Returns
Expand Down Expand Up @@ -505,8 +453,7 @@ pub enum FPCategory {
/// Primitive floating point numbers
pub trait Float: Real
+ Signed
+ Primitive
+ ApproxEq<Self> {
+ Primitive {
// FIXME (#5527): These should be associated constants
fn nan() -> Self;
fn infinity() -> Self;
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub use bool::Bool;
pub use c_str::ToCStr;
pub use char::Char;
pub use clone::{Clone, DeepClone};
pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
pub use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
pub use default::Default;
pub use from_str::FromStr;
Expand Down
37 changes: 0 additions & 37 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,43 +741,6 @@ pub fn std_macros() -> @str {
)
)
macro_rules! assert_approx_eq (
($given:expr , $expected:expr) => (
{
use std::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 std::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);
}
}
)
)
/// A utility macro for indicating unreachable code. It will fail if
/// executed. This is occasionally useful to put after loops that never
/// terminate normally, but instead directly return from a function.
Expand Down
14 changes: 0 additions & 14 deletions src/test/run-fail/assert-approx-eq-macro-fail.rs

This file was deleted.

16 changes: 0 additions & 16 deletions src/test/run-pass/assert-approx-eq-macro-success.rs

This file was deleted.

Loading