B
     \                 @   s   d dl mZmZmZ d dlZd dlZd dlZd dlm	Z
 d dlmZmZmZmZ d dlmZmZ d dlmZmZ d dlm  mZ e Zed e Ze
ejdG d	d
 d
eZedkre
   dS )    )print_functiondivisionabsolute_importN)unittest_support)jitcffi_supporttypeserrors)compile_isolatedFlags)TestCasetagZenable_pyobjectz4CFFI not supported -- please install the cffi modulec               @   s   e Zd ZdZdd Zdd ZefddZdd	 Zd
d Z	e
ddd ZefddZdd Zdd Zdd Zdd Zdd Zdd Zdd Ze
ddd  Zd!d" Zd#d$ Zeejd%k d&d'd( Zd)d* Zd+d, Zd-d. Zd/S )0TestCFFIFc             C   s   t   t   d S )N)modZinitZinit_ool)self r   4lib/python3.7/site-packages/numba/tests/test_cffi.pysetUp   s    zTestCFFI.setUpc             C   s>   t tjtj}| t|jd | |jd t	j
 d S )N   r   )r   Zmap_typer   ZffiZtypeofcffi_sinassertEquallenargsr   double)r   Z	signaturer   r   r   test_type_map!   s    zTestCFFI.test_type_mapc             C   s>   t |tjg|d}|j}x dD ]}| |||| qW d S )N)flags)g333333r   g?gQ	@)r
   r   r   entry_pointZassertPreciseEqual)r   pyfuncr   crescfuncxr   r   r   _test_function&   s    
zTestCFFI._test_functionc             C   s   |  tj d S )N)r"   r   use_cffi_sin)r   r   r   r   test_sin_function-   s    zTestCFFI.test_sin_functionc             C   s:   t j}t|dtd}|j}| | d | | d d S )Nr   )r   T)r   Zuse_cffi_boolean_truer
   no_pyobj_flagsr   r   )r   r   r   r    r   r   r   test_bool_function_ool0   s
    zTestCFFI.test_bool_function_oolZ	importantc             C   s   | j tjtd d S )N)r   )r"   r   r#   r%   )r   r   r   r   test_sin_function_npm7   s    zTestCFFI.test_sin_function_npmc             C   s   |  tj d S )N)r"   r   use_cffi_sin_ool)r   r   r   r   r   test_sin_function_ool;   s    zTestCFFI.test_sin_function_oolc             C   s   | j tjtd d S )N)r   )r"   r   r(   r%   )r   r   r   r   test_sin_function_npm_ool>   s    z"TestCFFI.test_sin_function_npm_oolc             C   s   |  tj d S )N)r"   r   Zuse_two_funcs)r   r   r   r   test_two_funcsA   s    zTestCFFI.test_two_funcsc             C   s   |  tj d S )N)r"   r   Zuse_two_funcs_ool)r   r   r   r   test_two_funcs_oolE   s    zTestCFFI.test_two_funcs_oolc             C   s   t j}tdd|}xt jt jdft jt jdft jt jdft jt jdft jt jdft jt jdft jt jdft jt jdft jt jdft jt jdft jt jdft jt jdfgD ].\}}}||||}||||}| || qW | t|j	d|j	 d S )NT)nopythong      ?g      r   )
r   Zuse_func_pointerr   r   Zcffi_cosZcffi_sin_oolZcffi_cos_oolr   r   Z	overloads)r   r   r    ZfaZfbr!   ZexpectedZgotr   r   r   test_function_pointerH   s&    zTestCFFI.test_function_pointerc             C   s(   t j}tdd|}| | |  d S )NT)r-   )r   Zuse_user_defined_symbolsr   r   )r   r   r    r   r   r   test_user_defined_symbols^   s    z"TestCFFI.test_user_defined_symbolsc             C   s"   ||| t j|t | d S )N)nptestingassert_allclosesin)r   r    r!   yr   r   r   check_vector_sinc   s    
zTestCFFI.check_vector_sinc             C   s:   t d|}t |}tdd|}| ||| d S )N
   T)r-   )r0   arangeastype
zeros_liker   r5   )r   r   dtyper!   r4   r    r   r   r   _test_from_buffer_numpy_arrayg   s    
z&TestCFFI._test_from_buffer_numpy_arrayc             C   s   |  tjtj d S )N)r;   r   vector_sin_float32r0   float32)r   r   r   r   test_from_buffer_float32m   s    z!TestCFFI.test_from_buffer_float32c             C   s   |  tjtj d S )N)r;   r   Zvector_sin_float64r0   Zfloat64)r   r   r   r   test_from_buffer_float64q   s    z!TestCFFI.test_from_buffer_float64c             C   s   d}t |t |d |d d  }t |}tddtj}||| t j|j| tddtj	}||| t j|j
| d S )Nr6         y              ?T)r-   )r0   r7   Zzerosr   r   Zvector_extract_realr1   Zassert_equalrealZvector_extract_imagimag)r   nr!   r4   Z
real_cfuncZ
imag_cfuncr   r   r   test_from_buffer_structt   s    "


z TestCFFI.test_from_buffer_struct)rA   z.buffer protocol on array.array needs Python 3+c             C   sL   t j}tdd|}tdtd}tddgt| }| ||| d S )NT)r-   fr6   r   )r   r<   r   arrayranger   r5   )r   r   r    r!   r4   r   r   r   test_from_buffer_pyarray   s
    z!TestCFFI.test_from_buffer_pyarrayc          	   C   sr   t j}tdd|}tdtjd d d }t|}| t	j
}||| W d Q R X | dt|j d S )NT)r-   r6   r@   z3from_buffer() unsupported on non-contiguous buffers)r   r<   r   r0   r7   r8   r=   r9   assertRaisesr	   TypingErrorassertInstr	exception)r   r   r    r!   r4   raisesr   r   r   test_from_buffer_error   s    
zTestCFFI.test_from_buffer_errorc          	   C   s>  t jddgdt jd}t |}t jddgddggdt jd}t |}t jddgdt jd}t |}t jddgddggdt jd}t |}|d}	tj}
td	d
|
}| ||| ||| t 	|}ddg|d< t j
|| | ||| | tj}||| W d Q R X t j
||	 | dt|j d S )Nr   r@   C)orderr:   rA      FKT)r-   r   zAfrom_buffer() only supports multidimensional arrays with C layout)r0   rG   r=   r9   copyr   r<   r   r5   r3   r1   r2   rJ   r	   rK   rL   rM   rN   )r   Zc1Zc1_zerosZc2Zc2_zerosf1Zf1_zerosf2Zf2_zerosZf2_copyr   r    Zsin_c2rO   r   r   r   "test_from_buffer_numpy_multi_array   s,    






z+TestCFFI.test_from_buffer_numpy_multi_arrayc                s\   t jtddfdd tdd fdd}d}| ||||d   d	S )
zj
        Issue #2263

        Linkage error due to multiple definition of global tracking symbol.
        T)r-   c                s    | S )Nr   )r!   )my_sinr   r   inner   s    z2TestCFFI.test_indirect_multiple_use.<locals>.innerc                s    | | d  S )Nr   r   )r!   )r[   rZ   r   r   foo   s    z0TestCFFI.test_indirect_multiple_use.<locals>.foog+?r   N)r   r   r   r   )r   r\   r!   r   )r[   rZ   r   test_indirect_multiple_use   s
    z#TestCFFI.test_indirect_multiple_useN)__name__
__module____qualname__Z_numba_parallel_test_r   r   enable_pyobj_flagsr"   r$   r&   r   r'   r)   r*   r+   r,   r.   r/   r5   r;   r>   r?   rE   unittestZskipIfsysversion_inforI   rP   rY   r]   r   r   r   r   r      s.   r   __main__)!Z
__future__r   r   r   rG   Znumpyr0   rc   Znumbar   rb   r   r   r   r	   Znumba.compilerr
   r   Znumba.tests.supportr   r   Znumba.tests.cffi_usecasesZtestsZcffi_usecasesr   ra   setr%   Z
skipUnlessZ	SUPPORTEDr   r^   mainr   r   r   r   <module>   s"   
 0