8
8
import unittest
9
9
from unittest import mock
10
10
from mantid .testing import assert_almost_equal
11
- from mantid .simpleapi import CreateWorkspace
11
+ from mantid .simpleapi import CreateWorkspace , CreateSingleValuedWorkspace
12
12
13
13
14
14
class AssertAlmostEqualTest (unittest .TestCase ):
@@ -21,8 +21,16 @@ def setUpClass(self):
21
21
self .ws2 = ws2
22
22
self .ws3 = ws3
23
23
24
+ # SIMPLE CASES
25
+
26
+ def test_noargs (self ):
27
+ # compare (ws1 - ws2) < 1e-10
28
+ another1 = self .ws1 .clone ()
29
+ assert_almost_equal (self .ws1 , another1 )
30
+
24
31
def test_simple (self ):
25
- assert_almost_equal (self .ws1 , self .ws2 , atol = 1 , rtol = 1 )
32
+ # simple test with wide tolerances to always pass
33
+ assert_almost_equal (self .ws1 , self .ws2 , atol = 1 , rtol = 2 )
26
34
27
35
def test_atol (self ):
28
36
# compare (ws1 - ws2) < atol
@@ -32,10 +40,82 @@ def test_rtol(self):
32
40
# compare (ws1 - ws2) / (0.5 * (ws1 + ws2)) < rtol
33
41
assert_almost_equal (self .ws1 , self .ws3 , rtol = 0.7 )
34
42
35
- def test_raises (self ):
43
+ # VALIDATION TESTS
44
+
45
+ def test_validate_forbidden_keys (self ):
46
+ with self .assertRaises (TypeError ):
47
+ assert_almost_equal (self .ws1 , self .ws1 , Workspace1 = self .ws3 )
48
+ with self .assertRaises (TypeError ):
49
+ assert_almost_equal (self .ws1 , self .ws1 , Workspace2 = self .ws3 )
50
+ with self .assertRaises (ValueError ):
51
+ assert_almost_equal (self .ws1 , self .ws1 , Tolerance = 0.01 )
52
+ with self .assertRaises (ValueError ):
53
+ assert_almost_equal (self .ws1 , self .ws1 , ToleranceRelErr = True )
54
+
55
+ def test_validate_atol (self ):
56
+ with self .assertRaises (ValueError ):
57
+ assert_almost_equal (self .ws1 , self .ws2 , atol = - 1.0 , rtol = 1.0 )
58
+
59
+ def test_validate_rtol (self ):
36
60
with self .assertRaises (ValueError ):
61
+ assert_almost_equal (self .ws1 , self .ws2 , ato1 = 1.0 , rtol = - 1.0 )
62
+
63
+ # NEGATIVE TESTS
64
+
65
+ def test_fail_unequal (self ):
66
+ with self .assertRaises (AssertionError ):
37
67
assert_almost_equal (self .ws1 , self .ws2 )
38
68
69
+ def test_fail_absolute (self ):
70
+ with self .assertRaises (AssertionError ):
71
+ assert_almost_equal (self .ws1 , self .ws2 , atol = 1e-3 )
72
+
73
+ def test_fail_relative (self ):
74
+ with self .assertRaises (AssertionError ):
75
+ assert_almost_equal (self .ws1 , self .ws2 , rtol = 1.0e-4 )
76
+
77
+ # MIXED CASES
78
+
79
+ def test_both_atol_fails (self ):
80
+ atol = 0.01
81
+ rtol = 0.01
82
+ ts1 = CreateSingleValuedWorkspace (1000000.1 )
83
+ ts2 = CreateSingleValuedWorkspace (1000000.0 )
84
+ # ensure rtol passes, atol fails separately
85
+ assert_almost_equal (ts1 , ts2 , rtol = rtol )
86
+ with self .assertRaises (AssertionError ):
87
+ assert_almost_equal (ts1 , ts2 , atol = atol )
88
+ # ensure both fail
89
+ with self .assertRaises (AssertionError ):
90
+ assert_almost_equal (ts1 , ts2 , atol = atol , rtol = rtol )
91
+
92
+ def test_both_rtol_fails (self ):
93
+ atol = 0.01
94
+ rtol = 0.01
95
+ tes1 = CreateSingleValuedWorkspace (0.00001 )
96
+ tes2 = CreateSingleValuedWorkspace (0.00010 )
97
+ # ensure atol passes, rtol fails separately
98
+ assert_almost_equal (tes1 , tes2 , atol = atol )
99
+ with self .assertRaises (AssertionError ):
100
+ assert_almost_equal (tes1 , tes2 , rtol = rtol )
101
+ # ensure both fail
102
+ with self .assertRaises (AssertionError ):
103
+ assert_almost_equal (tes1 , tes2 , atol = atol , rtol = rtol )
104
+
105
+ def test_both_both_fail (self ):
106
+ atol = 0.01
107
+ rtol = 0.01
108
+ ts1 = CreateSingleValuedWorkspace (100.01 )
109
+ ts2 = CreateSingleValuedWorkspace (200.02 )
110
+ # ensure rtol, atol fail separately
111
+ with self .assertRaises (AssertionError ):
112
+ assert_almost_equal (ts1 , ts2 , atol = atol )
113
+ with self .assertRaises (AssertionError ):
114
+ assert_almost_equal (ts1 , ts2 , rtol = rtol )
115
+ # ensure both fail
116
+ with self .assertRaises (AssertionError ):
117
+ assert_almost_equal (ts1 , ts2 , atol = atol , rtol = rtol )
118
+
39
119
40
120
if __name__ == "__main__" :
41
121
unittest .main ()
0 commit comments