Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_complex.ComplexTest Class Reference

List of all members.

Public Member Functions

def assertAlmostEqual
def assertCloseAbs
def check_div
def test_truediv
def test_truediv
def test_floordiv
def test_richcompare
def test_richcompare_boundaries
def test_mod
def test_divmod
def test_pow
def test_boolcontext
def test_conjugate
def test_constructor
def test_hash
def test_abs
def test_repr_str
def test_negative_zero_repr_str
def test_neg
def test_file
def test_getnewargs
def test_plus_minus_0j
def test_negated_imaginary_literal
def test_overflow
def test_repr_roundtrip
def test_format

Public Attributes

 value

Detailed Description

Definition at line 12 of file test_complex.py.


Member Function Documentation

def test.test_complex.ComplexTest.assertAlmostEqual (   self,
  a,
  b 
)

Definition at line 14 of file test_complex.py.

00014 
00015     def assertAlmostEqual(self, a, b):
00016         if isinstance(a, complex):
00017             if isinstance(b, complex):
00018                 unittest.TestCase.assertAlmostEqual(self, a.real, b.real)
00019                 unittest.TestCase.assertAlmostEqual(self, a.imag, b.imag)
00020             else:
00021                 unittest.TestCase.assertAlmostEqual(self, a.real, b)
00022                 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.)
00023         else:
00024             if isinstance(b, complex):
00025                 unittest.TestCase.assertAlmostEqual(self, a, b.real)
00026                 unittest.TestCase.assertAlmostEqual(self, 0., b.imag)
00027             else:
00028                 unittest.TestCase.assertAlmostEqual(self, a, b)

Here is the caller graph for this function:

def test.test_complex.ComplexTest.assertCloseAbs (   self,
  x,
  y,
  eps = 1e-9 
)
Return true iff floats x and y "are close\

Definition at line 29 of file test_complex.py.

00029 
00030     def assertCloseAbs(self, x, y, eps=1e-9):
00031         """Return true iff floats x and y "are close\""""
00032         # put the one with larger magnitude second
00033         if abs(x) > abs(y):
00034             x, y = y, x
00035         if y == 0:
00036             return abs(x) < eps
00037         if x == 0:
00038             return abs(y) < eps
00039         # check that relative difference < eps
00040         self.assertTrue(abs((x-y)/y) < eps)
00041 
00042     def assertFloatsAreIdentical(self, x, y):
00043         """assert that floats x and y are identical, in the sense that:
00044         (1) both x and y are nans, or
00045         (2) both x and y are infinities, with the same sign, or
00046         (3) both x and y are zeros, with the same sign, or
00047         (4) x and y are both finite and nonzero, and x == y
00048 
00049         """
00050         msg = 'floats {!r} and {!r} are not identical'
00051 
00052         if isnan(x) or isnan(y):
00053             if isnan(x) and isnan(y):
00054                 return
00055         elif x == y:
00056             if x != 0.0:
                return

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_complex.ComplexTest.check_div (   self,
  x,
  y 
)
Compute complex z=x*y, and check that z/x==y and z/y==x.

Definition at line 57 of file test_complex.py.

00057 
00058             # both zero; check that signs match
00059             elif copysign(1.0, x) == copysign(1.0, y):
00060                 return
00061             else:
00062                 msg += ': zeros have different signs'
00063         self.fail(msg.format(x, y))
00064 
00065     def assertClose(self, x, y, eps=1e-9):
00066         """Return true iff complexes x and y "are close\""""
00067         self.assertCloseAbs(x.real, y.real, eps)
00068         self.assertCloseAbs(x.imag, y.imag, eps)
00069 
00070     def check_div(self, x, y):
        """Compute complex z=x*y, and check that z/x==y and z/y==x."""

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 367 of file test_complex.py.

00367 
00368                 return None
00369 
00370         self.assertAlmostEqual(complex(complex0(1j)), 42j)
00371         self.assertAlmostEqual(complex(complex1(1j)), 2j)
        self.assertRaises(TypeError, complex, complex2(1j))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 192 of file test_complex.py.

00192 
00193             self.fail("should fail 0.0 to negative or complex power")
00194 
00195         # The following is used to exercise certain code paths
00196         self.assertEqual(a ** 105, a ** 105)
        self.assertEqual(a ** -105, a ** -105)

Here is the call graph for this function:

Definition at line 197 of file test_complex.py.

00197 
00198         self.assertEqual(a ** -30, a ** -30)
00199 
        self.assertEqual(0.0j ** 0, 1)

Here is the call graph for this function:

Definition at line 200 of file test_complex.py.

00200 
00201 
00202         b = 5.1+2.3j
00203         self.assertRaises(ValueError, pow, a, b, 0)
00204 
00205     def test_boolcontext(self):
00206         for i in range(100):
00207             self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
00208         self.assertTrue(not complex(0.0, 0.0))
00209 
00210     def test_conjugate(self):
00211         self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
00212 
00213     def test_constructor(self):
00214         class OS:
00215             def __init__(self, value): self.value = value
00216             def __complex__(self): return self.value
00217         class NS(object):
00218             def __init__(self, value): self.value = value
00219             def __complex__(self): return self.value
00220         self.assertEqual(complex(OS(1+10j)), 1+10j)
00221         self.assertEqual(complex(NS(1+10j)), 1+10j)
00222         self.assertRaises(TypeError, complex, OS(None))
00223         self.assertRaises(TypeError, complex, NS(None))
00224         self.assertRaises(TypeError, complex, {})
00225 
00226         self.assertAlmostEqual(complex("1+10j"), 1+10j)
00227         self.assertAlmostEqual(complex(10), 10+0j)
00228         self.assertAlmostEqual(complex(10.0), 10+0j)
00229         self.assertAlmostEqual(complex(10), 10+0j)
00230         self.assertAlmostEqual(complex(10+0j), 10+0j)
00231         self.assertAlmostEqual(complex(1,10), 1+10j)
00232         self.assertAlmostEqual(complex(1,10), 1+10j)
00233         self.assertAlmostEqual(complex(1,10.0), 1+10j)
00234         self.assertAlmostEqual(complex(1,10), 1+10j)
00235         self.assertAlmostEqual(complex(1,10), 1+10j)
00236         self.assertAlmostEqual(complex(1,10.0), 1+10j)
00237         self.assertAlmostEqual(complex(1.0,10), 1+10j)
00238         self.assertAlmostEqual(complex(1.0,10), 1+10j)
00239         self.assertAlmostEqual(complex(1.0,10.0), 1+10j)
00240         self.assertAlmostEqual(complex(3.14+0j), 3.14+0j)
00241         self.assertAlmostEqual(complex(3.14), 3.14+0j)
00242         self.assertAlmostEqual(complex(314), 314.0+0j)
00243         self.assertAlmostEqual(complex(314), 314.0+0j)
00244         self.assertAlmostEqual(complex(3.14+0j, 0j), 3.14+0j)
00245         self.assertAlmostEqual(complex(3.14, 0.0), 3.14+0j)
00246         self.assertAlmostEqual(complex(314, 0), 314.0+0j)
00247         self.assertAlmostEqual(complex(314, 0), 314.0+0j)
00248         self.assertAlmostEqual(complex(0j, 3.14j), -3.14+0j)
00249         self.assertAlmostEqual(complex(0.0, 3.14j), -3.14+0j)
00250         self.assertAlmostEqual(complex(0j, 3.14), 3.14j)
00251         self.assertAlmostEqual(complex(0.0, 3.14), 3.14j)
00252         self.assertAlmostEqual(complex("1"), 1+0j)
00253         self.assertAlmostEqual(complex("1j"), 1j)
00254         self.assertAlmostEqual(complex(),  0)
00255         self.assertAlmostEqual(complex("-1"), -1)
00256         self.assertAlmostEqual(complex("+1"), +1)
00257         self.assertAlmostEqual(complex("(1+2j)"), 1+2j)
00258         self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j)
00259         self.assertAlmostEqual(complex("3.14+1J"), 3.14+1j)
00260         self.assertAlmostEqual(complex(" ( +3.14-6J )"), 3.14-6j)
00261         self.assertAlmostEqual(complex(" ( +3.14-J )"), 3.14-1j)
00262         self.assertAlmostEqual(complex(" ( +3.14+j )"), 3.14+1j)
00263         self.assertAlmostEqual(complex("J"), 1j)
00264         self.assertAlmostEqual(complex("( j )"), 1j)
00265         self.assertAlmostEqual(complex("+J"), 1j)
00266         self.assertAlmostEqual(complex("( -j)"), -1j)
00267         self.assertAlmostEqual(complex('1e-500'), 0.0 + 0.0j)
00268         self.assertAlmostEqual(complex('-1e-500j'), 0.0 - 0.0j)
00269         self.assertAlmostEqual(complex('-1e-500+1e-500j'), -0.0 + 0.0j)
00270 
00271         class complex2(complex): pass
00272         self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j)
00273         self.assertAlmostEqual(complex(real=17, imag=23), 17+23j)
00274         self.assertAlmostEqual(complex(real=17+23j), 17+23j)
00275         self.assertAlmostEqual(complex(real=17+23j, imag=23), 17+46j)
00276         self.assertAlmostEqual(complex(real=1+2j, imag=3+4j), -3+5j)
00277 
00278         # check that the sign of a zero in the real or imaginary part
00279         # is preserved when constructing from two floats.  (These checks
00280         # are harmless on systems without support for signed zeros.)
00281         def split_zeros(x):
00282             """Function that produces different results for 0. and -0."""
00283             return atan2(x, -1.)
00284 
00285         self.assertEqual(split_zeros(complex(1., 0.).imag), split_zeros(0.))
00286         self.assertEqual(split_zeros(complex(1., -0.).imag), split_zeros(-0.))
00287         self.assertEqual(split_zeros(complex(0., 1.).real), split_zeros(0.))
00288         self.assertEqual(split_zeros(complex(-0., 1.).real), split_zeros(-0.))
00289 
00290         c = 3.14 + 1j
00291         self.assertTrue(complex(c) is c)
00292         del c
00293 
00294         self.assertRaises(TypeError, complex, "1", "1")
00295         self.assertRaises(TypeError, complex, 1, "1")
00296 
00297         # SF bug 543840:  complex(string) accepts strings with \0
00298         # Fixed in 2.3.
00299         self.assertRaises(ValueError, complex, '1+1j\0j')
00300 
00301         self.assertRaises(TypeError, int, 5+3j)
00302         self.assertRaises(TypeError, int, 5+3j)
00303         self.assertRaises(TypeError, float, 5+3j)
00304         self.assertRaises(ValueError, complex, "")
00305         self.assertRaises(TypeError, complex, None)
00306         self.assertRaises(ValueError, complex, "\0")
00307         self.assertRaises(ValueError, complex, "3\09")
00308         self.assertRaises(TypeError, complex, "1", "2")
00309         self.assertRaises(TypeError, complex, "1", 42)
00310         self.assertRaises(TypeError, complex, 1, "2")
00311         self.assertRaises(ValueError, complex, "1+")
00312         self.assertRaises(ValueError, complex, "1+1j+1j")
00313         self.assertRaises(ValueError, complex, "--")
00314         self.assertRaises(ValueError, complex, "(1+2j")
00315         self.assertRaises(ValueError, complex, "1+2j)")
00316         self.assertRaises(ValueError, complex, "1+(2j)")
00317         self.assertRaises(ValueError, complex, "(1+2j)123")
00318         self.assertRaises(ValueError, complex, "x")
00319         self.assertRaises(ValueError, complex, "1j+2")
00320         self.assertRaises(ValueError, complex, "1e1ej")
00321         self.assertRaises(ValueError, complex, "1e++1ej")
00322         self.assertRaises(ValueError, complex, ")1+2j(")
00323         # the following three are accepted by Python 2.6
00324         self.assertRaises(ValueError, complex, "1..1j")
00325         self.assertRaises(ValueError, complex, "1.11.1j")
00326         self.assertRaises(ValueError, complex, "1e1.1j")
00327 
00328         # check that complex accepts long unicode strings
00329         self.assertEqual(type(complex("1"*500)), complex)
00330         # check whitespace processing
00331         self.assertEqual(complex('\N{EM SPACE}(\N{EN SPACE}1+1j ) '), 1+1j)
00332 
00333         class EvilExc(Exception):
00334             pass
00335 
00336         class evilcomplex:
00337             def __complex__(self):
00338                 raise EvilExc
00339 
00340         self.assertRaises(EvilExc, complex, evilcomplex())
00341 
00342         class float2:
00343             def __init__(self, value):
00344                 self.value = value
00345             def __float__(self):
00346                 return self.value
00347 
00348         self.assertAlmostEqual(complex(float2(42.)), 42)
00349         self.assertAlmostEqual(complex(real=float2(17.), imag=float2(23.)), 17+23j)
00350         self.assertRaises(TypeError, complex, float2(None))
00351 
00352         class complex0(complex):
00353             """Test usage of __complex__() when inheriting from 'complex'"""
00354             def __complex__(self):
00355                 return 42j
00356 
00357         class complex1(complex):
00358             """Test usage of __complex__() with a __new__() method"""
00359             def __new__(self, value=0j):
00360                 return complex.__new__(self, 2*value)
            def __complex__(self):

Definition at line 149 of file test_complex.py.

00149 
00150             pow = 52 + i
00151             mult = 2 ** i
00152             check(2 ** pow, range(1, 101), lambda delta: delta % mult == 0)
            check(2 ** pow, range(1, 101), lambda delta: False, float(i))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 418 of file test_complex.py.

00418 
00419         test(complex(-0., 1.),  "(-0+1j)")
00420         test(complex(0., -1.),  "-1j")
00421         test(complex(-0., -1.), "(-0-1j)")
00422 
00423         test(complex(0., 0.),   "0j")
00424         test(complex(0., -0.),  "-0j")
00425         test(complex(-0., 0.),  "(-0+0j)")
00426         test(complex(-0., -0.), "(-0-0j)")
00427 
00428     def test_neg(self):
00429         self.assertEqual(-(1+6j), -1-6j)
00430 
00431     def test_file(self):
00432         a = 3.33+4.43j
00433         b = 5.1+2.3j

Here is the call graph for this function:

Definition at line 96 of file test_complex.py.

00096 
00097         for i in range(100):
00098             self.check_div(complex(random(), random()),
00099                            complex(random(), random()))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 499 of file test_complex.py.

00499 
00500         inf, nan = float('inf'), float('nan')
00501         infj, nanj = complex(0.0, inf), complex(0.0, nan)
00502         for x in vals:
00503             for y in vals:
00504                 z = complex(x, y)
00505                 roundtrip = eval(repr(z))
00506                 # adding 0.0 has no effect beside changing -0.0 to 0.0
00507                 self.assertFloatsAreIdentical(0.0 + z.real,
00508                                               0.0 + roundtrip.real)
00509                 self.assertFloatsAreIdentical(0.0 + z.imag,
00510                                               0.0 + roundtrip.imag)
00511 
00512     def test_format(self):
00513         # empty format string is same as str()
00514         self.assertEqual(format(1+3j, ''), str(1+3j))
00515         self.assertEqual(format(1.5+3.5j, ''), str(1.5+3.5j))
00516         self.assertEqual(format(3j, ''), str(3j))
00517         self.assertEqual(format(3.2j, ''), str(3.2j))
00518         self.assertEqual(format(3+0j, ''), str(3+0j))
00519         self.assertEqual(format(3.2+0j, ''), str(3.2+0j))
00520 
00521         # empty presentation type should still be analogous to str,
00522         # even when format string is nonempty (issue #5920).
00523         self.assertEqual(format(3.2+0j, '-'), str(3.2+0j))
00524         self.assertEqual(format(3.2+0j, '<'), str(3.2+0j))
00525         z = 4/7. - 100j/7.
00526         self.assertEqual(format(z, ''), str(z))
00527         self.assertEqual(format(z, '-'), str(z))
00528         self.assertEqual(format(z, '<'), str(z))
00529         self.assertEqual(format(z, '10'), str(z))
00530         z = complex(0.0, 3.0)
00531         self.assertEqual(format(z, ''), str(z))
00532         self.assertEqual(format(z, '-'), str(z))
00533         self.assertEqual(format(z, '<'), str(z))
00534         self.assertEqual(format(z, '2'), str(z))
00535         z = complex(-0.0, 2.0)
00536         self.assertEqual(format(z, ''), str(z))
00537         self.assertEqual(format(z, '-'), str(z))
00538         self.assertEqual(format(z, '<'), str(z))
00539         self.assertEqual(format(z, '3'), str(z))
00540 
00541         self.assertEqual(format(1+3j, 'g'), '1+3j')
00542         self.assertEqual(format(3j, 'g'), '0+3j')
00543         self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j')
00544 
00545         self.assertEqual(format(1.5+3.5j, '+g'), '+1.5+3.5j')
00546         self.assertEqual(format(1.5-3.5j, '+g'), '+1.5-3.5j')
00547         self.assertEqual(format(1.5-3.5j, '-g'), '1.5-3.5j')
00548         self.assertEqual(format(1.5+3.5j, ' g'), ' 1.5+3.5j')
00549         self.assertEqual(format(1.5-3.5j, ' g'), ' 1.5-3.5j')
00550         self.assertEqual(format(-1.5+3.5j, ' g'), '-1.5+3.5j')
00551         self.assertEqual(format(-1.5-3.5j, ' g'), '-1.5-3.5j')
00552 
00553         self.assertEqual(format(-1.5-3.5e-20j, 'g'), '-1.5-3.5e-20j')
00554         self.assertEqual(format(-1.5-3.5j, 'f'), '-1.500000-3.500000j')
00555         self.assertEqual(format(-1.5-3.5j, 'F'), '-1.500000-3.500000j')
00556         self.assertEqual(format(-1.5-3.5j, 'e'), '-1.500000e+00-3.500000e+00j')
00557         self.assertEqual(format(-1.5-3.5j, '.2e'), '-1.50e+00-3.50e+00j')
00558         self.assertEqual(format(-1.5-3.5j, '.2E'), '-1.50E+00-3.50E+00j')
00559         self.assertEqual(format(-1.5e10-3.5e5j, '.2G'), '-1.5E+10-3.5E+05j')
00560 
00561         self.assertEqual(format(1.5+3j, '<20g'),  '1.5+3j              ')
00562         self.assertEqual(format(1.5+3j, '*<20g'), '1.5+3j**************')
00563         self.assertEqual(format(1.5+3j, '>20g'),  '              1.5+3j')
00564         self.assertEqual(format(1.5+3j, '^20g'),  '       1.5+3j       ')
00565         self.assertEqual(format(1.5+3j, '<20'),   '(1.5+3j)            ')
00566         self.assertEqual(format(1.5+3j, '>20'),   '            (1.5+3j)')
00567         self.assertEqual(format(1.5+3j, '^20'),   '      (1.5+3j)      ')
00568         self.assertEqual(format(1.123-3.123j, '^20.2'), '     (1.1-3.1j)     ')
00569 
00570         self.assertEqual(format(1.5+3j, '20.2f'), '          1.50+3.00j')
00571         self.assertEqual(format(1.5+3j, '>20.2f'), '          1.50+3.00j')
00572         self.assertEqual(format(1.5+3j, '<20.2f'), '1.50+3.00j          ')
00573         self.assertEqual(format(1.5e20+3j, '<20.2f'), '150000000000000000000.00+3.00j')
00574         self.assertEqual(format(1.5e20+3j, '>40.2f'), '          150000000000000000000.00+3.00j')
00575         self.assertEqual(format(1.5e20+3j, '^40,.2f'), '  150,000,000,000,000,000,000.00+3.00j  ')
00576         self.assertEqual(format(1.5e21+3j, '^40,.2f'), ' 1,500,000,000,000,000,000,000.00+3.00j ')
00577         self.assertEqual(format(1.5e21+3000j, ',.2f'), '1,500,000,000,000,000,000,000.00+3,000.00j')
00578 
00579         # Issue 7094: Alternate formatting (specified by #)
00580         self.assertEqual(format(1+1j, '.0e'), '1e+00+1e+00j')
00581         self.assertEqual(format(1+1j, '#.0e'), '1.e+00+1.e+00j')
00582         self.assertEqual(format(1+1j, '.0f'), '1+1j')
00583         self.assertEqual(format(1+1j, '#.0f'), '1.+1.j')
00584         self.assertEqual(format(1.1+1.1j, 'g'), '1.1+1.1j')
00585         self.assertEqual(format(1.1+1.1j, '#g'), '1.10000+1.10000j')
00586 
00587         # Alternate doesn't make a difference for these, they format the same with or without it
00588         self.assertEqual(format(1+1j, '.1e'),  '1.0e+00+1.0e+00j')
00589         self.assertEqual(format(1+1j, '#.1e'), '1.0e+00+1.0e+00j')
00590         self.assertEqual(format(1+1j, '.1f'),  '1.0+1.0j')
00591         self.assertEqual(format(1+1j, '#.1f'), '1.0+1.0j')
00592 
00593         # Misc. other alternate tests
00594         self.assertEqual(format((-1.5+0.5j), '#f'), '-1.500000+0.500000j')
00595         self.assertEqual(format((-1.5+0.5j), '#.0f'), '-2.+0.j')
00596         self.assertEqual(format((-1.5+0.5j), '#e'), '-1.500000e+00+5.000000e-01j')
00597         self.assertEqual(format((-1.5+0.5j), '#.0e'), '-2.e+00+5.e-01j')
00598         self.assertEqual(format((-1.5+0.5j), '#g'), '-1.50000+0.500000j')
00599         self.assertEqual(format((-1.5+0.5j), '.0g'), '-2+0.5j')
00600         self.assertEqual(format((-1.5+0.5j), '#.0g'), '-2.+0.5j')
00601 
00602         # zero padding is invalid
00603         self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f')
00604 
00605         # '=' alignment is invalid
00606         self.assertRaises(ValueError, (1.5+3j).__format__, '=20')
00607 
00608         # integer presentation types are an error
00609         for t in 'bcdoxX':
00610             self.assertRaises(ValueError, (1.5+0.5j).__format__, t)
00611 
00612         # make sure everything works in ''.format()
00613         self.assertEqual('*{0:.3f}*'.format(3.14159+2.71828j), '*3.142+2.718j*')
00614 
00615         # issue 3382
00616         self.assertEqual(format(complex(NAN, NAN), 'f'), 'nan+nanj')
00617         self.assertEqual(format(complex(1, NAN), 'f'), '1.000000+nanj')
00618         self.assertEqual(format(complex(NAN, 1), 'f'), 'nan+1.000000j')
00619         self.assertEqual(format(complex(NAN, -1), 'f'), 'nan-1.000000j')
        self.assertEqual(format(complex(NAN, NAN), 'F'), 'NAN+NANj')

Here is the call graph for this function:

Definition at line 434 of file test_complex.py.

00434 
00435         fo = None
00436         try:
00437             fo = open(support.TESTFN, "w")
00438             print(a, b, file=fo)
00439             fo.close()
00440             fo = open(support.TESTFN, "r")
00441             self.assertEqual(fo.read(), ("%s %s\n" % (a, b)))
        finally:

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 361 of file test_complex.py.

00361 
00362                 return self
00363 
00364         class complex2(complex):
00365             """Make sure that __complex__() calls fail if anything other than a
00366             complex is returned"""
            def __complex__(self):

Here is the caller graph for this function:

Definition at line 143 of file test_complex.py.

00143 
00144                 self.assertIs(complex.__eq__(z, i), is_equal(delta))
00145                 self.assertIs(complex.__ne__(z, i), not is_equal(delta))
00146         # For IEEE-754 doubles the following should hold:
00147         #    x in [2 ** (52 + i), 2 ** (53 + i + 1)] -> x mod 2 ** i == 0
00148         # where the interval is representable, of course.
        for i in range(1, 10):

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 415 of file test_complex.py.

00415 
00416             test_fn(str(v), expected)
00417 
        test(complex(0., 1.),   "1j")

Here is the caller graph for this function:

Definition at line 450 of file test_complex.py.

00450 
00451         self.assertEqual((-0j).__getnewargs__(), (0.0, -0.0))
00452         self.assertEqual(complex(0, INF).__getnewargs__(), (0.0, INF))
00453         self.assertEqual(complex(INF, 0).__getnewargs__(), (INF, 0.0))
00454 
00455     @support.requires_IEEE_754
00456     def test_plus_minus_0j(self):
00457         # test that -0j and 0j literals are not identified
00458         z1, z2 = 0j, -0j
00459         self.assertEqual(atan2(z1.imag, -1.), atan2(0., -1.))
00460         self.assertEqual(atan2(z2.imag, -1.), atan2(-0., -1.))
00461 
00462     @support.requires_IEEE_754
00463     def test_negated_imaginary_literal(self):
00464         z0 = -0j
        z1 = -7j

Here is the call graph for this function:

Definition at line 400 of file test_complex.py.

00400 
00401         test(complex(NAN, NAN), "(nan+nanj)")
00402 
00403         test(complex(0, INF), "infj")
00404         test(complex(0, -INF), "-infj")
00405         test(complex(0, NAN), "nanj")
00406 
00407         self.assertEqual(1-6j,complex(repr(1-6j)))
00408         self.assertEqual(1+6j,complex(repr(1+6j)))
00409         self.assertEqual(-6j,complex(repr(-6j)))
00410         self.assertEqual(6j,complex(repr(6j)))
00411 
00412     @support.requires_IEEE_754
00413     def test_negative_zero_repr_str(self):
00414         def test(v, expected, test_fn=self.assertEqual):
            test_fn(repr(v), expected)

Here is the call graph for this function:

Definition at line 466 of file test_complex.py.

00466 
00467         # Note: In versions of Python < 3.2, a negated imaginary literal
00468         # accidentally ended up with real part 0.0 instead of -0.0, thanks to a
00469         # modification during CST -> AST translation (see issue #9011).  That's
00470         # fixed in Python 3.2.
        self.assertFloatsAreIdentical(z0.real, -0.0)

Here is the caller graph for this function:

Definition at line 443 of file test_complex.py.

00443 
00444                 fo.close()
00445             support.unlink(support.TESTFN)
00446 
00447     def test_getnewargs(self):
00448         self.assertEqual((1+2j).__getnewargs__(), (1.0, 2.0))
        self.assertEqual((1-2j).__getnewargs__(), (1.0, -2.0))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 153 of file test_complex.py.

00153 
00154         check(2 ** 53, range(-100, 0), lambda delta: True)
00155 
00156     def test_mod(self):
00157         # % is no longer supported on complex numbers
00158         self.assertRaises(TypeError, (1+1j).__mod__, 0+0j)
00159         self.assertRaises(TypeError, lambda: (3.33+4.43j) % 0)
00160         self.assertRaises(TypeError, (1+1j).__mod__, 4.3j)
00161 
00162     def test_divmod(self):
00163         self.assertRaises(TypeError, divmod, 1+1j, 1+0j)
00164         self.assertRaises(TypeError, divmod, 1+1j, 0+0j)
00165 
00166     def test_pow(self):
00167         self.assertAlmostEqual(pow(1+1j, 0+0j), 1.0)
00168         self.assertAlmostEqual(pow(0+0j, 2+0j), 0.0)
00169         self.assertRaises(ZeroDivisionError, pow, 0+0j, 1j)
00170         self.assertAlmostEqual(pow(1j, -1), 1/1j)
00171         self.assertAlmostEqual(pow(1j, 200), 1)
00172         self.assertRaises(ValueError, pow, 1+1j, 1+1j, 1+1j)
00173 
00174         a = 3.33+4.43j
00175         self.assertEqual(a ** 0j, 1)
00176         self.assertEqual(a ** 0.+0.j, 1)
00177 
00178         self.assertEqual(3j ** 0j, 1)
00179         self.assertEqual(3j ** 0, 1)
00180 
00181         try:
00182             0j ** a
00183         except ZeroDivisionError:
00184             pass
00185         else:
00186             self.fail("should fail 0.0 to negative or complex power")
00187 
00188         try:
00189             0j ** (3-2j)
00190         except ZeroDivisionError:
00191             pass
        else:

Here is the call graph for this function:

Definition at line 472 of file test_complex.py.

00472 
00473         self.assertFloatsAreIdentical(z1.real, -0.0)
00474         self.assertFloatsAreIdentical(z1.imag, -7.0)
00475         self.assertFloatsAreIdentical(z2.real, -0.0)
00476         self.assertFloatsAreIdentical(z2.imag, -INF)
00477 
00478     @support.requires_IEEE_754
00479     def test_overflow(self):
00480         self.assertEqual(complex("1e500"), complex(INF, 0.0))
00481         self.assertEqual(complex("-1e500j"), complex(0.0, -INF))
00482         self.assertEqual(complex("-1e500+1.8e308j"), complex(-INF, INF))
00483 
00484     @support.requires_IEEE_754
00485     def test_repr_roundtrip(self):
00486         vals = [0.0, 1e-500, 1e-315, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN]
00487         vals += [-v for v in vals]
00488 
00489         # complex(repr(z)) should recover z exactly, even for complex
00490         # numbers involving an infinity, nan, or negative zero
00491         for x in vals:
00492             for y in vals:
00493                 z = complex(x, y)
00494                 roundtrip = complex(repr(z))
00495                 self.assertFloatsAreIdentical(z.real, roundtrip.real)
00496                 self.assertFloatsAreIdentical(z.imag, roundtrip.imag)
00497 
00498         # if we predefine some constants, then eval(repr(z)) should
        # also work, except that it might change the sign of zeros

Here is the call graph for this function:

Definition at line 372 of file test_complex.py.

00372 
00373 
00374     def test_hash(self):
00375         for x in range(-30, 30):
00376             self.assertEqual(hash(x), hash(complex(x, 0)))
00377             x /= 3.0    # now check against floating point
00378             self.assertEqual(hash(x), hash(complex(x, 0.)))
00379 
00380     def test_abs(self):
00381         nums = [complex(x/3., y/7.) for x in range(-9,9) for y in range(-9,9)]
00382         for num in nums:
00383             self.assertAlmostEqual((num.real**2 + num.imag**2)  ** 0.5, abs(num))
00384 
00385     def test_repr_str(self):
00386         def test(v, expected, test_fn=self.assertEqual):
00387             test_fn(repr(v), expected)
00388             test_fn(str(v), expected)
00389 
00390         test(1+6j, '(1+6j)')
00391         test(1-6j, '(1-6j)')
00392 
00393         test(-(1+0j), '(-1+-0j)', test_fn=self.assertNotEqual)
00394 
00395         test(complex(1., INF), "(1+infj)")
00396         test(complex(1., -INF), "(1-infj)")
00397         test(complex(INF, 1), "(inf+1j)")
00398         test(complex(-INF, INF), "(-inf+infj)")
        test(complex(NAN, 1), "(nan+1j)")

Here is the call graph for this function:

Definition at line 100 of file test_complex.py.

00100 
00101         self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
00102         # FIXME: The following currently crashes on Alpha
00103         # self.assertRaises(OverflowError, pow, 1e200+1j, 1e200+1j)
00104 
00105     def test_truediv(self):
00106         self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)
00107         self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
00108 
00109     def test_floordiv(self):
00110         self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 1.5+0j)
00111         self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 0+0j)
00112 
00113     def test_richcompare(self):
00114         self.assertIs(complex.__eq__(1+1j, 1<<10000), False)
00115         self.assertIs(complex.__lt__(1+1j, None), NotImplemented)
00116         self.assertIs(complex.__eq__(1+1j, 1+1j), True)
00117         self.assertIs(complex.__eq__(1+1j, 2+2j), False)
00118         self.assertIs(complex.__ne__(1+1j, 1+1j), False)
00119         self.assertIs(complex.__ne__(1+1j, 2+2j), True)
00120         for i in range(1, 100):
00121             f = i / 100.0
00122             self.assertIs(complex.__eq__(f+0j, f), True)
00123             self.assertIs(complex.__ne__(f+0j, f), False)
00124             self.assertIs(complex.__eq__(complex(f, f), f), False)
00125             self.assertIs(complex.__ne__(complex(f, f), f), True)
        self.assertIs(complex.__lt__(1+1j, 2+2j), NotImplemented)

Here is the call graph for this function:

Definition at line 126 of file test_complex.py.

00126 
00127         self.assertIs(complex.__le__(1+1j, 2+2j), NotImplemented)
00128         self.assertIs(complex.__gt__(1+1j, 2+2j), NotImplemented)
00129         self.assertIs(complex.__ge__(1+1j, 2+2j), NotImplemented)
00130         self.assertRaises(TypeError, operator.lt, 1+1j, 2+2j)
00131         self.assertRaises(TypeError, operator.le, 1+1j, 2+2j)
00132         self.assertRaises(TypeError, operator.gt, 1+1j, 2+2j)
00133         self.assertRaises(TypeError, operator.ge, 1+1j, 2+2j)
00134         self.assertIs(operator.eq(1+1j, 1+1j), True)
00135         self.assertIs(operator.eq(1+1j, 2+2j), False)
00136         self.assertIs(operator.ne(1+1j, 1+1j), False)
00137         self.assertIs(operator.ne(1+1j, 2+2j), True)
00138 
00139     def test_richcompare_boundaries(self):
00140         def check(n, deltas, is_equal, imag = 0.0):
00141             for delta in deltas:
00142                 i = n + delta
                z = complex(i, imag)

Here is the call graph for this function:

Definition at line 71 of file test_complex.py.

00071 
00072         z = x * y
00073         if x != 0:
00074             q = z / x
00075             self.assertClose(q, y)
00076             q = z.__truediv__(x)
00077             self.assertClose(q, y)
00078         if y != 0:
00079             q = z / y
00080             self.assertClose(q, x)
00081             q = z.__truediv__(y)
00082             self.assertClose(q, x)
00083 
00084     def test_truediv(self):
00085         simple_real = [float(i) for i in range(-5, 6)]
00086         simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
00087         for x in simple_complex:
00088             for y in simple_complex:
00089                 self.check_div(x, y)
00090 
00091         # A naive complex division algorithm (such as in 2.0) is very prone to
        # nonsense errors for these (overflows and underflows).

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 92 of file test_complex.py.

00092 
00093         self.check_div(complex(1e200, 1e200), 1+0j)
00094         self.check_div(complex(1e-200, 1e-200), 1+0j)
00095 
        # Just for fun.

Here is the call graph for this function:


Member Data Documentation

Definition at line 202 of file test_complex.py.


The documentation for this class was generated from the following file: