o
    8ήct3                     @   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 ddlZddlmZ eejd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j0e/ej0ej1ej1Z2e.e/ej3e/ej3ej1ej1Z4e.ej5ej5ej1ej1Z6G dd deZ7G dd deZ8eG dd deZ9e:dkre;  dS dS ) z
Tests for @cfunc and friends.
    N)
namedtuple)cfunccarrayfarraynjit)typestypingutils)TestCasetagcaptured_stderr)numpy_supportz4CFFI not supported -- please install the cffi modulec                 C   s   | | S N abr   r   =/tmp/pip-target-vg8gfxp4/lib/python/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	in_ptrout_ptrmnin_outsijfuncr   r   cfarray_usecase5   s   

z-make_cfarray_usecase.<locals>.cfarray_usecaser   r7   r8   r   r6   r   make_cfarray_usecase3   s   r:   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,   r6   r   r   r8   M   s   
z3make_cfarray_dtype_usecase.<locals>.cfarray_usecaser   r9   r   r6   r   make_cfarray_dtype_usecaseJ   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__assertIs__wrapped__native_nameassertIsInstancestrassertInaddressintctypescastc_void_pvalueassertPreciseEqual)selffsymboladdrctr   r   r   
test_basico   s   zTestCFunc.test_basicc                 C   s@   ddl m} | \}}ttt}||j}| |d d S )Nr   )cffi_usecasesg      @)	numba.testsrX   load_inline_moduler   
square_sigr   _numba_test_funcptrcffirQ   )rR   rX   ffilibrS   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   int64r   rQ   rM   )rR   rS   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   rc   r   r   rQ   rM   rB   getvaluerJ   )rR   rS   errr`   r   r   r   test_errors   s   zTestCFunc.test_errorsc                 C   s2   t tt}| }| |j| | d| d S )Nzfadd double)r   rA   r   inspect_llvmrJ   rG   )rR   rS   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)forceobjNzUntyped global name 'object')
assertRaisesNotImplementedErrorr   rA   r   assertTypingErrorr   rJ   rI   	exception)rR   raisesr   r   r   test_object_mode   s   
zTestCFunc.test_object_modeN)
rC   
__module__rD   rW   skip_cffi_unsupportedra   re   ri   rl   rs   r   r   r   r   r?   m   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   r;   )r*   arangereshapeastyper=   emptyr#   r%   )rR   pointer_factoryr7   r   r2   r   r   r   run_carray_usecase   s   zTestCArray.run_carray_usecasec                 C   s(   |  ||}|  ||}| || d S r   )r   rQ   )rR   r~   pyfuncr   expectedgotr   r   r   check_carray_usecase   s   zTestCArray.check_carray_usecasec                 C   s   |j t jS r   )rM   data_asrO   rR   arrr   r   r   make_voidptr   s   zTestCArray.make_voidptrc                 C      |j t t jS r   )rM   r   POINTERc_floatr   r   r   r   make_float32_pointer      zTestCArray.make_float32_pointerc                 C   r   r   )rM   r   r   c_doubler   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   )rQ   rB   rM   data)r   r   rR   r   r   eq   s   z*TestCArray.check_carray_farray.<locals>.eqr!   ry   )orderKz%mismatching dtype 'int32' for pointer)r*   rz   r{   r|   r=   copyr   r%   sizeravelr<   r   int32viewrn   	TypeErrorrM   r   rJ   rI   rq   )rR   r7   r   r   baser   rr   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   actual_sizer   r   	<genexpr>
  s    z.TestCArray.make_carray_sigs.<locals>.<genexpr>N)
r   r   r   intcuintpuint32uintctupleargsreturn_type)rR   
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   r   r   rM   rp   carray_float64_usecase_sigrJ   rI   rq   carray_voidptr_usecase_sig)rR   usecasedtype_usecaser   sigrS   rr   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)rC   rt   rD   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv      s    (rv   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)r]   r   cdef)rR   srcr   r^   r   r   r   get_ffiD  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use_record_dtype   i1f2d3af4)	   )r<   r%   myfuncr   r   )r   typeofcffi_supportmap_typerH   r   RecordrB   lenr   r=   float64NestedArrayr   	Signaturer   CPointerr   r   )rR   r^   r   nbtyper   r   r   r   r   test_type_parsingK  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   tmpr4   elemr   r   r   calch  s   z0TestCffiStruct.test_cfunc_callback.<locals>.calcc                    s   t | |} |S r   )r   )ptrr0   r   r   r   r   fooq  s   
z/TestCffiStruct.test_cfunc_callback.<locals>.foozbig_struct[3]zbig_struct*r   {      r   r   rw   size_t)bufferr<   r%   )r   r   r   r   r   r   newrN   r   r   r   r   r   rL   rM   r*   ndarrayr   r   as_dtyperB   )rR   r^   r   nb_big_structr   r   mydatar   r4   r5   rU   r   arrayexpectr   r   r   test_cfunc_callbackb  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   rn   
ValueErrorr   r   r   rB   rI   rq   )rR   r^   rr   r   r   r   test_unsupport_bitsize  s   z%TestCffiStruct.test_unsupport_bitsizeN)rC   rt   rD   c_sourcer   r   r   r   r   r   r   r   r   3  s    +r   __main__)<r   rM   os
subprocesssyscollectionsr   numpyr*   numbar   r   r   r   
numba.corer   r   r	   numba.core.typing.cffi_utilscore
cffi_utilsr   numba.tests.supportr
   r   r   unittestnumba.npr   
skipUnless	SUPPORTEDru   r   r   r   rA   rc   r[   r   r#   r:   r   r   r>   r   r   voidr   r=   r   r   r   r   voidptrr   r?   rv   r   rC   mainr   r   r   r   <module>   sb    

I}g