diff --git a/src/macros/assert.rs b/src/macros/assert.rs index 46976693..c791cfc4 100644 --- a/src/macros/assert.rs +++ b/src/macros/assert.rs @@ -27,17 +27,3 @@ macro_rules! assert_approx_eq_ulps( } }) ); - -/// Asserts approximate equality of two values with the `ApproxEq` trait. -#[macro_export] -macro_rules! assert_approx_eq( - ($given: expr, $expected: expr) => ({ - let (given_val, expected_val) = (&($given), &($expected)); - if !ApproxEq::approx_eq(given_val, expected_val) { - panic!("assertion failed: `left ≈ right` (left: `{:?}`, right: `{:?}`, tolerance: `{:?}`)", - *given_val, *expected_val, - ApproxEq::approx_epsilon(Some(*given_val)) - ) - } - }) -); diff --git a/src/structs/dmatrix_macros.rs b/src/structs/dmatrix_macros.rs index 80e99761..346d40f2 100644 --- a/src/structs/dmatrix_macros.rs +++ b/src/structs/dmatrix_macros.rs @@ -873,13 +873,13 @@ macro_rules! dmat_impl( impl> ApproxEq for $dmatrix { #[inline] - fn approx_epsilon(_: Option<$dmatrix>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$dmatrix>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/isometry_macros.rs b/src/structs/isometry_macros.rs index 14cfb5fd..6000c01a 100644 --- a/src/structs/isometry_macros.rs +++ b/src/structs/isometry_macros.rs @@ -378,13 +378,13 @@ macro_rules! isometry_impl( */ impl> ApproxEq for $t { #[inline] - fn approx_epsilon(_: Option<$t>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$t>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/matrix_macros.rs b/src/structs/matrix_macros.rs index acfeb2cf..4f3d12ee 100644 --- a/src/structs/matrix_macros.rs +++ b/src/structs/matrix_macros.rs @@ -277,13 +277,13 @@ macro_rules! matrix_impl( */ impl> ApproxEq for $t { #[inline] - fn approx_epsilon(_: Option<$t>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$t>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/quaternion.rs b/src/structs/quaternion.rs index 3d96e51a..1656e13c 100644 --- a/src/structs/quaternion.rs +++ b/src/structs/quaternion.rs @@ -389,13 +389,13 @@ impl Rand for UnitQuaternion { impl> ApproxEq for UnitQuaternion { #[inline] - fn approx_epsilon(_: Option>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/rotation_macros.rs b/src/structs/rotation_macros.rs index dfcdd835..02981503 100644 --- a/src/structs/rotation_macros.rs +++ b/src/structs/rotation_macros.rs @@ -341,13 +341,13 @@ macro_rules! rotation_impl( */ impl> ApproxEq for $t { #[inline] - fn approx_epsilon(_: Option<$t>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$t>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/similarity_macros.rs b/src/structs/similarity_macros.rs index 73e8aaf7..85903d97 100644 --- a/src/structs/similarity_macros.rs +++ b/src/structs/similarity_macros.rs @@ -353,13 +353,13 @@ macro_rules! similarity_impl( */ impl> ApproxEq for $t { #[inline] - fn approx_epsilon(_: Option<$t>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$t>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/vector_macros.rs b/src/structs/vector_macros.rs index 7bd0e931..22987910 100644 --- a/src/structs/vector_macros.rs +++ b/src/structs/vector_macros.rs @@ -34,13 +34,13 @@ macro_rules! vectorlike_impl( */ impl> ApproxEq for $t { #[inline] - fn approx_epsilon(_: Option<$t>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$t>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/structs/vectorn_macros.rs b/src/structs/vectorn_macros.rs index 74749375..05046c1a 100644 --- a/src/structs/vectorn_macros.rs +++ b/src/structs/vectorn_macros.rs @@ -582,13 +582,13 @@ macro_rules! vecn_dvec_common_impl( */ impl $(, $param : ArrayLength)*> ApproxEq for $vecn { #[inline] - fn approx_epsilon(_: Option<$vecn>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } #[inline] - fn approx_ulps(_: Option<$vecn>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } #[inline] diff --git a/src/traits/operations.rs b/src/traits/operations.rs index 7059e30e..8a7a7066 100644 --- a/src/traits/operations.rs +++ b/src/traits/operations.rs @@ -152,13 +152,13 @@ pub trait PartialOrder { /// Trait for testing approximate equality pub trait ApproxEq: Sized { /// Default epsilon for approximation. - fn approx_epsilon(unused_mut: Option) -> Eps; + fn approx_epsilon() -> Eps; /// Tests approximate equality using a custom epsilon. fn approx_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool; /// Default ULPs for approximation. - fn approx_ulps(unused_mut: Option) -> u32; + fn approx_ulps() -> u32; /// Tests approximate equality using units in the last place (ULPs) fn approx_eq_ulps(&self, other: &Self, ulps: u32) -> bool; @@ -166,13 +166,13 @@ pub trait ApproxEq: Sized { /// Tests approximate equality. #[inline] fn approx_eq(&self, other: &Self) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon(None::)) + self.approx_eq_eps(other, &Self::approx_epsilon()) } } impl ApproxEq for f32 { #[inline] - fn approx_epsilon(_: Option) -> f32 { + fn approx_epsilon() -> f32 { 1.0e-6 } @@ -181,7 +181,7 @@ impl ApproxEq for f32 { ::abs(&(*self - *other)) < *epsilon } - fn approx_ulps(_: Option) -> u32 { + fn approx_ulps() -> u32 { 8 } @@ -203,7 +203,7 @@ impl ApproxEq for f32 { impl ApproxEq for f64 { #[inline] - fn approx_epsilon(_: Option) -> f64 { + fn approx_epsilon() -> f64 { 1.0e-6 } @@ -212,7 +212,7 @@ impl ApproxEq for f64 { ::abs(&(*self - *other)) < *approx_epsilon } - fn approx_ulps(_: Option) -> u32 { + fn approx_ulps() -> u32 { 8 } @@ -231,16 +231,16 @@ impl ApproxEq for f64 { } impl<'a, N, T: ApproxEq> ApproxEq for &'a T { - fn approx_epsilon(_: Option<&'a T>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } fn approx_eq_eps(&self, other: &&'a T, approx_epsilon: &N) -> bool { ApproxEq::approx_eq_eps(*self, *other, approx_epsilon) } - fn approx_ulps(_: Option<&'a T>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } fn approx_eq_ulps(&self, other: &&'a T, ulps: u32) -> bool { @@ -249,16 +249,16 @@ impl<'a, N, T: ApproxEq> ApproxEq for &'a T { } impl<'a, N, T: ApproxEq> ApproxEq for &'a mut T { - fn approx_epsilon(_: Option<&'a mut T>) -> N { - ApproxEq::approx_epsilon(None::) + fn approx_epsilon() -> N { + >::approx_epsilon() } fn approx_eq_eps(&self, other: &&'a mut T, approx_epsilon: &N) -> bool { ApproxEq::approx_eq_eps(*self, *other, approx_epsilon) } - fn approx_ulps(_: Option<&'a mut T>) -> u32 { - ApproxEq::approx_ulps(None::) + fn approx_ulps() -> u32 { + >::approx_ulps() } fn approx_eq_ulps(&self, other: &&'a mut T, ulps: u32) -> bool { diff --git a/tests/assert.rs b/tests/assert.rs index bb538cb3..bd2bb33e 100644 --- a/tests/assert.rs +++ b/tests/assert.rs @@ -4,13 +4,24 @@ extern crate nalgebra; use nalgebra::{ApproxEq, Vector2}; +use std::fmt::Debug; + +// Replace the assert_approx_eq! macro so that we can have type inference. +fn test_approx_eq(given: &T, expected: &T) where T: Debug + ApproxEq, N: Debug { + if !given.approx_eq(expected) { + panic!("assertion failed: `left ≈ right` (left: `{:?}`, right: `{:?}`, tolerance: `{:?}`)", + *given, *expected, + T::approx_epsilon() + ) + } +} #[test] fn assert_approx_eq_f64() { let a = 1.0f64; let b = 1.0f64 + 1.0e-12f64; - assert_approx_eq!(a, b); - assert_approx_eq!(&a, &b); + test_approx_eq(&a, &b); + test_approx_eq(&(&a), &(&b)); } #[test] @@ -18,7 +29,7 @@ fn assert_approx_eq_f64() { fn assert_approx_eq_vec2_f32_fail() { let a = Vector2::new(1.0f32, 0.0); let b = Vector2::new(1.1f32, 0.1); - assert_approx_eq!(a, b); + test_approx_eq(&a, &b); } #[test]