o
    gV`                     @   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 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   E/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_cmath.py
<listcomp>   s     r   r   c                 C      g | ]	\}}t ||qS r   r   r   r   r   r   r          r   ffffff@r   ffffffc                 C   r   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   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      ,@      ;r0   r1   r   r   r   r2   <   r3   c                 C   s   t tdd| _d S )Nzutf-8)encoding)openr   test_valuesselfr   r   r   setUp>   s   zCMathTests.setUpc                 C   s   | j   d S N)r6   closer7   r   r   r   tearDownA      zCMathTests.tearDownc                 C   s   	 d}t |st |rt |rt |rd S n||kr6|dkr$d S t d|t d|kr2d S |d7 }| ||| d S )Nz&floats {!r} and {!r} are not identicalr         ?z: zeros have different signs)mathisnancopysignfailformat)r8   r   r   msgr   r   r   assertFloatIdenticalD   s   zCMathTests.assertFloatIdenticalc                 C   s&   	 |  |j|j |  |j|j d S r:   )rE   realimag)r8   r   r   r   r   r   assertComplexIdentical[   s   z!CMathTests.assertComplexIdenticalV瞯<
       Nc                 C   s   	 t |rt |rd S | |pd| t |r-||kr"d S | |p+d|| |sH|sHt d|t d|krH| |pFd|| zt|| }W n	 tyY   Y nw |t||t| krgd S | |ppd|| d S )Nz{!r} should be nanz>finite result where infinity expected: expected {!r}, got {!r}r>   z,zero has wrong sign: expected {!r}, got {!r}z({!r} and {!r} are not sufficiently close)	r?   r@   rB   rC   isinfrA   absOverflowErrormax)r8   abZrel_errabs_errrD   Zabsolute_errorr   r   r   rAssertAlmostEquale   s4   






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placesrD   zcmath.e is {}; should be {})assertAlmostEqualr   r	   rC   e)r8   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
   rF   r?   rG   rW   
assertTruer@   r   rX   reprr7   r   r   r   test_infinity_and_nan_constants   s   z*CMathTests.test_infinity_and_nan_constantsc                    s,  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|  ||  |  ||  qd S )!Nyz@7Ă??g]ֆznot complex             @c                   @      e Zd Zdd Zdd ZdS )z.CMathTests.test_user_object.<locals>.MyComplexc                 S   
   || _ d S r:   valuer8   rc   r   r   r   __init__      
z7CMathTests.test_user_object.<locals>.MyComplex.__init__c                 S      | j S r:   rb   r7   r   r   r   __complex__      z:CMathTests.test_user_object.<locals>.MyComplex.__complex__N__name__
__module____qualname__re   rh   r   r   r   r   	MyComplex       rn   c                   @   r`   )z0CMathTests.test_user_object.<locals>.MyComplexOSc                 S   ra   r:   rb   rd   r   r   r   re      rf   z9CMathTests.test_user_object.<locals>.MyComplexOS.__init__c                 S   rg   r:   rb   r7   r   r   r   rh      ri   z<CMathTests.test_user_object.<locals>.MyComplexOS.__complex__Nrj   r   r   r   r   MyComplexOS   ro   rp   c                   @      e Zd ZdS )z2CMathTests.test_user_object.<locals>.SomeExceptionNrk   rl   rm   r   r   r   r   SomeException       rs   c                          e Zd Z fddZdS )z7CMathTests.test_user_object.<locals>.MyComplexExceptionc                        r:   r   r7   rs   r   r   rh         zCCMathTests.test_user_object.<locals>.MyComplexException.__complex__Nrk   rl   rm   rh   r   rw   r   r   MyComplexException       rz   c                       ru   )z9CMathTests.test_user_object.<locals>.MyComplexExceptionOSc                    rv   r:   r   r7   rw   r   r   rh      rx   zECMathTests.test_user_object.<locals>.MyComplexExceptionOS.__complex__Nry   r   rw   r   r   MyComplexExceptionOS   r{   r|   c                   @   rq   )z;CMathTests.test_user_object.<locals>.NeitherComplexNorFloatNrr   r   r   r   r   NeitherComplexNorFloat   rt   r}   c                   @   rq   )z=CMathTests.test_user_object.<locals>.NeitherComplexNorFloatOSNrr   r   r   r   r   NeitherComplexNorFloatOS   rt   r~   c                   @   r`   )z*CMathTests.test_user_object.<locals>.Indexc                 S      dS N   r   r7   r   r   r   __int__       z2CMathTests.test_user_object.<locals>.Index.__int__c                 S   r   r   r   r7   r   r   r   	__index__   r   z4CMathTests.test_user_object.<locals>.Index.__index__N)rk   rl   rm   r   r   r   r   r   r   Index   s    r   c                   @   s   e Zd Zdd ZdS )z*CMathTests.test_user_object.<locals>.MyIntc                 S   r   r   r   r7   r   r   r   r      r   z2CMathTests.test_user_object.<locals>.MyInt.__int__N)rk   rl   rm   r   r   r   r   r   MyInt   s    r   c                       $   e Zd ZfddZ fddZdS )z4CMathTests.test_user_object.<locals>.FloatAndComplexc                        S r:   r   r7   flt_argr   r   	__float__   rx   z>CMathTests.test_user_object.<locals>.FloatAndComplex.__float__c                    r   r:   r   r7   cx_argr   r   rh      rx   z@CMathTests.test_user_object.<locals>.FloatAndComplex.__complex__Nrk   rl   rm   r   rh   r   r   r   r   r   FloatAndComplex       r   c                       r   )z6CMathTests.test_user_object.<locals>.FloatAndComplexOSc                    r   r:   r   r7   r   r   r   r      rx   z@CMathTests.test_user_object.<locals>.FloatAndComplexOS.__float__c                    r   r:   r   r7   r   r   r   rh      rx   zBCMathTests.test_user_object.<locals>.FloatAndComplexOS.__complex__Nr   r   r   r   r   FloatAndComplexOS   r   r   c                       ru   )z.CMathTests.test_user_object.<locals>.JustFloatc                    r   r:   r   r7   r   r   r   r      rx   z8CMathTests.test_user_object.<locals>.JustFloat.__float__Nrk   rl   rm   r   r   r   r   r   	JustFloat   r{   r   c                       ru   )z0CMathTests.test_user_object.<locals>.JustFloatOSc                    r   r:   r   r7   r   r   r   r      rx   z:CMathTests.test_user_object.<locals>.JustFloatOS.__float__Nr   r   r   r   r   JustFloatOS   r{   r   )objectNotImplemented	Exceptiontest_functionsrY   intassertRaises	TypeError)r8   Znon_complexesrn   rp   rz   r|   r}   r~   r   r   r   r   r   r   fZbad_complexr   )rs   r   r   r   test_user_object   sF   

zCMathTests.test_user_objectc                 C   sV   | j D ]}dD ]}| ||||  qq| j D ]}dD ]	}| t|| qqd S )N)r   r_   )rO   Zlong_string0Z1j )r   rY   r   r   r   )r8   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 ]'\}}tt|}	tt|}
|D ]}|
|}| |	||j | d|j qRqBd
D ]}|D ]}t||}| t|||j | d|j qpqld S )N)g{Gz?g?g?      ?g?gGz?c                 S      g | ]}| qS r   r   r   r   r   r   r   r   &      z6CMathTests.test_cmath_matches_math.<locals>.<listcomp>)r   r>         r>   c                 S   s   g | ]}d | qS )r>   r   r   r   r   r   r   *  s    r   c                 S   r   r   r   r   r   r   r   r   .  r   )r    r"   r$   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   )r   r_   g      $@)	itemsr   r?   r   rR   rF   rY   rG   r)   )r8   r6   Zunit_intervalZpositiveZnonnegativeZ	real_liner   fnvaluesZfloat_fnZ
complex_fnvzbaser   r   r   test_cmath_matches_math  sH   


z"CMathTests.test_cmath_matches_mathc                 C   s  dh}d }t jdkr&t d }zttt|d}W n	 ty%   Y nw dd }dd }tt	D ]\}}}}	}
}}t
||	}t
|
|}|d urR|d	k rR||v rRq2|d
krY|}n|dkr`|}ntt|}d|v smd|v rz||}W n	 ty|   Y q2w | d||||	 d|v rz||}W n	 ty   Y q2w | 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 q2d S )NZtan0064darwinr   .c                 S   s   	 t | j| jS r:   )r   r   rF   rG   r   r   r   r   rect_complex\  s   z5CMathTests.test_specific_values.<locals>.rect_complexc                 S   s   	 t t|  S r:   )r   r   r   r   r   r   polar_complexa  s   z6CMathTests.test_specific_values.<locals>.polar_complex)
   r^   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*   rI   rJ   z{}: {}(complex({!r}, {!r}))
Expected: complex({!r}, {!r})
Received: complex({!r}, {!r})
Received value insufficiently close to expected value.)rQ   rD   )rD   )sysplatformZmac_vertuplemapr   split
ValueErrorr   r   r   r   r   rB   rC   rM   rL   rF   rG   rR   )r8   ZSKIP_ON_TIGERZosx_versionZversion_txtr   r   idr   ZarZaiZereiflagsr   expectedfunctionactualZreal_abs_errZerror_messager   r   r   test_specific_valuesN  s   




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 r:   )ziprR   )r   r   ZgotrU   gfuncr8   r   r   check  s   z%CMathTests.check_polar.<locals>.checkr   )r   r   r]   )r>   r   r>                 ?r   y             g      @r
   r      r^      r   )r	   floatr   )r8   r   r   r
   r   r   r   r   check_polar  s.   

 zCMathTests.check_polarc                 C   s   |  t d S r:   )r   r   r7   r   r   r   
test_polar  r=   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 w )N   r   )r   r   r   r   r   polar_with_errno_set  s   z9CMathTests.test_polar_errno.<locals>.polar_with_errno_set)Z	_testcapir   r   )r8   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| qtd S )Nr   r   r>   r   y      Yny      Ynr   r   y             r   r   g      r   r   g      ?)
rT   r   r	   rY   r   INFcomplex_nansrZ   r?   r@   r8   r   r   r   r   
test_phase  sD   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_zerosrY   rL   complex_infinitiesr   r   NANrZ   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   rM   rL   r   r7   r   r   r   test_abs_overflows  s   zCMathTests.test_abs_overflowsc                 C   sF   d}t |j|d  |kst |j|d  |kr!| ||f d S d S )NgHz>r   r]   )rL   rF   rG   rB   )r8   rO   rP   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   r]   )r>   r   )r   r   r   )r   r>   )r   r   )r   r   r	   r7   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 ]}t||}| t|t|o+t| qqd S )Nz-infr   r   r   r   r
   r   )r   r   rY   r   isfiniter?   )r8   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 Nr]   r   r   )assertFalser   r@   r   rZ   r   r   r7   r   r   r   
test_isnan,     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   rK   r   rZ   r   r   r7   r   r   r   
test_isinf7  r   zCMathTests.test_isinfc                 C       t D ]}| t|| qd S r:   )r   rH   r   r/   r   r   r   r   testTanhSignB     zCMathTests.testTanhSignc                 C   r   r:   )r   rH   r   r$   r   r   r   r   testAtanSignK  r   zCMathTests.testAtanSignc                 C   r   r:   )r   rH   r   r%   r   r   r   r   testAtanhSignP  r   zCMathTests.testAtanhSign)rI   rJ   N) rk   rl   rm   r   appendr9   r<   rE   rH   rR   rV   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   r   4   sJ    

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 sw   Y  |  t | jdddd W d    n1 s5w   Y  |  t | jddddd W d    d S 1 sTw   Y  d S )Nr   rel_tolabs_tol)r   r   )r   r   iscloser7   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)r8   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)r8   Znear_zero_examplesr   r   r   test_complex_near_zeron  s
   z#IsCloseTests.test_complex_near_zeroN)rk   rl   rm   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   rk   mainr   r   r   r   <module>   sp    
    &)