o
    1&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   CC:\wamp64\www\opt\env\Lib\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   ZlibraryZcptrZcfuncZctypes_fntyfunctionmoduler   r   r   	call_func)   s   

z5StructureTestCase.compile_function.<locals>.call_func)r
   FunctionTyper   ctypes	CFUNCTYPE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!   	pythonapiZPyByteArray_AsString	py_objectargtypes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 )Nr3   r   )ref)r(   r9   r   Zinttoptrr
   ZPointerType_typer:   r   r   r1   )
r   struct_classbufoffsetr   r&   r   r<   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*   rE   ZassertNotEqualstruct	pack_intor;   )r   rA   Z
struct_fmtZstruct_argsrB   expectedrC   r   r&   r   rD   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       rT   z=iH)xV4ͫ      rV      rW   )r   	StructurerL   r
   r   IntTyperN   rO   r   rT   fmtr   r&   rD   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                   @   rM   )z.StructureTestCase.test_float_fields.<locals>.SrN   rO   N)rP   rQ   rR   r   Zfloat64Zfloat32rS   r   r   r   r   rT   m   rU   rT   z=df)Gz?=
ףp=@r_   r`   )	r   rZ   rL   r
   r   Z
DoubleTyperN   Z	FloatTyperO   r\   r   r   r   test_float_fieldsl   s   "z#StructureTestCase.test_float_fieldsN)r   )rP   rQ   rR   r   
contextlibcontextmanagerr(   r1   r>   rE   rL   r^   ra   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)rP   rQ   rR   __doc__rk   r   r   r   r   rd   x   s    rd   __main__)rb   r!   rI   sysZllvmlite.irr
   numpynpZunittestZ
numba.corer   r   r   r   Znumba.core.compiler_lockr   Znumba.tests.supportr   r   r[   ZintpZbitwidthr   r   r   rd   rP   mainr   r   r   r   <module>   s"    c