a
    zeD`                     @   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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f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f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 )               complex.0xy r   %/usr/lib/python3.9/test/test_cmath.py
<listcomp>       r   r   c                 C   s   g | ]\}}t ||qS r   r   r   r   r   r   r      r   r   ffffff@r   ffffffc                 C   s   g | ]\}}t ||qS r   r   r   r   r   r   r   %   r   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d6d7 Zed8d9 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   r   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>;   r   zCMathTests.<lambda>c                 C   s   t d| S )Ny      ,@      ;r.   r/   r   r   r   r0   <   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 r1   )r3   closer4   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 identicalNr         ?z: zeros have different signs)mathisnancopysignfailformat)r5   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)r@   realimag)r5   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|| zt|| }W n ty   Y n0 |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}r9   z,zero has wrong sign: expected {!r}, got {!r}z({!r} and {!r} are not sufficiently close)	r:   r;   r=   r>   isinfr<   absOverflowErrormax)r5   abZrel_errabs_errr?   Zabsolute_errorr   r   r   rAssertAlmostEquale   s0    



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placesr?   zcmath.e is {}; should be {})assertAlmostEqualr   r	   r>   e)r5   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 )Nr   r
   infjr   nanj)assertEqualr   r
   rA   r:   rB   rR   
assertTruer;   r   rS   reprr4   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}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 }| jD ]*}| ||| | ||| | ||
 | | || | | || | | || | | || |t|  | t||  | t||	  | t||  |D ]*}| t||| | t||| q|  ||  |  ||  q d S )!Nyz@7Ă??g]ֆznot complex             @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 r1   valuer5   r\   r   r   r   __init__   s    z7CMathTests.test_user_object.<locals>.MyComplex.__init__c                 S   s   | j S r1   r[   r4   r   r   r   __complex__   s    z:CMathTests.test_user_object.<locals>.MyComplex.__complex__N__name__
__module____qualname__r^   r_   r   r   r   r   	MyComplex   s   rd   c                   @   s   e Zd Zdd Zdd ZdS )z0CMathTests.test_user_object.<locals>.MyComplexOSc                 S   s
   || _ d S r1   r[   r]   r   r   r   r^      s    z9CMathTests.test_user_object.<locals>.MyComplexOS.__init__c                 S   s   | j S r1   r[   r4   r   r   r   r_      s    z<CMathTests.test_user_object.<locals>.MyComplexOS.__complex__Nr`   r   r   r   r   MyComplexOS   s   re   c                   @   s   e Zd ZdS )z2CMathTests.test_user_object.<locals>.SomeExceptionNra   rb   rc   r   r   r   r   SomeException   s   rg   c                       s   e Zd Z fddZdS )z7CMathTests.test_user_object.<locals>.MyComplexExceptionc                    s    d S r1   r   r4   rg   r   r   r_      s    zCCMathTests.test_user_object.<locals>.MyComplexException.__complex__Nra   rb   rc   r_   r   rh   r   r   MyComplexException   s   rj   c                       s   e Zd Z fddZdS )z9CMathTests.test_user_object.<locals>.MyComplexExceptionOSc                    s    d S r1   r   r4   rh   r   r   r_      s    zECMathTests.test_user_object.<locals>.MyComplexExceptionOS.__complex__Nri   r   rh   r   r   MyComplexExceptionOS   s   rk   c                   @   s   e Zd ZdS )z;CMathTests.test_user_object.<locals>.NeitherComplexNorFloatNrf   r   r   r   r   NeitherComplexNorFloat   s   rl   c                   @   s   e Zd ZdS )z=CMathTests.test_user_object.<locals>.NeitherComplexNorFloatOSNrf   r   r   r   r   NeitherComplexNorFloatOS   s   rm   c                   @   s   e Zd Zdd Zdd ZdS )z*CMathTests.test_user_object.<locals>.Indexc                 S   s   dS N   r   r4   r   r   r   __int__   r   z2CMathTests.test_user_object.<locals>.Index.__int__c                 S   s   dS rn   r   r4   r   r   r   	__index__   r   z4CMathTests.test_user_object.<locals>.Index.__index__N)ra   rb   rc   rp   rq   r   r   r   r   Index   s   rr   c                   @   s   e Zd Zdd ZdS )z*CMathTests.test_user_object.<locals>.MyIntc                 S   s   dS rn   r   r4   r   r   r   rp      r   z2CMathTests.test_user_object.<locals>.MyInt.__int__N)ra   rb   rc   rp   r   r   r   r   MyInt   s   rs   c                       s$   e Zd ZfddZ fddZdS )z4CMathTests.test_user_object.<locals>.FloatAndComplexc                    s    S r1   r   r4   flt_argr   r   	__float__   s    z>CMathTests.test_user_object.<locals>.FloatAndComplex.__float__c                    s    S r1   r   r4   cx_argr   r   r_      s    z@CMathTests.test_user_object.<locals>.FloatAndComplex.__complex__Nra   rb   rc   rv   r_   r   rx   ru   r   r   FloatAndComplex   s   r{   c                       s$   e Zd ZfddZ fddZdS )z6CMathTests.test_user_object.<locals>.FloatAndComplexOSc                    s    S r1   r   r4   rt   r   r   rv      s    z@CMathTests.test_user_object.<locals>.FloatAndComplexOS.__float__c                    s    S r1   r   r4   rw   r   r   r_      s    zBCMathTests.test_user_object.<locals>.FloatAndComplexOS.__complex__Nry   r   rz   r   r   FloatAndComplexOS   s   r|   c                       s   e Zd Z fddZdS )z.CMathTests.test_user_object.<locals>.JustFloatc                    s    S r1   r   r4   rt   r   r   rv      s    z8CMathTests.test_user_object.<locals>.JustFloat.__float__Nra   rb   rc   rv   r   rt   r   r   	JustFloat   s   r~   c                       s   e Zd Z fddZdS )z0CMathTests.test_user_object.<locals>.JustFloatOSc                    s    S r1   r   r4   rt   r   r   rv      s    z:CMathTests.test_user_object.<locals>.JustFloatOS.__float__Nr}   r   rt   r   r   JustFloatOS   s   r   )objectNotImplemented	Exceptiontest_functionsrT   intassertRaises	TypeError)r5   Znon_complexesrd   re   rj   rk   rl   rm   rr   rs   r{   r|   r~   r   fZbad_complexr   )rg   rx   ru   r   test_user_object   sD    
zCMathTests.test_user_objectc                 C   sV   | j D ]&}dD ]}| ||||  qq| j D ]}dD ]}| t|| q<q4d S )N)ro   rZ   )rJ   Zlong_string0Z1j )r   rT   rv   r   r   )r5   r   argr   r   r   test_input_type  s    

zCMathTests.test_input_typec                 C   s  g d}|dd |D  g d }|dg dd |D  }dg| }dg| dd |D  }|||||||||||||d	}|  D ]N\}}tt|}	tt|}
|D ],}|
|}| |	||j | d|j qqd
D ]>}|D ]4}t||}| t|||j | d|j qqd S )N)g{Gz?g?g?      ?g?gGz?c                 S   s   g | ]
}| qS r   r   r   r   r   r   r   r   &  r   z6CMathTests.test_cmath_matches_math.<locals>.<listcomp>)r   r9         r9   c                 S   s   g | ]}d | qS )r9   r   r   r   r   r   r   *  r   r   c                 S   s   g | ]
}| qS r   r   r   r   r   r   r   .  r   )r   r    r"   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   )r   rZ   g      $@)	itemsr   r:   r   rM   rA   rT   rB   r'   )r5   r3   Zunit_intervalZpositiveZnonnegativeZ	real_liner   fnvaluesZfloat_fnZ
complex_fnvzbaser   r   r   test_cmath_matches_math  sB    


z"CMathTests.test_cmath_matches_mathc                 C   s(  dh}d }t jdkrLt d }zttt|d}W n tyJ   Y n0 dd }dd }tt	D ]\}}}}	}
}}t
||	}t
|
|}|d ur|d	k r||v rqd|d
kr|}n|dkr|}n
tt|}d|v sd|v rz||}W n ty   Y qdY n0 | d||||	 d|v r`z||}W n tyH   Y qdY n0 | d||||	 ||}d|v rt
t|j|j}t
t|j|j}d|v rt
|jt|j}t
|jt|j}|dv rd}nd}d||||	|j|j|j|j}| j|j|j||d | j|j|j|d qdd 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   rA   rB   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_complexa  s    z6CMathTests.test_specific_values.<locals>.polar_complex)
   rY   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(   rD   rE   z{}: {}(complex({!r}, {!r}))
Expected: complex({!r}, {!r})
Received: complex({!r}, {!r})
Received value insufficiently close to expected value.)rL   r?   )r?   )sysplatformZmac_vertuplemapr   split
ValueErrorr   r   r   r   r   r=   r>   rH   rG   rA   rB   rM   )r5   ZSKIP_ON_TIGERZosx_versionZversion_txtr   r   idr   arZaiZereiflagsr   expectedfunctionactualZreal_abs_errZerror_messager   r   r   test_specific_valuesN  sx    









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                    s,    | }t ||D ]\}}|| qd S r1   )ziprM   )r   r   ZgotrP   gfuncr5   r   r   check  s    z%CMathTests.check_polar.<locals>.checkr   )r   r   rX   )r9   r   r9                 ?ro   y             g      @r
   r      rY      r   )r	   floatr   )r5   r   r   r
   r   r   r   r   check_polar  s.    

 zCMathTests.check_polarc                 C   s   |  t d S r1   )r   r   r4   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 | W  d S  d 0 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   )r5   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 tD ]}| t	t| qd S )Nr   r   r9   r   y      Yny      Ynr   ro   y             r   r   g      r   r   g      ?)
rO   r   r	   rT   r   INFcomplex_nansrU   r:   r;   r5   r   r   r   r   
test_phase  sB    zCMathTests.test_phasec                 C   s~  t D ]}| t|d qtD ]}| t|t q| 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 )Nr   r   r   r   )
complex_zerosrT   rG   complex_infinitiesr   r   NANrU   r:   r;   r   r   r   r   test_abs  s"    zCMathTests.test_absc                 C   s   |  tttdd d S )NgU)r   rH   rG   r   r4   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   rX   )rG   rA   rB   r=   )r5   rJ   rK   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   rX   )r9   r   )r   r   ro   )r   r9   )r   r   )r   r   r	   r4   r   r   r   	test_rect  s
    zCMathTests.test_rectc                 C   sb   t dddddt dt dg}|D ]:}|D ]0}t||}| t|t|oVt| q*q"d S )Nz-infr   r   r   r   r
   r   )r   r   rT   r   isfiniter:   )r5   Z	real_valsr   r   r   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 NrX   r   r   )assertFalser   r;   r   rU   r   r   r4   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 r   )r   r   rF   r   rU   r   r   r4   r   r   r   
test_isinf7  s    zCMathTests.test_isinfc                 C   s    t D ]}| t|| qd S r1   )r   rC   r   r-   r   r   r   r   testTanhSignB  s    zCMathTests.testTanhSignc                 C   s    t D ]}| t|| qd S r1   )r   rC   r   r"   r   r   r   r   testAtanSignK  s    zCMathTests.testAtanSignc                 C   s    t D ]}| t|| qd S r1   )r   rC   r   r#   r   r   r   r   testAtanhSignP  s    zCMathTests.testAtanhSign)rD   rE   N) ra   rb   rc   r   appendr6   r8   r@   rC   rM   rQ   rW   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   4   sH   
  
2b0
V
)
	

r   c                   @   s*   e Zd ZejZdd Zdd Zdd ZdS )IsCloseTestsc                 C   s   |  t  | jdddd W d    n1 s00    Y  |  t  | jdddd W d    n1 sj0    Y  |  t" | jddddd W d    n1 s0    Y  d S )Nr   rel_tolabs_tol)r   r   )r   r   iscloser4   r   r   r   test_reject_complex_tolerancesY  s    ..z+IsCloseTests.test_reject_complex_tolerancesc                 C   s(   g d}| j |dd | j|dd d S )N))      ?      ?y    ?      ?)r   y      ?    ?)y            ?y          ?)y      ?      y      ?g-q=r   gvIh%<=)assertAllCloseassertAllNotClose)r5   Zcomplex_examplesr   r   r   test_complex_valuesc  s    z IsCloseTests.test_complex_valuesc                 C   sH   g d}| j |dd | j|dd | jdddd | jdddd d S )	N))y        MbP?r   )MbP?r   )MbP?MbP?r   )yMbPMbP?r   )MbP?MbPr   )yMbPMbPr   g~jtX?r   gMb@?r   r   gMb`?r   )r   r   ZassertIsCloseZassertIsNotClose)r5   Znear_zero_examplesr   r   r   test_complex_near_zeron  s
    z#IsCloseTests.test_complex_near_zeroN)ra   rb   rc   r   r   r   r   r   r   r   r   r   r   V  s   
r   __main__)Ztest.supportr   r   Ztest.test_mathr   r   Z	test_mathZunittestr   r:   r   r   r   r	   r   r   r   r   r   r   r   r   ZTestCaser   r   ra   mainr   r   r   r   <module>   sl   
    &)
