1111NAN = float ("nan" )
1212# These tests ensure that complex math does the right thing
1313
14+ ZERO_DIVISION = (
15+ (1 + 1j , 0 + 0j ),
16+ (1 + 1j , 0.0 ),
17+ (1 + 1j , 0 ),
18+ (1.0 , 0 + 0j ),
19+ (1 , 0 + 0j ),
20+ )
21+
1422class ComplexTest (unittest .TestCase ):
1523
1624 def assertAlmostEqual (self , a , b ):
@@ -99,20 +107,34 @@ def test_truediv(self):
99107 self .check_div (complex (random (), random ()),
100108 complex (random (), random ()))
101109
102- self .assertRaises (ZeroDivisionError , complex .__truediv__ , 1 + 1j , 0 + 0j )
103- self .assertRaises (OverflowError , pow , 1e200 + 1j , 1e200 + 1j )
104-
105110 self .assertAlmostEqual (complex .__truediv__ (2 + 0j , 1 + 1j ), 1 - 1j )
106- self .assertRaises (ZeroDivisionError , complex .__truediv__ , 1 + 1j , 0 + 0j )
107111
108112 for denom_real , denom_imag in [(0 , NAN ), (NAN , 0 ), (NAN , NAN )]:
109113 z = complex (0 , 0 ) / complex (denom_real , denom_imag )
110114 self .assertTrue (isnan (z .real ))
111115 self .assertTrue (isnan (z .imag ))
112116
117+ def test_truediv_zero_division (self ):
118+ for a , b in ZERO_DIVISION :
119+ with self .assertRaises (ZeroDivisionError ):
120+ a / b
121+
113122 def test_floordiv (self ):
114- self .assertRaises (TypeError , complex .__floordiv__ , 3 + 0j , 1.5 + 0j )
115- self .assertRaises (TypeError , complex .__floordiv__ , 3 + 0j , 0 + 0j )
123+ with self .assertRaises (TypeError ):
124+ (1 + 1j ) // (1 + 0j )
125+ with self .assertRaises (TypeError ):
126+ (1 + 1j ) // 1.0
127+ with self .assertRaises (TypeError ):
128+ (1 + 1j ) // 1
129+ with self .assertRaises (TypeError ):
130+ 1.0 // (1 + 0j )
131+ with self .assertRaises (TypeError ):
132+ 1 // (1 + 0j )
133+
134+ def test_floordiv_zero_division (self ):
135+ for a , b in ZERO_DIVISION :
136+ with self .assertRaises (TypeError ):
137+ a // b
116138
117139 def test_richcompare (self ):
118140 self .assertIs (complex .__eq__ (1 + 1j , 1 << 10000 ), False )
@@ -159,13 +181,32 @@ def check(n, deltas, is_equal, imag = 0.0):
159181
160182 def test_mod (self ):
161183 # % is no longer supported on complex numbers
162- self .assertRaises (TypeError , (1 + 1j ).__mod__ , 0 + 0j )
163- self .assertRaises (TypeError , lambda : (3.33 + 4.43j ) % 0 )
164- self .assertRaises (TypeError , (1 + 1j ).__mod__ , 4.3j )
184+ with self .assertRaises (TypeError ):
185+ (1 + 1j ) % (1 + 0j )
186+ with self .assertRaises (TypeError ):
187+ (1 + 1j ) % 1.0
188+ with self .assertRaises (TypeError ):
189+ (1 + 1j ) % 1
190+ with self .assertRaises (TypeError ):
191+ 1.0 % (1 + 0j )
192+ with self .assertRaises (TypeError ):
193+ 1 % (1 + 0j )
194+
195+ def test_mod_zero_division (self ):
196+ for a , b in ZERO_DIVISION :
197+ with self .assertRaises (TypeError ):
198+ a % b
165199
166200 def test_divmod (self ):
167201 self .assertRaises (TypeError , divmod , 1 + 1j , 1 + 0j )
168- self .assertRaises (TypeError , divmod , 1 + 1j , 0 + 0j )
202+ self .assertRaises (TypeError , divmod , 1 + 1j , 1.0 )
203+ self .assertRaises (TypeError , divmod , 1 + 1j , 1 )
204+ self .assertRaises (TypeError , divmod , 1.0 , 1 + 0j )
205+ self .assertRaises (TypeError , divmod , 1 , 1 + 0j )
206+
207+ def test_divmod_zero_division (self ):
208+ for a , b in ZERO_DIVISION :
209+ self .assertRaises (TypeError , divmod , a , b )
169210
170211 def test_pow (self ):
171212 self .assertAlmostEqual (pow (1 + 1j , 0 + 0j ), 1.0 )
@@ -174,6 +215,7 @@ def test_pow(self):
174215 self .assertAlmostEqual (pow (1j , - 1 ), 1 / 1j )
175216 self .assertAlmostEqual (pow (1j , 200 ), 1 )
176217 self .assertRaises (ValueError , pow , 1 + 1j , 1 + 1j , 1 + 1j )
218+ self .assertRaises (OverflowError , pow , 1e200 + 1j , 1e200 + 1j )
177219
178220 a = 3.33 + 4.43j
179221 self .assertEqual (a ** 0j , 1 )
0 commit comments