a 4dgx@sddlZddlmZddlmZmZddlmZddlmZm Z m Z ddl Z e dZ e dZGdd d ejZd d Zed kredS) N)support)VALID_UNDERSCORE_LITERALSINVALID_UNDERSCORE_LITERALS)random)atan2isnancopysigninfnanc@seZdZddZd=ddZddZd>dd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZejd d!Zd"d#Zd$d%Zd&d'Zd(d)Zejd*d+Zd,d-Zd.d/Zd0d1Zejd2d3Zejd4d5Zejd6d7Zejd8d9Z d:d;Z!dtj||j|jtj||j|jqtj||j|tj||jdn@t|trtj|||jtj|d|jntj|||dS)N) isinstancecomplexunittestTestCaseassertAlmostEqualrealimagselfabr-/usr/local/lib/python3.9/test/test_complex.pyrs   zComplexTest.assertAlmostEqual& .>cCs`t|t|kr||}}|dkr.t||kS|dkrBt||kS|t||||kdS)z+Return true iff floats x and y "are close".rN)abs assertTruerxyZepsrrrassertCloseAbss   zComplexTest.assertCloseAbscCstd}t|st|r*t|r^t|r^dSn4||kr^|dkr>dStd|td|krVdS|d7}||||dS)a#assert that floats x and y are identical, in the sense that: (1) both x and y are nans, or (2) both x and y are infinities, with the same sign, or (3) both x and y are zeros, with the same sign, or (4) x and y are both finite and nonzero, and x == y z&floats {!r} and {!r} are not identicalNr ?z: zeros have different signs)rrfailformat)rrrmsgrrrassertFloatsAreIdentical+sz$ComplexTest.assertFloatsAreIdenticalcCs(||j|j|||j|j|dS)z.Return true iff complexes x and y "are close".N)r rrrrrr assertCloseBszComplexTest.assertClosecCsp||}|dkr:||}|||||}||||dkrl||}|||||}|||dS)z8Compute complex z=x*y, and check that z/x==y and z/y==x.rN)r& __truediv__)rrrzqrrr check_divGs     zComplexTest.check_divcs8ddtddDfddD}|D]}|D]}|||q2q*|tddd|tdddtd D]$}|ttttttqr|ttjd d |ttd d | td d d|ttjd d dt ft dft t ffD]<\}}tddt||}| t |j | t |jqdS)NcSsg|] }t|qSr)float).0irrr Vz,ComplexTest.test_truediv..cs g|]}D]}t||q qSr)rr,rrZ simple_realrrr.Wr/gZbti?N~hd??yZbti?@y?r)ranger*rr assertRaisesZeroDivisionErrorr' OverflowErrorpowrNANrrrr)rZsimple_complexrrr-Z denom_realZ denom_imagr(rr3r test_truedivUs&  zComplexTest.test_truedivcCs(|ttjdd|ttjdddS)N@y?r8)r; TypeErrorr __floordiv__rrrr test_floordivqszComplexTest.test_floordivcCs|tddd>d|tddt|tddd|tddd|tddd|tdddtddD]p}|d}|t|d |d|t|d |d|tt|||d|tt|||dq|tddt|tddt|tddt|t ddt| t t j dd| t t jdd| t t jdd| t t jdd|t ddd|t ddd|t ddd|t ddddS) Nr7i'FTy@@r6gY@r8)assertIsr__eq____lt__NotImplemented__ne__r:__le____gt____ge__r;rBoperatorltlegtgeeqne)rr-frrrtest_richcompareus0zComplexTest.test_richcomparecsdfdd }tddD]R}d|}d||d|tddfd d |d|tddd d t|q|d td ddd dS)Nr csR|D]H}||}t||}t||||t|||| qdSN)rrGrHrK)nZdeltasZis_equalrdeltar-r(rDrrchecks  z6ComplexTest.test_richcompare_boundaries..checkrF 4ecs |dkS)NrrrZ)multrrr/z9ComplexTest.test_richcompare_boundaries..cSsdS)NFrr`rrrrbr/lircSsdS)NTrr`rrrrbr/)r )r:r+)rr[r-r>r)rarrtest_richcompare_boundariess "z'ComplexTest.test_richcompare_boundariescCs4|tdjd|tdd|tdjddS)Nr7r8cSsddS)Np= ף @Q@rrrrrrrbr/z&ComplexTest.test_mod..y333333@)r;rB__mod__rDrrrtest_modszComplexTest.test_modcCs$|ttdd|ttdddS)Nr7r4r8)r;rBdivmodrDrrr test_divmodszComplexTest.test_divmodcCs`|tddd|tddd|ttdd|tddd|tdd d |ttdddd }||dd ||ddd |d d |d d z d|WntyYn 0|d z ddWntyYn 0|d ||d|d||d|d||d|d|d d d}|tt||ddS)Nr7r8r!r9r ?yrFrdr4z,should fail 0.0 to negative or complex powery@iiffffff@ffffff@r)rr>r;r< ValueError assertEqualr"rrrrtest_pows6         zComplexTest.test_powcCs@tdD] }|ttdtdq|tdd dS)Nr6gư>r )r:rrr)rr-rrrtest_boolcontexts zComplexTest.test_boolcontextcCs|tddddS)Ng333333@g#@y333333@#)r&r conjugaterDrrrtest_conjugateszComplexTest.test_conjugatec s0Gddd}Gdddt}|t|dd|t|dd|tt|d|tt|d|tti|tt|d|tt|d|tdd|td d |td d |td d |td d |tdd d|tdd d|tdd d|tdd d|tdd d|tdd d|td d d|td d d|td d d|td d |tdd |tdd|tdd|td dd |tddd |tddd|tddd|tddd|tddd|tddd|tddd|tdd|tdd|td|tdd|tdd|tdd|tdd |td!d"|td#d$|td%d&|td'd"|td(d|td)d|td*d|td+d,|td-d|td.d|td/dGd0d1d1t}|t|d2d2|td3d4d5d6|td6d7d6|td6d4d5d8|tdd9d5d:d;d<}||td dj|d||td d=j|d=||tdd j|d||td=d j|d=d"}|t||u~|ttdd|ttdd|t td>|tt d?|tt d?|tt d?|t td@|ttd| tdAtd|t tdB|t tdC|ttddD|ttddE|ttddD|t tdF|t tdG|t tdH|t tdI|t tdJ|t tdK|t tdL|t tdM|t tdN|t tdO|t tdP|t tdQ| tdRtddSid| tdTtdddSi|t tdU|t tdV|t tdW|t tdXt|tdYd2|t tdZGd[d\d\tGfd]d^d^}|t|Gd_d`d`}|t|dadE|t|db|dcd5d6|tt|dGdddede}|t|dEdf|tdg|dEdh|tt|dSdi|ttdg|dSdiGdjdkdk} |tt| |ttdg| Gdldmdmt} Gdndodot} Gdpd1d1t}|t| ddq|t$|t| ddrWdn1s0Y|tt|ddS)sNc@seZdZddZddZdS)z(ComplexTest.test_constructor..OScSs ||_dSrXvaluerrvrrr__init__r/z1ComplexTest.test_constructor..OS.__init__cSs|jSrXrurDrrr __complex__r/z4ComplexTest.test_constructor..OS.__complex__N__name__ __module__ __qualname__rxryrrrrOSsr~c@seZdZddZddZdS)z(ComplexTest.test_constructor..NScSs ||_dSrXrurwrrrrxr/z1ComplexTest.test_constructor..NS.__init__cSs|jSrXrurDrrrryr/z4ComplexTest.test_constructor..NS.__complex__NrzrrrrNSsry?$@g?rFz1+10jr\y$@g$@r!yQ @gQ @i:ys@r8r ryQ @yQ 1r41jriz-1rjz+1z(1+2j)?@z (1.3+2.2j)y?@z3.14+1JyQ @?z ( +3.14-6J )yQ @z ( +3.14-J )yQ @z ( +3.14+j )Jz( j )z+Jz( -j)yz1e-500z-1e-500jz-1e-500+1e-500jc@s eZdZdS).ComplexTest.test_constructor..complex2Nr{r|r}rrrrcomplex2r/rr7)rry1@7@)ry1@G@y@@y@cSs t|dS)z7Function that produces different results for 0. and -0.)r)rrrr split_zerossz1ComplexTest.test_constructor..split_zerosz1+1jjy@@znot 'NoneType'z392*z1+z1+1j+1jz--z(1+2jz1+2j)z1+(2j)z (1+2j)123rz1j+2Z1e1ejz1e++1ejz)1+2j(z7first argument must be a string or a number, not 'dict'r^z,second argument must be a number, not 'dict'z1..1jz1.11.1jz1e1.1jA11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111u ( 1+1j ) uこんにちはc@s eZdZdS)z-ComplexTest.test_constructor..EvilExcNrrrrrEvilExc_srcseZdZfddZdS)z1ComplexTest.test_constructor..evilcomplexcsdSrXrrDrrrrycsz=ComplexTest.test_constructor..evilcomplex.__complex__N)r{r|r}ryrrrr evilcomplexbsrc@seZdZddZddZdS)z,ComplexTest.test_constructor..float2cSs ||_dSrXrurwrrrrxisz5ComplexTest.test_constructor..float2.__init__cSs|jSrXrurDrrr __float__ksz6ComplexTest.test_constructor..float2.__float__N)r{r|r}rxrrrrrfloat2hsrgE@g1@g7@c@seZdZddZddZdS)z-ComplexTest.test_constructor..MyIndexcSs ||_dSrXrurwrrrrxssz6ComplexTest.test_constructor..MyIndex.__init__cSs|jSrXrurDrrr __index__usz7ComplexTest.test_constructor..MyIndex.__index__N)r{r|r}rxrrrrrMyIndexrsryE@{y^@E@ic@seZdZddZdS)z+ComplexTest.test_constructor..MyIntcSsdS)NrrrDrrr__int__~sz3ComplexTest.test_constructor..MyInt.__int__N)r{r|r}rrrrrMyInt}src@seZdZdZddZdS)z.ComplexTest.test_constructor..complex0z:Test usage of __complex__() when inheriting from 'complex'cSsdS)NE@rrDrrrrysz:ComplexTest.test_constructor..complex0.__complex__Nr{r|r}__doc__ryrrrrcomplex0src@s"eZdZdZdddZddZdS) z.ComplexTest.test_constructor..complex1z3Test usage of __complex__() with a __new__() methodr8cSst|d|S)Nr^)r__new__rwrrrrsz6ComplexTest.test_constructor..complex1.__new__cSs|SrXrrDrrrrysz:ComplexTest.test_constructor..complex1.__complex__N)r8)r{r|r}rrryrrrrcomplex1s rc@seZdZdZddZdS)rz`Make sure that __complex__() calls fail if anything other than a complex is returnedcSsdSrXrrDrrrrysz:ComplexTest.test_constructor..complex2.__complex__Nrrrrrrsr@)objectrprr;rBrrrrrointr+ZassertRaisesRegextype Exceptionr=Z assertWarnsDeprecationWarning) rr~rrrcrrrrrrrrrtest_constructors   4zComplexTest.test_constructorc CsDGdddt}ddtt tfD]}ddtt tfD]}|j||dt||}||j|||j||||}|t||||j|||j|t|||}|t|t||j|||j||t||}|t||||j|||j|Wdq61s20Yq6q dS)Nc@s eZdZdS)z>ComplexTest.test_constructor_special_numbers..complex2Nrrrrrrsrr r)rr) rINFr?ZsubTestr%rrrGr)rrrrr(rrr test_constructor_special_numberss&  z,ComplexTest.test_constructor_special_numbersc stD]JtfdddDs|tt|ttddqtD]2dvrbqTtfdddDsT|ttqTdS)Nc3s|]}|vVqdSrXrr,ZchZlitrr r/z/ComplexTest.test_underscores..ZxXoObB_r)Z0_7Z09_99c3s|]}|vVqdSrXrrrrrrr/) ranyrprevalreplacerr;rorDrrrtest_underscoresszComplexTest.test_underscorescCsPtddD]@}|t|tt|d|d}|t|tt|dq dS)Nrmr@r )r:rphashr)rrrrr test_hashszComplexTest.test_hashcCsFddtddD}|D](}||jd|jddt|qdS)NcSs.g|]&}tddD]}t|d|dqqS) rg@)r:rr2rrrr.r/z(ComplexTest.test_abs..rrr^g?)r:rrrr)rZnumsnumrrrtest_absszComplexTest.test_abscCs,|jfdd}|dd|dd|dd|jd |td td |td t d |ttd d|tt td|ttd d|td td|tttd|tdtd|tdt d|tdtd|dttd|dttd|dttd|dttddS)NcSs |t|||t||dSrXreprstrvZexpectedtest_fnrrrtestsz'ComplexTest.test_repr_str..testy?@z(1+6j)y?z(1-6j)yz(-1+-0j))rr!z(1+infj)z(1-infj)rFz(inf+1j)z (-inf+infj)z(nan+1j)z(1+nanj)z (nan+nanj)rinfjz-infjnanjyy@)rpZassertNotEqualrrr?rrrrrr test_repr_strs$  zComplexTest.test_repr_strcCs|jfdd}|tddd|tddd|tddd |tddd |tddd |tddd |tddd |tddddS)NcSs |t|||t||dSrXrrrrrrsz5ComplexTest.test_negative_zero_repr_str..testr r!rrz(-0+1j)rz-1jz(-0-1j)Z0jz-0jz(-0+0j)z(-0-0j))rprrrrrtest_negative_zero_repr_strsz'ComplexTest.test_negative_zero_repr_strcCs|dddS)Ny)rprDrrrtest_negszComplexTest.test_negc Csd}d}d}zlttjd}t|||d|ttjd}||d||fW|durl|jsl|ttjn$|dur|js|ttj0dS)Nrdrnw)filerz%s %s ) openrZTESTFNprintcloserpreadclosedunlink)rrrforrr test_files  zComplexTest.test_filecCsx|dd|dd|dd|dd|td td tf|ttd td fdS) Nr)r!@y?)r!gr)r r)r rrr )rp__getnewargs__rrrDrrrtest_getnewargss zComplexTest.test_getnewargscCs@d\}}|t|jdtdd|t|jdtdddS)N)r8rrr r)rprr)rz1z2rrrtest_plus_minus_0jszComplexTest.test_plus_minus_0jcCsfd}d}d}||jd||jd||jd||jd||jd||jt dS)Nryyrg)r%rrr)rZz0rrrrrtest_negated_imaginary_literalsz*ComplexTest.test_negated_imaginary_literalcCsJ|tdttd|tdtdt |tdtt tdS)NZ1e500r z-1e500jz-1e500+1.8e308j)rprrrDrrr test_overflow&szComplexTest.test_overflowc Csdddddddttg }|dd|D7}|D]D}|D]:}t||}tt|}||j|j||j|jq4q,td td }}td|td|}} |D]T}|D]J}t||}tt|}|d|jd|j|d|jd|jqqdS) Nr gi r5g( 0?go! @gd~QJcSsg|] }| qSrr)r,rrrrr./r/z3ComplexTest.test_repr_roundtrip..r r ) rr?rrr%rrr+r) rZvalsrrr(Z roundtripr r rrrrrtest_repr_roundtrip,s(      zComplexTest.test_repr_roundtripcCs&|tddtd|tddtd|tddtd|tddtd|tddtd|tddtd|tddtd|tdd tdd }|t|dt||t|dt||t|d t||t|d t|td d }|t|dt||t|dt||t|d t||t|dt|tdd}|t|dt||t|dt||t|d t||t|dt||tddd|tddd|tddd|tddd|tddd|tddd|tddd|tddd|tddd |td!dd"|td#dd$|td!d%d&|td!d'd&|td!d(d)|td!d*d+|td!d,d-|td.d/d0|td1d2d3|td1d4d5|td1d6d7|td1d8d9|td1d:d;|td1d<d=|td1d>d?|td@dAdB|td1dCdD|td1dEdD|td1dFdG|tdHdFdI|tdHdJdK|tdHdLdM|tdNdLdO|tdPdQdR|tdSdTdU|tdSdVdW|tdSdXdY|tdSdZd[|td\dd]|td\d^d_|tdSd`da|tdSdbda|tdSdcdd|tdSdedd|tdfdgdh|tdfdZdi|tdfdjdk|tdfdVdl|tdfd^dm|tdfdndo|tdfdpdq|tdrjds|td1jdtduD]}|tdrj|qx|dvdwdx|ttttd%dy|ttdztd%d{|tttdzd%d||tttd}d%d~|ttttd'd|ttdztd'd|tttdzd'd|tttd}d'd|ttttd%d|ttdztd%d|tttdzd%d|tttd}d%d|ttttd'd|ttdztd'd|tttdzd'd|tttd}d'ddS)Ny?@ry? @y@y @rAy @-20gz 1.5+3jz^20gz 1.5+3j z<20z(1.5+3j) z>20z (1.5+3j)z^20z (1.5+3j) y+?Clz^20.2z (1.1-3.1j) z20.2fz 1.50+3.00jz>20.2fz<20.2fz1.50+3.00j y0)VC D@z150000000000000000000.00+3.00jz>40.2fz( 150000000000000000000.00+3.00jz^40,.2fz( 150,000,000,000,000,000,000.00+3.00j y|3*+TTD@z( 1,500,000,000,000,000,000,000.00+3.00j y|3*+TTDp@z,.2fz*1,500,000,000,000,000,000,000.00+3,000.00jr7z.0ez 1e+00+1e+00jz#.0ez1.e+00+1.e+00jz.0fz1+1jz#.0fz1.+1.jy??z1.1+1.1jz#gz1.10000+1.10000jz.1ez1.0e+00+1.0e+00jz#.1ez.1fz1.0+1.0jz#.1fy?z#fz-1.500000+0.500000jz-2.+0.jz#ez-1.500000e+00+5.000000e-01jz-2.e+00+5.e-01jz-1.50000+0.500000jz.0gz-2+0.5jz#.0gz-2.+0.5jy??Z010fz=20ZbcdoxXz *{0:.3f}*yn! @ @z*3.142+2.718j*znan+nanjrFz 1.000000+nanjz nan+1.000000jrjz nan-1.000000jzNAN+NANjz 1.000000+NANjz NAN+1.000000jz NAN-1.000000jzinf+infjz 1.000000+infjz inf+1.000000jz inf-1.000000jzINF+INFjz 1.000000+INFjz INF+1.000000jz INF-1.000000j) rpr#rrr;ro __format__r?r)rr(trrr test_formatHs  zComplexTest.test_formatN)r)r)"r{r|r}rr r%r&r*r@rErWrcrfrhrqrrrtrrZrequires_IEEE_754rrrrrrrrrrrrrrrrrrr sH  'C       r cCsttdSrX)rZ run_unittestr rrrr test_mainsr__main__)rrrZtest.test_grammarrrrmathrrrrOr+rr?rr rr{rrrrs   8