@@ -24,55 +24,50 @@ def report_failure(self, out, test, example, got):
24
24
25
25
26
26
class TestApprox :
27
- @pytest .fixture
28
- def plus_minus (self ):
29
- return "\u00b1 "
30
-
31
- def test_repr_string (self , plus_minus ):
32
- tol1 , tol2 , infr = "1.0e-06" , "2.0e-06" , "inf"
33
- assert repr (approx (1.0 )) == "1.0 {pm} {tol1}" .format (pm = plus_minus , tol1 = tol1 )
34
- assert repr (
35
- approx ([1.0 , 2.0 ])
36
- ) == "approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])" .format (
37
- pm = plus_minus , tol1 = tol1 , tol2 = tol2
38
- )
39
- assert repr (
40
- approx ((1.0 , 2.0 ))
41
- ) == "approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))" .format (
42
- pm = plus_minus , tol1 = tol1 , tol2 = tol2
43
- )
27
+ def test_repr_string (self ):
28
+ assert repr (approx (1.0 )) == "1.0 ± 1.0e-06"
29
+ assert repr (approx ([1.0 , 2.0 ])) == "approx([1.0 ± 1.0e-06, 2.0 ± 2.0e-06])"
30
+ assert repr (approx ((1.0 , 2.0 ))) == "approx((1.0 ± 1.0e-06, 2.0 ± 2.0e-06))"
44
31
assert repr (approx (inf )) == "inf"
45
- assert repr (approx (1.0 , rel = nan )) == "1.0 {pm} ???" .format (pm = plus_minus )
46
- assert repr (approx (1.0 , rel = inf )) == "1.0 {pm} {infr}" .format (
47
- pm = plus_minus , infr = infr
48
- )
49
- assert repr (approx (1.0j , rel = inf )) == "1j"
32
+ assert repr (approx (1.0 , rel = nan )) == "1.0 ± ???"
33
+ assert repr (approx (1.0 , rel = inf )) == "1.0 ± inf"
50
34
51
35
# Dictionaries aren't ordered, so we need to check both orders.
52
36
assert repr (approx ({"a" : 1.0 , "b" : 2.0 })) in (
53
- "approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})" .format (
54
- pm = plus_minus , tol1 = tol1 , tol2 = tol2
55
- ),
56
- "approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})" .format (
57
- pm = plus_minus , tol1 = tol1 , tol2 = tol2
58
- ),
37
+ "approx({'a': 1.0 ± 1.0e-06, 'b': 2.0 ± 2.0e-06})" ,
38
+ "approx({'b': 2.0 ± 2.0e-06, 'a': 1.0 ± 1.0e-06})" ,
59
39
)
60
40
41
+ def test_repr_complex_numbers (self ):
42
+ assert repr (approx (inf + 1j )) == "(inf+1j)"
43
+ assert repr (approx (1.0j , rel = inf )) == "1j ± inf"
44
+
45
+ # can't compute a sensible tolerance
46
+ assert repr (approx (nan + 1j )) == "(nan+1j) ± ???"
47
+
48
+ assert repr (approx (1.0j )) == "1j ± 1.0e-06 ∠ ±180°"
49
+
50
+ # relative tolerance is scaled to |3+4j| = 5
51
+ assert repr (approx (3 + 4 * 1j )) == "(3+4j) ± 5.0e-06 ∠ ±180°"
52
+
53
+ # absolute tolerance is not scaled
54
+ assert repr (approx (3.3 + 4.4 * 1j , abs = 0.02 )) == "(3.3+4.4j) ± 2.0e-02 ∠ ±180°"
55
+
61
56
@pytest .mark .parametrize (
62
- "value, repr_string " ,
57
+ "value, expected_repr_string " ,
63
58
[
64
- (5.0 , "approx(5.0 {pm} 5.0e-06)" ),
65
- ([5.0 ], "approx([5.0 {pm} 5.0e-06])" ),
66
- ([[5.0 ]], "approx([[5.0 {pm} 5.0e-06]])" ),
67
- ([[5.0 , 6.0 ]], "approx([[5.0 {pm} 5.0e-06, 6.0 {pm} 6.0e-06]])" ),
68
- ([[5.0 ], [6.0 ]], "approx([[5.0 {pm} 5.0e-06], [6.0 {pm} 6.0e-06]])" ),
59
+ (5.0 , "approx(5.0 ± 5.0e-06)" ),
60
+ ([5.0 ], "approx([5.0 ± 5.0e-06])" ),
61
+ ([[5.0 ]], "approx([[5.0 ± 5.0e-06]])" ),
62
+ ([[5.0 , 6.0 ]], "approx([[5.0 ± 5.0e-06, 6.0 ± 6.0e-06]])" ),
63
+ ([[5.0 ], [6.0 ]], "approx([[5.0 ± 5.0e-06], [6.0 ± 6.0e-06]])" ),
69
64
],
70
65
)
71
- def test_repr_nd_array (self , plus_minus , value , repr_string ):
66
+ def test_repr_nd_array (self , value , expected_repr_string ):
72
67
"""Make sure that arrays of all different dimensions are repr'd correctly."""
73
68
np = pytest .importorskip ("numpy" )
74
69
np_array = np .array (value )
75
- assert repr (approx (np_array )) == repr_string . format ( pm = plus_minus )
70
+ assert repr (approx (np_array )) == expected_repr_string
76
71
77
72
def test_operator_overloading (self ):
78
73
assert 1 == approx (1 , rel = 1e-6 , abs = 1e-12 )
0 commit comments