11//! Assertions for array
22
33use ndarray:: * ;
4+ use std:: fmt:: Debug ;
45
56use super :: norm:: * ;
67use super :: types:: * ;
78
8- /// check two values are close in terms of the relative torrence
9- pub fn rclose < A : Scalar > ( test : A , truth : A , rtol : A :: Real ) -> Result < A :: Real , A :: Real > {
9+ /// check two values are close in terms of the relative tolerance
10+ pub fn rclose < A : Scalar > ( test : A , truth : A , rtol : A :: Real ) {
1011 let dev = ( test - truth) . abs ( ) / truth. abs ( ) ;
11- if dev < rtol {
12- Ok ( dev)
13- } else {
14- Err ( dev)
12+ if dev > rtol {
13+ eprintln ! ( "==== Assetion Failed ====" ) ;
14+ eprintln ! ( "Expected = {}" , truth) ;
15+ eprintln ! ( "Actual = {}" , test) ;
16+ panic ! ( "Too large deviation in relative tolerance: {}" , dev) ;
1517 }
1618}
1719
18- /// check two values are close in terms of the absolute torrence
19- pub fn aclose < A : Scalar > ( test : A , truth : A , atol : A :: Real ) -> Result < A :: Real , A :: Real > {
20+ /// check two values are close in terms of the absolute tolerance
21+ pub fn aclose < A : Scalar > ( test : A , truth : A , atol : A :: Real ) {
2022 let dev = ( test - truth) . abs ( ) ;
21- if dev < atol {
22- Ok ( dev)
23- } else {
24- Err ( dev)
23+ if dev > atol {
24+ eprintln ! ( "==== Assetion Failed ====" ) ;
25+ eprintln ! ( "Expected = {}" , truth) ;
26+ eprintln ! ( "Actual = {}" , test) ;
27+ panic ! ( "Too large deviation in absolute tolerance: {}" , dev) ;
2528 }
2629}
2730
2831/// check two arrays are close in maximum norm
29- pub fn close_max < A , S1 , S2 , D > (
30- test : & ArrayBase < S1 , D > ,
31- truth : & ArrayBase < S2 , D > ,
32- atol : A :: Real ,
33- ) -> Result < A :: Real , A :: Real >
32+ pub fn close_max < A , S1 , S2 , D > ( test : & ArrayBase < S1 , D > , truth : & ArrayBase < S2 , D > , atol : A :: Real )
3433where
3534 A : Scalar + Lapack ,
3635 S1 : Data < Elem = A > ,
3736 S2 : Data < Elem = A > ,
3837 D : Dimension ,
38+ D :: Pattern : PartialEq + Debug ,
3939{
40+ assert_eq ! ( test. dim( ) , truth. dim( ) ) ;
4041 let tol = ( test - truth) . norm_max ( ) ;
41- if tol < atol {
42- Ok ( tol)
43- } else {
44- Err ( tol)
42+ if tol > atol {
43+ eprintln ! ( "==== Assetion Failed ====" ) ;
44+ eprintln ! ( "Expected:\n {}" , truth) ;
45+ eprintln ! ( "Actual:\n {}" , test) ;
46+ panic ! ( "Too large deviation in maximum norm: {} > {}" , tol, atol) ;
4547 }
4648}
4749
4850/// check two arrays are close in L1 norm
49- pub fn close_l1 < A , S1 , S2 , D > (
50- test : & ArrayBase < S1 , D > ,
51- truth : & ArrayBase < S2 , D > ,
52- rtol : A :: Real ,
53- ) -> Result < A :: Real , A :: Real >
51+ pub fn close_l1 < A , S1 , S2 , D > ( test : & ArrayBase < S1 , D > , truth : & ArrayBase < S2 , D > , rtol : A :: Real )
5452where
5553 A : Scalar + Lapack ,
5654 S1 : Data < Elem = A > ,
5755 S2 : Data < Elem = A > ,
5856 D : Dimension ,
57+ D :: Pattern : PartialEq + Debug ,
5958{
59+ assert_eq ! ( test. dim( ) , truth. dim( ) ) ;
6060 let tol = ( test - truth) . norm_l1 ( ) / truth. norm_l1 ( ) ;
61- if tol < rtol {
62- Ok ( tol)
63- } else {
64- Err ( tol)
61+ if tol > rtol {
62+ eprintln ! ( "==== Assetion Failed ====" ) ;
63+ eprintln ! ( "Expected:\n {}" , truth) ;
64+ eprintln ! ( "Actual:\n {}" , test) ;
65+ panic ! ( "Too large deviation in L1-norm: {} > {}" , tol, rtol) ;
6566 }
6667}
6768
6869/// check two arrays are close in L2 norm
69- pub fn close_l2 < A , S1 , S2 , D > (
70- test : & ArrayBase < S1 , D > ,
71- truth : & ArrayBase < S2 , D > ,
72- rtol : A :: Real ,
73- ) -> Result < A :: Real , A :: Real >
70+ pub fn close_l2 < A , S1 , S2 , D > ( test : & ArrayBase < S1 , D > , truth : & ArrayBase < S2 , D > , rtol : A :: Real )
7471where
7572 A : Scalar + Lapack ,
7673 S1 : Data < Elem = A > ,
7774 S2 : Data < Elem = A > ,
7875 D : Dimension ,
76+ D :: Pattern : PartialEq + Debug ,
7977{
78+ assert_eq ! ( test. dim( ) , truth. dim( ) ) ;
8079 let tol = ( test - truth) . norm_l2 ( ) / truth. norm_l2 ( ) ;
81- if tol < rtol {
82- Ok ( tol)
83- } else {
84- Err ( tol)
80+ if tol > rtol {
81+ eprintln ! ( "==== Assetion Failed ====" ) ;
82+ eprintln ! ( "Expected:\n {}" , truth) ;
83+ eprintln ! ( "Actual:\n {}" , test) ;
84+ panic ! ( "Too large deviation in L2-norm: {} > {} " , tol, rtol) ;
8585 }
8686}
8787
@@ -90,10 +90,11 @@ macro_rules! generate_assert {
9090 #[ macro_export]
9191 macro_rules! $assert {
9292 ( $test: expr, $truth: expr, $tol: expr) => {
93- $crate:: $close( $test, $truth, $tol) . unwrap ( ) ;
93+ $crate:: $close( $test, $truth, $tol) ;
9494 } ;
9595 ( $test: expr, $truth: expr, $tol: expr; $comment: expr) => {
96- $crate:: $close( $test, $truth, $tol) . expect( $comment) ;
96+ eprintln!( $comment) ;
97+ $crate:: $close( $test, $truth, $tol) ;
9798 } ;
9899 }
99100 } ;
0 commit comments