o
    ۾i'                     @   s   d dl Z d dlZd dlmZmZ d dlm  m  m	Z
 d dlmZmZ d dlmZmZ d dlm  mZ d dlZddiZddiZeG dd	 d	eZed
krUe  dS dS )    N)jitnjit)typeserrors)TestCaseskip_unless_cffiforceobjTnopythonc                   @   s   e Zd ZdZdd Zdd ZefddZdd	 Zd
d Z	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dd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,S )-TestCFFIFc                 C   s   t   t   d S N)modinitinit_oolself r   I/home/ubuntu/.local/lib/python3.10/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   )cffi_supportmap_typer   ffitypeofcffi_sinassertEquallenargsr   double)r   	signaturer   r   r   test_type_map   s   zTestCFFI.test_type_mapc                 C   s:   t tjffi ||}dD ]}| |||| qd S )N)g333333r   g?gQ	@)r   r   r   assertPreciseEqual)r   pyfuncflagscfuncxr   r   r   _test_function"   s   zTestCFFI._test_functionc                 C      |  tj d S r   )r&   r   use_cffi_sinr   r   r   r   test_sin_function(      zTestCFFI.test_sin_functionc                 C   s2   t j}td|}| | d | | d d S )Nr   T)r   use_cffi_boolean_truer   r   r   r"   r$   r   r   r   test_bool_function_ool+   s   zTestCFFI.test_bool_function_oolc                 C      | j tjtd d S N)r#   )r&   r   r(   no_pyobj_flagsr   r   r   r   test_sin_function_npm1      zTestCFFI.test_sin_function_npmc                 C   r'   r   )r&   r   use_cffi_sin_ool)r   r#   r   r   r   test_sin_function_ool4   r*   zTestCFFI.test_sin_function_oolc                 C   r.   r/   )r&   r   r3   r0   r   r   r   r   test_sin_function_npm_ool7   r2   z"TestCFFI.test_sin_function_npm_oolc                 C   r'   r   )r&   r   use_two_funcsr   r   r   r   test_two_funcs:   s   zTestCFFI.test_two_funcsc                 C   r'   r   )r&   r   use_two_funcs_oolr   r   r   r   test_two_funcs_ool>   r*   zTestCFFI.test_two_funcs_oolc                 C   s   t j}tdd|}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t jt jdffD ]\}}}||||}||||}| || qT| t|j	d|j	 d S )NTr	   g      ?g      r   )
r   use_func_pointerr   r   cffi_coscffi_sin_oolcffi_cos_oolr   r   	overloads)r   r"   r$   fafbr%   expectedgotr   r   r   test_function_pointerA   s&   zTestCFFI.test_function_pointerc                 C   s(   t j}tdd|}| | |  d S )NTr:   )r   use_user_defined_symbolsr   r   r,   r   r   r   test_user_defined_symbolsW   s   z"TestCFFI.test_user_defined_symbolsc                 C   s"   ||| t j|t | d S r   )nptestingassert_allclosesin)r   r$   r%   yr   r   r   check_vector_sin\   s   
zTestCFFI.check_vector_sinc                 C   s:   t d|}t |}tdd|}| ||| d S )N
   Tr:   )rG   arangeastype
zeros_liker   rL   )r   r"   dtyper%   rK   r$   r   r   r   _test_from_buffer_numpy_array`   s   
z&TestCFFI._test_from_buffer_numpy_arrayc                 C      |  tjtj d S r   )rR   r   vector_sin_float32rG   float32r   r   r   r   test_from_buffer_float32f   r2   z!TestCFFI.test_from_buffer_float32c                 C   rS   r   )rR   r   vector_sin_float64rG   float64r   r   r   r   test_from_buffer_float64i   r2   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 )NrM         y              ?Tr:   )rG   rN   zerosr   r   vector_extract_realrH   assert_equalrealvector_extract_imagimag)r   nr%   rK   
real_cfunc
imag_cfuncr   r   r   test_from_buffer_structl   s   "


z TestCFFI.test_from_buffer_structc                 C   sL   t j}tdd|}tdtd}tddgt| }| ||| d S )NTr:   frM   r   )r   rT   r   arrayranger   rL   )r   r"   r$   r%   rK   r   r   r   test_from_buffer_pyarrayw   s
   z!TestCFFI.test_from_buffer_pyarrayc                 C   s   t j}tdd|}tdtjd d d }t|}| t	j
}||| W d    n1 s3w   Y  | dt|j d S )NTr:   rM   rZ   z3from_buffer() unsupported on non-contiguous buffers)r   rT   r   rG   rN   rO   rU   rP   assertRaisesr   TypingErrorassertInstr	exception)r   r"   r$   r%   rK   raisesr   r   r   test_from_buffer_error~   s   
zTestCFFI.test_from_buffer_errorc                 C   sR  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    n1 sw   Y  t j
||	 | dt|j d S )Nr   rZ   C)orderrQ   r[      FKTr:   r   zAfrom_buffer() only supports multidimensional arrays with C layout)rG   rg   rU   rP   copyr   rT   r   rL   rJ   rH   rI   rj   r   rk   rl   rm   rn   )r   c1c1_zerosc2c2_zerosf1f1_zerosf2f2_zerosf2_copyr"   r$   sin_c2ro   r   r   r   "test_from_buffer_numpy_multi_array   s0   






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.
        Tr:   c                    s    | S r   r   r%   )my_sinr   r   inner   s   z2TestCFFI.test_indirect_multiple_use.<locals>.innerc                    s    | | d  S )Nr   r   r   r   r   r   r   foo   s   z0TestCFFI.test_indirect_multiple_use.<locals>.foog+?r   N)r   r   r   r   )r   r   r%   r   r   r   test_indirect_multiple_use   s   $z#TestCFFI.test_indirect_multiple_useN)__name__
__module____qualname___numba_parallel_test_r   r   enable_pyobj_flagsr&   r)   r-   r1   r4   r5   r7   r9   rD   rF   rL   rR   rV   rY   re   ri   rp   r   r   r   r   r   r   r
      s.    r
   __main__)rg   numpyrG   numbar   r   numba.core.typing.cffi_utilscoretyping
cffi_utilsr   
numba.corer   r   numba.tests.supportr   r   numba.tests.cffi_usecasestestscffi_usecasesr   unittestr   r0   r
   r   mainr   r   r   r   <module>   s      *