@@ -109,6 +109,8 @@ def test_truediv(self):
109109 complex (random (), random ()))
110110
111111 self .assertAlmostEqual (complex .__truediv__ (2 + 0j , 1 + 1j ), 1 - 1j )
112+ self .assertRaises (TypeError , operator .truediv , 1j , None )
113+ self .assertRaises (TypeError , operator .truediv , None , 1j )
112114
113115 for denom_real , denom_imag in [(0 , NAN ), (NAN , 0 ), (NAN , NAN )]:
114116 z = complex (0 , 0 ) / complex (denom_real , denom_imag )
@@ -140,6 +142,7 @@ def test_floordiv_zero_division(self):
140142 def test_richcompare (self ):
141143 self .assertIs (complex .__eq__ (1 + 1j , 1 << 10000 ), False )
142144 self .assertIs (complex .__lt__ (1 + 1j , None ), NotImplemented )
145+ self .assertIs (complex .__eq__ (1 + 1j , None ), NotImplemented )
143146 self .assertIs (complex .__eq__ (1 + 1j , 1 + 1j ), True )
144147 self .assertIs (complex .__eq__ (1 + 1j , 2 + 2j ), False )
145148 self .assertIs (complex .__ne__ (1 + 1j , 1 + 1j ), False )
@@ -162,6 +165,7 @@ def test_richcompare(self):
162165 self .assertIs (operator .eq (1 + 1j , 2 + 2j ), False )
163166 self .assertIs (operator .ne (1 + 1j , 1 + 1j ), False )
164167 self .assertIs (operator .ne (1 + 1j , 2 + 2j ), True )
168+ self .assertIs (operator .eq (1 + 1j , 2.0 ), False )
165169
166170 def test_richcompare_boundaries (self ):
167171 def check (n , deltas , is_equal , imag = 0.0 ):
@@ -180,6 +184,27 @@ def check(n, deltas, is_equal, imag = 0.0):
180184 check (2 ** pow , range (1 , 101 ), lambda delta : False , float (i ))
181185 check (2 ** 53 , range (- 100 , 0 ), lambda delta : True )
182186
187+ def test_add (self ):
188+ self .assertEqual (1j + int (+ 1 ), complex (+ 1 , 1 ))
189+ self .assertEqual (1j + int (- 1 ), complex (- 1 , 1 ))
190+ self .assertRaises (OverflowError , operator .add , 1j , 10 ** 1000 )
191+ self .assertRaises (TypeError , operator .add , 1j , None )
192+ self .assertRaises (TypeError , operator .add , None , 1j )
193+
194+ def test_sub (self ):
195+ self .assertEqual (1j - int (+ 1 ), complex (- 1 , 1 ))
196+ self .assertEqual (1j - int (- 1 ), complex (1 , 1 ))
197+ self .assertRaises (OverflowError , operator .sub , 1j , 10 ** 1000 )
198+ self .assertRaises (TypeError , operator .sub , 1j , None )
199+ self .assertRaises (TypeError , operator .sub , None , 1j )
200+
201+ def test_mul (self ):
202+ self .assertEqual (1j * int (20 ), complex (0 , 20 ))
203+ self .assertEqual (1j * int (- 1 ), complex (0 , - 1 ))
204+ self .assertRaises (OverflowError , operator .mul , 1j , 10 ** 1000 )
205+ self .assertRaises (TypeError , operator .mul , 1j , None )
206+ self .assertRaises (TypeError , operator .mul , None , 1j )
207+
183208 def test_mod (self ):
184209 # % is no longer supported on complex numbers
185210 with self .assertRaises (TypeError ):
@@ -212,11 +237,18 @@ def test_divmod_zero_division(self):
212237 def test_pow (self ):
213238 self .assertAlmostEqual (pow (1 + 1j , 0 + 0j ), 1.0 )
214239 self .assertAlmostEqual (pow (0 + 0j , 2 + 0j ), 0.0 )
240+ self .assertEqual (pow (0 + 0j , 2000 + 0j ), 0.0 )
241+ self .assertEqual (pow (0 , 0 + 0j ), 1.0 )
242+ self .assertEqual (pow (- 1 , 0 + 0j ), 1.0 )
215243 self .assertRaises (ZeroDivisionError , pow , 0 + 0j , 1j )
244+ self .assertRaises (ZeroDivisionError , pow , 0 + 0j , - 1000 )
216245 self .assertAlmostEqual (pow (1j , - 1 ), 1 / 1j )
217246 self .assertAlmostEqual (pow (1j , 200 ), 1 )
218247 self .assertRaises (ValueError , pow , 1 + 1j , 1 + 1j , 1 + 1j )
219248 self .assertRaises (OverflowError , pow , 1e200 + 1j , 1e200 + 1j )
249+ self .assertRaises (TypeError , pow , 1j , None )
250+ self .assertRaises (TypeError , pow , None , 1j )
251+ self .assertAlmostEqual (pow (1j , 0.5 ), 0.7071067811865476 + 0.7071067811865475j )
220252
221253 a = 3.33 + 4.43j
222254 self .assertEqual (a ** 0j , 1 )
@@ -301,6 +333,7 @@ def test_boolcontext(self):
301333 for i in range (100 ):
302334 self .assertTrue (complex (random () + 1e-6 , random () + 1e-6 ))
303335 self .assertTrue (not complex (0.0 , 0.0 ))
336+ self .assertTrue (1j )
304337
305338 def test_conjugate (self ):
306339 self .assertClose (complex (5.3 , 9.8 ).conjugate (), 5.3 - 9.8j )
@@ -314,6 +347,8 @@ def __complex__(self): return self.value
314347 self .assertRaises (TypeError , complex , {})
315348 self .assertRaises (TypeError , complex , NS (1.5 ))
316349 self .assertRaises (TypeError , complex , NS (1 ))
350+ self .assertRaises (TypeError , complex , object ())
351+ self .assertRaises (TypeError , complex , NS (4.25 + 0.5j ), object ())
317352
318353 self .assertAlmostEqual (complex ("1+10j" ), 1 + 10j )
319354 self .assertAlmostEqual (complex (10 ), 10 + 0j )
@@ -359,6 +394,8 @@ def __complex__(self): return self.value
359394 self .assertAlmostEqual (complex ('1e-500' ), 0.0 + 0.0j )
360395 self .assertAlmostEqual (complex ('-1e-500j' ), 0.0 - 0.0j )
361396 self .assertAlmostEqual (complex ('-1e-500+1e-500j' ), - 0.0 + 0.0j )
397+ self .assertEqual (complex ('1-1j' ), 1.0 - 1j )
398+ self .assertEqual (complex ('1J' ), 1j )
362399
363400 class complex2 (complex ): pass
364401 self .assertAlmostEqual (complex (complex2 (1 + 1j )), 1 + 1j )
@@ -553,6 +590,8 @@ def test_hash(self):
553590 x /= 3.0 # now check against floating point
554591 self .assertEqual (hash (x ), hash (complex (x , 0. )))
555592
593+ self .assertNotEqual (hash (2000005 - 1j ), - 1 )
594+
556595 def test_abs (self ):
557596 nums = [complex (x / 3. , y / 7. ) for x in range (- 9 ,9 ) for y in range (- 9 ,9 )]
558597 for num in nums :
@@ -602,6 +641,14 @@ def test(v, expected, test_fn=self.assertEqual):
602641 test (complex (- 0. , 0. ), "(-0+0j)" )
603642 test (complex (- 0. , - 0. ), "(-0-0j)" )
604643
644+ def test_pos (self ):
645+ class ComplexSubclass (complex ):
646+ pass
647+
648+ self .assertEqual (+ (1 + 6j ), 1 + 6j )
649+ self .assertEqual (+ ComplexSubclass (1 , 6 ), 1 + 6j )
650+ self .assertIs (type (+ ComplexSubclass (1 , 6 )), complex )
651+
605652 def test_neg (self ):
606653 self .assertEqual (- (1 + 6j ), - 1 - 6j )
607654
0 commit comments