U
    b»{j8  ã                   @   sn   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ G dd„ de jƒZedkrje  ¡  dS )	é    N)Úcontextmanager)Úir)ÚtypesÚtypingÚcallconvÚcpuÚcgutils)Ú
cpu_targetc                   @   s4   e Zd ZdZedd„ ƒZdd„ Zdd„ Zdd	„ Zd
S )ÚTestCompileCachezœ
    Tests that the caching in BaseContext.compile_internal() works correctly by
    checking the state of the cache when it is used by the CPUContext.
    c              	   c   s¦   t j}t j}| ¡  d¡}| |¡�x t d¡}t 	t
jt
j¡}|j |j|j¡}t ||d¡}|j |¡}|jsvt‚| d¡}	t |	¡}
||
||fV  W 5 Q R X d S )NZtestingZtest_moduleZtest_fnÚentry)r	   Útyping_contextZtarget_contextZcodegenZcreate_libraryZpush_code_libraryr   ÚModuler   Ú	signaturer   Zint32Ú	call_convÚget_function_typeÚreturn_typeÚargsr   Úget_or_insert_functionÚget_argumentsÚis_declarationÚAssertionErrorÚappend_basic_blockÚ	IRBuilder)Úselfr   ÚcontextÚlibÚmoduleÚsigZ	llvm_fntyÚfunctionr   Zentry_blockÚbuilder© r    ú]/var/www/html/bwave/python/venv/lib/python3.8/site-packages/numba/tests/test_compile_cache.pyÚ_context_builder_sig_args   s"    

ÿÿ


z*TestCompileCache._context_builder_sig_argsc              	   C   sF  dd„ }dd„ }|   ¡ ��"\}}}}t|jƒ}|  |d t|jƒ¡ | ||||¡ |  |d t|jƒ¡ | ||||¡ |  |d t|jƒ¡ | ||||¡ |  |d t|jƒ¡ t tjtj¡}|j	 
|j|j¡}	t |j|	d¡}
|j	 |
¡}|
jsþt‚|
 d	¡}t |¡}| ||||¡ |  |d
 t|jƒ¡ W 5 Q R X d S )Nc                 S   s   d|  S )Né   r    ©Úir    r    r!   Útimes2%   s    z+TestCompileCache.test_cache.<locals>.times2c                 S   s   | d S )Né   r    r$   r    r    r!   Útimes3(   s    z+TestCompileCache.test_cache.<locals>.times3r   é   r#   Z	test_fn_2r   r'   )r"   ÚlenÚcached_internal_funcÚassertEqualÚcompile_internalr   r   r   Úfloat64r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r&   r(   r   r   r   r   Úinitial_cache_sizeZsig2Z
llvm_fnty2Z	function2Zargs2Zentry_block2Zbuilder2r    r    r!   Ú
test_cache$   sN       

ÿ
ÿ
ÿ
ÿ
ÿ ÿ



ÿzTestCompileCache.test_cachec              	   C   sÂ   dd„ }|   ¡ �¨\}}}}|ddƒ}|ddƒ}|ddƒ}t|jƒ}	| ||||¡}
|  |	d t|jƒ¡ | ||||¡}|  |	d t|jƒ¡ | ||||¡}|  |	d t|jƒ¡ W 5 Q R X dS )zP
        Caching must not mix up closures reusing the same code object.
        c                    s   ‡ fdd„}|S )Nc                    s   ˆ |  S ©Nr    )Úz©Úyr    r!   ÚfY   s    z?TestCompileCache.test_closures.<locals>.make_closure.<locals>.fr    )Úxr4   r5   r    r3   r!   Úmake_closureX   s    z4TestCompileCache.test_closures.<locals>.make_closurer)   r#   N)r"   r*   r+   r-   r,   )r   r7   r   r   r   r   Zclo11Zclo12Zclo22r/   Zres1Zres2Zres3r    r    r!   Útest_closuresT   s,    
   




ÿ
ÿ
ÿzTestCompileCache.test_closuresc              	      sþ   dd„ ‰t  tjtj¡‰‡ ‡‡fdd„}|  ¡ �Ä\}‰ }}t d|¡}t d|¡}|j|d�}|j|d�}|j|d�}	t|j	ƒ}
|  
|
d t|j	ƒ¡ ||ƒ |  
|
d	 t|j	ƒ¡ ||ƒ |  
|
d	 t|j	ƒ¡ ||	ƒ |  
|
d
 t|j	ƒ¡ W 5 Q R X dS )zA
        Caching must not mix up different error models.
        c                 S   s   d|  S )Ng      ð?r    )r6   r    r    r!   Úinvt   s    z.TestCompileCache.test_error_model.<locals>.invc                    s   |   ˆ ˆˆ¡S r1   )Zcompile_subroutine)r   ©r   r9   Zinv_sigr    r!   Úcompile_invy   s    z6TestCompileCache.test_error_model.<locals>.compile_invÚpythonÚnumpy)Zerror_modelr   r)   r#   N)r   r   r   r.   r"   r   Zcreate_error_modelZ	subtargetr*   r+   r,   )r   r;   r   r   r   Zpy_error_modelZnp_error_modelZpy_context1Zpy_context2Z
np_contextr/   r    r:   r!   Útest_error_modelp   s:    
   

ÿ
ÿ
ÿ
ÿz!TestCompileCache.test_error_modelN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r0   r8   r>   r    r    r    r!   r
   
   s   
0r
   Ú__main__)ZunittestÚ
contextlibr   Zllvmliter   Z
numba.corer   r   r   r   r   Znumba.core.registryr	   ZTestCaser
   r?   Úmainr    r    r    r!   Ú<module>   s    