U
    Gvf                     @   s   d Z ddlZddlZddlZddlZddlZdddg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
   9/tmp/pip-unpacked-wheel-96ln3f52/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   sN  dd l }tj| j }	tj|	jd }
ddg}|r:td|rJ|t	|7 }|rtt
|dkrt|ddt
|d   g7 }|r|d|
 g7 }|d	kr|d
dg7 }n|dkr|d
|g7 }|d kr| jg}|d k	r|dkrt r|dt|g7 }ndd l}|d |dgt	| 7 }z||}W n* tk
rD } z
|j}W 5 d }~X Y nX |dkS )Nr   z--showlocalsz
--tb=shortzDoctests not supportedr   -vz--cov=r   z-mznot slow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   labelverboseZ
extra_argvZdoctestsZcoveragetestsparallelr   module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         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partialfuncexpected)r   r
   r
   r   setup_method]   s    z_TestPythranFunc.setup_methodc                 C   s>   t |}i }|j D ] \}}|jt jjk	r|j||< q|S r   )inspect	signature
parametersitemsdefault	Parameterempty)r   funcr2   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   max_lenarg_idx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 d S )Nr   )r;   )r   Z
dtype_listZ	dtype_idxr
   r
   r   	get_dtypeu   s    z_TestPythranFunc.get_dtypec                 C   s`   t |  D ]N}g }| jD ]4}| | j| d |}|| j| d | q| j|  qd S )Nr   r   )ranger?   r.   rA   appendZastypepythranfunc)r   Ztype_idx
args_arrayr=   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   r@   )r.   rC   rD   r   rE   r=   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.   rC   nprepeatrD   rG   r
   r
   r   test_strided   s    
 
z_TestPythranFunc.test_stridedN)r   r   r   r	   rJ   Zint8Zint16Zint32Zint64ZintcZintpZALL_INTEGERZfloat32Zfloat64Z	ALL_FLOATZ	complex64Z
complex128ZALL_COMPLEXr0   r:   r?   rA   rF   rH   rL   r
   r
   r
   r   r   L   s   
		c                  C   s   ddl m}  | ddk	S )zQ
    Check if the pytest-xdist plugin is installed, providing parallel tests
    r   	find_specZxdistN)importlib.utilrN   rM   r
   r
   r   r      s    r   c                 C   s   ddl }z$ttjd }d| tjd }W n< tk
rh   t }|dkrT|d d| |d }Y nX || d k r|| 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"     
 
c                 C   sx   dddddddddddddd	d
}t dd| | t j}|rT|d|kr\tdt|d||d  S )NrP   g      ?     @@g    eAg   mBg      @g      0Ag      Ag      pB) br9   MGTkbZMbZGbZTbZkibZMibZGibZTibz^\s*(\d+)\s*({0})\s*$|rI   zInvalid size stringr   )	rematchrS   joinkeysIgroupr   float)Zsize_strsuffixesmr
   r
   r   rQ      s,             rQ   c               	   C   s   zddl } |  jW S  ttfk
r,   Y nX tjdri }tdd:}|D ].}|	 }t
|d d ||d d < qNW 5 Q R X d	|kr|d	 S |d
 |d  S dS )zD
    Get information about memory available, not counting swap.
    r   Nlinuxz/proc/meminforr   rX   :ZmemavailableZmemfreecached)psutilZvirtual_memory	availableImportErrorAttributeErrorr   platform
startswithopensplitrf   striplower)rm   infoflinepr
   r
   r   rU      s    .rU   )r	   r   r`   r   ZnumpyrJ   r1   __all__RuntimeWarningr   r   r   r   r   rQ   rU   r
   r
   r
   r   <module>   s   
7H	