o
    i                     @   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r]e  dS 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   g/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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gtjf| R   jdt	|
 js,J 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   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_declarationZ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J tjj}tjg|_tj|_||S r	   )	
isinstance	bytearrayr"   Z	pythonapiZPyByteArray_AsStringZ	py_objectargtypesZc_void_prestype)r   baZba_as_stringr   r   r   get_bytearray_addr5   s
   
z$StructureTestCase.get_bytearray_addrc                 C   sx   |  d\}}}}||d |d }|| W d    n1 s#w   Y  | |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<\}}}}|||}||d t|}	||	t|j}	||||	d}||||fV  |tt	d W d    n1 sEw   Y  || 
| d S )Nr/   r   )ref)r'   r5   r   Zinttoptrr
   ZPointerType_typer6   r   r   r-   )
r   struct_classbufoffsetr   r%   r   r8   instZsptrr   r   r   run_struct_accessD   s   
	z#StructureTestCase.run_struct_accessc                 c   s    t dd }|d d  }d}| |||\}}}	}
|||
fV  W d    n1 s+w   Y  | || tj|||g|R   | || d S )N   !(      )r)   rA   ZassertNotEqualstruct	pack_intor7   )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   sx   G dd dt j}d}| ||d \}}}ttdd|_ttdd|_W d    d S 1 s5w   Y  d S )	Nc                   @       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       rP   z=iH)xV4ͫ      rR      rS   )r   	StructurerH   r
   r   IntTyperJ   rK   r   rP   fmtr   r%   r@   r   r   r   test_int_fieldsa   s   "z!StructureTestCase.test_int_fieldsc                 C   st   G dd dt j}d}| ||d\}}}tt d|_tt d|_W d    d S 1 s3w   Y  d S )Nc                   @   rI   )z.StructureTestCase.test_float_fields.<locals>.SrJ   rK   N)rL   rM   rN   r   Zfloat64Zfloat32rO   r   r   r   r   rP   m   rQ   rP   z=df)Gz?=
ףp=@r[   r\   )	r   rV   rH   r
   r   Z
DoubleTyperJ   Z	FloatTyperK   rX   r   r   r   test_float_fieldsl   s   "z#StructureTestCase.test_float_fieldsN)r   )rL   rM   rN   r   
contextlibcontextmanagerr'   r-   r:   rA   rH   rZ   r]   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)rL   rM   rN   __doc__rg   r   r   r   r   r`   x   s    r`   __main__)r^   r"   rE   sysZllvmlite.irr
   numpynpZunittestZ
numba.corer   r   r   r   Znumba.core.compiler_lockr   Znumba.tests.supportr   r   rW   ZintpZbitwidthr   r   r   r`   rL   mainr   r   r   r   <module>   s"    c