o
    Eb                     @   s   d Z ddlZddlZddlZddlZddlZg dZG dd deZ	G dd dZ
G dd	 d	Zd
d Zdd Zdd Zdd ZdS )z
Generic test utilities.

    N)PytestTestercheck_free_memory_TestPythranFuncc                   @   s   e Zd ZdZdS )FPUModeChangeWarningzWarning about FPU mode changeN)__name__
__module____qualname____doc__ r
   r
   7/usr/lib/python3/dist-packages/scipy/_lib/_testutils.pyr      s    r   c                   @   s&   e Zd ZdZdd Z		d
dd	ZdS )r   z)
    Pytest test runner entry point.
    c                 C   s
   || _ d S N)module_name)selfr   r
   r
   r   __init__   s   
zPytestTester.__init__fast   NFc              
   C   sR  dd l }tj| j }	tj|	jd }
ddg}|rtd|r%|t	|7 }|r:t
|dkr:|ddt
|d   g7 }|rC|d|
 g7 }|d	krN|d
dg7 }n
|dkrX|d
|g7 }|d u r`| jg}|d ur}|dkr}t rt|dt|g7 }n	dd l}|d |dgt	| 7 }z
||}W |dkS  ty } z|j}W Y d }~|dkS d }~ww )Nr   z--showlocalsz
--tb=shortzDoctests not supportedr   -vz--cov=r   z-mznot slowZfullz-nzMCould not run tests in parallel because pytest-xdist plugin is not available.z--pyargs)pytestsysmodulesr   ospathabspath__path__
ValueErrorlistint_pytest_has_xdiststrwarningswarnmain
SystemExitcode)r   ZlabelverboseZ
extra_argvZdoctestsZcoverageZtestsZparallelr   moduleZmodule_pathZpytest_argsr    r$   excr
   r
   r   __call__   s@   
zPytestTester.__call__)r   r   NFFNN)r   r   r   r	   r   r(   r
   r
   r
   r   r      s    r   c                   @   s|   e Zd ZdZejejejejej	ej
gZejejgZejejgZdd Zdd Zdd Zdd	 Zd
d Zdd Zdd ZdS )r   aL  
    These are situations that can be tested in our pythran tests:
    - A function with multiple array arguments and then
      other positional and keyword arguments.
    - A function with array-like keywords (e.g. `def somefunc(x0, x1=None)`.
    Note: list/tuple input is not yet tested!

    `self.arguments`: A dictionary which key is the index of the argument,
                      value is tuple(array value, all supported dtypes)
    `self.partialfunc`: A function used to freeze some non-array argument
                        that of no interests in the original function
    c                 C   s   i | _ d | _d | _d S r   )	argumentsZpartialfuncZexpected)r   r
   r
   r   setup_method]   s   
z_TestPythranFunc.setup_methodc                 C   s>   t |}i }|j D ]\}}|jt jjur|j||< q|S r   )inspect	signatureZ
parametersitemsdefaultZ	Parameterempty)r   funcr,   Zoptional_argskr   r
   r
   r   get_optional_argsb   s   

z"_TestPythranFunc.get_optional_argsc                 C   s2   d}| j D ]}t| j | d }||kr|}q|S )Nr   r   )r)   len)r   Zmax_lenarg_idxZcur_lenr
   r
   r   get_max_dtype_list_lengthl   s   
z*_TestPythranFunc.get_max_dtype_list_lengthc                 C   s    |t |d kr|d S || S )Nr   )r3   )r   Z
dtype_listZ	dtype_idxr
   r
   r   	get_dtypeu   s   z_TestPythranFunc.get_dtypec                 C   s`   t |  D ]'}g }| jD ]}| | j| d |}|| j| d | q| j|  qd S )Nr   r   )ranger5   r)   r7   appendZastypepythranfunc)r   Ztype_idx
args_arrayr4   Z	new_dtyper
   r
   r   test_all_dtypes}   s   
z _TestPythranFunc.test_all_dtypesc                 C   sF   g }| j D ]}|| j | d d d d d d d  q| j|  d S )Nr   r6   )r)   r9   r:   r   r;   r4   r
   r
   r   
test_views   s   
*z_TestPythranFunc.test_viewsc                 C   sH   g }| j D ]}|tj| j | d dddd d d  q| j|  d S )Nr      )Zaxis)r)   r9   nprepeatr:   r=   r
   r
   r   test_strided   s   
z_TestPythranFunc.test_stridedN)r   r   r   r	   r@   Zint8Zint16Zint32Zint64ZintcZintpZALL_INTEGERZfloat32Zfloat64Z	ALL_FLOATZ	complex64Z
complex128ZALL_COMPLEXr*   r2   r5   r7   r<   r>   rB   r
   r
   r
   r   r   L   s    
		r   c                  C   s   ddl m}  | dduS )zQ
    Check if the pytest-xdist plugin is installed, providing parallel tests
    r   	find_specZxdistN)importlib.utilrD   rC   r
   r
   r   r      s   r   c                 C   s   ddl }zttjd }d| tjd }W n ty3   t }|du r)|d d| |d }Y nw || d k rA|| dS dS )zJ
    Check *free_mb* of memory is available, otherwise do pytest.skip
    r   NZSCIPY_AVAILABLE_MEMz?{0} MB memory required, but environment SCIPY_AVAILABLE_MEM={1}zlCould not determine available memory; set SCIPY_AVAILABLE_MEM variable to free memory in MB to run the test.z,{0} MB memory required, but {1} MB available    .A)r   _parse_sizer   environformatKeyError_get_mem_availableskip)Zfree_mbr   Zmem_freemsgr
   r
   r   r      s"   

r   c                 C   sx   dddddddddddddd	d
}t dd| | t j}|r*|d|vr.tdt|d||d  S )NrF   g      ?     @@g    eAg   mBg      @g      0Ag      Ag      pB) br1   MGTZkbZMbZGbZTbZkibZMibZGibZTibz^\s*(\d+)\s*({0})\s*$|r?   zInvalid size stringr   )	rematchrI   joinkeysIgroupr   float)Zsize_strsuffixesmr
   r
   r   rG      s   rG   c               	   C   s   z
ddl } |  jW S  ttfy   Y nw tjdr]i }tdd"}|D ]}|	 }t
|d d ||d d < q&W d   n1 sHw   Y  d	|v rU|d	 S |d
 |d  S dS )zD
    Get information about memory available, not counting swap.
    r   NZlinuxz/proc/meminforr   rN   :ZmemavailableZmemfreecached)psutilZvirtual_memoryZ	availableImportErrorAttributeErrorr   platform
startswithopensplitr[   striplower)ra   infoflinepr
   r
   r   rK      s$   $rK   )r	   r   rU   r   Znumpyr@   r+   __all__RuntimeWarningr   r   r   r   r   rG   rK   r
   r
   r
   r   <module>   s    7H	