a
    –§»a°  ã                   @   s–   d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
Z
d dlZdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZG dd„ dejƒZedkr’e ¡  dS )é    N)Úcompile_isolated)ÚTypingError)Únjit)Útypesc                 C   s
   t  | ¡S ©N)r   Úint32©Úx© r
   úR/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/tests/test_casting.pyÚfloat_to_int
   s    r   c                 C   s   t  | ¡d S )Né   )r   Úfloat64r   r
   r
   r   Úint_to_float   s    r   c                 C   s
   t  | ¡S r   )r   Úuint32r   r
   r
   r   Úfloat_to_unsigned   s    r   c                 C   s
   t  | ¡S r   )r   Ú
complex128r   r
   r
   r   Úfloat_to_complex   s    r   c                   C   s   t  t  d¡¡ d S )N)é   )Únpr   Úzerosr
   r
   r
   r   Únumpy_scalar_cast_error   s    r   c                   @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚTestCastingc                 C   s~   t }t|tjgƒ}|j}|  |jjtj¡ |  |dƒ|dƒ¡ |  |dƒt	dƒ¡ |  |dƒ|dƒ¡ |  |dƒt	dƒ¡ d S )Nçš™™™™™(@gš™™™™™(À)
r   r   r   Úfloat32Úentry_pointÚassertEqualÚ	signatureÚreturn_typer   Úint©ÚselfZpyfuncZcrZcfuncr
   r
   r   Útest_float_to_int   s    zTestCasting.test_float_to_intc                 C   sR   t }t|tjgƒ}|j}|  |jjtj¡ |  |dƒ|dƒ¡ |  |dƒd¡ d S )NiA  g     d@)	r   r   r   Úint64r   r   r   r   r   r    r
   r
   r   Útest_int_to_float)   s    zTestCasting.test_int_to_floatc              
   C   sf   t }t|tjgƒ}|j}|  |jjtj¡ |  |dƒ|dƒ¡ |  |dƒt	 
dt	 dd¡¡d ¡ d S )Ng®Gáz®	@ÚIÚié   r   )r   r   r   r   r   r   r   r   r   ÚstructÚunpackÚpackr    r
   r
   r   Útest_float_to_unsigned2   s    ÿÿz"TestCasting.test_float_to_unsignedc                 C   sR   t }t|tjgƒ}|j}|  |jjtj¡ |  |dƒ|dƒ¡ |  |dƒd¡ d S )Ng®Gáz®	Ày®Gáz®	À        )	r   r   r   r   r   r   r   r   r   r    r
   r
   r   Útest_float_to_complex<   s    z!TestCasting.test_float_to_complexc                    sh   t dƒdd„ ƒ‰ ˆ  ¡  t dƒ‡ fdd„ƒ}tjdgtjd�}|  ||ƒ|d	 ¡ |  tˆ jƒd
¡ dS )z<Make sure this compiles.

        Cast C to A array
        z	f8(f8[:])c                 S   s   | d S )Nr   r
   r   r
   r
   r   ÚinnerI   s    z.TestCasting.test_array_to_array.<locals>.innerzf8(f8[::1])c                    s   ˆ | ƒS r   r
   r   ©r-   r
   r   ÚdriverO   s    z/TestCasting.test_array_to_array.<locals>.driveriÒ  ©Údtyper   é   N)r   Zdisable_compiler   Úarrayr   r   ÚlenZ	overloads)r!   r/   r	   r
   r.   r   Útest_array_to_arrayD   s    
zTestCasting.test_array_to_arrayc                 C   s”   t dd„ ƒ}tjdftjdftjdftjdftjdftjdftjt dd¡ftj	t 	d	¡fd
g	}|D ]*\}}tj
||d�}|  ||ƒ|d ¡ qdd S )Nc                 S   s   | j  | ¡S r   )r1   Útyper   r
   r
   r   r-   X   s    z-TestCasting.test_0darrayT_to_T.<locals>.innerTr   é   y      @       @r'   Úhz
2016-01-01)z<U3ÚABCr0   r
   )r   r   Zbool_r   r   r#   Z	complex64r   Ztimedelta64Z
datetime64r3   r   )r!   r-   ÚinputsÚTÚinpr	   r
   r
   r   Útest_0darrayT_to_TW   s    
÷zTestCasting.test_0darrayT_to_Tc                 C   sJ   |   t¡�}ttdƒ W d  ƒ n1 s*0    Y  |  dt|jƒ¡ dS )zv
        Ensure that a TypingError exception is raised if
        user tries to convert numpy array to scalar
        r
   Nz?Casting array(float64, 1d, C) to int32 directly is unsupported.)ÚassertRaisesr   r   r   ZassertInÚstrÚ	exception)r!   Zraisesr
   r
   r   Útest_array_to_scalarl   s
    (ÿz TestCasting.test_array_to_scalarc                 C   sR   t  t j¡}t  t j¡}||ƒ}t|ƒdd„ ƒ}|  |dƒd¡ |  |dƒ¡ dS )z�
        Test error due mishandling of Optional to Optional casting

        Related issue: https://github.com/numba/numba/issues/1718
        c                 S   s   | S r   r
   )Úar
   r
   r   Úfooƒ   s    z2TestCasting.test_optional_to_optional.<locals>.foor   N)r   ÚOptionalZintpr   r   r   ZassertIsNone)r!   Zopt_intZopt_fltÚsigrC   r
   r
   r   Útest_optional_to_optionalx   s    
z%TestCasting.test_optional_to_optionalN)Ú__name__Ú
__module__Ú__qualname__r"   r$   r+   r,   r5   r=   rA   rF   r
   r
   r
   r   r      s   	
r   Ú__main__)Únumpyr   Znumba.core.compilerr   Znumba.core.errorsr   Znumbar   Z
numba.corer   r(   Zunittestr   r   r   r   r   ZTestCaser   rG   Úmainr
   r
   r
   r   Ú<module>   s   n