B
    u9av                 @   s   d dl Z d dlmZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlZedZedZG dd	 d	e jZd
d Zedkre  dS )    N)support)VALID_UNDERSCORE_LITERALSINVALID_UNDERSCORE_LITERALS)random)atan2isnancopysigninfnanc               @   s  e Zd Zdd Zd=ddZdd Zd>dd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zejd d! Zd"d# Zd$d% Zd&d' Zd(d) Zejd*d+ Zd,d- Zd.d/ Zd0d1 Zejd2d3 Zejd4d5 Zejd6d7 Zejd8d9 Z d:d; Z!d<S )?ComplexTestc             C   s   t |trdt |tr>tj| |j|j tj| |j|j qtj| |j| tj| |jd n@t |trtj| ||j tj| d|j ntj| || d S )Ng        )
isinstancecomplexunittestTestCaseassertAlmostEqualrealimag)selfab r   "/usr/lib/python3.7/test_complex.pyr      s    


zComplexTest.assertAlmostEqual&.>c             C   s`   t |t |kr|| }}|dkr.t ||k S |dkrBt ||k S | t || | |k  d S )Nr   )abs
assertTrue)r   xyepsr   r   r   assertCloseAbs   s    
zComplexTest.assertCloseAbsc             C   st   d}t |st |r*t |r^t |r^d S n4||kr^|dkr>d S td|td|krVd S |d7 }| ||| d S )Nz&floats {!r} and {!r} are not identicalg        g      ?z: zeros have different signs)r   r   failformat)r   r   r   msgr   r   r   assertFloatsAreIdentical+   s    z$ComplexTest.assertFloatsAreIdenticalc             C   s(   |  |j|j| |  |j|j| d S )N)r   r   r   )r   r   r   r   r   r   r   assertCloseB   s    zComplexTest.assertClosec             C   sp   || }|dkr:|| }|  || ||}|  || |dkrl|| }|  || ||}|  || d S )Nr   )r#   __truediv__)r   r   r   zqr   r   r   	check_divG   s    

zComplexTest.check_divc                s8  dd t ddD   fdd D }x&|D ]}x|D ]}| || q6W q,W | tddd | tddd x0t d	D ]$}| tt t tt t  q|W | ttjd
d | tdd
d | ttjd
d xVdtftdfttfgD ]<\}}tddt|| }| 	t
|j | 	t
|j qW d S )Nc             S   s   g | ]}t |qS r   )float).0ir   r   r   
<listcomp>V   s    z,ComplexTest.test_truediv.<locals>.<listcomp>   c                s    g | ]} D ]}t ||qqS r   )r   )r)   r   r   )simple_realr   r   r+   W   s    gZbtiy      ?        gN~hd   y      ?      ?y                y       @        y      ?      r   )ranger'   r   r   assertRaisesZeroDivisionErrorr$   r   NANr   r   r   r   )r   Zsimple_complexr   r   r*   Z
denom_realZ
denom_imagr%   r   )r.   r   test_truedivU   s"    

 zComplexTest.test_truedivc             C   s(   |  ttjdd |  ttjdd d S )Ny      @        y      ?        y                )r1   	TypeErrorr   __floordiv__)r   r   r   r   test_floordivr   s    zComplexTest.test_floordivc             C   s  |  tddd> d |  tdd t |  tddd |  tddd |  tddd |  tddd x~tddD ]p}|d }|  t|d	 |d |  t|d	 |d |  tt|||d |  tt|||d qW |  tddt |  tddt |  tddt |  t	ddt | 
ttjdd | 
ttjdd | 
ttjdd | 
ttjdd |  tddd |  tddd |  tddd |  tddd d S )
Ny      ?      ?   i'  FTy       @       @r/   g      Y@y                )assertIsr   __eq____lt__NotImplemented__ne__r0   __le____gt____ge__r1   r5   operatorltlegtgeeqne)r   r*   fr   r   r   test_richcomparev   s0    zComplexTest.test_richcomparec                s   dfdd	}x`t ddD ]R}d| }d|  |d| t dd fd	d
 |d| t dddd
 t| qW |dt dddd
  d S )N        c                sV   xP|D ]H}| | }t ||} t ||||  t ||||  qW d S )N)r   r9   r:   r=   )nZdeltasZis_equalr   deltar*   r%   )r   r   r   check   s
    

z6ComplexTest.test_richcompare_boundaries.<locals>.checkr8   
   4      e   c                s   |   dkS )Nr   r   )rL   )multr   r   <lambda>       z9ComplexTest.test_richcompare_boundaries.<locals>.<lambda>c             S   s   dS )NFr   )rL   r   r   r   rS      rT   l          ir   c             S   s   dS )NTr   )rL   r   r   r   rS      rT   )rJ   )r0   r(   )r   rM   r*   powr   )rR   r   r   test_richcompare_boundaries   s    	$z'ComplexTest.test_richcompare_boundariesc             C   s4   |  tdjd |  tdd  |  tdjd d S )Ny      ?      ?y                c               S   s   dd S )Nyp=
ף
@Q@r   r   r   r   r   r   rS      rT   z&ComplexTest.test_mod.<locals>.<lambda>y        333333@)r1   r5   __mod__)r   r   r   r   test_mod   s    zComplexTest.test_modc             C   s$   |  ttdd |  ttdd d S )Ny      ?      ?y      ?        y                )r1   r5   divmod)r   r   r   r   test_divmod   s    zComplexTest.test_divmodc             C   sd  |  tddd |  tddd | ttdd |  tddd |  tdd	d
 | ttddd d}| |d d
 | |d d d
 | dd
 | dd
 yd|  W n tk
r   Y nX | d ydd  W n tk
r   Y nX | d | |d |d  | |d |d  | |d |d  | dd
 d}| tt||d d S )Ny      ?      ?y                g      ?y       @        g        y              ?y                 r8   yp=
ף
@Q@y      ?        z,should fail 0.0 to negative or complex powery      @       i   iiyffffff@ffffff@r   )r   rU   r1   r2   
ValueErrorassertEqualr   )r   r   r   r   r   r   test_pow   s6    

zComplexTest.test_powc             C   sD   x,t dD ] }| tt d t d  q
W | tdd  d S )Nr/   gư>g        )r0   r   r   r   )r   r*   r   r   r   test_boolcontext   s     zComplexTest.test_boolcontextc             C   s   |  tdd d d S )Ng333333@g#@y333333@#)r#   r   	conjugate)r   r   r   r   test_conjugate   s    zComplexTest.test_conjugatec       
   	      s  G dd d}G dd dt }| t|dd | t|dd | tt|d  | tt|d  | tti  | tt|d | tt|d | tdd | td	d
 | tdd
 | td	d
 | td
d
 | tdd	d | tdd	d | tddd | tdd	d | tdd	d | tddd | tdd	d | tdd	d | tddd | tdd | tdd | tdd | tdd | tddd | tddd | tddd | tddd | tddd | tddd | tddd | tddd | tdd | tdd | t d | tdd | tdd | tdd | tdd  | td!d" | td#d$ | td%d& | td'd" | td(d | td)d | td*d | td+d, | td-d | td.d | td/d G d0d1 d1t}| t|d2d2 | td3d4d5d6 | td6d7d6 | td6d4d5d8 | tdd9d5d: d;d< }| |tddj|d | |tdd=j|d= | |tddj|d | |td=dj|d= d"}| t||k ~| ttdd | ttdd | t	td> | tt
d? | tt
d? | ttd? | t	td@ | ttd  | tdAtd  | t	tdB | t	tdC | ttddD | ttddE | ttddD | t	tdF | t	tdG | t	tdH | t	tdI | t	tdJ | t	tdK | t	tdL | t	tdM | t	tdN | t	tdO | t	tdP | t	tdQ | tdRtddSid | tdTtdddSi | t	tdU | t	tdV | t	tdW | ttdXt | tdYd2 | t	tdZ G d[d\ d\t G  fd]d^d^}|  t|  G d_d` d`}| t|dadE | t|db|dcd5d6 | tt|d  G ddde det}G dfdg dgt}	G dhd1 d1t}| t|ddi | t | t|	ddj W d Q R X | tt|d d S )kNc               @   s   e Zd Zdd Zdd ZdS )z(ComplexTest.test_constructor.<locals>.OSc             S   s
   || _ d S )N)value)r   rd   r   r   r   __init__   rT   z1ComplexTest.test_constructor.<locals>.OS.__init__c             S   s   | j S )N)rd   )r   r   r   r   __complex__   rT   z4ComplexTest.test_constructor.<locals>.OS.__complex__N)__name__
__module____qualname__re   rf   r   r   r   r   OS   s   rj   c               @   s   e Zd Zdd Zdd ZdS )z(ComplexTest.test_constructor.<locals>.NSc             S   s
   || _ d S )N)rd   )r   rd   r   r   r   re      rT   z1ComplexTest.test_constructor.<locals>.NS.__init__c             S   s   | j S )N)rd   )r   r   r   r   rf      rT   z4ComplexTest.test_constructor.<locals>.NS.__complex__N)rg   rh   ri   re   rf   r   r   r   r   NS   s   rk   y      ?      $@g      ?r8   z1+10jrN   y      $@        g      $@g      ?yQ	@        gQ	@i:  y     s@        y                g        r   y        Q	@yQ	        1y      ?        1jy              ?z-1r[   z+1z(1+2j)y      ?       @z
(1.3+2.2j)y?@z3.14+1JyQ	@      ?z ( +3.14-6J )yQ	@      z ( +3.14-J )yQ	@      z ( +3.14+j )Jz( j )z+Jz( -j)y             z1e-500z-1e-500jz-1e-500+1e-500jc               @   s   e Zd ZdS )z.ComplexTest.test_constructor.<locals>.complex2N)rg   rh   ri   r   r   r   r   complex2  s    ro   y      ?      ?      )r   r   y      1@      7@)r   y      1@      G@y      @      @y            @c             S   s
   t | dS )Ng      )r   )r   r   r   r   split_zeros   s    z1ComplexTest.test_constructor.<locals>.split_zerosg       z1+1j jy      @      @ znot 'NoneType' z3 92*   z1+z1+1j+1jz--z(1+2jz1+2j)z1+(2j)z	(1+2j)123r   z1j+2Z1e1ejz1e++1ejz)1+2j(z7first argument must be a string or a number, not 'dict'rP   z,second argument must be a number, not 'dict'z1..1jz1.11.1jz1e1.1jA  11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111u    ( 1+1j ) u   こんにちはc               @   s   e Zd ZdS )z-ComplexTest.test_constructor.<locals>.EvilExcN)rg   rh   ri   r   r   r   r   EvilExc`  s   rw   c                   s   e Zd Z fddZdS )z1ComplexTest.test_constructor.<locals>.evilcomplexc                s    d S )Nr   )r   )rw   r   r   rf   d  s    z=ComplexTest.test_constructor.<locals>.evilcomplex.__complex__N)rg   rh   ri   rf   r   )rw   r   r   evilcomplexc  s   rx   c               @   s   e Zd Zdd Zdd ZdS )z,ComplexTest.test_constructor.<locals>.float2c             S   s
   || _ d S )N)rd   )r   rd   r   r   r   re   j  s    z5ComplexTest.test_constructor.<locals>.float2.__init__c             S   s   | j S )N)rd   )r   r   r   r   	__float__l  s    z6ComplexTest.test_constructor.<locals>.float2.__float__N)rg   rh   ri   re   ry   r   r   r   r   float2i  s   rz   g      E@g      1@g      7@c               @   s   e Zd Zdd ZdS )z.ComplexTest.test_constructor.<locals>.complex0c             S   s   dS )Ny              E@r   )r   r   r   r   rf   u  s    z:ComplexTest.test_constructor.<locals>.complex0.__complex__N)rg   rh   ri   rf   r   r   r   r   complex0s  s   r{   c               @   s   e Zd ZdddZdd ZdS )z.ComplexTest.test_constructor.<locals>.complex1                c             S   s   t | d| S )NrP   )r   __new__)r   rd   r   r   r   r}   z  s    z6ComplexTest.test_constructor.<locals>.complex1.__new__c             S   s   | S )Nr   )r   r   r   r   rf   |  s    z:ComplexTest.test_constructor.<locals>.complex1.__complex__N)r|   )rg   rh   ri   r}   rf   r   r   r   r   complex1x  s   
r~   c               @   s   e Zd Zdd ZdS )z.ComplexTest.test_constructor.<locals>.complex2c             S   s   d S )Nr   )r   r   r   r   rf     s    z:ComplexTest.test_constructor.<locals>.complex2.__complex__N)rg   rh   ri   rf   r   r   r   r   ro     s   y              E@y               @)objectr_   r   r1   r5   r   r   r   r   r^   intr(   ZassertRaisesRegextype	ExceptionZassertWarnsDeprecationWarning)
r   rj   rk   ro   rr   crx   rz   r{   r~   r   )rw   r   test_constructor   s    zComplexTest.test_constructorc             C   s8  G dd dt }x ddtt tfD ]
}xddtt tfD ]}| j||d t ||}| |j| | |j| |||}| t|| | |j| | |j| t |||}| t|t  | |j| | |j| |t ||}| t|| | |j| | |j| W d Q R X q>W q$W d S )Nc               @   s   e Zd ZdS )z>ComplexTest.test_constructor_special_numbers.<locals>.complex2N)rg   rh   ri   r   r   r   r   ro     s   ro   g        g       )r   r   )	r   INFr3   ZsubTestr"   r   r   r9   r   )r   ro   r   r   r%   r   r   r    test_constructor_special_numbers  s&    

z,ComplexTest.test_constructor_special_numbersc          	      s   xRt D ]J t fdddD s| t t  | t t dd qW x:tD ]2  dkrhqZt fdddD sZ| tt  qZW d S )Nc             3   s   | ]}| kV  qd S )Nr   )r)   ch)litr   r   	<genexpr>  s    z/ComplexTest.test_underscores.<locals>.<genexpr>ZxXoObB_rs   )Z0_7Z09_99c             3   s   | ]}| kV  qd S )Nr   )r)   r   )r   r   r   r     s    )	r   anyr_   r   evalreplacer   r1   r^   )r   r   )r   r   test_underscores  s    
 
zComplexTest.test_underscoresc             C   sT   xNt ddD ]@}| t|tt|d |d }| t|tt|d qW d S )Ni   r   g      @g        )r0   r_   hashr   )r   r   r   r   r   	test_hash  s    zComplexTest.test_hashc             C   sJ   dd t ddD }x0|D ](}| |jd |jd  d t| qW d S )Nc             S   s.   g | ]&}t d dD ]}t|d |d qqS )i	   g      @g      @)r0   r   )r)   r   r   r   r   r   r+     s    z(ComplexTest.test_abs.<locals>.<listcomp>ir   rP   g      ?)r0   r   r   r   r   )r   ZnumsZnumr   r   r   test_abs  s    
zComplexTest.test_absc             C   s,  | j fdd}|dd |dd |dd| jd	 |td
td |td
t d |ttdd |tt td |ttdd |tdtd |tttd |tdtd |tdt d |tdtd |  dttd |  dttd |  dttd |  dttd d S )Nc             S   s    |t | | |t| | d S )N)reprstr)vexpectedtest_fnr   r   r   test  s    z'ComplexTest.test_repr_str.<locals>.testy      ?      @z(1+6j)y      ?      z(1-6j)y             z(-1+-0j))r   g      ?z(1+infj)z(1-infj)r8   z(inf+1j)z(-inf+infj)z(nan+1j)z(1+nanj)z
(nan+nanj)r   infjz-infjnanjy             y              @)r_   ZassertNotEqualr   r   r3   r   )r   r   r   r   r   test_repr_str  s$    

zComplexTest.test_repr_strc             C   s   | j fdd}|tddd |tddd |tddd	 |tddd
 |tddd |tddd |tddd |tddd d S )Nc             S   s    |t | | |t| | d S )N)r   r   )r   r   r   r   r   r   r     s    z5ComplexTest.test_negative_zero_repr_str.<locals>.testg        g      ?rm   g       z(-0+1j)g      z-1jz(-0-1j)Z0jz-0jz(-0+0j)z(-0-0j))r_   r   )r   r   r   r   r   test_negative_zero_repr_str  s    z'ComplexTest.test_negative_zero_repr_strc             C   s   |  dd d S )Ny            )r_   )r   r   r   r   test_neg  s    zComplexTest.test_negc          	   C   s   d}d}d }zJt tjd}t|||d |  t tjd}| | d||f  W d |d k	rn|jsn|  ttj X d S )Nyp=
ף
@Q@yffffff@ffffff@w)filerz%s %s
)	openr   ZTESTFNprintcloser_   readclosedunlink)r   r   r   for   r   r   	test_file  s    zComplexTest.test_filec             C   sx   |  d d |  d d |  d d |  d d |  td	t d
tf |  ttd	 td
f d S )Ny      ?       @)g      ?g       @y      ?       )g      ?g       y               @)g        g       @y              )g        g       r   g        )r_   __getnewargs__r   r   )r   r   r   r   test_getnewargs  s    zComplexTest.test_getnewargsc             C   s@   d\}}|  t|jdtdd |  t|jdtdd d S )N)y                y              g      g        g       )r_   r   r   )r   z1z2r   r   r   test_plus_minus_0j  s    zComplexTest.test_plus_minus_0jc             C   sf   d}d}d}|  |jd |  |jd |  |jd |  |jd |  |jd |  |jt  d S )Ny              y             y             g       g      )r"   r   r   r   )r   Zz0r   r   r   r   r   test_negated_imaginary_literal  s    z*ComplexTest.test_negated_imaginary_literalc             C   sJ   |  tdttd |  tdtdt  |  tdtt t d S )NZ1e500g        z-1e500jz-1e500+1.8e308j)r_   r   r   )r   r   r   r   test_overflow  s    zComplexTest.test_overflowc       
   	   C   s  dddddddt tg	}|dd |D 7 }xP|D ]H}xB|D ]:}t||}tt|}| |j|j | |j|j q8W q.W td	td
 }}td|td| }}	x`|D ]X}xR|D ]J}t||}tt|}| d|j d|j  | d|j d|j  qW qW d S )Ng        gi    gN~hg(0?go!	@gd~QJc             S   s   g | ]
}| qS r   r   )r)   r   r   r   r   r+     s    z3ComplexTest.test_repr_roundtrip.<locals>.<listcomp>r	   r
   )	r   r3   r   r   r"   r   r   r(   r   )
r   Zvalsr   r   r%   Z	roundtripr	   r
   r   r   r   r   r   test_repr_roundtrip  s$    





zComplexTest.test_repr_roundtripc             C   s*  |  tddtd |  tddtd |  tddtd |  tddtd |  tddtd |  tddtd |  tddtd |  tdd	td d
}|  t|dt| |  t|dt| |  t|d	t| |  t|dt| tdd}|  t|dt| |  t|dt| |  t|d	t| |  t|dt| tdd}|  t|dt| |  t|dt| |  t|d	t| |  t|dt| |  tddd |  tddd |  tddd |  tddd |  tddd |  tddd |  tddd |  tddd |  tddd  |  td!dd" |  td#dd$ |  td!d%d& |  td!d'd& |  td!d(d) |  td!d*d+ |  td!d,d- |  td.d/d0 |  td1d2d3 |  td1d4d5 |  td1d6d7 |  td1d8d9 |  td1d:d; |  td1d<d= |  td1d>d? |  td@dAdB |  td1dCdD |  td1dEdD |  td1dFdG |  tdHdFdI |  tdHdJdK |  tdHdLdM |  tdNdLdO |  tdPdQdR |  tdSdTdU |  tdSdVdW |  tdSdXdY |  tdSdZd[ |  td\dd] |  td\d^d_ |  tdSd`da |  tdSdbda |  tdSdcdd |  tdSdedd |  tdfdgdh |  tdfdZdi |  tdfdjdk |  tdfdVdl |  tdfd^dm |  tdfdndo |  tdfdpdq | tdrjds | td1jdt xduD ]}| tdrj| qzW |  dvdwdx |  ttttd%dy |  ttdztd%d{ |  tttdzd%d| |  tttd}d%d~ |  ttttd'd |  ttdztd'd |  tttdzd'd |  tttd}d'd |  ttttd%d |  ttdztd%d |  tttdzd%d |  tttd}d%d |  ttttd'd |  ttdztd'd |  tttdzd'd |  tttd}d'd d S )Ny      ?      @rs   y      ?      @y              @y        	@y      @        y	@        -<y$I$I?%I$I,Z10g        g      @ru   g       g       @3gz1+3jz0+3jz1.5+3.5jz+gz	+1.5+3.5jy      ?      z	+1.5-3.5jz-gz1.5-3.5jz gz	 1.5+3.5jz	 1.5-3.5jy            @z	-1.5+3.5jy            z	-1.5-3.5jy      z-1.5-3.5e-20jrH   z-1.500000-3.500000jFez-1.500000e+00-3.500000e+00jz.2ez-1.50e+00-3.50e+00jz.2Ez-1.50E+00-3.50E+00jy       \z.2Gz-1.5E+10-3.5E+05jy      ?      @z<20gz1.5+3j              z*<20gz1.5+3j**************z>20gz              1.5+3jz^20gz       1.5+3j       z<20z(1.5+3j)            z>20z            (1.5+3j)z^20z      (1.5+3j)      y+?Clz^20.2z     (1.1-3.1j)     z20.2fz          1.50+3.00jz>20.2fz<20.2fz1.50+3.00j          y0)VC D      @z150000000000000000000.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*+TTD     p@z,.2fz*1,500,000,000,000,000,000,000.00+3,000.00jy      ?      ?z.0ez1e+00+1e+00jz#.0ez1.e+00+1.e+00jz.0fz1+1jz#.0fz1.+1.jy??z1.1+1.1jz#gz1.10000+1.10000jz.1ez1.0e+00+1.0e+00jz#.1ez.1fz1.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      ?      ?Z010fz=20ZbcdoxXz	*{0:.3f}*yn!	@	@z*3.142+2.718j*znan+nanjr8   z1.000000+nanjznan+1.000000jr[   znan-1.000000jzNAN+NANjz1.000000+NANjzNAN+1.000000jzNAN-1.000000jzinf+infjz1.000000+infjzinf+1.000000jzinf-1.000000jzINF+INFjz1.000000+INFjzINF+1.000000jzINF-1.000000j)	r_   r    r   r   r1   r^   
__format__r3   r   )r   r%   tr   r   r   test_format7  s    


zComplexTest.test_formatN)r   )r   )"rg   rh   ri   r   r   r"   r#   r'   r4   r7   rI   rV   rX   rZ   r`   ra   rc   r   r   Zrequires_IEEE_754r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s<   

' 1r   c               C   s   t t d S )N)r   Zrun_unittestr   r   r   r   r   	test_main  s    r   __main__)r   r   r   Ztest.test_grammarr   r   r   Zmathr   r   r   rA   r(   r   r3   r   r   r   rg   r   r   r   r   <module>   s         '