B
    u9a2;                 @   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mZ d dlmZ dZ	dZ
G dd dZG d	d
 d
ejZedkr|e  dS )    N)dedent)captured_stdout)captured_stderri@B    c               @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )	FakeTimerg      E@      ?c             C   s   d| _ d| _|| _| t_d S )Nr   )countsetup_callsseconds_per_incrementtimeit_fake_timer)selfr
    r   !/usr/lib/python3.7/test_timeit.py__init__   s    zFakeTimer.__init__c             C   s   | j | j| j  S )N)	BASE_TIMEr   r
   )r   r   r   r   __call__   s    zFakeTimer.__call__c             C   s   |  j d7  _ d S )N   )r   )r   r   r   r   inc!   s    zFakeTimer.incc             C   s   |  j d7  _ d S )Nr   )r	   )r   r   r   r   setup$   s    zFakeTimer.setupc             C   s
   || _ | S )z3Records 'timer' and returns self as callable timer.)Zsaved_timer)r   timerr   r   r   
wrap_timer'   s    zFakeTimer.wrap_timerN)r   )	__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r      s   
r   c               @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dZ
dZdd Zdd ZdiddZdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zdjd&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<Z!dkd>d?Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+e,-e.j/j0dRkdSdTdU Z1dVdW Z2dXdY Z3dZd[ Z4d\d] Z5d^d_ Z6dldadbZ7dcdd Z8dedf Z9dgdh Z:dS )m
TestTimeitc             C   s"   yt `W n tk
r   Y nX d S )N)r   r   AttributeError)r   r   r   r   tearDown.   s    zTestTimeit.tearDownc             C   s,   |  tddd |  tddd d S )N r      )assertEqualr   reindent)r   r   r   r   test_reindent_empty4   s    zTestTimeit.test_reindent_emptyc             C   s,   |  tddd |  tddd d S )Npassr   r   )r    r   r!   )r   r   r   r   test_reindent_single8   s    zTestTimeit.test_reindent_singlec             C   s,   |  tddd |  tddd d S )Nz

r   r   z

    
    )r    r   r!   )r   r   r   r   test_reindent_multi_empty<   s    z$TestTimeit.test_reindent_multi_emptyc             C   s,   |  tddd |  tddd d S )Nzprint()
pass
breakr   r   zprint()
    pass
    break)r    r   r!   )r   r   r   r   test_reindent_multi@   s    zTestTimeit.test_reindent_multic             C   s   | j ttjd d | j ttjdd | j ttjdd | j ttjdd | j ttjdd | j ttjdd | j ttjdd d S )N)stmtreturnyieldzyield from ()breakcontinuezfrom timeit import *)assertRaises
ValueErrorr   TimerSyntaxError)r   r   r   r   test_timer_invalid_stmtH   s    z"TestTimeit.test_timer_invalid_stmtc             C   s   | j ttjd d | j ttjdd | j ttjdd | j ttjdd | j ttjdd | j ttjdd | j ttjdd d S )N)r   r(   r)   zyield from ()r*   r+   zfrom timeit import *)r,   r-   r   r.   r/   )r   r   r   r   test_timer_invalid_setupQ   s    z#TestTimeit.test_timer_invalid_setupz(import timeit
timeit._fake_timer.setup()z&import timeit
timeit._fake_timer.inc()c             C   s   | j   d S )N)
fake_timerr   )r   r   r   r   fake_callable_setup]   s    zTestTimeit.fake_callable_setupc             C   s   | j   d S )N)r2   r   )r   r   r   r   fake_callable_stmt`   s    zTestTimeit.fake_callable_stmtNc             C   sr   t  | _tj||| j|d}i }|d kr.t}n||d< |jf |}| | jjd | | jj| | || d S )N)r'   r   r   globalsnumberr   )r   r2   r   r.   DEFAULT_NUMBERr    r	   r   )r   r'   r   r6   r5   tkwargs
delta_timer   r   r   r   c   s    zTestTimeit.timeitc             C   s   | j | j| jdd d S )Nr   )r6   )r   	fake_stmt
fake_setup)r   r   r   r   test_timeit_zero_itersu   s    z!TestTimeit.test_timeit_zero_itersc             C   s   | j | j| jdd d S )N   )r6   )r   r;   r<   )r   r   r   r   test_timeit_few_itersx   s    z TestTimeit.test_timeit_few_itersc             C   s   | j | j| jdd d S )Nr>   )r6   )r   r4   r<   )r   r   r   r   test_timeit_callable_stmt{   s    z$TestTimeit.test_timeit_callable_stmtc             C   s   | j | j| jdd d S )Nr>   )r6   )r   r;   r3   )r   r   r   r   test_timeit_callable_setup~   s    z%TestTimeit.test_timeit_callable_setupc             C   s   | j | j| jdd d S )Nr>   )r6   )r   r4   r3   )r   r   r   r   #test_timeit_callable_stmt_and_setup   s    z.TestTimeit.test_timeit_callable_stmt_and_setupc             C   s(   t j | j| jdt d}| |d d S )Nr   )r6   r   )r   r;   r<   r   r    )r   r:   r   r   r   test_timeit_function_zero_iters   s    
z*TestTimeit.test_timeit_function_zero_itersc             C   sX   t  atjdtd}| jt|jdd tjdtt dd t  }tjd|t dd d S )Nz_global_timer.inc())r'   r   r>   )r6   )r'   r   r5   r6   zlocal_timer.inc())r   Z_global_timerr   r.   r,   	NameErrorr5   locals)r   r8   Zlocal_timerr   r   r   test_timeit_globals_args   s    z#TestTimeit.test_timeit_globals_argsc             C   s   t  | _tj||| jd}i }|d kr,t}n||d< |d krBt}n||d< |jf |}| | jj| | | jj	||  | ||t
|g  d S )N)r'   r   r   repeatr6   )r   r2   r   r.   DEFAULT_REPEATr7   rG   r    r	   r   float)r   r'   r   rG   r6   r8   r9   delta_timesr   r   r   rG      s    zTestTimeit.repeatc             C   s   | j | j| jdd d S )Nr   )rG   )rG   r;   r<   )r   r   r   r   test_repeat_zero_reps   s    z TestTimeit.test_repeat_zero_repsc             C   s   | j | j| jdd d S )Nr   )r6   )rG   r;   r<   )r   r   r   r   test_repeat_zero_iters   s    z!TestTimeit.test_repeat_zero_itersc             C   s   | j | j| jddd d S )Nr>   r   )rG   r6   )rG   r;   r<   )r   r   r   r   test_repeat_few_reps_and_iters   s    z)TestTimeit.test_repeat_few_reps_and_itersc             C   s   | j | j| jddd d S )Nr>   r   )rG   r6   )rG   r4   r<   )r   r   r   r   test_repeat_callable_stmt   s    z$TestTimeit.test_repeat_callable_stmtc             C   s   | j | j| jddd d S )Nr>   r   )rG   r6   )rG   r;   r3   )r   r   r   r   test_repeat_callable_setup   s    z%TestTimeit.test_repeat_callable_setupc             C   s   | j | j| jddd d S )Nr>   r   )rG   r6   )rG   r4   r3   )r   r   r   r   #test_repeat_callable_stmt_and_setup   s    z.TestTimeit.test_repeat_callable_stmt_and_setupc             C   s(   t j| j| jdt d}| |g  d S )Nr   )rG   r   )r   rG   r;   r<   r   r    )r   rJ   r   r   r   test_repeat_function_zero_reps   s    
z)TestTimeit.test_repeat_function_zero_repsc             C   s.   t j| j| jdt d}| |tdg  d S )Nr   )r6   r   g        )r   rG   r;   r<   r   r    rH   )r   rJ   r   r   r   test_repeat_function_zero_iters   s    
z*TestTimeit.test_repeat_function_zero_itersc             C   sD   |  }| t|d | |d d | |d | d S )N   r   Z	Traceback)
splitlinesZassertGreaterlenZ
assertTrue
startswith)r   Z
exc_stringZexpected_exc_nameZ	exc_linesr   r   r   assert_exc_string   s    zTestTimeit.assert_exc_stringc             C   sJ   t  }td}y|  W n   || Y nX | | d d S )Nz1/0ZeroDivisionError)ioStringIOr   r.   	print_excrX   getvalue)r   sr8   r   r   r   test_print_exc   s    
zTestTimeit.test_print_excz"1 loop, best of 5: 1 sec per loop
      ?c          	   C   s   |d krt |d}|d kr g }n|d d  }|| j tjd d  }t }tj||jd W d Q R X |d d  tjd d < |	 S )N)r
   )argsZ_wrap_timer)
r   appendr;   syspathr   r   mainr   r]   )r   r
   switchesr   ra   Zorig_sys_pathr^   r   r   r   run_main   s    
zTestTimeit.run_mainc             C   s"   | j dgd}| |td d S )Nz--bad-switch)rf   zk            option --bad-switch not recognized
            use -h/--help for command line help
            )rg   r    r   )r   r^   r   r   r   test_main_bad_switch   s    zTestTimeit.test_main_bad_switchc             C   s   | j dd}| |d d S )Ng      @)r
   z$1 loop, best of 5: 5.5 sec per loop
)rg   r    )r   r^   r   r   r   test_main_seconds   s    zTestTimeit.test_main_secondsc             C   s   | j dd}| |d d S )NgI+v?)r
   z'50 loops, best of 5: 5.5 msec per loop
)rg   r    )r   r^   r   r   r   test_main_milliseconds   s    z!TestTimeit.test_main_millisecondsc             C   s    | j ddgd}| |d d S )Ngh㈵>z-n100)r
   rf   z(100 loops, best of 5: 2.5 usec per loop
)rg   r    )r   r^   r   r   r   test_main_microseconds  s    z!TestTimeit.test_main_microsecondsc             C   s    | j ddgd}| |d d S )Ng       @z-n35)r
   rf   z$35 loops, best of 5: 2 sec per loop
)rg   r    )r   r^   r   r   r   test_main_fixed_iters  s    z TestTimeit.test_main_fixed_itersc             C   s,   | j ddddgd}| |dt d  d S )Ng       @z-n35z-szprint("CustomSetup"))r
   rf   zCustomSetup
z$35 loops, best of 5: 2 sec per loop
)rg   r    rH   )r   r^   r   r   r   test_main_setup  s    zTestTimeit.test_main_setupc             C   s0   | j ddddddgd}| |dt d  d S )	Ng       @z-n35z-sza = "CustomSetup"zprint(a))r
   rf   zCustomSetup
z$35 loops, best of 5: 2 sec per loop
)rg   r    rH   )r   r^   r   r   r   test_main_multiple_setups  s    z$TestTimeit.test_main_multiple_setupsc             C   s    | j ddgd}| |d d S )Ng      N@z-r9)r
   rf   z#1 loop, best of 9: 60 sec per loop
)rg   r    )r   r^   r   r   r   test_main_fixed_reps  s    zTestTimeit.test_main_fixed_repsc             C   s    | j ddgd}| |d d S )Ng      N@z-r-5)r
   rf   z#1 loop, best of 1: 60 sec per loop
)rg   r    )r   r^   r   r   r   test_main_negative_reps  s    z"TestTimeit.test_main_negative_repsrS   zneed __doc__c             C   s$   | j dgd}| |tjd  d S )Nz-h)rf    )rg   r    r   __doc__)r   r^   r   r   r   test_main_help   s    zTestTimeit.test_main_helpc             C   s"   | j dgd}| |td d S )Nz-v)rf   z                1 loop -> 1 secs

                raw times: 1 sec, 1 sec, 1 sec, 1 sec, 1 sec

                1 loop, best of 5: 1 sec per loop
            )rg   r    r   )r   r^   r   r   r   test_main_verbose'  s    zTestTimeit.test_main_verbosec             C   s$   | j ddgd}| |td d S )NgiUMu>z-vv)r
   rf   a                  1 loop -> 3e-05 secs
                2 loops -> 6e-05 secs
                5 loops -> 0.00015 secs
                10 loops -> 0.0003 secs
                20 loops -> 0.0006 secs
                50 loops -> 0.0015 secs
                100 loops -> 0.003 secs
                200 loops -> 0.006 secs
                500 loops -> 0.015 secs
                1000 loops -> 0.03 secs
                2000 loops -> 0.06 secs
                5000 loops -> 0.15 secs
                10000 loops -> 0.3 secs

                raw times: 300 msec, 300 msec, 300 msec, 300 msec, 300 msec

                10000 loops, best of 5: 30 usec per loop
            )rg   r    r   )r   r^   r   r   r   test_main_very_verbose1  s    z!TestTimeit.test_main_very_verbosec          	   C   s   | j dddgd}| |d | j dddgd}| |d | j dddgd}| |d	 t }| j ddd
gd}W d Q R X | | d d S )Ng~jth?z-uZsec)r
   rf   z)100 loops, best of 5: 0.003 sec per loop
Zmsecz&100 loops, best of 5: 3 msec per loop
Zusecz*100 loops, best of 5: 3e+03 usec per loop
Zparsecz;Unrecognized unit. Please select nsec, usec, msec, or sec.
)rg   r    r   r]   )r   Zunit_secZ	unit_msecZ	unit_usecerror_stringioZinvalidr   r   r   test_main_with_time_unitG  s"    
z#TestTimeit.test_main_with_time_unitc          	   C   s4   t  }| jdgd}W d Q R X | | d d S )Nz1/0)rf   rY   )r   rg   rX   r]   )r   rv   r^   r   r   r   test_main_exception[  s    zTestTimeit.test_main_exceptionc          	   C   s6   t  }| jddgd}W d Q R X | | d d S )Nz-n1z1/0)rf   rY   )r   rg   rX   r]   )r   rv   r^   r   r   r   test_main_exception_fixed_reps`  s    z)TestTimeit.test_main_exception_fixed_reps      P?c             C   s(   t |d}tj| j| j|d}||S )N)r
   )r'   r   r   )r   r   r.   r;   r<   	autorange)r   r
   callbackr   r8   r   r   r   r{   e  s    
zTestTimeit.autorangec             C   s(   |   \}}| |d | |d d S )Ni  g     @?)r{   r    )r   	num_loops
time_takenr   r   r   test_autorangej  s    zTestTimeit.test_autorangec             C   s,   | j dd\}}| |d | |d d S )Ng      ?)r
   r   )r{   r    )r   r}   r~   r   r   r   test_autorange_secondo  s    z TestTimeit.test_autorange_secondc          	   C   sZ   dd }t  }| j|d\}}W d Q R X | |d | |d d}| | | d S )Nc             S   s   t d| | d S )Nz	{} {:.3f})printformat)abr   r   r   r|   u  s    z9TestTimeit.test_autorange_with_callback.<locals>.callback)r|   i  g     @?zQ1 0.001
2 0.002
5 0.005
10 0.010
20 0.020
50 0.049
100 0.098
200 0.195
500 0.488
)r   r{   r    r]   )r   r|   r^   r}   r~   Zexpectedr   r   r   test_autorange_with_callbackt  s    	z'TestTimeit.test_autorange_with_callback)NN)NN)r`   NN)rz   N);r   r   r   r   r"   r$   r%   r&   r0   r1   r<   r;   r3   r4   r   r=   r?   r@   rA   rB   rC   rF   rG   rK   rL   rM   rN   rO   rP   rQ   rR   rX   r_   ZMAIN_DEFAULT_OUTPUTrg   rh   ri   rj   rk   rl   rm   rn   ro   rp   unittestZskipIfrc   flagsoptimizers   rt   ru   rw   rx   ry   r{   r   r   r   r   r   r   r   r   ,   sf   		



	


r   __main__)r   r   rc   rZ   textwrapr   Ztest.supportr   r   r7   rH   r   ZTestCaser   r   re   r   r   r   r   <module>   s     ]