From 72577ee03ab7617c92a48fe0eac285036c297877 Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Sat, 11 May 2019 04:23:42 +0900 Subject: [PATCH 1/5] Drop commented assertions --- src/assert.rs | 3 --- tests/opnorm.rs | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/assert.rs b/src/assert.rs index 27a6ece4..51195bf0 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -92,9 +92,6 @@ macro_rules! generate_assert { ($test: expr,$truth: expr,$tol: expr) => { $crate::$close($test, $truth, $tol).unwrap(); }; - ($test: expr,$truth: expr,$tol: expr; $comment: expr) => { - $crate::$close($test, $truth, $tol).expect($comment); - }; } }; } // generate_assert! diff --git a/tests/opnorm.rs b/tests/opnorm.rs index 8e52bbd4..834263f0 100644 --- a/tests/opnorm.rs +++ b/tests/opnorm.rs @@ -6,9 +6,9 @@ fn test(a: Array2, one: f64, inf: f64, fro: f64) { println!("ONE = {:?}", a.opnorm_one()); println!("INF = {:?}", a.opnorm_inf()); println!("FRO = {:?}", a.opnorm_fro()); - assert_rclose!(a.opnorm_one().unwrap(), one, 1e-7; "One norm"); - assert_rclose!(a.opnorm_inf().unwrap(), inf, 1e-7; "Infinity norm"); - assert_rclose!(a.opnorm_fro().unwrap(), fro, 1e-7; "Frobenius norm"); + assert_rclose!(a.opnorm_one().unwrap(), one, 1e-7); + assert_rclose!(a.opnorm_inf().unwrap(), inf, 1e-7); + assert_rclose!(a.opnorm_fro().unwrap(), fro, 1e-7); } fn gen(i: usize, j: usize, rev: bool) -> Array2 { From 51f32ee371cc651c661cca272d541c5a221cadfb Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Sat, 11 May 2019 04:28:53 +0900 Subject: [PATCH 2/5] Panic from close_* functions (not as unwrap) --- src/assert.rs | 68 +++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/assert.rs b/src/assert.rs index 51195bf0..4a0bbce6 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -1,87 +1,79 @@ //! Assertions for array use ndarray::*; +use std::fmt::Debug; use super::norm::*; use super::types::*; /// check two values are close in terms of the relative torrence -pub fn rclose(test: A, truth: A, rtol: A::Real) -> Result { +pub fn rclose(test: A, truth: A, rtol: A::Real) { let dev = (test - truth).abs() / truth.abs(); - if dev < rtol { - Ok(dev) - } else { - Err(dev) + if dev > rtol { + eprintln!("Expected = {}", truth); + eprintln!("Actual = {}", test); + panic!("Too large deviation in relative torrence: {}", dev); } } /// check two values are close in terms of the absolute torrence -pub fn aclose(test: A, truth: A, atol: A::Real) -> Result { +pub fn aclose(test: A, truth: A, atol: A::Real) { let dev = (test - truth).abs(); - if dev < atol { - Ok(dev) - } else { - Err(dev) + if dev > atol { + eprintln!("Expected = {}", truth); + eprintln!("Actual = {}", test); + panic!("Too large deviation in absolute torrence: {}", dev); } } /// check two arrays are close in maximum norm -pub fn close_max( - test: &ArrayBase, - truth: &ArrayBase, - atol: A::Real, -) -> Result +pub fn close_max(test: &ArrayBase, truth: &ArrayBase, atol: A::Real) where A: Scalar + Lapack, S1: Data, S2: Data, D: Dimension, + D::Pattern: PartialEq + Debug, { let tol = (test - truth).norm_max(); - if tol < atol { - Ok(tol) - } else { - Err(tol) + if tol > atol { + eprintln!("Expected:\n{}", truth); + eprintln!("Actual:\n{}", test); + panic!("Too large deviation in maximum norm: {}", tol); } } /// check two arrays are close in L1 norm -pub fn close_l1( - test: &ArrayBase, - truth: &ArrayBase, - rtol: A::Real, -) -> Result +pub fn close_l1(test: &ArrayBase, truth: &ArrayBase, rtol: A::Real) where A: Scalar + Lapack, S1: Data, S2: Data, D: Dimension, + D::Pattern: PartialEq + Debug, { let tol = (test - truth).norm_l1() / truth.norm_l1(); - if tol < rtol { - Ok(tol) - } else { - Err(tol) + if tol > rtol { + eprintln!("Expected:\n{}", truth); + eprintln!("Actual:\n{}", test); + panic!("Too large deviation in L1-norm: {}", tol); } } /// check two arrays are close in L2 norm -pub fn close_l2( - test: &ArrayBase, - truth: &ArrayBase, - rtol: A::Real, -) -> Result +pub fn close_l2(test: &ArrayBase, truth: &ArrayBase, rtol: A::Real) where A: Scalar + Lapack, S1: Data, S2: Data, D: Dimension, + D::Pattern: PartialEq + Debug, { let tol = (test - truth).norm_l2() / truth.norm_l2(); - if tol < rtol { - Ok(tol) - } else { - Err(tol) + if tol > rtol { + eprintln!("Expected:\n{}", truth); + eprintln!("Actual:\n{}", test); + panic!("Too large deviation in L2-norm: {}", tol); } } @@ -90,7 +82,7 @@ macro_rules! generate_assert { #[macro_export] macro_rules! $assert { ($test: expr,$truth: expr,$tol: expr) => { - $crate::$close($test, $truth, $tol).unwrap(); + $crate::$close($test, $truth, $tol); }; } }; From 34fd91c43ab310635b6a2cfb5218534ba1a38003 Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Sat, 11 May 2019 04:44:32 +0900 Subject: [PATCH 3/5] Add assertion for array sizes --- src/assert.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/assert.rs b/src/assert.rs index 4a0bbce6..03c2a3b6 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -35,6 +35,7 @@ where D: Dimension, D::Pattern: PartialEq + Debug, { + assert_eq!(test.dim(), truth.dim()); let tol = (test - truth).norm_max(); if tol > atol { eprintln!("Expected:\n{}", truth); @@ -52,6 +53,7 @@ where D: Dimension, D::Pattern: PartialEq + Debug, { + assert_eq!(test.dim(), truth.dim()); let tol = (test - truth).norm_l1() / truth.norm_l1(); if tol > rtol { eprintln!("Expected:\n{}", truth); @@ -69,6 +71,7 @@ where D: Dimension, D::Pattern: PartialEq + Debug, { + assert_eq!(test.dim(), truth.dim()); let tol = (test - truth).norm_l2() / truth.norm_l2(); if tol > rtol { eprintln!("Expected:\n{}", truth); From bbc69f689977aeb1f43ae69fd5f0bc04a8115450 Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Sat, 11 May 2019 19:22:33 +0900 Subject: [PATCH 4/5] Restore comment syntax --- src/assert.rs | 4 ++++ tests/opnorm.rs | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/assert.rs b/src/assert.rs index 03c2a3b6..0d134c74 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -87,6 +87,10 @@ macro_rules! generate_assert { ($test: expr,$truth: expr,$tol: expr) => { $crate::$close($test, $truth, $tol); }; + ($test: expr,$truth: expr,$tol: expr; $comment: expr) => { + eprintln!($comment); + $crate::$close($test, $truth, $tol); + }; } }; } // generate_assert! diff --git a/tests/opnorm.rs b/tests/opnorm.rs index 834263f0..8e52bbd4 100644 --- a/tests/opnorm.rs +++ b/tests/opnorm.rs @@ -6,9 +6,9 @@ fn test(a: Array2, one: f64, inf: f64, fro: f64) { println!("ONE = {:?}", a.opnorm_one()); println!("INF = {:?}", a.opnorm_inf()); println!("FRO = {:?}", a.opnorm_fro()); - assert_rclose!(a.opnorm_one().unwrap(), one, 1e-7); - assert_rclose!(a.opnorm_inf().unwrap(), inf, 1e-7); - assert_rclose!(a.opnorm_fro().unwrap(), fro, 1e-7); + assert_rclose!(a.opnorm_one().unwrap(), one, 1e-7; "One norm"); + assert_rclose!(a.opnorm_inf().unwrap(), inf, 1e-7; "Infinity norm"); + assert_rclose!(a.opnorm_fro().unwrap(), fro, 1e-7; "Frobenius norm"); } fn gen(i: usize, j: usize, rev: bool) -> Array2 { From 3ef2d0086b6f0fb177f35813771a2879c55a4bdf Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Sat, 11 May 2019 19:32:18 +0900 Subject: [PATCH 5/5] Revise assertion output --- src/assert.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/assert.rs b/src/assert.rs index 0d134c74..670aabc8 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -6,23 +6,25 @@ use std::fmt::Debug; use super::norm::*; use super::types::*; -/// check two values are close in terms of the relative torrence +/// check two values are close in terms of the relative tolerance pub fn rclose(test: A, truth: A, rtol: A::Real) { let dev = (test - truth).abs() / truth.abs(); if dev > rtol { + eprintln!("==== Assetion Failed ===="); eprintln!("Expected = {}", truth); eprintln!("Actual = {}", test); - panic!("Too large deviation in relative torrence: {}", dev); + panic!("Too large deviation in relative tolerance: {}", dev); } } -/// check two values are close in terms of the absolute torrence +/// check two values are close in terms of the absolute tolerance pub fn aclose(test: A, truth: A, atol: A::Real) { let dev = (test - truth).abs(); if dev > atol { + eprintln!("==== Assetion Failed ===="); eprintln!("Expected = {}", truth); eprintln!("Actual = {}", test); - panic!("Too large deviation in absolute torrence: {}", dev); + panic!("Too large deviation in absolute tolerance: {}", dev); } } @@ -38,9 +40,10 @@ where assert_eq!(test.dim(), truth.dim()); let tol = (test - truth).norm_max(); if tol > atol { + eprintln!("==== Assetion Failed ===="); eprintln!("Expected:\n{}", truth); eprintln!("Actual:\n{}", test); - panic!("Too large deviation in maximum norm: {}", tol); + panic!("Too large deviation in maximum norm: {} > {}", tol, atol); } } @@ -56,9 +59,10 @@ where assert_eq!(test.dim(), truth.dim()); let tol = (test - truth).norm_l1() / truth.norm_l1(); if tol > rtol { + eprintln!("==== Assetion Failed ===="); eprintln!("Expected:\n{}", truth); eprintln!("Actual:\n{}", test); - panic!("Too large deviation in L1-norm: {}", tol); + panic!("Too large deviation in L1-norm: {} > {}", tol, rtol); } } @@ -74,9 +78,10 @@ where assert_eq!(test.dim(), truth.dim()); let tol = (test - truth).norm_l2() / truth.norm_l2(); if tol > rtol { + eprintln!("==== Assetion Failed ===="); eprintln!("Expected:\n{}", truth); eprintln!("Actual:\n{}", test); - panic!("Too large deviation in L2-norm: {}", tol); + panic!("Too large deviation in L2-norm: {} > {} ", tol, rtol); } }