@@ -24,55 +24,50 @@ def report_failure(self, out, test, example, got):
2424
2525
2626class 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))"
4431 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"
5034
5135 # Dictionaries aren't ordered, so we need to check both orders.
5236 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})" ,
5939 )
6040
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+
6156 @pytest .mark .parametrize (
62- "value, repr_string " ,
57+ "value, expected_repr_string " ,
6358 [
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]])" ),
6964 ],
7065 )
71- def test_repr_nd_array (self , plus_minus , value , repr_string ):
66+ def test_repr_nd_array (self , value , expected_repr_string ):
7267 """Make sure that arrays of all different dimensions are repr'd correctly."""
7368 np = pytest .importorskip ("numpy" )
7469 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
7671
7772 def test_operator_overloading (self ):
7873 assert 1 == approx (1 , rel = 1e-6 , abs = 1e-12 )
0 commit comments