3
rY]a                 @   s  d dl mZmZ d dlmZmZ d dljZd dlZd dlZd dl	Z	d dlm
Z
mZmZmZ d dlZd dlZd dlZedZedZdd dD Zd
d ed	fedfeefdefd	efdefdefe efe dfe d	fe dfe dfe e fde fde fd	e fde fee fedfedfgD Zdd ee fedfedfed	fedfeefe efdefdefd	efdefeefgD ZG dd dejZG dd dejZedkrej  dS )     )requires_IEEE_754cpython_only)parse_testfile	test_fileN)phasepolarrectpiinfnanc             C   s    g | ]}dD ]}t ||qqS )               )r   r   )complex).0xy r    lib/python3.6/test/test_cmath.py
<listcomp>   s    r           c             C   s   g | ]\}}t ||qS r   )r   )r   r   r   r   r   r   r      s    gffffff@c             C   s   g | ]\}}t ||qS r   )r   )r   r   r   r   r   r   r   %   s    c               @   s.  e Zd Zdd dND Zejdd  ejdd  dd Zdd Zdd Zdd ZdOd!d"Z	d#d$ Z
d%d& Zd'd( Zd)d* Zd+d, Zed-d. Zd/d0 Zd1d2 Zed3d4 Zd5d6 Zd7d8 Zed9d: Zd;d< Zd=d> Zd?d@ ZdAdB ZdCdD Zeejej dEdFkdGdHdI Z!edJdK Z"edLdM Z#d S )P
CMathTestsc             C   s   g | ]}t t|qS r   )getattrcmath)r   fnamer   r   r   r   6   s    zCMathTests.<listcomp>acosacoshasinasinhatanatanhcoscoshexploglog10sinsinhsqrttantanhc             C   s   t j| dS )Ng     @y                y     @        )r   r#   )r   r   r   r   <lambda>;   s    zCMathTests.<lambda>c             C   s   t jd| S )Ng      ,@y              ;@y      ,@      ;)r   r#   )r   r   r   r   r*   <   s    c             C   s   t t| _d S )N)openr   test_values)selfr   r   r   setUp>   s    zCMathTests.setUpc             C   s   | j j  d S )N)r,   close)r-   r   r   r   tearDownA   s    zCMathTests.tearDownc             C   s   d}t j|st j|r2t j|rjt j|rjdS n8||krj|dkrFdS t jd|t jd|krbdS |d7 }| j|j|| dS )a#  Fail unless 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 identicalNg        g      ?z: zeros have different signs)mathisnancopysignfailformat)r-   r   r   msgr   r   r   assertFloatIdenticalD   s    zCMathTests.assertFloatIdenticalc             C   s$   | j |j|j | j |j|j dS )zFail unless complex numbers x and y have equal values and signs.

        In particular, if x and y both have real (or imaginary) part
        zero, but the zeros have different signs, this test will fail.

        N)r7   realimag)r-   r   r   r   r   r   assertComplexIdentical[   s    z!CMathTests.assertComplexIdenticalV瞯<
       Nc             C   s   t j|r,t j|rdS | j|p(dj| t j|rX||krBdS | j|pTdj|| | r| rt jd|t jd|kr| j|pdj|| yt|| }W n tk
r   Y nX |t||t| krdS | j|pdj|| dS )a  Fail if the two floating-point numbers are not almost equal.

        Determine whether floating-point values a and b are equal to within
        a (small) rounding error.  The default values for rel_err and
        abs_err are chosen to be suitable for platforms where a float is
        represented by an IEEE 754 double.  They allow an error of between
        9 and 19 ulps.
        Nz{!r} should be nanz>finite result where infinity expected: expected {!r}, got {!r}g      ?z,zero has wrong sign: expected {!r}, got {!r}z({!r} and {!r} are not sufficiently close)	r1   r2   r4   r5   isinfr3   absOverflowErrormax)r-   abZrel_errabs_errr6   Zabsolute_errorr   r   r   rAssertAlmostEquale   s*    




zCMathTests.rAssertAlmostEqualc             C   sH   d}d}| j tj|ddjtj|d | j tj|ddjtj|d d S )NgiW
@g-DT!	@	   zcmath.pi is {}; should be {})Zplacesr6   zcmath.e is {}; should be {})assertAlmostEqualr   r	   r5   e)r-   Z
e_expectedZpi_expectedr   r   r   test_constants   s    zCMathTests.test_constantsc             C   s   | j tjjtj | j tjjd | j tjjd | j tjjtj | jtjtj	j | j tj	jd | j tj
jd | jtjtj
j | j ttjd | j ttjd | j ttj	d | j ttj
d d S )Ng        r
   infjr   nanj)assertEqualr   r
   r8   r1   r9   rI   
assertTruer2   r   rJ   repr)r-   r   r   r   test_infinity_and_nan_constants   s    z*CMathTests.test_infinity_and_nan_constantsc                s2  d"d#ddddd t  tg}G dd	 d	t }G d
d d}G dd dt G  fdddt }G  fddd}G dd dt }G dd d}G dd dt }G dd d}	G fdddt }
G fddd}G fdddt }G fd d!d!}x.| jD ]"}| j||| | j||| | j||
 | | j|| | | j|| | | j|| | | jt||  | jt||  | jt||  | jt||	  x2|D ]*}| jt||| | jt||| qW | j ||  | j ||  qW d S )$Ngz@y        7Ă??g]ֆ@znot complex      g       @c               @   s   e Zd Zdd Zdd ZdS )z.CMathTests.test_user_object.<locals>.MyComplexc             S   s
   || _ d S )N)value)r-   rQ   r   r   r   __init__   s    z7CMathTests.test_user_object.<locals>.MyComplex.__init__c             S   s   | j S )N)rQ   )r-   r   r   r   __complex__   s    z:CMathTests.test_user_object.<locals>.MyComplex.__complex__N)__name__
__module____qualname__rR   rS   r   r   r   r   	MyComplex   s   rW   c               @   s   e Zd Zdd Zdd ZdS )z0CMathTests.test_user_object.<locals>.MyComplexOSc             S   s
   || _ d S )N)rQ   )r-   rQ   r   r   r   rR      s    z9CMathTests.test_user_object.<locals>.MyComplexOS.__init__c             S   s   | j S )N)rQ   )r-   r   r   r   rS      s    z<CMathTests.test_user_object.<locals>.MyComplexOS.__complex__N)rT   rU   rV   rR   rS   r   r   r   r   MyComplexOS   s   rX   c               @   s   e Zd ZdS )z2CMathTests.test_user_object.<locals>.SomeExceptionN)rT   rU   rV   r   r   r   r   SomeException   s   rY   c                   s   e Zd Z fddZdS )z7CMathTests.test_user_object.<locals>.MyComplexExceptionc                s    d S )Nr   )r-   )rY   r   r   rS      s    zCCMathTests.test_user_object.<locals>.MyComplexException.__complex__N)rT   rU   rV   rS   r   )rY   r   r   MyComplexException   s   rZ   c                   s   e Zd Z fddZdS )z9CMathTests.test_user_object.<locals>.MyComplexExceptionOSc                s    d S )Nr   )r-   )rY   r   r   rS      s    zECMathTests.test_user_object.<locals>.MyComplexExceptionOS.__complex__N)rT   rU   rV   rS   r   )rY   r   r   MyComplexExceptionOS   s   r[   c               @   s   e Zd ZdS )z;CMathTests.test_user_object.<locals>.NeitherComplexNorFloatN)rT   rU   rV   r   r   r   r   NeitherComplexNorFloat   s   r\   c               @   s   e Zd ZdS )z=CMathTests.test_user_object.<locals>.NeitherComplexNorFloatOSN)rT   rU   rV   r   r   r   r   NeitherComplexNorFloatOS   s   r]   c               @   s   e Zd Zdd Zdd ZdS )z*CMathTests.test_user_object.<locals>.MyIntc             S   s   dS )N   r   )r-   r   r   r   __int__   s    z2CMathTests.test_user_object.<locals>.MyInt.__int__c             S   s   dS )Nr^   r   )r-   r   r   r   	__index__   s    z4CMathTests.test_user_object.<locals>.MyInt.__index__N)rT   rU   rV   r_   r`   r   r   r   r   MyInt   s   ra   c               @   s   e Zd Zdd Zdd ZdS )z,CMathTests.test_user_object.<locals>.MyIntOSc             S   s   dS )Nr^   r   )r-   r   r   r   r_      s    z4CMathTests.test_user_object.<locals>.MyIntOS.__int__c             S   s   dS )Nr^   r   )r-   r   r   r   r`      s    z6CMathTests.test_user_object.<locals>.MyIntOS.__index__N)rT   rU   rV   r_   r`   r   r   r   r   MyIntOS   s   rb   c                   s$   e Zd ZfddZ fddZdS )z4CMathTests.test_user_object.<locals>.FloatAndComplexc                s    S )Nr   )r-   )flt_argr   r   	__float__   s    z>CMathTests.test_user_object.<locals>.FloatAndComplex.__float__c                s    S )Nr   )r-   )cx_argr   r   rS      s    z@CMathTests.test_user_object.<locals>.FloatAndComplex.__complex__N)rT   rU   rV   rd   rS   r   )re   rc   r   r   FloatAndComplex   s   rf   c                   s$   e Zd ZfddZ fddZdS )z6CMathTests.test_user_object.<locals>.FloatAndComplexOSc                s    S )Nr   )r-   )rc   r   r   rd      s    z@CMathTests.test_user_object.<locals>.FloatAndComplexOS.__float__c                s    S )Nr   )r-   )re   r   r   rS      s    zBCMathTests.test_user_object.<locals>.FloatAndComplexOS.__complex__N)rT   rU   rV   rd   rS   r   )re   rc   r   r   FloatAndComplexOS   s   rg   c                   s   e Zd Z fddZdS )z.CMathTests.test_user_object.<locals>.JustFloatc                s    S )Nr   )r-   )rc   r   r   rd      s    z8CMathTests.test_user_object.<locals>.JustFloat.__float__N)rT   rU   rV   rd   r   )rc   r   r   	JustFloat   s   rh   c                   s   e Zd Z fddZdS )z0CMathTests.test_user_object.<locals>.JustFloatOSc                s    S )Nr   )r-   )rc   r   r   rd      s    z:CMathTests.test_user_object.<locals>.JustFloatOS.__float__N)rT   rU   rV   rd   r   )rc   r   r   JustFloatOS   s   ri   yz@7Ă??g]ֆ)objectNotImplemented	Exceptiontest_functionsrK   assertRaises	TypeError)r-   Znon_complexesrW   rX   rZ   r[   r\   r]   ra   rb   rf   rg   rh   ri   fZbad_complexr   )rY   re   rc   r   test_user_object   sB    


zCMathTests.test_user_objectc             C   sf   x4| j D ]*}x$dD ]}| j||||j  qW qW x*| j D ] }xd	D ]}| jt|| qHW q>W d S )
Nr^          @rA   long_string01j )r^   rr   )rA   rs   rt   ru   rv   )rm   rK   rd   rn   ro   )r-   rp   argr   r   r   test_input_type  s    
 
zCMathTests.test_input_typec             C   s6  ddddddg}|dd |D  d	d
dg }|d
g dd |D  }d	g| }d	g| dd |D  }|||||||||||||d}x^|j  D ]R\}}tt|}	tt|}
x4|D ],}|
|}| j|	||j | jd	|j qW qW xJdD ]B}x<|D ]4}tj||}| jtj|||j | jd	|j qW qW d S )Ng{Gz?g?g?      ?g?gGz?c             S   s   g | ]
}| qS r   r   )r   r   r   r   r   r   '  s    z6CMathTests.test_cmath_matches_math.<locals>.<listcomp>g        g      ?c             S   s   g | ]}d | qS )g      ?r   )r   r   r   r   r   r   +  s    c             S   s   g | ]
}| qS r   r   )r   r   r   r   r   r   /  s    )r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)          @      $@g      )ry   rz   r{   )	itemsr   r1   r   rD   r8   rK   r9   r#   )r-   r,   Zunit_intervalZpositiveZnonnegativeZ	real_linerm   fnvaluesZfloat_fnZ
complex_fnvzbaser   r   r   test_cmath_matches_math  s>    





z"CMathTests.test_cmath_matches_mathc          "   C   s0  dh}d }t jdkrNtj d }yttt|jd}W n tk
rL   Y nX dd }dd }xtt	D ]\}}}}	}
}}t
||	}t
|
|}|d k	r|dk r||krqj|dkr|}n|dkr|}n
tt|}d|ksd|kr y||}W n tk
r   wjY nX | jdj||||	 d|krfy||}W n tk
rN   wjY nX | jdj||||	 ||}d|krt
t|j|j}t
t|j|j}d|krt
|jt|j}t
|jt|j}|dkrd}nd}dj||||	|j|j|j|j}| j|j|j||d | j|j|j|d qjW d S )NZtan0064darwinr   .c             S   s   t j| j| jS )zaWrapped version of rect that accepts a complex number instead of
            two float arguments.)r   r   r8   r9   )r   r   r   r   rect_complex]  s    z5CMathTests.test_specific_values.<locals>.rect_complexc             S   s   t t|  S )zYWrapped version of polar that returns a complex number instead of
            two floats.)r   r   )r   r   r   r   polar_complexb  s    z6CMathTests.test_specific_values.<locals>.polar_complex
   rP   r   r   zdivide-by-zeroZinvalidz9ValueError not raised in test {}: {}(complex({!r}, {!r}))Zoverflowz<OverflowError not raised in test {}: {}(complex({!r}, {!r}))zignore-real-signzignore-imag-signr#   r$   gV瞯<g
       z{}: {}(complex({!r}, {!r}))
Expected: complex({!r}, {!r})
Received: complex({!r}, {!r})
Received value insufficiently close to expected value.)rC   r6   )r6   )r   rP   )r#   r$   )sysplatformZmac_vertuplemapintsplit
ValueErrorr   r   r   r   r   r4   r5   r?   r>   r8   r9   rD   )r-   ZSKIP_ON_TIGERZosx_versionZversion_txtr   r   idr}   arZaiZerZeiflagsrw   expectedZfunctionZactualZreal_abs_errZerror_messager   r   r   test_specific_valuesO  sl    
 






zCMathTests.test_specific_valuesc                s   fdd}|dd |dd |ddt f |ddt d f |dd
t  d f td}|t|d|df |t| d|t f |td||t d f |td| |t  d f |t|||t d f |t|| |t  d f |t| ||dt  d f |t| | |dt  d f td}|t|d||f |td|||f |t||||f |t||||f |t| |||f |t||||f |t|| ||f d S )Nc                s0    | }x"t ||D ]\}}j|| qW d S )N)ziprD   )rw   r   ZgotrG   g)funcr-   r   r   check  s    z%CMathTests.check_polar.<locals>.checkr           rO         ?y              ?r^   y              @g      @r
      rP      r   )r   r   )r   r   y             )r	   floatr   )r-   r   r   r
   r   r   )r   r-   r   check_polar  s.    

 zCMathTests.check_polarc             C   s   | j t d S )N)r   r   )r-   r   r   r   
test_polar  s    zCMathTests.test_polarc                s&   ddl m   fdd}| j| d S )Nr   )	set_errnoc                s     d zt | S  d X d S )N   r   )r   )r   )r   r   r   polar_with_errno_set  s    z9CMathTests.test_polar_errno.<locals>.polar_with_errno_set)Z	_testcapir   r   )r-   r   r   )r   r   test_polar_errno  s    zCMathTests.test_polar_errnoc             C   s  | j tdd | j tdd | j td
t | j tdt | j tdt  | j tdtd  | j tdt d  | jttddd | jttddd | jttddt | jttddt  | j ttt dt  | j ttt dt  | j ttt t dt  | j ttdt t d  | j ttdt t d  | j ttdt t d  | j ttdt t d  | j tttt t d	  | jtttdd | jtttdd | jtttdd | jtttdd | j tttttd	  | j ttdttd  | j ttdttd  | j ttdttd  | j ttdttd  | j ttt tdt  | j ttt dt | j ttt dt x"tD ]}| jtj	t| qW d S ) Nr   g        g      ?y        Yny              ?r^   gffffff@g      ?r   g      g      y      Yng      y      Yny             g       g       g       g       g       g       gffffffg      gffffffg       gffffffg       g       g       g       gffffff)
rF   r   r	   rK   r   INFcomplex_nansrL   r1   r2   )r-   r   r   r   r   
test_phase  sB    
zCMathTests.test_phasec             C   s  xt D ]}| jt|d qW xtD ]}| jt|t q$W | jtttt t | jtj	tttd | jtj	tttd | jtj	tttd | jtj	tttd | jttttt | jttt tt | jtj	ttdt | jtj	ttdt | jtj	ttdt | jtj	ttdt | jttttt | jtj	tttt d S )Ng        gffffff@gffffffg       gffffffg       )
complex_zerosrK   r>   complex_infinitiesr   r   NANrL   r1   r2   )r-   r   r   r   r   test_abs  s"    

zCMathTests.test_absc             C   s   | j tttdd d S )NgU)rn   r?   r>   r   )r-   r   r   r   test_abs_overflows  s    zCMathTests.test_abs_overflowsc             C   sB   d}t |j|d  |ks0t |j|d  |kr>| j||f d S )NgHz>r   rO   )r>   r8   r9   r4   )r-   rA   rB   Zepsr   r   r   assertCEqual  s    ,zCMathTests.assertCEqualc             C   sj   | j tddd | j tddd | j tdt d | j tdtd d	 | j tdt d d d S )Nr   rO         ?r^   )r   r   )r   r         )r   r   )r   r         )r   r   )r   r   r	   )r-   r   r   r   	test_rect  s
    zCMathTests.test_rectc             C   sj   t dddddt dt dg}xF|D ]>}x8|D ]0}t||}| jtj|tj|oZtj| q.W q$W d S )Nz-infgffffff@g        r
   r   gffffffg       )r   r   rK   r   Zisfiniter1   )r-   Z	real_valsr   r   r   r   r   r   test_isfinite$  s    



zCMathTests.test_isfinitec             C   s   | j tjd | j tjd | j tjt | jtjt | jtjttd | jtjtdt | jtjttt | jtjttt | jtjttt d S )NrO   y              ?r   )assertFalser   r2   r   rL   r   r   )r-   r   r   r   
test_isnan-  s    zCMathTests.test_isnanc             C   s   | j tjd | j tjd | j tjt | jtjt | jtjttd | jtjtdt | jtjttt | jtjttt | jtjttt d S )NrO   y              ?r   )r   r   r=   r   rL   r   r   )r-   r   r   r   
test_isinf8  s    zCMathTests.test_isinfTANH_PRESERVES_ZERO_SIGNr   z,system tanh() function doesn't copy the signc             C   s$   xt D ]}| jtj|| qW d S )N)r   r:   r   r)   )r-   r   r   r   r   testTanhSignC  s    
zCMathTests.testTanhSignc             C   s$   xt D ]}| jtj|| qW d S )N)r   r:   r   r   )r-   r   r   r   r   testAtanSignN  s    
zCMathTests.testAtanSignc             C   s$   xt D ]}| jtj|| qW d S )N)r   r:   r   r   )r-   r   r   r   r   testAtanhSignS  s    
zCMathTests.testAtanhSign)r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   )r;   r<   N)$rT   rU   rV   rm   appendr.   r0   r7   r:   rD   rH   rN   rq   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   unittestZskipIf	sysconfigget_config_varr   r   r   r   r   r   r   r   4   sD     
 
1c0W)		r   c               @   s*   e Zd ZejZdd Zdd Zdd ZdS )IsCloseTestsc             C   sx   | j t | jdddd W d Q R X | j t | jdddd W d Q R X | j t | jddddd W d Q R X d S )Ny              ?)rel_tol)abs_tol)r   r   )rn   ro   isclose)r-   r   r   r   test_reject_complex_tolerances\  s    z+IsCloseTests.test_reject_complex_tolerancesc             C   s,   ddddg}| j |dd | j|dd d S )Ng      ?y              ?g    ?y            ?y        ?g-q=)r   gvIh%<=      ?      ?    ?      ?)r   r         ?      ?      ?    ?)r   r   g                  ?g              ?)r   r         ?            ?)r   r   )assertAllCloseassertAllNotClose)r-   Zcomplex_examplesr   r   r   test_complex_valuesf  s    z IsCloseTests.test_complex_valuesc             C   sP   dd	ddddg}| j |dd | j|dd | jdddd | jdddd d S )N        MbP?r   MbP?g~jtX?)r   gMb@?gMb`?)r   r   )r   r   MbP?MbP?)r   r   gMbPMbPMbP?)r   r   MbP?MbP)r   r   gMbPMbPMbP)r   r   yMbP?MbPyMbP?MbP?yMbP?MbPyMbP?MbP?)r   r   ZassertIsCloseZassertIsNotClose)r-   Znear_zero_examplesr   r   r   test_complex_near_zeroq  s    z#IsCloseTests.test_complex_near_zeroN)rT   rU   rV   r   r   r   r   r   r   r   r   r   r   Y  s   
r   __main__       )r   r   g       gffffffg       gffffffgffffffg       gffffffg       gffffffg       gffffffg       )Ztest.supportr   r   Ztest.test_mathr   r   Z	test_mathr   r   r1   r   r   r   r	   r   r   r   r   r   r   r   r   r   ZTestCaser   r   rT   mainr   r   r   r   <module>   sj   

    ))
