def test_RegisterClock(self): TestStack.push(Timer.CLOCKS) Timer.register_clock('constant_time', constant_time) t = Timer() t.clock_name = 'constant_time' t.time() self.assertEqual(t.clock_name, 'constant_time') self.assertAlmostEqual(t.seconds, 1.) self.assertAlmostEqual(t.minutes, 1. / 60.) Timer.CLOCKS = TestStack.pop()
def test_Compatibility(self): TestStack.push(Timer.CLOCKS) Timer.register_clock('constant_time', constant_time) t = Timer('timer1', clock_name='clock') # Invalid for value in [ 0, 0., 'clock', time, Timer(clock_name='constant_time'), Timer(clock_name='time') ]: with self.subTest(value=value): self.assertFalse(t.is_compatible(value)) # Valid self.assertTrue(t.is_compatible(Timer(clock_name='clock'))) Timer.CLOCKS = TestStack.pop()
def test_GetInfo(self): TestStack.push(Timer.CLOCKS) # Timer with clock supported by time.get_clock_info() t = Timer() info = t.get_info() self.assertIsInstance(info, types.SimpleNamespace) for value in info.__dict__.values(): with self.subTest(value=value): self.assertIsNotNone(value) # Timer with custom function, does not supports time.get_clock_info() Timer.register_clock('constant_time', constant_time) t.clock_name = 'constant_time' info = t.get_info() self.assertIsInstance(info, types.SimpleNamespace) self.assertIsNotNone(info.implementation) self.assertIsNone(info.adjustable) self.assertIsNone(info.monotonic) self.assertIsNone(info.resolution) Timer.CLOCKS = TestStack.pop()
def test_RegisterClock(self): TestStack.push(Timer.CLOCKS) # Invalid key, valid value for keyval in [[1, 'clock'], [1., 'clock']]: with self.subTest(keyval=keyval): with self.assertRaises(KeyError): Timer.register_clock(*keyval) # Valid key, invalid value for keyval in [['clock', 1], ['clock', 1.], ['clock', 'clock']]: with self.subTest(keyval=keyval): with self.assertRaises(ValueError): Timer.register_clock(*keyval) # Valid Timer.register_clock('clock2', time.clock) self.assertTrue({'clock2': time.clock}.items() <= Timer.CLOCKS.items()) Timer.CLOCKS = TestStack.pop()