B
    u9a                 @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlZddlmZmZ G dd	 d	ejZd
d Zedd Zdd Zdd Zi Zded< ded< ded< edkre  dS )z"Test suite for the profile module.    N)unified_diff)StringIO)TESTFNrun_unittestunlink)contextmanager)testfunctimerc               @   s`   e Zd ZejZeZdddgZdZdd Z	dd Z
ed	d
 Zdd Zdd Zdd Zdd ZdS )ProfileTestprint_statsprint_callersprint_calleesz:0(max)c             C   s   t t d S )N)r   r   )self r   "/usr/lib/python3.7/test_profile.pytearDown   s    zProfileTest.tearDownc             C   s   t S )N)_ProfileOutput)r   r   r   r   get_expected_output   s    zProfileTest.get_expected_outputc                s   g }|  td}t }|dt t  |t |  x|| jD ]r}t }tj	||d}|
 d t||  |  }tjddd   fdd|D }|d	| q@W |S )
NgMbP?z
testfunc())streamZstdname.   c                s   g | ]} |kr|  qS r   )rstrip).0line)mod_namer   r   
<listcomp>.   s    z,ProfileTest.do_profiling.<locals>.<listcomp>
)profilerclassr	   runctxglobalslocalsappendmethodnamesr   pstatsStatsZ
strip_dirsZ
sort_statsgetattrgetvalue
splitlinesr   
__module__rsplitjoin)clsresultsprofZstart_timerZ
methodnamesstatsoutputr   )r   r   do_profiling   s    zProfileTest.do_profilingc             C   s   |   }|  }| |d d g }xnt| jD ]`\}}|| }||d  }||kr0|d| d| jj d |t	|
d|
ddd	 q0W |r| d| d S )
Nr   i  r   z
Stats.z output for z does not fit expectation:r    )Zlineterm)r1   r   ZassertEqual	enumerater"   r!   r   __name__extendr   splitfailr*   )r   r,   Zexpectedr7   imethodabr   r   r   test_cprofile2   s    zProfileTest.test_cprofilec          	   C   s|   ddddddg}xf|D ]^}t  }| td}||t t  tj||d}|  |	 }| 
| j|d	|| qW d S )
Nzmax([0])zmax([0], key=int)zmax([0], **dict(key=int))zmax(*([0],))zmax(*([0],), key=int)zmax(*([0],), **dict(key=int))gMbP?)r   z&Profiling {0!r} didn't report max:
{1})r   r   r	   r   r   r    r#   r$   r   r&   ZassertInexpected_max_outputformat)r   ZstmtsZstmtr.   r-   r/   Zresr   r   r   test_calling_conventionsC   s    

z$ProfileTest.test_calling_conventionsc          	   C   sD   t   | jd W d Q R X | jjdtd | tjt d S )Nzint('1'))filename)silentprofilermodulerunr   
assertTrueospathexists)r   r   r   r   test_runX   s    zProfileTest.test_runc          	   C   sT   t   | jdt t  W d Q R X | jjdt t td | tj	t d S )Nz
testfunc())r@   )
rA   rB   r   r   r    r   rD   rE   rF   rG   )r   r   r   r   test_runctx^   s
    zProfileTest.test_runctxN)r4   r(   __qualname__profileZProfiler   rB   r"   r=   r   r   classmethodr1   r<   r?   rH   rI   r   r   r   r   r
      s   
r
   c          	   C   s   |  d} td|   | }g }t| d*}x"|D ]}|| |dr4P q4W W d Q R X t| dV}|| |d x0t|j	D ]"\}}|d|||d  f  qW |d	 W d Q R X d S )
NcozRegenerating %s...rz#--cutwz_ProfileOutput = {}
z _ProfileOutput[%r] = """\
%s"""
r   z'
if __name__ == "__main__":
    main()
)
r   printr1   openr!   
startswith
writelineswriter3   r"   )r@   r+   r,   Znewfilefr   r8   r9   r   r   r   regenerate_expected_outputf   s     





rV   c              c   s&   t j} zt t _d V  W d | t _X d S )N)sysstdoutr   )rX   r   r   r   rA   z   s
    
rA   c               C   s   t t d S )N)r   r
   r   r   r   r   	test_main   s    rY   c               C   s    dt jkrt  n
ttt d S )Nz-r)rW   argvrY   rV   __file__r
   r   r   r   r   main   s    
r\   a         28   27.972    0.999   27.972    0.999 profilee.py:110(__getattr__)
        1  269.996  269.996  999.769  999.769 profilee.py:25(testfunc)
     23/3  149.937    6.519  169.917   56.639 profilee.py:35(factorial)
       20   19.980    0.999   19.980    0.999 profilee.py:48(mul)
        2   39.986   19.993  599.830  299.915 profilee.py:55(helper)
        4  115.984   28.996  119.964   29.991 profilee.py:73(helper1)
        2   -0.006   -0.003  139.946   69.973 profilee.py:84(helper2_indirect)
        8  311.976   38.997  399.912   49.989 profilee.py:88(helper2)
        8   63.976    7.997   79.960    9.995 profilee.py:98(subhelper)r   a  :0(append)                        <- profilee.py:73(helper1)(4)  119.964
:0(exc_info)                      <- profilee.py:73(helper1)(4)  119.964
:0(hasattr)                       <- profilee.py:73(helper1)(4)  119.964
                                     profilee.py:88(helper2)(8)  399.912
profilee.py:110(__getattr__)      <- :0(hasattr)(12)   11.964
                                     profilee.py:98(subhelper)(16)   79.960
profilee.py:25(testfunc)          <- <string>:1(<module>)(1)  999.767
profilee.py:35(factorial)         <- profilee.py:25(testfunc)(1)  999.769
                                     profilee.py:35(factorial)(20)  169.917
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:48(mul)               <- profilee.py:35(factorial)(20)  169.917
profilee.py:55(helper)            <- profilee.py:25(testfunc)(2)  999.769
profilee.py:73(helper1)           <- profilee.py:55(helper)(4)  599.830
profilee.py:84(helper2_indirect)  <- profilee.py:55(helper)(2)  599.830
profilee.py:88(helper2)           <- profilee.py:55(helper)(6)  599.830
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:98(subhelper)         <- profilee.py:88(helper2)(8)  399.912r   a  :0(hasattr)                       -> profilee.py:110(__getattr__)(12)   27.972
<string>:1(<module>)              -> profilee.py:25(testfunc)(1)  999.769
profilee.py:110(__getattr__)      ->
profilee.py:25(testfunc)          -> profilee.py:35(factorial)(1)  169.917
                                     profilee.py:55(helper)(2)  599.830
profilee.py:35(factorial)         -> profilee.py:35(factorial)(20)  169.917
                                     profilee.py:48(mul)(20)   19.980
profilee.py:48(mul)               ->
profilee.py:55(helper)            -> profilee.py:73(helper1)(4)  119.964
                                     profilee.py:84(helper2_indirect)(2)  139.946
                                     profilee.py:88(helper2)(6)  399.912
profilee.py:73(helper1)           -> :0(append)(4)   -0.004
profilee.py:84(helper2_indirect)  -> profilee.py:35(factorial)(2)  169.917
                                     profilee.py:88(helper2)(2)  399.912
profilee.py:88(helper2)           -> :0(hasattr)(8)   11.964
                                     profilee.py:98(subhelper)(8)   79.960
profilee.py:98(subhelper)         -> profilee.py:110(__getattr__)(16)   27.972r   __main__)__doc__rW   r#   ZunittestrE   Zdifflibr   ior   Ztest.supportr   r   r   
contextlibr   rK   Ztest.profileer   r	   ZTestCaser
   rV   rA   rY   r\   r   r4   r   r   r   r   <module>   s*   V		
