
    <e<                         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          Z G d	 d
ej                  Zedk    r ej                     dS dS )    N)dedent)captured_stdout)captured_stderri@B    c                   2    e Zd ZdZd	dZd Zd Zd Zd ZdS )
	FakeTimerg      E@      ?c                 H    d| _         d| _        || _        | t          _        d S )Nr   )countsetup_callsseconds_per_incrementtimeit_fake_timer)selfr   s     '/usr/lib/python3.11/test/test_timeit.py__init__zFakeTimer.__init__   s'    
#8"!    c                 0    | j         | j        | j        z  z   S N)	BASE_TIMEr   r   r   s    r   __call__zFakeTimer.__call__   s    ~
T-G GGGr   c                 &    | xj         dz  c_         d S N   )r   r   s    r   inczFakeTimer.inc!   s    

a



r   c                 &    | xj         dz  c_         d S r   )r   r   s    r   setupzFakeTimer.setup$   s    Ar   c                     	 || _         | S r   )saved_timer)r   timers     r   
wrap_timerzFakeTimer.wrap_timer'   s    A r   N)r	   )	__name__
__module____qualname__r   r   r   r   r   r"    r   r   r   r      sl        I" " " "H H H        r   r   c                      e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	Zd
Zd Zd Zd:dZd Zd Zd Zd Zd Zd Zd Zd:dZd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d!Z"d;d#Z#d$ Z$d% Z%d& Z&d' Z'd( Z(d) Z)d* Z*d+ Z+d, Z, e-j.        e/j0        j1        d-k    d.          d/             Z2d0 Z3d1 Z4d2 Z5d3 Z6d4 Z7d<d6Z8d7 Z9d8 Z:d9 Z;dS )=
TestTimeitc                 8    	 t           `d S # t          $ r Y d S w xY wr   )r   r   AttributeErrorr   s    r   tearDownzTestTimeit.tearDown.   s5    	""" 	 	 	DD	s    
c                     |                      t          j        dd          d           |                      t          j        dd          d           d S )N r      assertEqualr   reindentr   s    r   test_reindent_emptyzTestTimeit.test_reindent_empty4   sL    Q//444Q//44444r   c                     |                      t          j        dd          d           |                      t          j        dd          d           d S )Npassr   r.   r/   r   s    r   test_reindent_singlezTestTimeit.test_reindent_single8   sL    33V<<<33V<<<<<r   c                     |                      t          j        dd          d           |                      t          j        dd          d           d S )Nz

r   r.   z

    
    r/   r   s    r   test_reindent_multi_emptyz$TestTimeit.test_reindent_multi_empty<   sL    33V<<<33^DDDDDr   c                     |                      t          j        dd          d           |                      t          j        dd          d           d S )Nzprint()
pass
breakr   r.   zprint()
    pass
    breakr/   r   s    r   test_reindent_multizTestTimeit.test_reindent_multi@   sj    "A' '"	$ 	$ 	$ 	"A' '*	, 	, 	, 	, 	,r   c                    |                      t          t          j        d            |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d	d
           d S )Nstmtreturnyieldyield from ()breakcontinuefrom timeit import *  passzwhile False:
  passz  break)r   r<   assertRaises
ValueErrorr   TimerSyntaxErrorr   s    r   test_timer_invalid_stmtz"TestTimeit.test_timer_invalid_stmtH   s"   *fl>>>+v|(CCC+v|'BBB+v|/JJJ+v|'BBB+v|*EEE+v|:PQQQ+v|(CCC+v| 6Y 	 	H 	H 	H 	H 	Hr   c                 v   |                      t          t          j        d            |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           |                      t          t          j        d           d S )	N)r   r=   r>   r?   r@   rA   rB   rC   rD   r   s    r   test_timer_invalid_setupz#TestTimeit.test_timer_invalid_setupT   s    *fl$???+v|8DDD+v|7CCC+v|?KKK+v|7CCC+v|:FFF+v|;QRRR+v|8DDDDDr   c                     t          j        d           t          j        d           t          j        d           d S )Nr-   r;   z 
	z	# comment)r   rG   r   s    r   test_timer_empty_stmtz TestTimeit.test_timer_empty_stmt^   sA    ")$$$$+&&&&&&r   z(import timeit
timeit._fake_timer.setup()z&import timeit
timeit._fake_timer.inc()c                 8    | j                                          d S r   )
fake_timerr   r   s    r   fake_callable_setupzTestTimeit.fake_callable_setupf   s    r   c                 8    | j                                          d S r   )rO   r   r   s    r   fake_callable_stmtzTestTimeit.fake_callable_stmti   s    r   Nc                 N   t                      | _        t          j        ||| j        |          }i }|t          }n||d<    |j        di |}|                     | j        j        d           |                     | j        j        |           |                     ||           d S )N)r<   r   r!   globalsnumberr   r&   )r   rO   r   rG   DEFAULT_NUMBERr0   r   r   )r   r<   r   rU   rT   tkwargs
delta_times           r   r   zTestTimeit.timeitl   s    #++Ld%t! ! !>#FF%F8QX''''
4a888.777V,,,,,r   c                 J    |                      | j        | j        d           d S Nr   rU   r   	fake_stmt
fake_setupr   s    r   test_timeit_zero_itersz!TestTimeit.test_timeit_zero_iters~   $    DNDOA>>>>>r   c                 J    |                      | j        | j        d           d S N   r\   r]   r   s    r   test_timeit_few_itersz TestTimeit.test_timeit_few_iters   ra   r   c                 J    |                      | j        | j        d           d S rc   )r   rR   r_   r   s    r   test_timeit_callable_stmtz$TestTimeit.test_timeit_callable_stmt   s%    D+T_QGGGGGr   c                 J    |                      | j        | j        d           d S rc   )r   r^   rP   r   s    r   test_timeit_callable_setupz%TestTimeit.test_timeit_callable_setup   s%    DND$<QGGGGGr   c                 J    |                      | j        | j        d           d S rc   )r   rR   rP   r   s    r   #test_timeit_callable_stmt_and_setupz.TestTimeit.test_timeit_callable_stmt_and_setup   s4    D+( 	 	4 	4 	4 	4 	4r   c                     t          j         | j        | j        dt                                }|                     |d           d S )Nr   rU   r!   )r   r^   r_   r   r0   )r   rY   s     r   test_timeit_function_zero_itersz*TestTimeit.test_timeit_function_zero_iters   sE    ]4>4?1kk# # #
Q'''''r   c                 R   t                      at          j        dt                    }|                     t
          |j        d           t          j        dt          t                      d           t                      }t          j        d|t                      d           d S )Nz_global_timer.inc())r<   r!   rd   r\   )r<   r!   rT   rU   zlocal_timer.inc())r   _global_timerr   rG   rE   	NameErrorrT   locals)r   rW   local_timers      r   test_timeit_globals_argsz#TestTimeit.test_timeit_globals_args   s    !L3=III)QXa8880%ii	3 	3 	3 	3kk.k$hhq	2 	2 	2 	2 	2 	2r   c                    t                      | _        t          j        ||| j                  }i }|t          }n||d<   |t
          }n||d<    |j        di |}|                     | j        j        |           |                     | j        j	        ||z             |                     ||t          |          gz             d S )Nr<   r   r!   repeatrU   r&   )r   rO   r   rG   DEFAULT_REPEATrV   rw   r0   r   r   float)r   r<   r   rw   rU   rW   rX   delta_timess           r   rw   zTestTimeit.repeat   s    #++Ld%tGGG>#FF%F8>#FF%F8ah((((4f===.@@@ff&>?????r   c                 J    |                      | j        | j        d           d S )Nr   )rw   rw   r^   r_   r   s    r   test_repeat_zero_repsz TestTimeit.test_repeat_zero_reps   ra   r   c                 J    |                      | j        | j        d           d S r[   r|   r   s    r   test_repeat_zero_itersz!TestTimeit.test_repeat_zero_iters   ra   r   c                 L    |                      | j        | j        dd           d S Nrd   r   )rw   rU   r|   r   s    r   test_repeat_few_reps_and_itersz)TestTimeit.test_repeat_few_reps_and_iters   s&    DNDOAaHHHHHr   c                 L    |                      | j        | j        dd           d S r   )rw   rR   r_   r   s    r   test_repeat_callable_stmtz$TestTimeit.test_repeat_callable_stmt   s5    D+T_ 	 	$ 	$ 	$ 	$ 	$r   c                 L    |                      | j        | j        dd           d S r   )rw   r^   rP   r   s    r   test_repeat_callable_setupz%TestTimeit.test_repeat_callable_setup   s5    DND$< 	 	$ 	$ 	$ 	$ 	$r   c                 L    |                      | j        | j        dd           d S r   )rw   rR   rP   r   s    r   #test_repeat_callable_stmt_and_setupz.TestTimeit.test_repeat_callable_stmt_and_setup   s6    D+T-E 	 	$ 	$ 	$ 	$ 	$r   c                     t          j        | j        | j        dt	                                }|                     |g            d S )Nr   )rw   r!   )r   rw   r^   r_   r   r0   r   rz   s     r   test_repeat_function_zero_repsz)TestTimeit.test_repeat_function_zero_reps   sE    mDNDOAkk# # #b)))))r   c                     t          j        | j        | j        dt	                                }|                     |t          dgz             d S )Nr   rm   g        )r   rw   r^   r_   r   r0   rx   r   s     r   test_repeat_function_zero_itersz*TestTimeit.test_repeat_function_zero_iters   sL    mDNDOAkk# # #nu&<=====r   c                 ,   |                                 }|                     t          |          d           |                     |d                             d                     |                     |d                             |                     d S )N   r   	Traceback)
splitlinesassertGreaterlen
assertTrue
startswith)r   
exc_stringexpected_exc_name	exc_liness       r   assert_exc_stringzTestTimeit.assert_exc_string   s~    ))++	3y>>1---	!//<<===	"001BCCDDDDDr   c                    t          j                    }t          j        d          }	 |                                 n#  |                    |           Y nxY w|                     |                                d           d S )N1/0ZeroDivisionError)ioStringIOr   rG   	print_excr   getvalue)r   srW   s      r   test_print_exczTestTimeit.test_print_exc   sq    KMML	HHJJJJ	KKNNNNNqzz||-@AAAAAs	   > Az"1 loop, best of 5: 1 sec per loop
r	   c                 |   |t          |          }|g }n
|d d          }|                    | j                   t          j        d d          }t                      5 }t          j        ||j                   d d d            n# 1 swxY w Y   |d d          t          j        d d <   |	                                S )Nr   )args_wrap_timer)
r   appendr^   syspathr   r   mainr"   r   )r   r   switchesr!   r   orig_sys_pathr   s          r   run_mainzTestTimeit.run_main   s   =4IJJJEDDAAA;DDN### 	A!KTu/?@@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A#AAA&zz||s   BB
B
c                 z    |                      dg          }|                     |t          d                     d S )Nz--bad-switchr   zk            option --bad-switch not recognized
            use -h/--help for command line help
            r   r0   r   r   r   s     r   test_main_bad_switchzTestTimeit.test_main_bad_switch   sM    MMN#3M44F $   	 	 	 	 	r   c                 ^    |                      d          }|                     |d           d S )Ng      @r   z$1 loop, best of 5: 5.5 sec per loop
r   r0   r   s     r   test_main_secondszTestTimeit.test_main_seconds  s2    MMM44CDDDDDr   c                 ^    |                      d          }|                     |d           d S )NgI+v?r   z'50 loops, best of 5: 5.5 msec per loop
r   r   s     r   test_main_millisecondsz!TestTimeit.test_main_milliseconds	  s2    MMM77FGGGGGr   c                 b    |                      ddg          }|                     |d           d S )Ngh㈵>z-n100r   r   z(100 loops, best of 5: 2.5 usec per loop
r   r   s     r   test_main_microsecondsz!TestTimeit.test_main_microseconds  s6    MM	WIMNNGHHHHHr   c                 b    |                      ddg          }|                     |d           d S )N       @-n35r   $35 loops, best of 5: 2 sec per loop
r   r   s     r   test_main_fixed_itersz TestTimeit.test_main_fixed_iters  s6    MMvhMGGCDDDDDr   c                 z    |                      dg d          }|                     |dt          z  dz              d S )Nr   )r   -szprint("CustomSetup")r   CustomSetup
r   r   r0   rx   r   s     r   test_main_setupzTestTimeit.test_main_setup  sZ    MM???  A AOn<78 	9 	9 	9 	9 	9r   c                 z    |                      dg d          }|                     |dt          z  dz              d S )Nr   )r   r   za = "CustomSetup"r   zprint(a)r   r   r   r   r   s     r   test_main_multiple_setupsz$TestTimeit.test_main_multiple_setups  sZ    MMNNN  P POn<78 	9 	9 	9 	9 	9r   c                 b    |                      ddg          }|                     |d           d S )N      N@z-r9r   z#1 loop, best of 9: 60 sec per loop
r   r   s     r   test_main_fixed_repszTestTimeit.test_main_fixed_reps!  s6    MMwMGGBCCCCCr   c                 b    |                      ddg          }|                     |d           d S )Nr   z-r-5r   z#1 loop, best of 1: 60 sec per loop
r   r   s     r   test_main_negative_repsz"TestTimeit.test_main_negative_reps%  s6    MMxMHHBCCCCCr   r   zneed __doc__c                 z    |                      dg          }|                     |t          j        dz              d S )Nz-hr    )r   r0   r   __doc__r   s     r   test_main_helpzTestTimeit.test_main_help)  s<    MMD6M** 	FNS011111r   c                 z    |                      dg          }|                     |t          d                     d S )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   s     r   test_main_verbosezTestTimeit.test_main_verbose0  sL    MMD6M**F $   	 	 	 	 	r   c                 |    |                      ddg          }|                     |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   s     r   test_main_very_verbosez!TestTimeit.test_main_very_verbose:  sN    MM	UGMLLF $   	 	 	 	 	r   c                    |                      dddg          }|                     |d           |                      dddg          }|                     |d           |                      dddg          }|                     |d	           t                      5 }|                      ddd
g          }d d d            n# 1 swxY w Y   |                     |                                d           d S )Ng~jth?z-usecr   z)100 loops, best of 5: 0.003 sec per loop
msecz&100 loops, best of 5: 3 msec per loop
usecz*100 loops, best of 5: 3e+03 usec per loop
parsecz;Unrecognized unit. Please select nsec, usec, msec, or sec.
)r   r0   r   r   )r   unit_sec	unit_msec	unit_usecerror_stringioinvalids         r   test_main_with_time_unitz#TestTimeit.test_main_with_time_unitP  sx   ==u ! ( (<	> 	> 	>MM " ) )	9	; 	; 	;MM " ) )	=	? 	? 	?  	/.mm%"H- $ / /G	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	0022R	T 	T 	T 	T 	Ts   CC	Cc                     t                      5 }|                     dg          }d d d            n# 1 swxY w Y   |                     |                                d           d S )Nr   r   r   r   r   r   r   r   r   r   s      r   test_main_exceptionzTestTimeit.test_main_exceptiond  s     	0.w//A	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0~6688:MNNNNNs   377c                     t                      5 }|                     ddg          }d d d            n# 1 swxY w Y   |                     |                                d           d S )Nz-n1r   r   r   r   r   s      r   test_main_exception_fixed_repsz)TestTimeit.test_main_exception_fixed_repsi  s     	7.u~66A	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7~6688:MNNNNNs   488      P?c                     t          |          }t          j        | j        | j        |          }|                    |          S )Nr   rv   )r   r   rG   r^   r_   	autorange)r   r   callbackr!   rW   s        r   r   zTestTimeit.autorangen  s?    0EFFFLdnDO5QQQ{{8$$$r   c                     |                                  \  }}|                     |d           |                     |d           d S )N       @?r   r0   r   	num_loops
time_takens      r   test_autorangezTestTimeit.test_autoranges  sH     $ 0 0	:C(((X.....r   c                     |                      d          \  }}|                     |d           |                     |d           d S )Nr	   r   r   r   r   s      r   test_autorange_secondz TestTimeit.test_autorange_secondx  sL     $S I I	:A&&&S)))))r   c                 6   d }t                      5 }|                     |          \  }}d d d            n# 1 swxY w Y   |                     |d           |                     |d           d}|                     |                                |           d S )Nc                 L    t          d                    | |                     d S )Nz	{} {:.3f})printformat)abs     r   r   z9TestTimeit.test_autorange_with_callback.<locals>.callback~  s&    +$$Q**+++++r   )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   r0   r   )r   r   r   r   r   expecteds         r   test_autorange_with_callbackz'TestTimeit.test_autorange_with_callback}  s    	, 	, 	, 	F!$(NNHN$E$E!Iz	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	FC(((X..." 	x00000s   8<<)NN)r	   NN)r   N)<r#   r$   r%   r+   r2   r5   r7   r9   rI   rK   rM   r_   r^   rP   rR   r   r`   re   rg   ri   rk   rn   rt   rw   r}   r   r   r   r   r   r   r   r   r   MAIN_DEFAULT_OUTPUTr   r   r   r   r   r   r   r   r   r   unittestskipIfr   flagsoptimizer   r   r   r   r   r   r   r   r   r   r&   r   r   r(   r(   ,   sx         5 5 5= = =E E E, , ,
H 
H 
HE E E' ' '
 =J9I       - - - -$? ? ?? ? ?H H HH H H4 4 4( ( (
	2 	2 	2@ @ @ @*? ? ?? ? ?I I I$ $ $$ $ $$ $ $* * *
> > >
E E EB B B @     E E EH H HI I IE E E9 9 99 9 9D D DD D D X_SY'1,n==2 2 >=2    ,T T T(O O O
O O O
% % % %
/ / /
* * *
1 1 1 1 1r   r(   __main__)r   r   r   r   textwrapr   test.supportr   r   rV   rx   r   TestCaser(   r#   r   r&   r   r   <module>r     s      



 				       ( ( ( ( ( ( ( ( ( ( ( (         ,a1 a1 a1 a1 a1" a1 a1 a1H zHMOOOOO r   