def test_options(gs, gs_target, options, iterations=100): objective_fn = create_objective_fn(gs, gs_target, **options) with timed_block('Objective function'): for i in range(iterations): objective_fn(gs) print('({} iterations)'.format(iterations))
def test_stdout_output(self): with mock.patch('sys.stdout') as mock_out: with opt.timed_block('time'): pass call_count = mock_out.write.call_count self.assertEqual(call_count, 2, "unexpected number of lines written")
def test_time_dict_storage(self): timeDict = {} with opt.timed_block('time', timeDict): pass self.assertIn('time', timeDict) self.assertTrue(isinstance(timeDict['time'], numbers.Number), "time is not numeric") timeDict = defaultdict(list) with opt.timed_block('time', timeDict): pass self.assertIn('time', timeDict) self.assertTrue(isinstance(timeDict['time'], list), "time was not appended to list") self.assertTrue(isinstance(timeDict['time'][0], numbers.Number), "time is not numeric")
def test_pre_message(self): preMessage = "this is a pre-message!" with mock.patch('sys.stdout') as mock_out: with opt.timed_block('time', preMessage=preMessage): pass call_count = mock_out.write.call_count call_args_list = mock_out.write.call_args_list self.assertEqual(call_count, 4, "unexpected number of lines written") self.assertIn(preMessage, call_args_list[0][0], "pre-message not written first")
def test_timer(self): duration = 0.03 timeDict = {} with opt.timed_block('time', timeDict): sleep(duration) self.assertGreaterEqual(timeDict['time'], duration) tolerance = 0.01 # this should deliberately be large, for repeatability self.assertLessEqual( timeDict['time'], duration + tolerance, "timed block result is greater than {} seconds off".format( tolerance))