o
    HXh<                     @   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 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   :/opt/python-3.10.19/usr/lib/python3.10/test/test_timeit.py__init__   s   
zFakeTimer.__init__c                 C   s   | j | j| j  S N)	BASE_TIMEr   r
   r   r   r   r   __call__      zFakeTimer.__call__c                 C      |  j d7  _ d S N   )r   r   r   r   r   inc!   r   zFakeTimer.incc                 C   r   r   )r	   r   r   r   r   setup$   r   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d Z
dZdZdd Zdd ZdkddZdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zdkd(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<d= Z!d>Z"dld@dAZ#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,e-.e/j0j1dTkdUdVdW Z2dXdY Z3dZd[ Z4d\d] Z5d^d_ Z6d`da Z7dmdcddZ8dedf Z9dgdh Z:didj Z;dS )n
TestTimeitc                 C   s    zt `W d S  ty   Y d S w r   )r   r   AttributeErrorr   r   r   r   tearDown.   s
   
zTestTimeit.tearDownc                 C   ,   |  tddd |  tddd d S )N r      assertEqualr   Zreindentr   r   r   r   test_reindent_empty4      zTestTimeit.test_reindent_emptyc                 C   r#   )Npassr   r%   r&   r   r   r   r   test_reindent_single8   r)   zTestTimeit.test_reindent_singlec                 C   ,   |  tddd |  tddd d S )Nz

r   r%   z

    
    r&   r   r   r   r   test_reindent_multi_empty<   r)   z$TestTimeit.test_reindent_multi_emptyc                 C   r,   )Nzprint()
pass
breakr   r%   zprint()
    pass
    breakr&   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 | j ttjdd | j ttjd	d
d d S )Nstmtreturnyieldyield from ()breakcontinuefrom timeit import *  passzwhile False:
  passz  break)r   r0   assertRaises
ValueErrorr   TimerSyntaxErrorr   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 | j ttjdd d S )	N)r   r1   r2   r3   r4   r5   r6   r7   r8   r   r   r   r   test_timer_invalid_setupT   s   z#TestTimeit.test_timer_invalid_setupc                 C   s(   t jdd t jdd t jdd d S )Nr$   r/   z 
	z	# comment)r   r;   r   r   r   r   test_timer_empty_stmt^      z TestTimeit.test_timer_empty_stmtz(import timeit
timeit._fake_timer.setup()z&import timeit
timeit._fake_timer.inc()c                 C      | j   d S r   )
fake_timerr   r   r   r   r   fake_callable_setupf      zTestTimeit.fake_callable_setupc                 C   rA   r   )rB   r   r   r   r   r   fake_callable_stmti   rD   zTestTimeit.fake_callable_stmtNc                 C   sv   t  | _tj||| j|d}i }|d u rt}n||d< |jdi |}| | jjd | | jj| | || d S )N)r0   r   r   globalsnumberr   r   )r   rB   r   r;   DEFAULT_NUMBERr'   r	   r   )r   r0   r   rG   rF   tkwargs
delta_timer   r   r   r   l   s   zTestTimeit.timeitc                 C      | j | j| jdd d S Nr   rG   r   	fake_stmt
fake_setupr   r   r   r   test_timeit_zero_iters~      z!TestTimeit.test_timeit_zero_itersc                 C   rL   N   rN   rO   r   r   r   r   test_timeit_few_iters   rS   z TestTimeit.test_timeit_few_itersc                 C   rL   rT   )r   rE   rQ   r   r   r   r   test_timeit_callable_stmt   rS   z$TestTimeit.test_timeit_callable_stmtc                 C   rL   rT   )r   rP   rC   r   r   r   r   test_timeit_callable_setup   rS   z%TestTimeit.test_timeit_callable_setupc                 C   rL   rT   )r   rE   rC   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   rG   r   )r   rP   rQ   r   r'   )r   rK   r   r   r   test_timeit_function_zero_iters      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())r0   r   rU   rN   )r0   r   rF   rG   zlocal_timer.inc())r   Z_global_timerr   r;   r9   	NameErrorrF   locals)r   rI   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 u rt}n||d< |d u r!t}n||d< |jdi |}| | jj| | | jj	||  | ||t
|g  d S )Nr0   r   r   repeatrG   r   )r   rB   r   r;   DEFAULT_REPEATrH   ra   r'   r	   r   float)r   r0   r   ra   rG   rI   rJ   delta_timesr   r   r   ra      s   zTestTimeit.repeatc                 C   rL   )Nr   )ra   ra   rP   rQ   r   r   r   r   test_repeat_zero_reps   rS   z TestTimeit.test_repeat_zero_repsc                 C   rL   rM   re   r   r   r   r   test_repeat_zero_iters   rS   z!TestTimeit.test_repeat_zero_itersc                 C      | j | j| jddd d S NrU   r   )ra   rG   re   r   r   r   r   test_repeat_few_reps_and_iters   s   z)TestTimeit.test_repeat_few_reps_and_itersc                 C   rh   ri   )ra   rE   rQ   r   r   r   r   test_repeat_callable_stmt      
z$TestTimeit.test_repeat_callable_stmtc                 C   rh   ri   )ra   rP   rC   r   r   r   r   test_repeat_callable_setup   rl   z%TestTimeit.test_repeat_callable_setupc                 C   rh   ri   )ra   rE   rC   r   r   r   r   #test_repeat_callable_stmt_and_setup   rl   z.TestTimeit.test_repeat_callable_stmt_and_setupc                 C   s(   t j| j| jdt d}| |g  d S )Nr   )ra   r   )r   ra   rP   rQ   r   r'   r   rd   r   r   r   test_repeat_function_zero_reps   r\   z)TestTimeit.test_repeat_function_zero_repsc                 C   s.   t j| j| jdt d}| |tdg  d S )Nr   rZ   g        )r   ra   rP   rQ   r   r'   rb   ro   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   sF   t  }td}z|  W n	   || Y | | d d S )N1/0ZeroDivisionError)ioStringIOr   r;   	print_excrw   getvalue)r   srI   r   r   r   test_print_exc   s   
zTestTimeit.test_print_excz"1 loop, best of 5: 1 sec per loop
r   c                 C   s   |d u r	t |d}|d u rg }n|d d  }|| j tjd d  }t }tj||jd W d    n1 s9w   Y  |d d  tjd d < |	 S )Nr
   )argsZ_wrap_timer)
r   appendrP   syspathr   r   mainr   r}   )r   r
   switchesr   r   Zorig_sys_pathr~   r   r   r   run_main   s   
zTestTimeit.run_mainc                 C   "   | j dgd}| |td d S )Nz--bad-switchr   zk            option --bad-switch not recognized
            use -h/--help for command line help
            r   r'   r   r   r~   r   r   r   test_main_bad_switch      zTestTimeit.test_main_bad_switchc                 C      | j dd}| |d d S )Ng      @r   z$1 loop, best of 5: 5.5 sec per loop
r   r'   r   r   r   r   test_main_seconds     zTestTimeit.test_main_secondsc                 C   r   )NgI+v?r   z'50 loops, best of 5: 5.5 msec per loop
r   r   r   r   r   test_main_milliseconds	  r   z!TestTimeit.test_main_millisecondsc                 C       | j ddgd}| |d d S )Ngh㈵>z-n100r
   r   z(100 loops, best of 5: 2.5 usec per loop
r   r   r   r   r   test_main_microseconds     z!TestTimeit.test_main_microsecondsc                 C   r   )N       @-n35r   $35 loops, best of 5: 2 sec per loop
r   r   r   r   r   test_main_fixed_iters  r   z TestTimeit.test_main_fixed_itersc                 C   *   | j dg dd}| |dt d  d S )Nr   )r   -szprint("CustomSetup")r   CustomSetup
r   r   r'   rb   r   r   r   r   test_main_setup     
zTestTimeit.test_main_setupc                 C   r   )Nr   )r   r   za = "CustomSetup"r   zprint(a)r   r   r   r   r   r   r   r   test_main_multiple_setups  r   z$TestTimeit.test_main_multiple_setupsc                 C   r   )N      N@z-r9r   z#1 loop, best of 9: 60 sec per loop
r   r   r   r   r   test_main_fixed_reps!  r   zTestTimeit.test_main_fixed_repsc                 C   r   )Nr   z-r-5r   z#1 loop, best of 1: 60 sec per loop
r   r   r   r   r   test_main_negative_reps%  r   z"TestTimeit.test_main_negative_repsrr   zneed __doc__c                 C   s$   | j dgd}| |tjd  d S )Nz-hr    )r   r'   r   __doc__r   r   r   r   test_main_help)  s   zTestTimeit.test_main_helpc                 C   r   )Nz-vr   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
            r   r   r   r   r   test_main_verbose0  r   zTestTimeit.test_main_verbosec                 C   s$   | j ddgd}| |td d S )NgiUMu>z-vvr   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
            r   r   r   r   r   test_main_very_verbose:  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    n1 sDw   Y  | | d d S )Ng~jth?z-uZsecr   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.
)r   r'   r   r}   )r   Zunit_secZ	unit_msecZ	unit_usecerror_stringioinvalidr   r   r   test_main_with_time_unitP  s4   
z#TestTimeit.test_main_with_time_unitc                 C   sH   t  }| jdgd}W d    n1 sw   Y  | | d d S )Nrx   r   ry   r   r   rw   r}   r   r   r~   r   r   r   test_main_exceptiond  s   zTestTimeit.test_main_exceptionc                 C   sJ   t  }| jddgd}W d    n1 sw   Y  | | d d S )Nz-n1rx   r   ry   r   r   r   r   r   test_main_exception_fixed_repsi  s   z)TestTimeit.test_main_exception_fixed_reps      P?c                 C   s(   t |d}tj| j| j|d}||S )Nr   r`   )r   r   r;   rP   rQ   	autorange)r   r
   callbackr   rI   r   r   r   r   n  s   

zTestTimeit.autorangec                 C   s(   |   \}}| |d | |d d S )N       @?r   r'   r   	num_loops
time_takenr   r   r   test_autoranges  r@   zTestTimeit.test_autorangec                 C   s,   | j dd\}}| |d | |d d S )Nr   r   r   r   r   r   r   r   test_autorange_secondx  s   z TestTimeit.test_autorange_secondc                 C   sn   dd }t  }| j|d\}}W d    n1 sw   Y  | |d | |d d}| | | d S )Nc                 S   s   t d| | d S )Nz	{} {:.3f})printformat)abr   r   r   r   ~  s   z9TestTimeit.test_autorange_with_callback.<locals>.callback)r   r   r   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_callback}  s   	z'TestTimeit.test_autorange_with_callback)NN)r   NN)r   N)<r   r   r   r"   r(   r+   r-   r.   r=   r>   r?   rQ   rP   rC   rE   r   rR   rV   rW   rX   rY   r[   r_   ra   rf   rg   rj   rk   rm   rn   rp   rq   rw   r   ZMAIN_DEFAULT_OUTPUTr   r   r   r   r   r   r   r   r   r   unittestZskipIfr   flagsoptimizer   r   r   r   r   r   r   r   r   r   r   r   r   r   r    ,   sl    




	



r    __main__)r   r   r   rz   textwrapr   Ztest.supportr   r   rH   rb   r   ZTestCaser    r   r   r   r   r   r   <module>   s       f