@@ -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 # TODO: RUSTPYTHON
167171 @unittest .expectedFailure
@@ -182,6 +186,27 @@ def check(n, deltas, is_equal, imag = 0.0):
182186 check (2 ** pow , range (1 , 101 ), lambda delta : False , float (i ))
183187 check (2 ** 53 , range (- 100 , 0 ), lambda delta : True )
184188
189+ def test_add (self ):
190+ self .assertEqual (1j + int (+ 1 ), complex (+ 1 , 1 ))
191+ self .assertEqual (1j + int (- 1 ), complex (- 1 , 1 ))
192+ self .assertRaises (OverflowError , operator .add , 1j , 10 ** 1000 )
193+ self .assertRaises (TypeError , operator .add , 1j , None )
194+ self .assertRaises (TypeError , operator .add , None , 1j )
195+
196+ def test_sub (self ):
197+ self .assertEqual (1j - int (+ 1 ), complex (- 1 , 1 ))
198+ self .assertEqual (1j - int (- 1 ), complex (1 , 1 ))
199+ self .assertRaises (OverflowError , operator .sub , 1j , 10 ** 1000 )
200+ self .assertRaises (TypeError , operator .sub , 1j , None )
201+ self .assertRaises (TypeError , operator .sub , None , 1j )
202+
203+ def test_mul (self ):
204+ self .assertEqual (1j * int (20 ), complex (0 , 20 ))
205+ self .assertEqual (1j * int (- 1 ), complex (0 , - 1 ))
206+ self .assertRaises (OverflowError , operator .mul , 1j , 10 ** 1000 )
207+ self .assertRaises (TypeError , operator .mul , 1j , None )
208+ self .assertRaises (TypeError , operator .mul , None , 1j )
209+
185210 def test_mod (self ):
186211 # % is no longer supported on complex numbers
187212 with self .assertRaises (TypeError ):
@@ -214,11 +239,18 @@ def test_divmod_zero_division(self):
214239 def test_pow (self ):
215240 self .assertAlmostEqual (pow (1 + 1j , 0 + 0j ), 1.0 )
216241 self .assertAlmostEqual (pow (0 + 0j , 2 + 0j ), 0.0 )
242+ self .assertEqual (pow (0 + 0j , 2000 + 0j ), 0.0 )
243+ self .assertEqual (pow (0 , 0 + 0j ), 1.0 )
244+ self .assertEqual (pow (- 1 , 0 + 0j ), 1.0 )
217245 self .assertRaises (ZeroDivisionError , pow , 0 + 0j , 1j )
246+ self .assertRaises (ZeroDivisionError , pow , 0 + 0j , - 1000 )
218247 self .assertAlmostEqual (pow (1j , - 1 ), 1 / 1j )
219248 self .assertAlmostEqual (pow (1j , 200 ), 1 )
220249 self .assertRaises (ValueError , pow , 1 + 1j , 1 + 1j , 1 + 1j )
221250 self .assertRaises (OverflowError , pow , 1e200 + 1j , 1e200 + 1j )
251+ self .assertRaises (TypeError , pow , 1j , None )
252+ self .assertRaises (TypeError , pow , None , 1j )
253+ self .assertAlmostEqual (pow (1j , 0.5 ), 0.7071067811865476 + 0.7071067811865475j )
222254
223255 a = 3.33 + 4.43j
224256 self .assertEqual (a ** 0j , 1 )
@@ -303,6 +335,7 @@ def test_boolcontext(self):
303335 for i in range (100 ):
304336 self .assertTrue (complex (random () + 1e-6 , random () + 1e-6 ))
305337 self .assertTrue (not complex (0.0 , 0.0 ))
338+ self .assertTrue (1j )
306339
307340 def test_conjugate (self ):
308341 self .assertClose (complex (5.3 , 9.8 ).conjugate (), 5.3 - 9.8j )
@@ -318,6 +351,8 @@ def __complex__(self): return self.value
318351 self .assertRaises (TypeError , complex , {})
319352 self .assertRaises (TypeError , complex , NS (1.5 ))
320353 self .assertRaises (TypeError , complex , NS (1 ))
354+ self .assertRaises (TypeError , complex , object ())
355+ self .assertRaises (TypeError , complex , NS (4.25 + 0.5j ), object ())
321356
322357 self .assertAlmostEqual (complex ("1+10j" ), 1 + 10j )
323358 self .assertAlmostEqual (complex (10 ), 10 + 0j )
@@ -363,6 +398,8 @@ def __complex__(self): return self.value
363398 self .assertAlmostEqual (complex ('1e-500' ), 0.0 + 0.0j )
364399 self .assertAlmostEqual (complex ('-1e-500j' ), 0.0 - 0.0j )
365400 self .assertAlmostEqual (complex ('-1e-500+1e-500j' ), - 0.0 + 0.0j )
401+ self .assertEqual (complex ('1-1j' ), 1.0 - 1j )
402+ self .assertEqual (complex ('1J' ), 1j )
366403
367404 class complex2 (complex ): pass
368405 self .assertAlmostEqual (complex (complex2 (1 + 1j )), 1 + 1j )
@@ -533,8 +570,12 @@ class complex2(complex):
533570 self .assertFloatsAreIdentical (z .real , x )
534571 self .assertFloatsAreIdentical (z .imag , y )
535572
536- # TODO: RUSTPYTHON
537- @unittest .expectedFailure
573+ def test_constructor_negative_nans_from_string (self ):
574+ self .assertEqual (copysign (1. , complex ("-nan" ).real ), - 1. )
575+ self .assertEqual (copysign (1. , complex ("-nanj" ).imag ), - 1. )
576+ self .assertEqual (copysign (1. , complex ("-nan-nanj" ).real ), - 1. )
577+ self .assertEqual (copysign (1. , complex ("-nan-nanj" ).imag ), - 1. )
578+
538579 def test_underscores (self ):
539580 # check underscores
540581 for lit in VALID_UNDERSCORE_LITERALS :
@@ -553,6 +594,8 @@ def test_hash(self):
553594 x /= 3.0 # now check against floating point
554595 self .assertEqual (hash (x ), hash (complex (x , 0. )))
555596
597+ self .assertNotEqual (hash (2000005 - 1j ), - 1 )
598+
556599 def test_abs (self ):
557600 nums = [complex (x / 3. , y / 7. ) for x in range (- 9 ,9 ) for y in range (- 9 ,9 )]
558601 for num in nums :
@@ -575,6 +618,7 @@ def test(v, expected, test_fn=self.assertEqual):
575618 test (complex (NAN , 1 ), "(nan+1j)" )
576619 test (complex (1 , NAN ), "(1+nanj)" )
577620 test (complex (NAN , NAN ), "(nan+nanj)" )
621+ test (complex (- NAN , - NAN ), "(nan+nanj)" )
578622
579623 test (complex (0 , INF ), "infj" )
580624 test (complex (0 , - INF ), "-infj" )
@@ -601,6 +645,14 @@ def test(v, expected, test_fn=self.assertEqual):
601645 test (complex (- 0. , 0. ), "(-0+0j)" )
602646 test (complex (- 0. , - 0. ), "(-0-0j)" )
603647
648+ def test_pos (self ):
649+ class ComplexSubclass (complex ):
650+ pass
651+
652+ self .assertEqual (+ (1 + 6j ), 1 + 6j )
653+ self .assertEqual (+ ComplexSubclass (1 , 6 ), 1 + 6j )
654+ self .assertIs (type (+ ComplexSubclass (1 , 6 )), complex )
655+
604656 def test_neg (self ):
605657 self .assertEqual (- (1 + 6j ), - 1 - 6j )
606658
0 commit comments