<s> import unittest <EOL> from test import test_support <EOL> from random import random <EOL> from math import atan2 , isnan , copysign <EOL> INF = float ( "<STR_LIT>" ) <EOL> NAN = float ( "<STR_LIT>" ) <EOL> class ComplexTest ( unittest . TestCase ) : <EOL> def assertAlmostEqual ( self , a , b ) : <EOL> if isinstance ( a , complex ) : <EOL> if isinstance ( b , complex ) : <EOL> unittest . TestCase . assertAlmostEqual ( self , a . real , b . real ) <EOL> unittest . TestCase . assertAlmostEqual ( self , a . imag , b . imag ) <EOL> else : <EOL> unittest . TestCase . assertAlmostEqual ( self , a . real , b ) <EOL> unittest . TestCase . assertAlmostEqual ( self , a . imag , <NUM_LIT:0.> ) <EOL> else : <EOL> if isinstance ( b , complex ) : <EOL> unittest . TestCase . assertAlmostEqual ( self , a , b . real ) <EOL> unittest . TestCase . assertAlmostEqual ( self , <NUM_LIT:0.> , b . imag ) <EOL> else : <EOL> unittest . TestCase . assertAlmostEqual ( self , a , b ) <EOL> def assertCloseAbs ( self , x , y , eps = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if abs ( x ) > abs ( y ) : <EOL> x , y = y , x <EOL> if y == <NUM_LIT:0> : <EOL> return abs ( x ) < eps <EOL> if x == <NUM_LIT:0> : <EOL> return abs ( y ) < eps <EOL> self . assertTrue ( abs ( ( x - y ) / y ) < eps ) <EOL> def assertFloatsAreIdentical ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' <EOL> if isnan ( x ) or isnan ( y ) : <EOL> if isnan ( x ) and isnan ( y ) : <EOL> return <EOL> elif x == y : <EOL> if x != <NUM_LIT:0.0> : <EOL> return <EOL> elif copysign ( <NUM_LIT:1.0> , x ) == copysign ( <NUM_LIT:1.0> , y ) : <EOL> return <EOL> else : <EOL> msg += '<STR_LIT>' <EOL> self . fail ( msg . format ( x , y ) ) <EOL> def assertClose ( self , x , y , eps = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . assertCloseAbs ( x . real , y . real , eps ) <EOL> self . assertCloseAbs ( x . imag , y . imag , eps ) <EOL> def check_div ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> z = x * y <EOL> if x != <NUM_LIT:0> : <EOL> q = z / x <EOL> self . assertClose ( q , y ) <EOL> q = z . __div__ ( x ) <EOL> self . assertClose ( q , y ) <EOL> q = z . __truediv__ ( x ) <EOL> self . assertClose ( q , y ) <EOL> if y != <NUM_LIT:0> : <EOL> q = z / y <EOL> self . assertClose ( q , x ) <EOL> q = z . __div__ ( y ) <EOL> self . assertClose ( q , x ) <EOL> q = z . __truediv__ ( y ) <EOL> self . assertClose ( q , x ) <EOL> def test_div ( self ) : <EOL> simple_real = [ float ( i ) for i in xrange ( - <NUM_LIT:5> , <NUM_LIT:6> ) ] <EOL> simple_complex = [ complex ( x , y ) for x in simple_real for y in simple_real ] <EOL> for x in simple_complex : <EOL> for y in simple_complex : <EOL> self . check_div ( x , y ) <EOL> self . check_div ( complex ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . check_div ( complex ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> self . check_div ( complex ( random ( ) , random ( ) ) , <EOL> complex ( random ( ) , random ( ) ) ) <EOL> self . assertRaises ( ZeroDivisionError , complex . __div__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:0> + <NUM_LIT> ) <EOL> self . assertRaises ( OverflowError , pow , <NUM_LIT> + <NUM_LIT> , <NUM_LIT> + <NUM_LIT> ) <EOL> def test_truediv ( self ) : <EOL> self . assertAlmostEqual ( complex . __truediv__ ( <NUM_LIT:2> + <NUM_LIT> , <NUM_LIT:1> + <NUM_LIT> ) , <NUM_LIT:1> - <NUM_LIT> ) <EOL> self . assertRaises ( ZeroDivisionError , complex . __truediv__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:0> + <NUM_LIT> ) <EOL> for denom_real , denom_imag in [ ( <NUM_LIT:0> , NAN ) , ( NAN , <NUM_LIT:0> ) , ( NAN , NAN ) ] : <EOL> z = complex ( <NUM_LIT:0> , <NUM_LIT:0> ) / complex ( denom_real , denom_imag ) <EOL> self . assertTrue ( isnan ( z . real ) ) <EOL> self . assertTrue ( isnan ( z . imag ) ) <EOL> def test_floordiv ( self ) : <EOL> self . assertAlmostEqual ( complex . __floordiv__ ( <NUM_LIT:3> + <NUM_LIT> , <NUM_LIT> + <NUM_LIT> ) , <NUM_LIT:2> ) <EOL> self . assertRaises ( ZeroDivisionError , complex . __floordiv__ , <NUM_LIT:3> + <NUM_LIT> , <NUM_LIT:0> + <NUM_LIT> ) <EOL> def test_coerce ( self ) : <EOL> self . assertRaises ( OverflowError , complex . __coerce__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:1> L << <NUM_LIT> ) <EOL> def test_no_implicit_coerce ( self ) : <EOL> class A ( object ) : <EOL> def __coerce__ ( self , other ) : <EOL> raise RuntimeError <EOL> __hash__ = None <EOL> def __cmp__ ( self , other ) : <EOL> return - <NUM_LIT:1> <EOL> a = A ( ) <EOL> self . assertRaises ( TypeError , lambda : a + <NUM_LIT> ) <EOL> self . assertTrue ( a < <NUM_LIT> ) <EOL> def test_richcompare ( self ) : <EOL> self . assertEqual ( complex . __eq__ ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:1> L << <NUM_LIT> ) , False ) <EOL> self . assertEqual ( complex . __lt__ ( <NUM_LIT:1> + <NUM_LIT> , None ) , NotImplemented ) <EOL> self . assertIs ( complex . __eq__ ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:1> + <NUM_LIT> ) , True ) <EOL> self . assertIs ( complex . __eq__ ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) , False ) <EOL> self . assertIs ( complex . __ne__ ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:1> + <NUM_LIT> ) , False ) <EOL> self . assertIs ( complex . __ne__ ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) , True ) <EOL> self . assertRaises ( TypeError , complex . __lt__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex . __le__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex . __gt__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex . __ge__ , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) <EOL> def test_richcompare_boundaries ( self ) : <EOL> def check ( n , deltas , is_equal , imag = <NUM_LIT:0.0> ) : <EOL> for delta in deltas : <EOL> i = n + delta <EOL> z = complex ( i , imag ) <EOL> self . assertIs ( complex . __eq__ ( z , i ) , is_equal ( delta ) ) <EOL> self . assertIs ( complex . __ne__ ( z , i ) , not is_equal ( delta ) ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> pow = <NUM_LIT> + i <EOL> mult = <NUM_LIT:2> ** i <EOL> check ( <NUM_LIT:2> ** pow , range ( <NUM_LIT:1> , <NUM_LIT> ) , lambda delta : delta % mult == <NUM_LIT:0> ) <EOL> check ( <NUM_LIT:2> ** pow , range ( <NUM_LIT:1> , <NUM_LIT> ) , lambda delta : False , float ( i ) ) <EOL> check ( <NUM_LIT:2> ** <NUM_LIT> , range ( - <NUM_LIT:100> , <NUM_LIT:0> ) , lambda delta : True ) <EOL> def test_mod ( self ) : <EOL> self . assertRaises ( ZeroDivisionError , ( <NUM_LIT:1> + <NUM_LIT> ) . __mod__ , <NUM_LIT:0> + <NUM_LIT> ) <EOL> a = <NUM_LIT> + <NUM_LIT> <EOL> try : <EOL> a % <NUM_LIT:0> <EOL> except ZeroDivisionError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_divmod ( self ) : <EOL> self . assertRaises ( ZeroDivisionError , divmod , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:0> + <NUM_LIT> ) <EOL> def test_pow ( self ) : <EOL> self . assertAlmostEqual ( pow ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:0> + <NUM_LIT> ) , <NUM_LIT:1.0> ) <EOL> self . assertAlmostEqual ( pow ( <NUM_LIT:0> + <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ZeroDivisionError , pow , <NUM_LIT:0> + <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( pow ( <NUM_LIT> , - <NUM_LIT:1> ) , <NUM_LIT:1> / <NUM_LIT> ) <EOL> self . assertAlmostEqual ( pow ( <NUM_LIT> , <NUM_LIT:200> ) , <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , pow , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT:1> + <NUM_LIT> ) <EOL> a = <NUM_LIT> + <NUM_LIT> <EOL> self . assertEqual ( a ** <NUM_LIT> , <NUM_LIT:1> ) <EOL> self . assertEqual ( a ** <NUM_LIT:0.> + <NUM_LIT> , <NUM_LIT:1> ) <EOL> self . assertEqual ( <NUM_LIT> ** <NUM_LIT> , <NUM_LIT:1> ) <EOL> self . assertEqual ( <NUM_LIT> ** <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> try : <EOL> <NUM_LIT> ** a <EOL> except ZeroDivisionError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> <NUM_LIT> ** ( <NUM_LIT:3> - <NUM_LIT> ) <EOL> except ZeroDivisionError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( a ** <NUM_LIT> , a ** <NUM_LIT> ) <EOL> self . assertEqual ( a ** - <NUM_LIT> , a ** - <NUM_LIT> ) <EOL> self . assertEqual ( a ** - <NUM_LIT:30> , a ** - <NUM_LIT:30> ) <EOL> self . assertEqual ( <NUM_LIT> ** <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> b = <NUM_LIT> + <NUM_LIT> <EOL> self . assertRaises ( ValueError , pow , a , b , <NUM_LIT:0> ) <EOL> def test_boolcontext ( self ) : <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> self . assertTrue ( complex ( random ( ) + <NUM_LIT> , random ( ) + <NUM_LIT> ) ) <EOL> self . assertTrue ( not complex ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) ) <EOL> def test_conjugate ( self ) : <EOL> self . assertClose ( complex ( <NUM_LIT> , <NUM_LIT> ) . conjugate ( ) , <NUM_LIT> - <NUM_LIT> ) <EOL> def test_constructor ( self ) : <EOL> class OS : <EOL> def __init__ ( self , value ) : self . value = value <EOL> def __complex__ ( self ) : return self . value <EOL> class NS ( object ) : <EOL> def __init__ ( self , value ) : self . value = value <EOL> def __complex__ ( self ) : return self . value <EOL> self . assertEqual ( complex ( OS ( <NUM_LIT:1> + <NUM_LIT> ) ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertEqual ( complex ( NS ( <NUM_LIT:1> + <NUM_LIT> ) ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex , OS ( None ) ) <EOL> self . assertRaises ( TypeError , complex , NS ( None ) ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:10> ) , <NUM_LIT:10> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> ) , <NUM_LIT:10> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:10> L ) , <NUM_LIT:10> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:10> + <NUM_LIT> ) , <NUM_LIT:10> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1> , <NUM_LIT:10> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1> , <NUM_LIT:10> L ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1> , <NUM_LIT> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1> L , <NUM_LIT:10> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1> L , <NUM_LIT:10> L ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1> L , <NUM_LIT> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1.0> , <NUM_LIT:10> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1.0> , <NUM_LIT:10> L ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:1.0> , <NUM_LIT> ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> + <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> L ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> + <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> , <NUM_LIT:0.0> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> , <NUM_LIT:0> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> L , <NUM_LIT:0> L ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> , <NUM_LIT> ) , - <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:0.0> , <NUM_LIT> ) , - <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( <NUM_LIT:0.0> , <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT:1>" ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( ) , <NUM_LIT:0> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , - <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , + <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> - <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> - <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( "<STR_LIT>" ) , - <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( '<STR_LIT>' ) , <NUM_LIT:0.0> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( '<STR_LIT>' ) , <NUM_LIT:0.0> - <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( '<STR_LIT>' ) , - <NUM_LIT:0.0> + <NUM_LIT> ) <EOL> class complex2 ( complex ) : pass <EOL> self . assertAlmostEqual ( complex ( complex2 ( <NUM_LIT:1> + <NUM_LIT> ) ) , <NUM_LIT:1> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( real = <NUM_LIT> , imag = <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( real = <NUM_LIT> + <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( real = <NUM_LIT> + <NUM_LIT> , imag = <NUM_LIT> ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( real = <NUM_LIT:1> + <NUM_LIT> , imag = <NUM_LIT:3> + <NUM_LIT> ) , - <NUM_LIT:3> + <NUM_LIT> ) <EOL> def split_zeros ( x ) : <EOL> """<STR_LIT>""" <EOL> return atan2 ( x , - <NUM_LIT:1.> ) <EOL> self . assertEqual ( split_zeros ( complex ( <NUM_LIT:1.> , <NUM_LIT:0.> ) . imag ) , split_zeros ( <NUM_LIT:0.> ) ) <EOL> self . assertEqual ( split_zeros ( complex ( <NUM_LIT:1.> , - <NUM_LIT:0.> ) . imag ) , split_zeros ( - <NUM_LIT:0.> ) ) <EOL> self . assertEqual ( split_zeros ( complex ( <NUM_LIT:0.> , <NUM_LIT:1.> ) . real ) , split_zeros ( <NUM_LIT:0.> ) ) <EOL> self . assertEqual ( split_zeros ( complex ( - <NUM_LIT:0.> , <NUM_LIT:1.> ) . real ) , split_zeros ( - <NUM_LIT:0.> ) ) <EOL> c = <NUM_LIT> + <NUM_LIT> <EOL> self . assertTrue ( complex ( c ) is c ) <EOL> del c <EOL> self . assertRaises ( TypeError , complex , "<STR_LIT:1>" , "<STR_LIT:1>" ) <EOL> self . assertRaises ( TypeError , complex , <NUM_LIT:1> , "<STR_LIT:1>" ) <EOL> if test_support . have_unicode : <EOL> self . assertEqual ( complex ( unicode ( "<STR_LIT>" ) ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , complex , '<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , int , <NUM_LIT:5> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , long , <NUM_LIT:5> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , float , <NUM_LIT:5> + <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( TypeError , complex , None ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( TypeError , complex , "<STR_LIT:1>" , "<STR_LIT:2>" ) <EOL> self . assertRaises ( TypeError , complex , "<STR_LIT:1>" , <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex , <NUM_LIT:1> , "<STR_LIT:2>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> if test_support . have_unicode : <EOL> self . assertRaises ( ValueError , complex , unicode ( "<STR_LIT:x>" ) ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , complex , "<STR_LIT>" ) <EOL> if test_support . have_unicode : <EOL> self . assertEqual ( type ( complex ( unicode ( "<STR_LIT:1>" * <NUM_LIT> ) ) ) , complex ) <EOL> class EvilExc ( Exception ) : <EOL> pass <EOL> class evilcomplex : <EOL> def __complex__ ( self ) : <EOL> raise EvilExc <EOL> self . assertRaises ( EvilExc , complex , evilcomplex ( ) ) <EOL> class float2 : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __float__ ( self ) : <EOL> return self . value <EOL> self . assertAlmostEqual ( complex ( float2 ( <NUM_LIT> ) ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( real = float2 ( <NUM_LIT> ) , imag = float2 ( <NUM_LIT> ) ) , <NUM_LIT> + <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex , float2 ( None ) ) <EOL> class complex0 ( complex ) : <EOL> """<STR_LIT>""" <EOL> def __complex__ ( self ) : <EOL> return <NUM_LIT> <EOL> class complex1 ( complex ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( self , value = <NUM_LIT> ) : <EOL> return complex . __new__ ( self , <NUM_LIT:2> * value ) <EOL> def __complex__ ( self ) : <EOL> return self <EOL> class complex2 ( complex ) : <EOL> """<STR_LIT>""" <EOL> def __complex__ ( self ) : <EOL> return None <EOL> self . assertAlmostEqual ( complex ( complex0 ( <NUM_LIT> ) ) , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( complex ( complex1 ( <NUM_LIT> ) ) , <NUM_LIT> ) <EOL> self . assertRaises ( TypeError , complex , complex2 ( <NUM_LIT> ) ) <EOL> def test_subclass ( self ) : <EOL> class xcomplex ( complex ) : <EOL> def __add__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) + other ) <EOL> __radd__ = __add__ <EOL> def __sub__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) + other ) <EOL> __rsub__ = __sub__ <EOL> def __mul__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) * other ) <EOL> __rmul__ = __mul__ <EOL> def __div__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) / other ) <EOL> def __rdiv__ ( self , other ) : <EOL> return xcomplex ( other / complex ( self ) ) <EOL> __truediv__ = __div__ <EOL> __rtruediv__ = __rdiv__ <EOL> def __floordiv__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) // other ) <EOL> def __rfloordiv__ ( self , other ) : <EOL> return xcomplex ( other // complex ( self ) ) <EOL> def __pow__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) ** other ) <EOL> def __rpow__ ( self , other ) : <EOL> return xcomplex ( other ** complex ( self ) ) <EOL> def __mod__ ( self , other ) : <EOL> return xcomplex ( complex ( self ) % other ) <EOL> def __rmod__ ( self , other ) : <EOL> return xcomplex ( other % complex ( self ) ) <EOL> infix_binops = ( '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:*>' , '<STR_LIT>' , '<STR_LIT:%>' , '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> xcomplex_values = ( xcomplex ( <NUM_LIT:1> ) , xcomplex ( <NUM_LIT> ) , <EOL> xcomplex ( - <NUM_LIT:10> + <NUM_LIT> ) , xcomplex ( <NUM_LIT:3> + <NUM_LIT> ) , <EOL> xcomplex ( <NUM_LIT:3> - <NUM_LIT> ) ) <EOL> test_values = ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:10> - <NUM_LIT> , xcomplex ( <NUM_LIT:1> + <NUM_LIT> ) , <EOL> xcomplex ( <NUM_LIT:1> + <NUM_LIT> ) , xcomplex ( <NUM_LIT:10> + <NUM_LIT> ) ) <EOL> for op in infix_binops : <EOL> for x in xcomplex_values : <EOL> for y in test_values : <EOL> a = '<STR_LIT>' % op <EOL> b = '<STR_LIT>' % op <EOL> def test_hash ( self ) : <EOL> for x in xrange ( - <NUM_LIT:30> , <NUM_LIT:30> ) : <EOL> self . assertEqual ( hash ( x ) , hash ( complex ( x , <NUM_LIT:0> ) ) ) <EOL> x /= <NUM_LIT> <EOL> self . assertEqual ( hash ( x ) , hash ( complex ( x , <NUM_LIT:0.> ) ) ) <EOL> def test_abs ( self ) : <EOL> nums = [ complex ( x / <NUM_LIT> , y / <NUM_LIT> ) for x in xrange ( - <NUM_LIT:9> , <NUM_LIT:9> ) for y in xrange ( - <NUM_LIT:9> , <NUM_LIT:9> ) ] <EOL> for num in nums : <EOL> self . assertAlmostEqual ( ( num . real ** <NUM_LIT:2> + num . imag ** <NUM_LIT:2> ) ** <NUM_LIT:0.5> , abs ( num ) ) <EOL> def test_repr ( self ) : <EOL> self . assertEqual ( repr ( <NUM_LIT:1> + <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( repr ( <NUM_LIT:1> - <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertNotEqual ( repr ( - ( <NUM_LIT:1> + <NUM_LIT> ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> - <NUM_LIT> , complex ( repr ( <NUM_LIT:1> - <NUM_LIT> ) ) ) <EOL> self . assertEqual ( <NUM_LIT:1> + <NUM_LIT> , complex ( repr ( <NUM_LIT:1> + <NUM_LIT> ) ) ) <EOL> self . assertEqual ( - <NUM_LIT> , complex ( repr ( - <NUM_LIT> ) ) ) <EOL> self . assertEqual ( <NUM_LIT> , complex ( repr ( <NUM_LIT> ) ) ) <EOL> self . assertEqual ( repr ( complex ( <NUM_LIT:1.> , INF ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( <NUM_LIT:1.> , - INF ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( INF , <NUM_LIT:1> ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( - INF , INF ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( NAN , <NUM_LIT:1> ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( <NUM_LIT:1> , NAN ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( NAN , NAN ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( <NUM_LIT:0> , INF ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( <NUM_LIT:0> , - INF ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( complex ( <NUM_LIT:0> , NAN ) ) , "<STR_LIT>" ) <EOL> def test_file ( self ) : <EOL> a = <NUM_LIT> + <NUM_LIT> <EOL> b = <NUM_LIT> + <NUM_LIT> <EOL> fo = None <EOL> try : <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:wb>" ) <EOL> print >> fo , a , b <EOL> fo . close ( ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:rb>" ) <EOL> self . assertEqual ( fo . read ( ) , "<STR_LIT>" % ( a , b ) ) <EOL> finally : <EOL> if ( fo is not None ) and ( not fo . closed ) : <EOL> fo . close ( ) <EOL> test_support . unlink ( test_support . TESTFN ) <EOL> def test_getnewargs ( self ) : <EOL> self . assertEqual ( ( <NUM_LIT:1> + <NUM_LIT> ) . __getnewargs__ ( ) , ( <NUM_LIT:1.0> , <NUM_LIT> ) ) <EOL> self . assertEqual ( ( <NUM_LIT:1> - <NUM_LIT> ) . __getnewargs__ ( ) , ( <NUM_LIT:1.0> , - <NUM_LIT> ) ) <EOL> self . assertEqual ( ( <NUM_LIT> ) . __getnewargs__ ( ) , ( <NUM_LIT:0.0> , <NUM_LIT> ) ) <EOL> self . assertEqual ( ( - <NUM_LIT> ) . __getnewargs__ ( ) , ( <NUM_LIT:0.0> , - <NUM_LIT:0.0> ) ) <EOL> self . assertEqual ( complex ( <NUM_LIT:0> , INF ) . __getnewargs__ ( ) , ( <NUM_LIT:0.0> , INF ) ) <EOL> self . assertEqual ( complex ( INF , <NUM_LIT:0> ) . __getnewargs__ ( ) , ( INF , <NUM_LIT:0.0> ) ) <EOL> if float . __getformat__ ( "<STR_LIT>" ) . startswith ( "<STR_LIT>" ) : <EOL> def test_plus_minus_0j ( self ) : <EOL> z1 , z2 = <NUM_LIT> , - <NUM_LIT> <EOL> self . assertEqual ( atan2 ( z1 . imag , - <NUM_LIT:1.> ) , atan2 ( <NUM_LIT:0.> , - <NUM_LIT:1.> ) ) <EOL> self . assertEqual ( atan2 ( z2 . imag , - <NUM_LIT:1.> ) , atan2 ( - <NUM_LIT:0.> , - <NUM_LIT:1.> ) ) <EOL> @ unittest . skipUnless ( float . __getformat__ ( "<STR_LIT>" ) . startswith ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> def test_overflow ( self ) : <EOL> self . assertEqual ( complex ( "<STR_LIT>" ) , complex ( INF , <NUM_LIT:0.0> ) ) <EOL> self . assertEqual ( complex ( "<STR_LIT>" ) , complex ( <NUM_LIT:0.0> , - INF ) ) <EOL> self . assertEqual ( complex ( "<STR_LIT>" ) , complex ( - INF , INF ) ) <EOL> @ unittest . skipUnless ( float . __getformat__ ( "<STR_LIT>" ) . startswith ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> def test_repr_roundtrip ( self ) : <EOL> vals = [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , INF , NAN ] <EOL> vals += [ - v for v in vals ] <EOL> for x in vals : <EOL> for y in vals : <EOL> z = complex ( x , y ) <EOL> roundtrip = complex ( repr ( z ) ) <EOL> self . assertFloatsAreIdentical ( z . real , roundtrip . real ) <EOL> self . assertFloatsAreIdentical ( z . imag , roundtrip . imag ) <EOL> inf , nan = float ( '<STR_LIT>' ) , float ( '<STR_LIT>' ) <EOL> infj , nanj = complex ( <NUM_LIT:0.0> , inf ) , complex ( <NUM_LIT:0.0> , nan ) <EOL> for x in vals : <EOL> for y in vals : <EOL> z = complex ( x , y ) <EOL> roundtrip = eval ( repr ( z ) ) <EOL> self . assertFloatsAreIdentical ( <NUM_LIT:0.0> + z . real , <EOL> <NUM_LIT:0.0> + roundtrip . real ) <EOL> self . assertFloatsAreIdentical ( <NUM_LIT:0.0> + z . imag , <EOL> <NUM_LIT:0.0> + roundtrip . imag ) <EOL> def test_format ( self ) : <EOL> self . assertEqual ( format ( <NUM_LIT:1> + <NUM_LIT> , '<STR_LIT>' ) , str ( <NUM_LIT:1> + <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , str ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT> , '<STR_LIT>' ) , str ( <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT> , '<STR_LIT>' ) , str ( <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT:3> + <NUM_LIT> , '<STR_LIT>' ) , str ( <NUM_LIT:3> + <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , str ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT:->' ) , str ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT:<>' ) , str ( <NUM_LIT> + <NUM_LIT> ) ) <EOL> z = <NUM_LIT:4> / <NUM_LIT> - <NUM_LIT> / <NUM_LIT> <EOL> self . assertEqual ( format ( z , '<STR_LIT>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:->' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:<>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT>' ) , str ( z ) ) <EOL> z = complex ( <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> self . assertEqual ( format ( z , '<STR_LIT>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:->' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:<>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:2>' ) , str ( z ) ) <EOL> z = complex ( - <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> self . assertEqual ( format ( z , '<STR_LIT>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:->' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:<>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( z , '<STR_LIT:3>' ) , str ( z ) ) <EOL> self . assertEqual ( format ( <NUM_LIT:1> + <NUM_LIT> , '<STR_LIT:g>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> , '<STR_LIT:g>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT:g>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT:g>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT:f>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT:F>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT:e>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( - <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> - <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( format ( <NUM_LIT> + <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , ( <NUM_LIT> + <NUM_LIT> ) . __format__ , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , ( <NUM_LIT> + <NUM_LIT> ) . __format__ , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , ( <NUM_LIT> + <NUM_LIT> ) . __format__ , '<STR_LIT>' ) <EOL> for t in '<STR_LIT>' : <EOL> self . assertRaises ( ValueError , ( <NUM_LIT> + <NUM_LIT> ) . __format__ , t ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( <NUM_LIT> + <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( INF + <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( INF + <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( - INF + <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( - INF + <NUM_LIT> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( INF , INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( INF , INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( INF , - INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( INF , - INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( - INF , INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( - INF , INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( - INF , - INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( - INF , - INF ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( NAN , <NUM_LIT:0> ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( NAN , <NUM_LIT:0> ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( NAN , NAN ) ) , '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' . format ( complex ( NAN , NAN ) ) , '<STR_LIT>' ) <EOL> def test_main ( ) : <EOL> with test_support . check_warnings ( ( "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) ) : <EOL> test_support . run_unittest ( ComplexTest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
