o
    i3                     @   s  d Z ddlZddlZddlZddlZddlmZ ddlZddl	m
Z
mZmZmZ ddlmZmZmZ ddlm  m  mZ ddlmZmZmZmZ ddlZddlmZ dd	 Zd
d Zdd Z dZ!dZ"dZ#dd Z$dZ%dd Z&e&eZ'e&eZ(dd Z)e)eZ*e)eZ+e,e-ej.e-ej.ej/ej/Z0e,e-ej1e-ej1ej/ej/Z2e,ej3ej3ej/ej/Z4G dd deZ5G dd deZ6eG dd deZ7e8dkre9  dS dS )z
Tests for @cfunc and friends.
    N)
namedtuple)cfunccarrayfarraynjit)typestypingutils)TestCaseskip_unless_cffitagcaptured_stderr)numpy_supportc                 C   s   | | S N abr   r   e/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/tests/test_cfunc.pyadd_usecase      r   c                 C   s   | | }|S r   r   )r   r   cr   r   r   div_usecase   s   r   c                 C   s   | d S )N   r   )r   r   r   r   square_usecase   r   r   zfloat64(float64, float64)zfloat64(int64, int64)zfloat64(float64)c                 C   s   t   | | S r   )objectr   r   r   r   objmode_usecase'   s   r      c                        fdd}|S )Nc           	         s    | ||f} |t }|j|d< |j|dd< |j|dd< |jj|d< |jj|d< d}t||D ]\}}||||f ||  7 }q3||d< d S )Nr                  )	CARRAY_USECASE_OUT_LENndimshapestridesflagsc_contiguousf_contiguousnpndindex	Zin_ptrZout_ptrmnZin_outsijfuncr   r   cfarray_usecase1   s   

z-make_cfarray_usecase.<locals>.cfarray_usecaser   r5   r6   r   r4   r   make_cfarray_usecase/   s   r8   c                    r   )Nc           	         s    | ||ft jd} |tt j}|j|d< |j|dd< |j|dd< |jj|d< |jj|d< d}t 	||D ]\}}||||f ||  7 }q8||d< d S )Ndtyper   r   r    r!   r"   r#   )
r+   float32r$   r%   r&   r'   r(   r)   r*   r,   r-   r4   r   r   r6   I   s   
z3make_cfarray_dtype_usecase.<locals>.cfarray_usecaser   r7   r   r4   r   make_cfarray_dtype_usecaseF   s   r<   c                   @   s@   e Zd Zdd Zedd Zdd Zdd Zd	d
 Zdd Z	dS )	TestCFuncc                 C   s   t tt}| |jd | |jd | |jt |j}| 	|t
 | d| |j}| 	|t |j}| t|tjj| | |ddd dS )z8
        Basic usage and properties of a cfunc.
        r          @g      @g      @N)r   add_sigr   assertEqual__name____qualname__ZassertIs__wrapped__native_nameassertIsInstancestrassertInaddressintctypescastc_void_pvalueassertPreciseEqual)selffsymboladdrctr   r   r   
test_basick   s   zTestCFunc.test_basicc                 C   s@   ddl m} | \}}ttt}||j}| |d d S )Nr   )cffi_usecasesg      @)	Znumba.testsrU   Zload_inline_moduler   
square_sigr   Z_numba_test_funcptrcffirN   )rO   rU   ffilibrP   resr   r   r   	test_cffi   s
   zTestCFunc.test_cffic                 C   s.   t tdtjidt}| |ddd d S )Nr   )localsr   r    r>   )r   div_sigr   Zint64r   rN   rJ   )rO   rP   r   r   r   test_locals   s   zTestCFunc.test_localsc                 C   s   t tt}t }| |ddd W d    n1 sw   Y  | | d t }|dd}| |d W d    n1 sEw   Y  | }| d| | d| d S )	Nr!   r   g      @ r   g        zZeroDivisionError:zException ignored)	r   r]   r   r   rN   rJ   r@   getvaluerG   )rO   rP   errrZ   r   r   r   test_errors   s   zTestCFunc.test_errorsc                 C   s2   t tt}| }| |j| | d| d S )Nzfadd double)r   r?   r   Zinspect_llvmrG   rD   )rO   rP   Zirr   r   r   test_llvm_ir   s   zTestCFunc.test_llvm_irc                 C   s   |  t ttddt W d   n1 sw   Y  |  }ttt W d   n1 s2w   Y  | dt|j	 dS )z7
        Object mode is currently unsupported.
        T)ZforceobjNzUntyped global name 'object')
assertRaisesNotImplementedErrorr   r?   r   assertTypingErrorr   rG   rF   	exception)rO   raisesr   r   r   test_object_mode   s   
zTestCFunc.test_object_modeN)
rA   
__module__rB   rT   r   r[   r^   rb   rc   ri   r   r   r   r   r=   i   s    
	r=   c                   @   sp   e Zd 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d Zdd Zdd Zdd ZdS )
TestCArrayz*
    Tests for carray() and farray().
    c                 C   sJ   t dddt j}t jtt jd}|||||g|jR   |S )N
      r   r    r9   )r+   arangereshapeastyper;   emptyr$   r&   )rO   pointer_factoryr5   r   r0   r   r   r   run_carray_usecase   s   zTestCArray.run_carray_usecasec                 C   s(   |  ||}|  ||}| || d S r   )rt   rN   )rO   rs   pyfuncr   expectedgotr   r   r   check_carray_usecase   s   zTestCArray.check_carray_usecasec                 C   s   |j t jS r   )rJ   data_asrL   rO   Zarrr   r   r   make_voidptr   s   zTestCArray.make_voidptrc                 C      |j t t jS r   )rJ   ry   POINTERZc_floatrz   r   r   r   make_float32_pointer      zTestCArray.make_float32_pointerc                 C   r|   r   )rJ   ry   r}   Zc_doublerz   r   r   r   make_float64_pointer   r   zTestCArray.make_float64_pointerc                    s   fdd}t ddt jj|d}| ||j}||| | ||j}|||	d | ||j|j
}||| | ||jt j}||| | ||j|j
}||| | ||jt j}|||t j  t | ||j W d    n1 sw   Y   t ||jj|j W d    n1 sw   Y   t}| ||jt j W d    n1 sw   Y   dt|j d S )Nc                    s$     | |  | jj|jj d S r   )rN   r@   rJ   data)rw   rv   rO   r   r   eq   s   z*TestCArray.check_carray_farray.<locals>.eqr"   rn   )orderKz%mismatching dtype 'int32' for pointer)r+   ro   rp   rq   r;   copyr~   r&   sizeZravelr:   r{   int32viewrd   	TypeErrorrJ   r   rG   rF   rg   )rO   r5   r   r   baser   rh   r   r   r   check_carray_farray   s4    



zTestCArray.check_carray_farrayc                 C      |  td dS )z,
        Test pure Python carray().
        CN)r   r   r   r   r   r   test_carray      zTestCArray.test_carrayc                 C   r   )z,
        Test pure Python farray().
        FN)r   r   r   r   r   r   test_farray   r   zTestCArray.test_farrayc                 #   sL    t jt jt jt jt jt jfD ] t fdd|jD }|j	| V  qdS )z
        Generate a bunch of concrete signatures by varying the width
        and signedness of size arguments (see issue #1923).
        c                 3   s"    | ]}|t jkr n|V  qd S r   )r   intp).0r   Zactual_sizer   r   	<genexpr>  s    z.TestCArray.make_carray_sigs.<locals>.<genexpr>N)
r   r   r   ZintcuintpZuint32Zuintctupleargsreturn_type)rO   Z
formal_sigr   r   r   r   make_carray_sigs   s   zTestCArray.make_carray_sigsc                 C   s   |}|  tD ]}t||}| | j||j q|}|  tD ]}t||}| | j||j q |  }tt|}W d    n1 sGw   Y  | dt	|j
 |}|  tD ]}t||}| | j||j q\d S )Nz7mismatching dtype 'float32' for pointer type 'float64*')r   carray_float32_usecase_sigr   rx   r~   rJ   rf   carray_float64_usecase_sigrG   rF   rg   carray_voidptr_usecase_sig)rO   ZusecaseZdtype_usecaseru   sigrP   rh   r   r   r   check_numba_carray_farray
  s&   
z$TestCArray.check_numba_carray_farrayc                 C      |  tt dS )zK
        Test Numba-compiled carray() against pure Python carray()
        N)r   carray_usecasecarray_dtype_usecaser   r   r   r   test_numba_carray"  r   zTestCArray.test_numba_carrayc                 C   r   )zK
        Test Numba-compiled farray() against pure Python farray()
        N)r   farray_usecasefarray_dtype_usecaser   r   r   r   test_numba_farray(  r   zTestCArray.test_numba_farrayN)rA   rj   rB   __doc__rt   rx   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   rk      s    (rk   c                   @   s4   e Zd ZdZefddZdd Zdd Zdd	 Zd
S )TestCffiStructz
typedef struct _big_struct {
    int    i1;
    float  f2;
    double d3;
    float  af4[9];
} big_struct;

typedef struct _error {
    int bits:4;
} error;

typedef double (*myfunc)(big_struct*, size_t);
c                 C   s    ddl m} | }|| |S )Nr   )FFI)rW   r   Zcdef)rO   srcr   rX   r   r   r   get_ffi@  s   
zTestCffiStruct.get_ffic                 C   s   |   }|d}tj|dd}| |tj | t|d | |dtj	 | |dtj
 | |dtj | |dtjtj
d	d
 |d}tj|dd}| |tj | |jd t| | |jd tj | |jtj d S )N
big_structTZuse_record_dtype   i1f2d3af4)	   )r:   r&   myfuncr   r   )r   typeofcffi_supportmap_typerE   r   ZRecordr@   lenr   r;   float64ZNestedArrayr   	Signaturer   CPointerr   r   )rO   rX   r   Znbtyper   r   r   r   r   test_type_parsingG  s$   

z TestCffiStruct.test_type_parsingc                    s  |   }|d}tj|dd}tj|ddd}tdd  t| fdd}|d	}|d
|}tdD ]+}|d || _	|d || _
d| d || _tdD ]}	|d |	 || j|	< qVq9t|d|}
||
d}tj||t|dd} |}| || d S )Nr   Tr   r   c                 S   sH   d}t | jD ]}| | }||j|j |j 7 }|| | j 7 }q|S )Nr   )ranger   r   r   r   r   sum)r   tmpr2   elemr   r   r   calcd  s   z0TestCffiStruct.test_cfunc_callback.<locals>.calcc                    s   t | |} |S r   )r   )ptrr/   r   r   r   r   foom  s   
z/TestCffiStruct.test_cfunc_callback.<locals>.foozbig_struct[3]zbig_struct*r    {      r   r   rl   Zsize_t)bufferr:   r&   )r   r   r   r   r   r   newrK   r   r   r   r   r   rI   rJ   r+   Zndarrayr   r   Zas_dtyper@   )rO   rX   r   Znb_big_structr   r   Zmydatar   r2   r3   rR   rw   arrayexpectr   r   r   test_cfunc_callback^  s4   


z"TestCffiStruct.test_cfunc_callbackc                 C   s\   |   }| t}tj|ddd W d    n1 sw   Y  | dt|j d S )NerrorTr   z0field 'bits' has bitshift, this is not supported)	r   rd   
ValueErrorr   r   r   r@   rF   rg   )rO   rX   rh   r   r   r   test_unsupport_bitsize  s   z%TestCffiStruct.test_unsupport_bitsizeN)rA   rj   rB   Zc_sourcer   r   r   r   r   r   r   r   r   /  s    +r   __main__):r   rJ   os
subprocesssyscollectionsr   numpyr+   Znumbar   r   r   r   Z
numba.corer   r   r	   Znumba.core.typing.cffi_utilscoreZ
cffi_utilsr   Znumba.tests.supportr
   r   r   r   ZunittestZnumba.npr   r   r   r   r?   r]   rV   r   r$   r8   r   r   r<   r   r   voidr   r;   r   r   r   r   Zvoidptrr   r=   rk   r   rA   mainr   r   r   r   <module>   sZ    

I}g