B
    18\a                 @   s  d dl mZmZ d dlmZmZ d dlm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  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 ))g        g       )complex).0xy r   S/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_cmath.py
<listcomp>   s    r   )g        g       c             C   s   g | ]\}}t ||qS r   )r   )r   r   r   r   r   r   r      s    g        gffffff@g       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 dD Zedd  edd  dd Zd	d
 Z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edd Zd d! Zd"d# Zed$d% Zd&d' Zd(d) Zed*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zeee d6d7kd8d9d: Z!ed;d< Z"ed=d> Z#dS )@
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 | dS )Ny     @        )r   r    )r   r   r   r   <lambda>;       zCMathTests.<lambda>c             C   s   t d| S )Ny      ,@      ;)r   r    )r   r   r   r   r'   <   r(   c             C   s   t t| _d S )N)openr   test_values)selfr   r   r   setUp>   s    zCMathTests.setUpc             C   s   | j   d S )N)r*   close)r+   r   r   r   tearDownA   s    zCMathTests.tearDownc             C   s   d}t |st |r2t |rjt |rjdS n8||krj|dkrFdS t d|t d|krbdS |d7 }| ||| 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 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)r5   realimag)r+   r   r   r   r   r   assertComplexIdentical[   s    z!CMathTests.assertComplexIdenticalV瞯<
       Nc             C   s   t |r,t |rdS | |p(d| t |rX||krBdS | |pTd|| |s|st d|t d|kr| |pd|| yt|| }W n tk
r   Y nX |t||t| krdS | |pd|| 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)	r/   r0   r2   r3   isinfr1   absOverflowErrormax)r+   abZrel_errabs_errr4   Zabsolute_errorr   r   r   rAssertAlmostEquale   s*    




zCMathTests.rAssertAlmostEqualc             C   sH   d}d}| j tj|ddtj|d | j tj|ddtj|d d S )NgiW
@g-DT!	@	   zcmath.pi is {}; should be {})placesr4   zcmath.e is {}; should be {})assertAlmostEqualr   r	   r3   e)r+   Z
e_expectedZpi_expectedr   r   r   test_constants   s    zCMathTests.test_constantsc             C   s   |  tjjtj |  tjjd |  tjjd |  tjjtj | ttj	j |  tj	jd |  tj
jd | ttj
j |  ttjd |  ttjd |  ttj	d |  ttj
d d S )Ng        r
   infjr   nanj)assertEqualr   r
   r6   r/   r7   rH   
assertTruer0   r   rI   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 ]"}| ||| | ||| | ||
 | | || | | || | | || | | t||  | t||  | t||  | t||	  x2|D ]*}| t||| | t||| qW |  ||  |  ||  qW d S )!Nyz@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+   rP   r   r   r   __init__   s    z7CMathTests.test_user_object.<locals>.MyComplex.__init__c             S   s   | j S )N)rP   )r+   r   r   r   __complex__   s    z:CMathTests.test_user_object.<locals>.MyComplex.__complex__N)__name__
__module____qualname__rQ   rR   r   r   r   r   	MyComplex   s   rV   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)rP   )r+   rP   r   r   r   rQ      s    z9CMathTests.test_user_object.<locals>.MyComplexOS.__init__c             S   s   | j S )N)rP   )r+   r   r   r   rR      s    z<CMathTests.test_user_object.<locals>.MyComplexOS.__complex__N)rS   rT   rU   rQ   rR   r   r   r   r   MyComplexOS   s   rW   c               @   s   e Zd ZdS )z2CMathTests.test_user_object.<locals>.SomeExceptionN)rS   rT   rU   r   r   r   r   SomeException   s   rX   c                   s   e Zd Z fddZdS )z7CMathTests.test_user_object.<locals>.MyComplexExceptionc                s    d S )Nr   )r+   )rX   r   r   rR      s    zCCMathTests.test_user_object.<locals>.MyComplexException.__complex__N)rS   rT   rU   rR   r   )rX   r   r   MyComplexException   s   rY   c                   s   e Zd Z fddZdS )z9CMathTests.test_user_object.<locals>.MyComplexExceptionOSc                s    d S )Nr   )r+   )rX   r   r   rR      s    zECMathTests.test_user_object.<locals>.MyComplexExceptionOS.__complex__N)rS   rT   rU   rR   r   )rX   r   r   MyComplexExceptionOS   s   rZ   c               @   s   e Zd ZdS )z;CMathTests.test_user_object.<locals>.NeitherComplexNorFloatN)rS   rT   rU   r   r   r   r   NeitherComplexNorFloat   s   r[   c               @   s   e Zd ZdS )z=CMathTests.test_user_object.<locals>.NeitherComplexNorFloatOSN)rS   rT   rU   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__   r(   z2CMathTests.test_user_object.<locals>.MyInt.__int__c             S   s   dS )Nr]   r   )r+   r   r   r   	__index__   r(   z4CMathTests.test_user_object.<locals>.MyInt.__index__N)rS   rT   rU   r^   r_   r   r   r   r   MyInt   s   r`   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^      r(   z4CMathTests.test_user_object.<locals>.MyIntOS.__int__c             S   s   dS )Nr]   r   )r+   r   r   r   r_      r(   z6CMathTests.test_user_object.<locals>.MyIntOS.__index__N)rS   rT   rU   r^   r_   r   r   r   r   MyIntOS   s   ra   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   rR      s    z@CMathTests.test_user_object.<locals>.FloatAndComplex.__complex__N)rS   rT   rU   rc   rR   r   )rd   rb   r   r   FloatAndComplex   s   re   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+   )rb   r   r   rc      s    z@CMathTests.test_user_object.<locals>.FloatAndComplexOS.__float__c                s    S )Nr   )r+   )rd   r   r   rR      s    zBCMathTests.test_user_object.<locals>.FloatAndComplexOS.__complex__N)rS   rT   rU   rc   rR   r   )rd   rb   r   r   FloatAndComplexOS   s   rf   c                   s   e Zd Z fddZdS )z.CMathTests.test_user_object.<locals>.JustFloatc                s    S )Nr   )r+   )rb   r   r   rc      s    z8CMathTests.test_user_object.<locals>.JustFloat.__float__N)rS   rT   rU   rc   r   )rb   r   r   	JustFloat   s   rg   c                   s   e Zd Z fddZdS )z0CMathTests.test_user_object.<locals>.JustFloatOSc                s    S )Nr   )r+   )rb   r   r   rc      s    z:CMathTests.test_user_object.<locals>.JustFloatOS.__float__N)rS   rT   rU   rc   r   )rb   r   r   JustFloatOS   s   rh   )objectNotImplemented	Exceptiontest_functionsrJ   assertRaises	TypeError)r+   Znon_complexesrV   rW   rY   rZ   r[   r\   r`   ra   re   rf   rg   rh   fZbad_complexr   )rX   rd   rb   r   test_user_object   sB    


zCMathTests.test_user_objectc             C   sf   x4| j D ]*}x$dD ]}| ||||  qW qW x*| j D ] }xdD ]}| t|| qHW q>W d S )N)r]   g       @)r?   Zlong_string0Z1j )rl   rJ   rc   rm   rn   )r+   ro   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^|  D ]R\}}tt|}	tt|}
x4|D ],}|
|}| |	||j | d	|j qW qW xJdD ]B}x<|D ]4}t||}| t|||j | d	|j qW qW d S )Ng{Gz?g?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      ?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      ?g       @g      $@)	itemsr   r/   r   rB   r6   rJ   r7   r    )r+   r*   Zunit_intervalZpositiveZnonnegativeZ	real_linerl   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 d }yttt|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 | d||||	 d|krfy||}W n tk
rN   wjY nX | d||||	 ||}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||d | j|j|j|d qjW d S )NZtan0064darwinr   .c             S   s   t | j| jS )zaWrapped version of rect that accepts a complex number instead of
            two float arguments.)r   r   r6   r7   )ry   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   )ry   r   r   r   polar_complexb  s    z6CMathTests.test_specific_values.<locals>.polar_complex)
   rO   r   r   zdivide-by-zeroinvalidz9ValueError not raised in test {}: {}(complex({!r}, {!r}))Zoverflowz<OverflowError not raised in test {}: {}(complex({!r}, {!r}))zignore-real-signzignore-imag-sign)r    r!   gV瞯<g
       z{}: {}(complex({!r}, {!r}))
Expected: complex({!r}, {!r})
Received: complex({!r}, {!r})
Received value insufficiently close to expected value.)rA   r4   )r4   )sysplatformmac_vertuplemapintsplit
ValueErrorr   r   r   r   r   r2   r3   r=   r<   r6   r7   rB   )r+   ZSKIP_ON_TIGERZosx_versionversion_txtr~   r   idrv   araiZereiflagsrs   expectedfunctionactualZreal_abs_err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 ]\}}|| qW d S )N)ziprB   )rs   r   ZgotrF   g)funcr+   r   r   check  s    z%CMathTests.check_polar.<locals>.checkr   )g        g        rN   )g      ?g        g      ?y              ?r]   y             g      @r
   g           rO      r   )r	   floatr   )r+   r   r   r
   r   r   )r   r+   r   check_polar  s.    

 zCMathTests.check_polarc             C   s   |  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}| | d S )Nr   )	set_errnoc                s     d zt | S  d X d S )N   r   )r   )ry   )r   r   r   polar_with_errno_set  s    z9CMathTests.test_polar_errno.<locals>.polar_with_errno_set)	_testcapir   r   )r+   r   r   )r   r   test_polar_errno  s    zCMathTests.test_polar_errnoc             C   s  |  tdd |  tdd |  tdt |  tdt |  tdt  |  tdtd  |  td	t d  | ttddd | ttdd
d
 | ttd
dt | ttd
d
t  |  ttt d
t  |  ttt dt  |  ttt t dt  |  ttdt t d  |  ttd
t t d  |  ttdt t d  |  ttdt t d  |  tttt t d  | tttdd
 | tttd
d
 | tttdd | tttdd |  tttttd  |  ttdttd  |  ttdttd  |  ttd
ttd  |  ttdttd  |  ttt tdt  |  ttt dt |  ttt dt x"tD ]}| t	t| qW d S )Nr   g        g      ?g      y      Yny      Yny              ?r]   y             g       gffffffg      gffffff@r   g      ?)
rE   r   r	   rJ   r   INFcomplex_nansrK   r/   r0   )r+   ry   r   r   r   
test_phase  sB    
zCMathTests.test_phasec             C   s  xt D ]}| t|d qW xtD ]}| t|t q$W | tttt t | t	tttd | t	tttd | t	tttd | t	tttd | ttttt | ttt tt | t	ttdt | t	ttdt | t	ttdt | t	ttdt | ttttt | t	tttt d S )Ng        gffffffg       gffffff@)
complex_zerosrJ   r<   complex_infinitiesr   r   NANrK   r/   r0   )r+   ry   r   r   r   test_abs  s"    

zCMathTests.test_absc             C   s   |  tttdd d S )NgU)rm   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>| ||f d S )NgHz>r   rN   )r<   r6   r7   r2   )r+   r?   r@   Zepsr   r   r   assertCEqual  s    ,zCMathTests.assertCEqualc             C   sj   |  tddd |  tddd |  tdt d |  tdtd d |  tdt d d d S )	Nr   )r   r   rN   )g      ?r   )g      r   r]   )r   g      ?)r   g      )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||}| t|t|oZt| q.W q$W d S )Nz-infgffffffg       g        gffffff@r
   r   )r   r   rJ   r   isfiniter/   )r+   Z	real_valsr   r   ry   r   r   r   test_isfinite$  s    



zCMathTests.test_isfinitec             C   s   |  td |  td |  tt | tt | tttd | ttdt | tttt | tttt | tttt d S )NrN   y              ?r   )assertFalser   r0   r   rK   r   r   )r+   r   r   r   
test_isnan-  s    zCMathTests.test_isnanc             C   s   |  td |  td |  tt | tt | tttd | ttdt | tttt | tttt | tttt d S )NrN   y              ?r   )r   r   r;   r   rK   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 ]}| t|| qW d S )N)r   r8   r   r&   )r+   ry   r   r   r   testTanhSignC  s    
zCMathTests.testTanhSignc             C   s$   xt D ]}| t|| qW d S )N)r   r8   r   r   )r+   ry   r   r   r   testAtanSignN  s    
zCMathTests.testAtanSignc             C   s$   xt D ]}| t|| qW d S )N)r   r8   r   r   )r+   ry   r   r   r   testAtanhSignS  s    
zCMathTests.testAtanhSign)r9   r:   N)$rS   rT   rU   rl   appendr,   r.   r5   r8   rB   rG   rM   rp   rt   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   unittestskipIf	sysconfigget_config_varr   r   r   r   r   r   r   r   4   s>   
 
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   |  t | jdddd W d Q R X |  t | jdddd W d Q R X |  t | jddddd W d Q R X d S )Ny              ?)rel_tol)abs_tol)r   r   )rm   rn   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 )N)y      ?      ?y    ?      ?)y      ?      ?y      ?    ?)y            ?y          ?)y      ?      y      ?g-q=)r   gvIh%<=)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)y        MbP?r   )gMbP?r   )yMbP?MbP?r   )yMbPMbP?r   )yMbP?MbPr   )yMbPMbPr   g~jtX?)r   gMb@?yMbP?MbPyMbP?MbP?gMb`?gMbP?)r   r   ZassertIsCloseZassertIsNotClose)r+   Znear_zero_examplesr   r   r   test_complex_near_zeroq  s    z#IsCloseTests.test_complex_near_zeroN)rS   rT   rU   r   r   r   r   r   r   r   r   r   r   Y  s   
r   __main__)Ztest.supportr   r   Ztest.test_mathr   r   Z	test_mathr   r   r/   r   r   r   r	   r   r   r   r   r   r   r   r   r   TestCaser   r   rS   mainr   r   r   r   <module>   sj   
    ))
