B
    u9a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    /usr/lib/python3.7/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   Zfnamer   r   r   r   6   s    zCMathTests.<listcomp>)acosZacoshasinZ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)r2   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,   r-   r/   r0   isinfr.   absOverflowErrormax)r(   abZrel_errabs_errr1   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 {})Zplacesr1   zcmath.e is {}; should be {})assertAlmostEqualr   r	   r0   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
   r3   r,   r4   rD   
assertTruer-   r   rE   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(   rL   r   r   r   __init__   s    z7CMathTests.test_user_object.<locals>.MyComplex.__init__c             S   s   | j S )N)rL   )r(   r   r   r   __complex__   s    z:CMathTests.test_user_object.<locals>.MyComplex.__complex__N)__name__
__module____qualname__rM   rN   r   r   r   r   	MyComplex   s   rR   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)rL   )r(   rL   r   r   r   rM      s    z9CMathTests.test_user_object.<locals>.MyComplexOS.__init__c             S   s   | j S )N)rL   )r(   r   r   r   rN      s    z<CMathTests.test_user_object.<locals>.MyComplexOS.__complex__N)rO   rP   rQ   rM   rN   r   r   r   r   MyComplexOS   s   rS   c               @   s   e Zd ZdS )z2CMathTests.test_user_object.<locals>.SomeExceptionN)rO   rP   rQ   r   r   r   r   SomeException   s   rT   c                   s   e Zd Z fddZdS )z7CMathTests.test_user_object.<locals>.MyComplexExceptionc                s    d S )Nr   )r(   )rT   r   r   rN      s    zCCMathTests.test_user_object.<locals>.MyComplexException.__complex__N)rO   rP   rQ   rN   r   )rT   r   r   MyComplexException   s   rU   c                   s   e Zd Z fddZdS )z9CMathTests.test_user_object.<locals>.MyComplexExceptionOSc                s    d S )Nr   )r(   )rT   r   r   rN      s    zECMathTests.test_user_object.<locals>.MyComplexExceptionOS.__complex__N)rO   rP   rQ   rN   r   )rT   r   r   MyComplexExceptionOS   s   rV   c               @   s   e Zd ZdS )z;CMathTests.test_user_object.<locals>.NeitherComplexNorFloatN)rO   rP   rQ   r   r   r   r   NeitherComplexNorFloat   s   rW   c               @   s   e Zd ZdS )z=CMathTests.test_user_object.<locals>.NeitherComplexNorFloatOSN)rO   rP   rQ   r   r   r   r   NeitherComplexNorFloatOS   s   rX   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 )NrY   r   )r(   r   r   r   	__index__   r%   z4CMathTests.test_user_object.<locals>.MyInt.__index__N)rO   rP   rQ   rZ   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 )NrY   r   )r(   r   r   r   rZ      r%   z4CMathTests.test_user_object.<locals>.MyIntOS.__int__c             S   s   dS )NrY   r   )r(   r   r   r   r[      r%   z6CMathTests.test_user_object.<locals>.MyIntOS.__index__N)rO   rP   rQ   rZ   r[   r   r   r   r   MyIntOS   s   r]   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   rN      s    z@CMathTests.test_user_object.<locals>.FloatAndComplex.__complex__N)rO   rP   rQ   r_   rN   r   )r`   r^   r   r   FloatAndComplex   s   ra   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(   )r^   r   r   r_      s    z@CMathTests.test_user_object.<locals>.FloatAndComplexOS.__float__c                s    S )Nr   )r(   )r`   r   r   rN      s    zBCMathTests.test_user_object.<locals>.FloatAndComplexOS.__complex__N)rO   rP   rQ   r_   rN   r   )r`   r^   r   r   FloatAndComplexOS   s   rb   c                   s   e Zd Z fddZdS )z.CMathTests.test_user_object.<locals>.JustFloatc                s    S )Nr   )r(   )r^   r   r   r_      s    z8CMathTests.test_user_object.<locals>.JustFloat.__float__N)rO   rP   rQ   r_   r   )r^   r   r   	JustFloat   s   rc   c                   s   e Zd Z fddZdS )z0CMathTests.test_user_object.<locals>.JustFloatOSc                s    S )Nr   )r(   )r^   r   r   r_      s    z:CMathTests.test_user_object.<locals>.JustFloatOS.__float__N)rO   rP   rQ   r_   r   )r^   r   r   JustFloatOS   s   rd   )objectNotImplemented	Exceptiontest_functionsrF   assertRaises	TypeError)r(   Znon_complexesrR   rS   rU   rV   rW   rX   r\   r]   ra   rb   rc   rd   fZbad_complexr   )rT   r`   r^   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)rY   g       @)r<   Zlong_string0Z1j )rh   rF   r_   ri   rj   )r(   rk   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   r?   r3   rF   r4   r   )r(   r'   Zunit_intervalZpositiveZnonnegativeZ	real_linerh   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   r3   r4   )ru   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   )ru   r   r   r   polar_complexb  s    z6CMathTests.test_specific_values.<locals>.polar_complex)
   rK   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-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.)r>   r1   )r1   )sysplatformZmac_vertuplemapintsplit
ValueErrorr   r   r   r   r   r/   r0   r:   r9   r3   r4   r?   )r(   ZSKIP_ON_TIGERZosx_versionZversion_txtrz   r{   idrr   arZaiZerZeiflagsro   expectedZfunction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 ]\}}|| qW d S )N)zipr?   )ro   r   ZgotrB   g)funcr(   r   r   check  s    z%CMathTests.check_polar.<locals>.checkr   )g        g        rJ   )g      ?g        g      ?y              ?rY   y             g      @r
   g           rK      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   )ru   )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  |  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              ?rY   y             g       gffffffg      gffffff@r   g      ?)
rA   r   r	   rF   r   INFcomplex_nansrG   r,   r-   )r(   ru   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_zerosrF   r9   complex_infinitiesr   r   NANrG   r,   r-   )r(   ru   r   r   r   test_abs  s"    

zCMathTests.test_absc             C   s   |  tttdd d S )NgU)ri   r:   r9   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   rJ   )r9   r3   r4   r/   )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   rJ   )g      ?r   )g      r   rY   )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   rF   r   Zisfiniter,   )r(   Z	real_valsr   r   ru   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 )NrJ   y              ?r   )assertFalser   r-   r   rG   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 )NrJ   y              ?r   )r   r   r8   r   rG   r   r   )r(   r   r   r   
test_isinf8  s    zCMathTests.test_isinfZ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   r5   r   r#   )r(   ru   r   r   r   testTanhSignC  s    
zCMathTests.testTanhSignc             C   s$   xt D ]}| t|| qW d S )N)r   r5   r   r   )r(   ru   r   r   r   testAtanSignN  s    
zCMathTests.testAtanSignc             C   s$   xt D ]}| t|| qW d S )N)r   r5   r   r   )r(   ru   r   r   r   testAtanhSignS  s    
zCMathTests.testAtanhSign)r6   r7   N)$rO   rP   rQ   rh   appendr)   r+   r2   r5   r?   rC   rI   rl   rp   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   unittestZskipIf	sysconfigZ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   )ri   rj   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)rO   rP   rQ   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   ZTestCaser   r   rO   mainr   r   r   r   <module>   sj   
    ))
