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
9
/// 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 > {
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 ! ( "Expected = {}" , truth ) ;
14
+ eprintln ! ( "Actual = {}" , test ) ;
15
+ panic ! ( "Too large deviation in relative torrence: {}" , dev) ;
15
16
}
16
17
}
17
18
18
19
/// 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
+ pub fn aclose < A : Scalar > ( test : A , truth : A , atol : A :: Real ) {
20
21
let dev = ( test - truth) . abs ( ) ;
21
- if dev < atol {
22
- Ok ( dev )
23
- } else {
24
- Err ( dev)
22
+ if dev > atol {
23
+ eprintln ! ( "Expected = {}" , truth ) ;
24
+ eprintln ! ( "Actual = {}" , test ) ;
25
+ panic ! ( "Too large deviation in absolute torrence: {}" , dev) ;
25
26
}
26
27
}
27
28
28
29
/// 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 >
30
+ pub fn close_max < A , S1 , S2 , D > ( test : & ArrayBase < S1 , D > , truth : & ArrayBase < S2 , D > , atol : A :: Real )
34
31
where
35
32
A : Scalar + Lapack ,
36
33
S1 : Data < Elem = A > ,
37
34
S2 : Data < Elem = A > ,
38
35
D : Dimension ,
36
+ D :: Pattern : PartialEq + Debug ,
39
37
{
40
38
let tol = ( test - truth) . norm_max ( ) ;
41
- if tol < atol {
42
- Ok ( tol )
43
- } else {
44
- Err ( tol)
39
+ if tol > atol {
40
+ eprintln ! ( "Expected: \n {}" , truth ) ;
41
+ eprintln ! ( "Actual: \n {}" , test ) ;
42
+ panic ! ( "Too large deviation in maximum norm: {}" , tol) ;
45
43
}
46
44
}
47
45
48
46
/// 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 >
47
+ pub fn close_l1 < A , S1 , S2 , D > ( test : & ArrayBase < S1 , D > , truth : & ArrayBase < S2 , D > , rtol : A :: Real )
54
48
where
55
49
A : Scalar + Lapack ,
56
50
S1 : Data < Elem = A > ,
57
51
S2 : Data < Elem = A > ,
58
52
D : Dimension ,
53
+ D :: Pattern : PartialEq + Debug ,
59
54
{
60
55
let tol = ( test - truth) . norm_l1 ( ) / truth. norm_l1 ( ) ;
61
- if tol < rtol {
62
- Ok ( tol )
63
- } else {
64
- Err ( tol)
56
+ if tol > rtol {
57
+ eprintln ! ( "Expected: \n {}" , truth ) ;
58
+ eprintln ! ( "Actual: \n {}" , test ) ;
59
+ panic ! ( "Too large deviation in L1-norm: {}" , tol) ;
65
60
}
66
61
}
67
62
68
63
/// 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 >
64
+ pub fn close_l2 < A , S1 , S2 , D > ( test : & ArrayBase < S1 , D > , truth : & ArrayBase < S2 , D > , rtol : A :: Real )
74
65
where
75
66
A : Scalar + Lapack ,
76
67
S1 : Data < Elem = A > ,
77
68
S2 : Data < Elem = A > ,
78
69
D : Dimension ,
70
+ D :: Pattern : PartialEq + Debug ,
79
71
{
80
72
let tol = ( test - truth) . norm_l2 ( ) / truth. norm_l2 ( ) ;
81
- if tol < rtol {
82
- Ok ( tol )
83
- } else {
84
- Err ( tol)
73
+ if tol > rtol {
74
+ eprintln ! ( "Expected: \n {}" , truth ) ;
75
+ eprintln ! ( "Actual: \n {}" , test ) ;
76
+ panic ! ( "Too large deviation in L2-norm: {}" , tol) ;
85
77
}
86
78
}
87
79
@@ -90,7 +82,7 @@ macro_rules! generate_assert {
90
82
#[ macro_export]
91
83
macro_rules! $assert {
92
84
( $test: expr, $truth: expr, $tol: expr) => {
93
- $crate:: $close( $test, $truth, $tol) . unwrap ( ) ;
85
+ $crate:: $close( $test, $truth, $tol) ;
94
86
} ;
95
87
}
96
88
} ;
0 commit comments