1
1
//! Assertions for array
2
2
3
3
use ndarray:: * ;
4
+ use std:: fmt:: Debug ;
4
5
5
6
use super :: norm:: * ;
6
7
use super :: types:: * ;
7
8
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 ) {
10
11
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) ;
15
17
}
16
18
}
17
19
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 ) {
20
22
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) ;
25
28
}
26
29
}
27
30
28
31
/// 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 )
34
33
where
35
34
A : Scalar + Lapack ,
36
35
S1 : Data < Elem = A > ,
37
36
S2 : Data < Elem = A > ,
38
37
D : Dimension ,
38
+ D :: Pattern : PartialEq + Debug ,
39
39
{
40
+ assert_eq ! ( test. dim( ) , truth. dim( ) ) ;
40
41
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) ;
45
47
}
46
48
}
47
49
48
50
/// 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 )
54
52
where
55
53
A : Scalar + Lapack ,
56
54
S1 : Data < Elem = A > ,
57
55
S2 : Data < Elem = A > ,
58
56
D : Dimension ,
57
+ D :: Pattern : PartialEq + Debug ,
59
58
{
59
+ assert_eq ! ( test. dim( ) , truth. dim( ) ) ;
60
60
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) ;
65
66
}
66
67
}
67
68
68
69
/// 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 )
74
71
where
75
72
A : Scalar + Lapack ,
76
73
S1 : Data < Elem = A > ,
77
74
S2 : Data < Elem = A > ,
78
75
D : Dimension ,
76
+ D :: Pattern : PartialEq + Debug ,
79
77
{
78
+ assert_eq ! ( test. dim( ) , truth. dim( ) ) ;
80
79
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) ;
85
85
}
86
86
}
87
87
@@ -90,10 +90,11 @@ macro_rules! generate_assert {
90
90
#[ macro_export]
91
91
macro_rules! $assert {
92
92
( $test: expr, $truth: expr, $tol: expr) => {
93
- $crate:: $close( $test, $truth, $tol) . unwrap ( ) ;
93
+ $crate:: $close( $test, $truth, $tol) ;
94
94
} ;
95
95
( $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) ;
97
98
} ;
98
99
}
99
100
} ;
0 commit comments