summaryrefslogtreecommitdiff
path: root/Lib/test/test_timeit.py
diff options
context:
space:
mode:
authorMariatta Wijaya <mariatta.wijaya@gmail.com>2017-02-06 20:16:58 -0800
committerMariatta Wijaya <mariatta.wijaya@gmail.com>2017-02-06 20:16:58 -0800
commitda79bcf8ac7ae72218ab023e1ed54390bc1a3a27 (patch)
tree74845e2dbd9521d9748b9c32f1922f4123083bf3 /Lib/test/test_timeit.py
parente3c7e835bdfc97750eb9b7fc0ad2493108c2d438 (diff)
parent1fe806ac56f8b83694d24ab604eb695d00bc8497 (diff)
downloadcpython-da79bcf8ac7ae72218ab023e1ed54390bc1a3a27.tar.gz
Issue #29371: merge with 3.5
Diffstat (limited to 'Lib/test/test_timeit.py')
-rw-r--r--Lib/test/test_timeit.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index 2db3c1bed2..1a95e2979c 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -354,6 +354,28 @@ class TestTimeit(unittest.TestCase):
s = self.run_main(switches=['-n1', '1/0'])
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
+ def autorange(self, callback=None):
+ timer = FakeTimer(seconds_per_increment=0.001)
+ t = timeit.Timer(stmt=self.fake_stmt, setup=self.fake_setup, timer=timer)
+ return t.autorange(callback)
+
+ def test_autorange(self):
+ num_loops, time_taken = self.autorange()
+ self.assertEqual(num_loops, 1000)
+ self.assertEqual(time_taken, 1.0)
+
+ def test_autorange_with_callback(self):
+ def callback(a, b):
+ print("{} {:.3f}".format(a, b))
+ with captured_stdout() as s:
+ num_loops, time_taken = self.autorange(callback)
+ self.assertEqual(num_loops, 1000)
+ self.assertEqual(time_taken, 1.0)
+ expected = ('10 0.010\n'
+ '100 0.100\n'
+ '1000 1.000\n')
+ self.assertEqual(s.getvalue(), expected)
+
if __name__ == '__main__':
unittest.main()