Merge pull request #43 from aepsil0n/assert_approx_eq_macros

Implement assertion macros for ApproxEq
This commit is contained in:
Sébastien Crozet 2014-11-23 16:58:56 +01:00
commit 313d6f92d1
4 changed files with 65 additions and 0 deletions

View File

@ -188,6 +188,7 @@ pub use linalg::{
mod structs; mod structs;
mod traits; mod traits;
mod linalg; mod linalg;
mod macros;
// mod lower_triangular; // mod lower_triangular;
// mod chol; // mod chol;

28
src/macros/assert.rs Normal file
View File

@ -0,0 +1,28 @@
/// Asserts approximate equality within a given tolerance of two values with the
/// `ApproxEq` trait.
#[macro_export]
macro_rules! assert_approx_eq_eps(
($given: expr, $expected: expr, $eps: expr) => ({
let eps = &($eps);
let (given_val, expected_val) = (&($given), &($expected));
if !ApproxEq::approx_eq_eps(given_val, expected_val, eps) {
panic!("assertion failed: `left ≈ right` (left: `{}`, right: `{}`, tolerance: `{}`)",
*given_val, *expected_val, *eps
)
}
})
)
/// 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))
)
}
})
)

1
src/macros/mod.rs Normal file
View File

@ -0,0 +1 @@
mod assert;

35
tests/assert.rs Normal file
View File

@ -0,0 +1,35 @@
//! Assertion macro tests
#![feature(phase)]
#[phase(plugin)]
extern crate nalgebra;
extern crate nalgebra;
use nalgebra::{ApproxEq, Vec2};
#[test]
fn assert_approx_eq_f64() {
let a = 1.0f64;
let b = 1.0f64 + 1.0e-12f64;
assert_approx_eq!(a, b);
}
#[test]
#[should_fail]
fn assert_approx_eq_vec2_f32_fail() {
let a = Vec2::new(1.0f32, 0.0);
let b = Vec2::new(1.1f32, 0.1);
assert_approx_eq!(a, b);
}
#[test]
fn assert_approx_eq_eps_f32() {
assert_approx_eq_eps!(1.0f32, 1.1, 0.2);
}
#[test]
#[should_fail]
fn assert_approx_eq_eps_f64_fail() {
assert_approx_eq_eps!(1.0f64, 1.1, 0.05);
}