o
    ¦¸¢iI  ã                   @   sŽ   d dl 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rEe	 ¡  dS dS )é    N)ÚTypingError)Únjit)Útypesc                 C   ó
   t  | ¡S ©N)r   Úint32©Úx© r
   úV/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/numba/tests/test_casting.pyÚfloat_to_int	   ó   
r   c                 C   s   t  | ¡d S )Né   )r   Úfloat64r   r
   r
   r   Úint_to_float   s   r   c                 C   r   r   )r   Úuint32r   r
   r
   r   Úfloat_to_unsigned   r   r   c                 C   r   r   )r   Ú
complex128r   r
   r
   r   Úfloat_to_complex   r   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fƒ|ƒ}|  |jd jtj¡ |  |dƒ|dƒ¡ |  |dƒtdƒ¡ |  |dƒ|dƒ¡ |  |dƒtdƒ¡ d S )Nr   çš™™™™™(@gš™™™™™(À)	r   r   r   Úfloat32ÚassertEqualÚnopython_signaturesÚreturn_typer   Úint©ÚselfÚpyfuncÚcfuncr
   r
   r   Útest_float_to_int   s   zTestCasting.test_float_to_intc                 C   óR   t }ttjfƒ|ƒ}|  |jd jtj¡ |  |dƒ|dƒ¡ |  |dƒd¡ d S )Nr   iA  g     d@)r   r   r   Úint64r   r   r   r   r    r
   r
   r   Útest_int_to_float'   s   ÿzTestCasting.test_int_to_floatc              
   C   sf   t }ttjfƒ|ƒ}|  |jd jtj¡ |  |dƒ|dƒ¡ |  |dƒt 	dt 
dd¡¡d ¡ d S )Nr   g®Gáz®	@ÚIÚié   )r   r   r   r   r   r   r   r   ÚstructÚunpackÚpackr    r
   r
   r   Útest_float_to_unsigned0   s   ÿ
ÿz"TestCasting.test_float_to_unsignedc                 C   r%   )Nr   g®Gáz®	Ày®Gáz®	À        )r   r   r   r   r   r   r   r   r    r
   r
   r   Útest_float_to_complex9   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   ÚinnerF   ó   z.TestCasting.test_array_to_array.<locals>.innerzf8(f8[::1])c                    s   ˆ | ƒS r   r
   r   ©r0   r
   r   ÚdriverL   r1   z/TestCasting.test_array_to_array.<locals>.driveriÒ  ©Údtyper   é   N)r   Údisable_compiler   Úarrayr   r   ÚlenÚ	overloads)r!   r3   r	   r
   r2   r   Útest_array_to_arrayA   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 ¡ q2d S )Nc                 S   s   | j  | ¡S r   )r5   Útyper   r
   r
   r   r0   U   s   z-TestCasting.test_0darrayT_to_T.<locals>.innerTr   é   y      @       @r*   Úhz
2016-01-01)z<U3ÚABCr4   r
   )r   r   Úbool_r   r   r&   Ú	complex64r   Útimedelta64Ú
datetime64r8   r   )r!   r0   ÚinputsÚTÚinpr	   r
   r
   r   Útest_0darrayT_to_TT   s    
÷þzTestCasting.test_0darrayT_to_Tc                 C   sL   |   t¡}tdƒtƒ W d  ƒ n1 sw   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   ÚassertInÚstrÚ	exception)r!   Úraisesr
   r
   r   Útest_array_to_scalari   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   ÚOptionalÚintpr   r   r   ÚassertIsNone)r!   Úopt_intÚopt_fltÚsigrO   r
   r
   r   Útest_optional_to_optionalu   s   
z%TestCasting.test_optional_to_optionalN)Ú__name__Ú
__module__Ú__qualname__r$   r'   r.   r/   r;   rG   rM   rV   r
   r
   r
   r   r      s    
		r   Ú__main__)Únumpyr   Únumba.core.errorsr   Únumbar   Ú
numba.corer   r+   Úunittestr   r   r   r   r   ÚTestCaser   rW   Úmainr
   r
   r
   r   Ú<module>   s    lÿ