<s> import timeit <EOL> import unittest <EOL> import sys <EOL> import io <EOL> import time <EOL> from textwrap import dedent <EOL> from test . support import run_unittest <EOL> from test . support import captured_stdout <EOL> from test . support import captured_stderr <EOL> DEFAULT_NUMBER = <NUM_LIT> <EOL> DEFAULT_REPEAT = <NUM_LIT:3> <EOL> class FakeTimer : <EOL> BASE_TIME = <NUM_LIT> <EOL> def __init__ ( self , seconds_per_increment = <NUM_LIT:1.0> ) : <EOL> self . count = <NUM_LIT:0> <EOL> self . setup_calls = <NUM_LIT:0> <EOL> self . seconds_per_increment = seconds_per_increment <EOL> timeit . _fake_timer = self <EOL> def __call__ ( self ) : <EOL> return self . BASE_TIME + self . count * self . seconds_per_increment <EOL> def inc ( self ) : <EOL> self . count += <NUM_LIT:1> <EOL> def setup ( self ) : <EOL> self . setup_calls += <NUM_LIT:1> <EOL> def wrap_timer ( self , timer ) : <EOL> """<STR_LIT>""" <EOL> self . saved_timer = timer <EOL> return self <EOL> class TestTimeit ( unittest . TestCase ) : <EOL> def tearDown ( self ) : <EOL> try : <EOL> del timeit . _fake_timer <EOL> except AttributeError : <EOL> pass <EOL> def test_reindent_empty ( self ) : <EOL> self . assertEqual ( timeit . reindent ( "<STR_LIT>" , <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( timeit . reindent ( "<STR_LIT>" , <NUM_LIT:4> ) , "<STR_LIT>" ) <EOL> def test_reindent_single ( self ) : <EOL> self . assertEqual ( timeit . reindent ( "<STR_LIT>" , <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( timeit . reindent ( "<STR_LIT>" , <NUM_LIT:4> ) , "<STR_LIT>" ) <EOL> def test_reindent_multi_empty ( self ) : <EOL> self . assertEqual ( timeit . reindent ( "<STR_LIT>" , <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( timeit . reindent ( "<STR_LIT>" , <NUM_LIT:4> ) , "<STR_LIT>" ) <EOL> def test_reindent_multi ( self ) : <EOL> self . assertEqual ( timeit . reindent ( <EOL> "<STR_LIT>" , <NUM_LIT:0> ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( timeit . reindent ( <EOL> "<STR_LIT>" , <NUM_LIT:4> ) , <EOL> "<STR_LIT>" ) <EOL> def test_timer_invalid_stmt ( self ) : <EOL> self . assertRaises ( ValueError , timeit . Timer , stmt = None ) <EOL> def test_timer_invalid_setup ( self ) : <EOL> self . assertRaises ( ValueError , timeit . Timer , setup = None ) <EOL> fake_setup = "<STR_LIT>" <EOL> fake_stmt = "<STR_LIT>" <EOL> def fake_callable_setup ( self ) : <EOL> self . fake_timer . setup ( ) <EOL> def fake_callable_stmt ( self ) : <EOL> self . fake_timer . inc ( ) <EOL> def timeit ( self , stmt , setup , number = None ) : <EOL> self . fake_timer = FakeTimer ( ) <EOL> t = timeit . Timer ( stmt = stmt , setup = setup , timer = self . fake_timer ) <EOL> kwargs = { } <EOL> if number is None : <EOL> number = DEFAULT_NUMBER <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = number <EOL> delta_time = t . timeit ( ** kwargs ) <EOL> self . assertEqual ( self . fake_timer . setup_calls , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fake_timer . count , number ) <EOL> self . assertEqual ( delta_time , number ) <EOL> def test_timeit_zero_iters ( self ) : <EOL> self . timeit ( self . fake_stmt , self . fake_setup , number = <NUM_LIT:0> ) <EOL> def test_timeit_few_iters ( self ) : <EOL> self . timeit ( self . fake_stmt , self . fake_setup , number = <NUM_LIT:3> ) <EOL> def test_timeit_callable_stmt ( self ) : <EOL> self . timeit ( self . fake_callable_stmt , self . fake_setup , number = <NUM_LIT:3> ) <EOL> def test_timeit_callable_stmt_and_setup ( self ) : <EOL> self . timeit ( self . fake_callable_stmt , <EOL> self . fake_callable_setup , number = <NUM_LIT:3> ) <EOL> def test_timeit_function_zero_iters ( self ) : <EOL> delta_time = timeit . timeit ( self . fake_stmt , self . fake_setup , number = <NUM_LIT:0> , <EOL> timer = FakeTimer ( ) ) <EOL> self . assertEqual ( delta_time , <NUM_LIT:0> ) <EOL> def repeat ( self , stmt , setup , repeat = None , number = None ) : <EOL> self . fake_timer = FakeTimer ( ) <EOL> t = timeit . Timer ( stmt = stmt , setup = setup , timer = self . fake_timer ) <EOL> kwargs = { } <EOL> if repeat is None : <EOL> repeat = DEFAULT_REPEAT <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = repeat <EOL> if number is None : <EOL> number = DEFAULT_NUMBER <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = number <EOL> delta_times = t . repeat ( ** kwargs ) <EOL> self . assertEqual ( self . fake_timer . setup_calls , repeat ) <EOL> self . assertEqual ( self . fake_timer . count , repeat * number ) <EOL> self . assertEqual ( delta_times , repeat * [ float ( number ) ] ) <EOL> def test_repeat_zero_reps ( self ) : <EOL> self . repeat ( self . fake_stmt , self . fake_setup , repeat = <NUM_LIT:0> ) <EOL> def test_repeat_zero_iters ( self ) : <EOL> self . repeat ( self . fake_stmt , self . fake_setup , number = <NUM_LIT:0> ) <EOL> def test_repeat_few_reps_and_iters ( self ) : <EOL> self . repeat ( self . fake_stmt , self . fake_setup , repeat = <NUM_LIT:3> , number = <NUM_LIT:5> ) <EOL> def test_repeat_callable_stmt ( self ) : <EOL> self . repeat ( self . fake_callable_stmt , self . fake_setup , <EOL> repeat = <NUM_LIT:3> , number = <NUM_LIT:5> ) <EOL> def test_repeat_callable_stmt_and_setup ( self ) : <EOL> self . repeat ( self . fake_callable_stmt , self . fake_callable_setup , <EOL> repeat = <NUM_LIT:3> , number = <NUM_LIT:5> ) <EOL> def test_repeat_function_zero_reps ( self ) : <EOL> delta_times = timeit . repeat ( self . fake_stmt , self . fake_setup , repeat = <NUM_LIT:0> , <EOL> timer = FakeTimer ( ) ) <EOL> self . assertEqual ( delta_times , [ ] ) <EOL> def test_repeat_function_zero_iters ( self ) : <EOL> delta_times = timeit . repeat ( self . fake_stmt , self . fake_setup , number = <NUM_LIT:0> , <EOL> timer = FakeTimer ( ) ) <EOL> self . assertEqual ( delta_times , DEFAULT_REPEAT * [ <NUM_LIT:0.0> ] ) <EOL> def assert_exc_string ( self , exc_string , expected_exc_name ) : <EOL> exc_lines = exc_string . splitlines ( ) <EOL> self . assertGreater ( len ( exc_lines ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( exc_lines [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( exc_lines [ - <NUM_LIT:1> ] . startswith ( expected_exc_name ) ) <EOL> def test_print_exc ( self ) : <EOL> s = io . StringIO ( ) <EOL> t = timeit . Timer ( "<STR_LIT>" ) <EOL> try : <EOL> t . timeit ( ) <EOL> except : <EOL> t . print_exc ( s ) <EOL> self . assert_exc_string ( s . getvalue ( ) , '<STR_LIT>' ) <EOL> MAIN_DEFAULT_OUTPUT = "<STR_LIT>" <EOL> def run_main ( self , seconds_per_increment = <NUM_LIT:1.0> , switches = None , timer = None ) : <EOL> if timer is None : <EOL> timer = FakeTimer ( seconds_per_increment = seconds_per_increment ) <EOL> if switches is None : <EOL> args = [ ] <EOL> else : <EOL> args = switches [ : ] <EOL> args . append ( self . fake_stmt ) <EOL> orig_sys_path = sys . path [ : ] <EOL> with captured_stdout ( ) as s : <EOL> timeit . main ( args = args , _wrap_timer = timer . wrap_timer ) <EOL> sys . path [ : ] = orig_sys_path [ : ] <EOL> return s . getvalue ( ) <EOL> def test_main_bad_switch ( self ) : <EOL> s = self . run_main ( switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , dedent ( """<STR_LIT>""" ) ) <EOL> def test_main_seconds ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> def test_main_milliseconds ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> def test_main_microseconds ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> , switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> def test_main_fixed_iters ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> , switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> def test_main_setup ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> , <EOL> switches = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , "<STR_LIT>" * <NUM_LIT:3> + <EOL> "<STR_LIT>" ) <EOL> def test_main_fixed_reps ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> , switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> def test_main_negative_reps ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> , switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> @ unittest . skipIf ( sys . flags . optimize >= <NUM_LIT:2> , "<STR_LIT>" ) <EOL> def test_main_help ( self ) : <EOL> s = self . run_main ( switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , timeit . __doc__ + '<STR_LIT:U+0020>' ) <EOL> def test_main_using_time ( self ) : <EOL> fake_timer = FakeTimer ( ) <EOL> s = self . run_main ( switches = [ '<STR_LIT>' ] , timer = fake_timer ) <EOL> self . assertEqual ( s , self . MAIN_DEFAULT_OUTPUT ) <EOL> self . assertIs ( fake_timer . saved_timer , time . time ) <EOL> def test_main_using_clock ( self ) : <EOL> fake_timer = FakeTimer ( ) <EOL> s = self . run_main ( switches = [ '<STR_LIT:-c>' ] , timer = fake_timer ) <EOL> self . assertEqual ( s , self . MAIN_DEFAULT_OUTPUT ) <EOL> self . assertIs ( fake_timer . saved_timer , time . clock ) <EOL> def test_main_verbose ( self ) : <EOL> s = self . run_main ( switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , dedent ( """<STR_LIT>""" ) ) <EOL> def test_main_very_verbose ( self ) : <EOL> s = self . run_main ( seconds_per_increment = <NUM_LIT> , switches = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s , dedent ( """<STR_LIT>""" ) ) <EOL> def test_main_exception ( self ) : <EOL> with captured_stderr ( ) as error_stringio : <EOL> s = self . run_main ( switches = [ '<STR_LIT>' ] ) <EOL> self . assert_exc_string ( error_stringio . getvalue ( ) , '<STR_LIT>' ) <EOL> def test_main_exception_fixed_reps ( self ) : <EOL> with captured_stderr ( ) as error_stringio : <EOL> s = self . run_main ( switches = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assert_exc_string ( error_stringio . getvalue ( ) , '<STR_LIT>' ) <EOL> def test_main ( ) : <EOL> run_unittest ( TestTimeit ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
