@@ -109,6 +109,8 @@ def test_truediv(self):
109
109
complex (random (), random ()))
110
110
111
111
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 )
112
114
113
115
for denom_real , denom_imag in [(0 , NAN ), (NAN , 0 ), (NAN , NAN )]:
114
116
z = complex (0 , 0 ) / complex (denom_real , denom_imag )
@@ -140,6 +142,7 @@ def test_floordiv_zero_division(self):
140
142
def test_richcompare (self ):
141
143
self .assertIs (complex .__eq__ (1 + 1j , 1 << 10000 ), False )
142
144
self .assertIs (complex .__lt__ (1 + 1j , None ), NotImplemented )
145
+ self .assertIs (complex .__eq__ (1 + 1j , None ), NotImplemented )
143
146
self .assertIs (complex .__eq__ (1 + 1j , 1 + 1j ), True )
144
147
self .assertIs (complex .__eq__ (1 + 1j , 2 + 2j ), False )
145
148
self .assertIs (complex .__ne__ (1 + 1j , 1 + 1j ), False )
@@ -162,6 +165,7 @@ def test_richcompare(self):
162
165
self .assertIs (operator .eq (1 + 1j , 2 + 2j ), False )
163
166
self .assertIs (operator .ne (1 + 1j , 1 + 1j ), False )
164
167
self .assertIs (operator .ne (1 + 1j , 2 + 2j ), True )
168
+ self .assertIs (operator .eq (1 + 1j , 2.0 ), False )
165
169
166
170
def test_richcompare_boundaries (self ):
167
171
def check (n , deltas , is_equal , imag = 0.0 ):
@@ -180,6 +184,27 @@ def check(n, deltas, is_equal, imag = 0.0):
180
184
check (2 ** pow , range (1 , 101 ), lambda delta : False , float (i ))
181
185
check (2 ** 53 , range (- 100 , 0 ), lambda delta : True )
182
186
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
+
183
208
def test_mod (self ):
184
209
# % is no longer supported on complex numbers
185
210
with self .assertRaises (TypeError ):
@@ -212,11 +237,18 @@ def test_divmod_zero_division(self):
212
237
def test_pow (self ):
213
238
self .assertAlmostEqual (pow (1 + 1j , 0 + 0j ), 1.0 )
214
239
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 )
215
243
self .assertRaises (ZeroDivisionError , pow , 0 + 0j , 1j )
244
+ self .assertRaises (ZeroDivisionError , pow , 0 + 0j , - 1000 )
216
245
self .assertAlmostEqual (pow (1j , - 1 ), 1 / 1j )
217
246
self .assertAlmostEqual (pow (1j , 200 ), 1 )
218
247
self .assertRaises (ValueError , pow , 1 + 1j , 1 + 1j , 1 + 1j )
219
248
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 )
220
252
221
253
a = 3.33 + 4.43j
222
254
self .assertEqual (a ** 0j , 1 )
@@ -301,6 +333,7 @@ def test_boolcontext(self):
301
333
for i in range (100 ):
302
334
self .assertT
F438
rue (complex (random () + 1e-6 , random () + 1e-6 ))
303
335
self .assertTrue (not complex (0.0 , 0.0 ))
336
+ self .assertTrue (1j )
304
337
305
338
def test_conjugate (self ):
306
339
self .assertClose (complex (5.3 , 9.8 ).conjugate (), 5.3 - 9.8j )
@@ -314,6 +347,8 @@ def __complex__(self): return self.value
314
347
self .assertRaises (TypeError , complex , {})
315
348
self .assertRaises (TypeError , complex , NS (1.5 ))
316
349
self .assertRaises (TypeError , complex , NS (1 ))
350
+ self .assertRaises (TypeError , complex , object ())
351
+ self .assertRaises (TypeError , complex , NS (4.25 + 0.5j ), object ())
317
352
318
353
self .assertAlmostEqual (complex ("1+10j" ), 1 + 10j )
319
354
self .assertAlmostEqual (complex (10 ), 10 + 0j )
@@ -359,6 +394,8 @@ def __complex__(self): return self.value
359
394
self .assertAlmostEqual (complex ('1e-500' ), 0.0 + 0.0j )
360
395
self .assertAlmostEqual (complex ('-1e-500j' ), 0.0 - 0.0j )
361
396
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 )
362
399
363
400
class complex2 (complex ): pass
364
401
self .assertAlmostEqual (complex (complex2 (1 + 1j )), 1 + 1j )
@@ -553,6 +590,8 @@ def test_hash(self):
553
590
x /= 3.0 # now check against floating point
554
591
self .assertEqual (hash (x ), hash (complex (x , 0. )))
555
592
593
+ self .assertNotEqual (hash (2000005 - 1j ), - 1 )
594
+
556
595
def test_abs (self ):
557
596
nums = [complex (x / 3. , y / 7. ) for x in range (- 9 ,9 ) for y in range (- 9 ,9 )]
558
597
for num in nums :
@@ -602,6 +641,14 @@ def test(v, expected, test_fn=self.assertEqual):
602
641
test (complex (- 0. , 0. ), "(-0+0j)" )
603
642
test (complex (- 0. , - 0. ), "(-0-0j)" )
604
643
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
+
605
652
def test_neg (self ):
606
653
self .assertEqual (- (1 + 6j ), - 1 - 6j )
607
654
0 commit comments