U
    9%e                     @   s   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Zd dl	m
Z
mZmZmZ d dlmZ d dlmZmZ ee
jjZdd ZG dd deZG d	d
 d
eZedkre  dS )    N)typestypingcgutilscpu)global_compiler_lock)TestCaserun_in_subprocessc                 C   s   t t| S N)irConstantmachine_int)n r   W/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/test_cgutils.pymachine_const   s    r   c                   @   s`   e Zd Zdd Zejdd Zdd Zdd Zejdd
dZ	ejdd Z
dd Zdd ZdS )StructureTestCasec                 C   s   t  }t|| _d S r	   )r   Contextr   Z
CPUContextcontext)selfZtyping_contextr   r   r   setUp   s    zStructureTestCase.setUpc                 #   s   t ttg| }tjtjftjf|   jdt	|
 jsTtd}t |}dg}t fdd}j|j|fV  d S )N entryTc                     sB   j  }|d  }| |j} |}||  S )Nztest_module.%s)r   codegenZcreate_libraryidZadd_ir_moduleZget_pointer_to_functionname)argsr   ZlibraryZcptrZcfuncZctypes_fntyfunctionmoduler   r   r   	call_func)   s    

z5StructureTestCase.compile_function.<locals>.call_func)r
   FunctionTyper   ctypesZ	CFUNCTYPEZc_size_tr   create_moduler   Zget_or_insert_functionr   Zis_declarationAssertionErrorZappend_basic_blockZ	IRBuilderr   r   )r   nargsZ	llvm_fntyZentry_blockbuilderfirstr   r   r   r   compile_function   s    




z"StructureTestCase.compile_functionc                 C   s0   t |tsttjj}tjg|_tj|_	||S r	   )

isinstance	bytearrayr#   r!   Z	pythonapiZPyByteArray_AsStringZ	py_objectZargtypesZc_void_prestype)r   baZba_as_stringr   r   r   get_bytearray_addr5   s
    
z$StructureTestCase.get_bytearray_addrc              	   C   sd   |  d,\}}}}||d |d }|| W 5 Q R X | |ddd | |ddd d S )	N   r                  )r'   addretassertEqual)r   r   r%   r   callresr   r   r   test_compile_function<   s
    z'StructureTestCase.test_compile_functionr   c           
   	   c   s   |  dn\}}}}|||}||d t|}	||	t|j}	||||	d}||||fV  |tt	d W 5 Q R X || 
| d S )Nr.   r   )ref)r'   r4   r   Zinttoptrr
   ZPointerType_typer5   r   r   r,   )
r   struct_classbufoffsetr   r%   r   r7   instZsptrr   r   r   run_struct_accessD   s    
z#StructureTestCase.run_struct_accessc              	   c   sz   t dd }|d d  }d}| |||\}}}	}
|||
fV  W 5 Q R X | || tj|||f|  | || d S )N   !(      )r)   r@   ZassertNotEqualstruct	pack_intor6   )r   r<   Z
struct_fmtZstruct_argsr=   expectedr>   r   r%   r   r?   r   r   r   run_simple_struct_testQ   s    
z(StructureTestCase.run_simple_struct_testc              	   C   sb   G dd dt j}d}| ||d4\}}}ttdd|_ttdd|_W 5 Q R X d S )	Nc                   @   s    e Zd ZdejfdejfgZdS )z,StructureTestCase.test_int_fields.<locals>.SabN)__name__
__module____qualname__r   Zint32Zuint16_fieldsr   r   r   r   Sb   s   rN   z=iH)xV4ͫ      rO      rP   )r   	StructurerG   r
   r   IntTyperH   rI   r   rN   fmtr   r%   r?   r   r   r   test_int_fieldsa   s    z!StructureTestCase.test_int_fieldsc              	   C   s^   G dd dt j}d}| ||d0\}}}tt d|_tt d|_W 5 Q R X d S )Nc                   @   s    e Zd ZdejfdejfgZdS )z.StructureTestCase.test_float_fields.<locals>.SrH   rI   N)rJ   rK   rL   r   Zfloat64Zfloat32rM   r   r   r   r   rN   m   s   rN   z=df)Gz?=
ףp=@rX   rY   )	r   rS   rG   r
   r   Z
DoubleTyperH   Z	FloatTyperI   rU   r   r   r   test_float_fieldsl   s    z#StructureTestCase.test_float_fieldsN)r   )rJ   rK   rL   r   
contextlibcontextmanagerr'   r,   r9   r@   rG   rW   rZ   r   r   r   r   r      s   

r   c                   @   s   e Zd ZdZdd ZdS )TestCGContextz/Tests for code generation context functionalityc                 C   s4   d}d| d}t |\}}| t||  d S )Ni@ az  if 1:
        from numba import njit, types
        from numba.extending import intrinsic

        @intrinsic
        def printf(tyctx, int_arg):
            sig = types.void(int_arg)
            def codegen(cgctx, builder, sig, llargs):
                cgctx.printf(builder, "%d\n", *llargs)
            return sig, codegen

        @njit
        def foo():
            printf(z)

        foo()
        )r   ZassertInstrdecode)r   valuecodeout_r   r   r   test_printf{   s    zTestCGContext.test_printfN)rJ   rK   rL   __doc__rd   r   r   r   r   r]   x   s   r]   __main__)r[   r!   rD   sysZllvmlite.irr
   numpynpZunittestZ
numba.corer   r   r   r   Znumba.core.compiler_lockr   Znumba.tests.supportr   r   rT   ZintpZbitwidthr   r   r   r]   rJ   mainr   r   r   r   <module>   s   c