Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_timeit.TestTimeit Class Reference
Collaboration diagram for test.test_timeit.TestTimeit:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def tearDown
def test_reindent_empty
def test_reindent_single
def test_reindent_multi_empty
def test_reindent_multi
def test_timer_invalid_stmt
def test_timer_invalid_setup
def fake_callable_setup
def fake_callable_stmt
def timeit
def test_timeit_zero_iters
def test_timeit_few_iters
def test_timeit_callable_stmt
def test_timeit_callable_stmt_and_setup
def test_timeit_function_zero_iters
def repeat
def test_repeat_zero_reps
def test_repeat_zero_iters
def test_repeat_few_reps_and_iters
def test_repeat_callable_stmt
def test_repeat_callable_stmt_and_setup
def test_repeat_function_zero_reps
def test_repeat_function_zero_iters
def assert_exc_string
def test_print_exc
def run_main
def test_main_bad_switch
def test_main_seconds
def test_main_milliseconds
def test_main_microseconds
def test_main_fixed_iters
def test_main_setup
def test_main_fixed_reps
def test_main_negative_reps
def test_main_help
def test_main_using_time
def test_main_using_clock
def test_main_verbose
def test_main_very_verbose
def test_main_exception
def test_main_exception_fixed_reps

Public Attributes

 fake_timer

Static Public Attributes

string fake_setup = "import timeit; timeit._fake_timer.setup()"
string fake_stmt = "import timeit; timeit._fake_timer.inc()"
string MAIN_DEFAULT_OUTPUT = "10 loops, best of 3: 1 sec per loop\n"

Detailed Description

Definition at line 46 of file test_timeit.py.


Member Function Documentation

def test.test_timeit.TestTimeit.assert_exc_string (   self,
  exc_string,
  expected_exc_name 
)

Definition at line 184 of file test_timeit.py.

00184 
00185     def assert_exc_string(self, exc_string, expected_exc_name):
00186         exc_lines = exc_string.splitlines()
00187         self.assertGreater(len(exc_lines), 2)
00188         self.assertTrue(exc_lines[0].startswith('Traceback'))
00189         self.assertTrue(exc_lines[-1].startswith(expected_exc_name))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 83 of file test_timeit.py.

00083 
00084     def fake_callable_setup(self):
00085         self.fake_timer.setup()

Here is the caller graph for this function:

Definition at line 86 of file test_timeit.py.

00086 
00087     def fake_callable_stmt(self):
00088         self.fake_timer.inc()

Here is the caller graph for this function:

def test.test_timeit.TestTimeit.repeat (   self,
  stmt,
  setup,
  repeat = None,
  number = None 
)

Definition at line 130 of file test_timeit.py.

00130 
00131     def repeat(self, stmt, setup, repeat=None, number=None):
00132         self.fake_timer = FakeTimer()
00133         t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
00134         kwargs = {}
00135         if repeat is None:
00136             repeat = DEFAULT_REPEAT
00137         else:
00138             kwargs['repeat'] = repeat
00139         if number is None:
00140             number = DEFAULT_NUMBER
00141         else:
00142             kwargs['number'] = number
00143         delta_times = t.repeat(**kwargs)
00144         self.assertEqual(self.fake_timer.setup_calls, repeat)
00145         self.assertEqual(self.fake_timer.count, repeat * number)
00146         self.assertEqual(delta_times, repeat * [float(number)])

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_timeit.TestTimeit.run_main (   self,
  seconds_per_increment = 1.0,
  switches = None,
  timer = None 
)

Definition at line 201 of file test_timeit.py.

00201 
00202     def run_main(self, seconds_per_increment=1.0, switches=None, timer=None):
00203         if timer is None:
00204             timer = FakeTimer(seconds_per_increment=seconds_per_increment)
00205         if switches is None:
00206             args = []
00207         else:
00208             args = switches[:]
00209         args.append(self.fake_stmt)
00210         # timeit.main() modifies sys.path, so save and restore it.
00211         orig_sys_path = sys.path[:]
00212         with captured_stdout() as s:
00213             timeit.main(args=args, _wrap_timer=timer.wrap_timer)
00214         sys.path[:] = orig_sys_path[:]
00215         return s.getvalue()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 48 of file test_timeit.py.

00048 
00049     def tearDown(self):
00050         try:
00051             del timeit._fake_timer
00052         except AttributeError:
00053             pass

Here is the caller graph for this function:

Definition at line 216 of file test_timeit.py.

00216 
00217     def test_main_bad_switch(self):
00218         s = self.run_main(switches=['--bad-switch'])
00219         self.assertEqual(s, dedent("""\
00220             option --bad-switch not recognized
00221             use -h/--help for command line help
00222             """))

Here is the call graph for this function:

Definition at line 290 of file test_timeit.py.

00290 
00291     def test_main_exception(self):
00292         with captured_stderr() as error_stringio:
00293             s = self.run_main(switches=['1/0'])
00294         self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')

Here is the call graph for this function:

Definition at line 295 of file test_timeit.py.

00295 
00296     def test_main_exception_fixed_reps(self):
00297         with captured_stderr() as error_stringio:
00298             s = self.run_main(switches=['-n1', '1/0'])
00299         self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
00300 

Here is the call graph for this function:

Definition at line 235 of file test_timeit.py.

00235 
00236     def test_main_fixed_iters(self):
00237         s = self.run_main(seconds_per_increment=2.0, switches=['-n35'])
00238         self.assertEqual(s, "35 loops, best of 3: 2 sec per loop\n")

Here is the call graph for this function:

Definition at line 245 of file test_timeit.py.

00245 
00246     def test_main_fixed_reps(self):
00247         s = self.run_main(seconds_per_increment=60.0, switches=['-r9'])
00248         self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n")

Here is the call graph for this function:

Definition at line 253 of file test_timeit.py.

00253 
00254     def test_main_help(self):
00255         s = self.run_main(switches=['-h'])
00256         # Note: It's not clear that the trailing space was intended as part of
00257         # the help text, but since it's there, check for it.
00258         self.assertEqual(s, timeit.__doc__ + ' ')

Here is the call graph for this function:

Definition at line 231 of file test_timeit.py.

00231 
00232     def test_main_microseconds(self):
00233         s = self.run_main(seconds_per_increment=0.0000025, switches=['-n100'])
00234         self.assertEqual(s, "100 loops, best of 3: 2.5 usec per loop\n")

Here is the call graph for this function:

Definition at line 227 of file test_timeit.py.

00227 
00228     def test_main_milliseconds(self):
00229         s = self.run_main(seconds_per_increment=0.0055)
00230         self.assertEqual(s, "100 loops, best of 3: 5.5 msec per loop\n")

Here is the call graph for this function:

Definition at line 249 of file test_timeit.py.

00249 
00250     def test_main_negative_reps(self):
00251         s = self.run_main(seconds_per_increment=60.0, switches=['-r-5'])
00252         self.assertEqual(s, "10 loops, best of 1: 60 sec per loop\n")

Here is the call graph for this function:

Definition at line 223 of file test_timeit.py.

00223 
00224     def test_main_seconds(self):
00225         s = self.run_main(seconds_per_increment=5.5)
00226         self.assertEqual(s, "10 loops, best of 3: 5.5 sec per loop\n")

Here is the call graph for this function:

Definition at line 239 of file test_timeit.py.

00239 
00240     def test_main_setup(self):
00241         s = self.run_main(seconds_per_increment=2.0,
00242                 switches=['-n35', '-s', 'print("CustomSetup")'])
00243         self.assertEqual(s, "CustomSetup\n" * 3 +
00244                 "35 loops, best of 3: 2 sec per loop\n")

Here is the call graph for this function:

Definition at line 265 of file test_timeit.py.

00265 
00266     def test_main_using_clock(self):
00267         fake_timer = FakeTimer()
00268         s = self.run_main(switches=['-c'], timer=fake_timer)
00269         self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT)
00270         self.assertIs(fake_timer.saved_timer, time.clock)

Here is the call graph for this function:

Definition at line 259 of file test_timeit.py.

00259 
00260     def test_main_using_time(self):
00261         fake_timer = FakeTimer()
00262         s = self.run_main(switches=['-t'], timer=fake_timer)
00263         self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT)
00264         self.assertIs(fake_timer.saved_timer, time.time)

Here is the call graph for this function:

Definition at line 271 of file test_timeit.py.

00271 
00272     def test_main_verbose(self):
00273         s = self.run_main(switches=['-v'])
00274         self.assertEqual(s, dedent("""\
00275                 10 loops -> 10 secs
00276                 raw times: 10 10 10
00277                 10 loops, best of 3: 1 sec per loop
00278             """))

Here is the call graph for this function:

Definition at line 279 of file test_timeit.py.

00279 
00280     def test_main_very_verbose(self):
00281         s = self.run_main(seconds_per_increment=0.000050, switches=['-vv'])
00282         self.assertEqual(s, dedent("""\
00283                 10 loops -> 0.0005 secs
00284                 100 loops -> 0.005 secs
00285                 1000 loops -> 0.05 secs
00286                 10000 loops -> 0.5 secs
00287                 raw times: 0.5 0.5 0.5
00288                 10000 loops, best of 3: 50 usec per loop
00289             """))

Here is the call graph for this function:

Definition at line 190 of file test_timeit.py.

00190 
00191     def test_print_exc(self):
00192         s = io.StringIO()
00193         t = timeit.Timer("1/0")
00194         try:
00195             t.timeit()
00196         except:
00197             t.print_exc(s)
00198         self.assert_exc_string(s.getvalue(), 'ZeroDivisionError')

Here is the call graph for this function:

Definition at line 54 of file test_timeit.py.

00054 
00055     def test_reindent_empty(self):
00056         self.assertEqual(timeit.reindent("", 0), "")
00057         self.assertEqual(timeit.reindent("", 4), "")

Here is the call graph for this function:

Definition at line 66 of file test_timeit.py.

00066 
00067     def test_reindent_multi(self):
00068         self.assertEqual(timeit.reindent(
00069             "print()\npass\nbreak", 0),
00070             "print()\npass\nbreak")
00071         self.assertEqual(timeit.reindent(
00072             "print()\npass\nbreak", 4),
00073             "print()\n    pass\n    break")

Here is the call graph for this function:

Definition at line 62 of file test_timeit.py.

00062 
00063     def test_reindent_multi_empty(self):
00064         self.assertEqual(timeit.reindent("\n\n", 0), "\n\n")
00065         self.assertEqual(timeit.reindent("\n\n", 4), "\n    \n    ")

Here is the call graph for this function:

Definition at line 58 of file test_timeit.py.

00058 
00059     def test_reindent_single(self):
00060         self.assertEqual(timeit.reindent("pass", 0), "pass")
00061         self.assertEqual(timeit.reindent("pass", 4), "pass")

Here is the call graph for this function:

Definition at line 160 of file test_timeit.py.

00160 
00161     def test_repeat_callable_stmt(self):
00162         self.repeat(self.fake_callable_stmt, self.fake_setup,
00163                 repeat=3, number=5)

Here is the call graph for this function:

Definition at line 164 of file test_timeit.py.

00164 
00165     def test_repeat_callable_stmt_and_setup(self):
00166         self.repeat(self.fake_callable_stmt, self.fake_callable_setup,
00167                 repeat=3, number=5)

Here is the call graph for this function:

Definition at line 157 of file test_timeit.py.

00157 
00158     def test_repeat_few_reps_and_iters(self):
00159         self.repeat(self.fake_stmt, self.fake_setup, repeat=3, number=5)

Here is the call graph for this function:

Definition at line 179 of file test_timeit.py.

00179 
00180     def test_repeat_function_zero_iters(self):
00181         delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, number=0,
00182                 timer=FakeTimer())
00183         self.assertEqual(delta_times, DEFAULT_REPEAT * [0.0])

Here is the call graph for this function:

Definition at line 174 of file test_timeit.py.

00174 
00175     def test_repeat_function_zero_reps(self):
00176         delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, repeat=0,
00177                 timer=FakeTimer())
00178         self.assertEqual(delta_times, [])

Here is the call graph for this function:

Definition at line 154 of file test_timeit.py.

00154 
00155     def test_repeat_zero_iters(self):
00156         self.repeat(self.fake_stmt, self.fake_setup, number=0)

Here is the call graph for this function:

Definition at line 151 of file test_timeit.py.

00151 
00152     def test_repeat_zero_reps(self):
00153         self.repeat(self.fake_stmt, self.fake_setup, repeat=0)

Here is the call graph for this function:

Definition at line 112 of file test_timeit.py.

00112 
00113     def test_timeit_callable_stmt(self):
00114         self.timeit(self.fake_callable_stmt, self.fake_setup, number=3)

Here is the call graph for this function:

Definition at line 115 of file test_timeit.py.

00115 
00116     def test_timeit_callable_stmt_and_setup(self):
00117         self.timeit(self.fake_callable_stmt,
00118                 self.fake_callable_setup, number=3)

Here is the call graph for this function:

Definition at line 109 of file test_timeit.py.

00109 
00110     def test_timeit_few_iters(self):
00111         self.timeit(self.fake_stmt, self.fake_setup, number=3)

Here is the call graph for this function:

Definition at line 125 of file test_timeit.py.

00125 
00126     def test_timeit_function_zero_iters(self):
00127         delta_time = timeit.timeit(self.fake_stmt, self.fake_setup, number=0,
00128                 timer=FakeTimer())
00129         self.assertEqual(delta_time, 0)

Here is the call graph for this function:

Definition at line 106 of file test_timeit.py.

00106 
00107     def test_timeit_zero_iters(self):
00108         self.timeit(self.fake_stmt, self.fake_setup, number=0)

Here is the call graph for this function:

Definition at line 77 of file test_timeit.py.

00077 
00078     def test_timer_invalid_setup(self):
00079         self.assertRaises(ValueError, timeit.Timer, setup=None)

Here is the call graph for this function:

Definition at line 74 of file test_timeit.py.

00074 
00075     def test_timer_invalid_stmt(self):
00076         self.assertRaises(ValueError, timeit.Timer, stmt=None)

Here is the call graph for this function:

def test.test_timeit.TestTimeit.timeit (   self,
  stmt,
  setup,
  number = None 
)

Definition at line 89 of file test_timeit.py.

00089 
00090     def timeit(self, stmt, setup, number=None):
00091         self.fake_timer = FakeTimer()
00092         t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
00093         kwargs = {}
00094         if number is None:
00095             number = DEFAULT_NUMBER
00096         else:
00097             kwargs['number'] = number
00098         delta_time = t.timeit(**kwargs)
00099         self.assertEqual(self.fake_timer.setup_calls, 1)
00100         self.assertEqual(self.fake_timer.count, number)
00101         self.assertEqual(delta_time, number)

Here is the caller graph for this function:


Member Data Documentation

string test.test_timeit.TestTimeit.fake_setup = "import timeit; timeit._fake_timer.setup()" [static]

Definition at line 80 of file test_timeit.py.

string test.test_timeit.TestTimeit.fake_stmt = "import timeit; timeit._fake_timer.inc()" [static]

Definition at line 81 of file test_timeit.py.

Definition at line 90 of file test_timeit.py.

string test.test_timeit.TestTimeit.MAIN_DEFAULT_OUTPUT = "10 loops, best of 3: 1 sec per loop\n" [static]

Definition at line 199 of file test_timeit.py.


The documentation for this class was generated from the following file: