def test_abs(self): """This tests that negative time offsets can be converted to positive ones using abs.""" tests_ts = [(abs(TimeOffset(10, 1)), TimeOffset(10, 1)), (abs(TimeOffset(10, 1, -1)), TimeOffset(10, 1))] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_repr(self): """This tests that the repr function turns time offsets into second:nanosecond pairs.""" tests_ts = [ (repr(TimeOffset(10, 1)), "10:1"), (repr(TimeOffset(10, 1, -1)), "-10:1"), ] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_from_timeoffset(self): """This tests that TimeOffsets can be created with a variety of values.""" tests_ts = [ (TimeOffset.from_timeoffset(TimeOffset(0, 0)), TimeOffset(0, 0)), (TimeOffset.from_timeoffset(TimeOffset(1001, 0)), TimeOffset(1001, 0)), (TimeOffset.from_timeoffset(TimeOffset(1001, 1001)), TimeOffset(1001, 1001)), ] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_compare(self): """This tests comparison of timestamps.""" tests_ts = [(Timestamp(1, 2) == Timestamp(1, 2), True), (Timestamp(1, 2) != Timestamp(1, 3), True), (Timestamp(1, 0) < Timestamp(1, 2), True), (Timestamp(1, 2) <= Timestamp(1, 2), True), (Timestamp(2, 0) > Timestamp(1, 0), True), (Timestamp(2, 0) >= Timestamp(2, 0), True), (Timestamp(2, 0) < Timestamp(1, 0), False), (Timestamp(2, 0) == Timestamp(3, 0), False), (Timestamp(2, 0) == 2, True), (Timestamp(2, 0) > 1, True), (Timestamp(2, 0) < 3, True), (TimeOffset(2, 0) < 3, True), (TimeOffset(1, 0, 1) < TimeOffset(1, 0, -1), False)] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_from_sec_frac(self): """This tests that timeoffsets can be instantiated from fractional second values.""" tests_ts = [ (TimeOffset.from_sec_frac("1.000000001"), TimeOffset(1, 1)), (TimeOffset.from_sec_frac("-1.000000001"), TimeOffset(1, 1, sign=-1)), (TimeOffset.from_sec_frac("1.000001POTATO"), TimeOffset(1, 1000)), (TimeOffset.from_sec_frac("1"), TimeOffset(1, 0)), ] for t in tests_ts: self.assertEqual(t[0], t[1]) bad_params = [ ("0.0.1", ), ] for params in bad_params: with self.assertRaises(TsValueError): TimeOffset.from_sec_frac(*params)
def test_average(self): """This tests that time offsets can be averaged.""" toff1 = TimeOffset(11, 976) toff2 = TimeOffset(21, 51) toff_avg = (toff1 * 49 + toff2) / 50 avg = int((toff1.to_nanosec() * 49 + toff2.to_nanosec()) / 50) self.assertEqual(avg, toff_avg.to_nanosec())
def test_from_nanosec(self): """This tests that time offsets can be created from nanosecond values.""" tests_ts = [(TimeOffset.from_nanosec(1), TimeOffset(0, 1)), (TimeOffset.from_nanosec(1000000000), TimeOffset(1, 0)), (TimeOffset.from_nanosec(-1), TimeOffset(0, 1, -1))] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_compare(self): """This tests comparison of timestamps.""" with mock.patch('__builtin__.__import__', side_effect=import_mock("pyipputils.ipptimestamp", False)): reload(nmoscommon.timestamp) from nmoscommon.timestamp import Timestamp, TimeOffset, IPP_UTILS, TsValueError self.assertFalse(IPP_UTILS) tests_ts = [(Timestamp(1, 2) == Timestamp(1, 2), True), (Timestamp(1, 2) != Timestamp(1, 3), True), (Timestamp(1, 0) < Timestamp(1, 2), True), (Timestamp(1, 2) <= Timestamp(1, 2), True), (Timestamp(2, 0) > Timestamp(1, 0), True), (Timestamp(2, 0) >= Timestamp(2, 0), True), (Timestamp(2, 0) < Timestamp(1, 0), False), (Timestamp(2, 0) == Timestamp(3, 0), False), (Timestamp(2, 0) == 2, True), (Timestamp(2, 0) > 1, True), (Timestamp(2, 0) < 3, True), (TimeOffset(2, 0) < 3, True), (TimeOffset(1, 0, 1) < TimeOffset(1, 0, -1), False)] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_from_count(self): """This tests that time offsets can be created from counts at a specified frequency.""" tests_ts = [(TimeOffset.from_count(1, 50, 1), TimeOffset(0, 20000000)), (TimeOffset.from_count(75, 50, 1), TimeOffset(1, 500000000)), (TimeOffset.from_count(-75, 50, 1), TimeOffset(1, 500000000, -1))] for t in tests_ts: self.assertEqual(t[0], t[1]) bad_params = [(1, 0, 1), (1, 1, 0)] for params in bad_params: with self.assertRaises(TsValueError): TimeOffset.from_count(*params)
def test_set_value(self): """This tests that time offsets can have their value set.""" tests_ts = [(TimeOffset(0, 0), TimeOffset(0, 1), (0, 1)), (TimeOffset(0, 0), TimeOffset(1, 0), (1, 0)), (TimeOffset(0, 0), TimeOffset(0, 1, -1), (0, 1, -1))] for t in tests_ts: t[0].set_value(*t[2]) self.assertEqual(t[0], t[1])
def test_from_sec_nsec(self): """This tests that time offsets can be created from second:nanosecond pairs.""" tests_ts = [ (TimeOffset.from_sec_nsec("1:1"), TimeOffset(1, 1)), (TimeOffset.from_sec_nsec("-1:1"), TimeOffset(1, 1, sign=-1)), (TimeOffset.from_sec_nsec("1"), TimeOffset(1, 0)), ] for t in tests_ts: self.assertEqual(t[0], t[1]) bad_params = [ ("0:0:1", ), ] for params in bad_params: with self.assertRaises(TsValueError): TimeOffset.from_sec_nsec(*params)
def test_convert_sec_nsec(self): """This tests that the conversion to and from TAI second:nanosecond pairs works as expected.""" tests_ts = [("0:0", TimeOffset(0, 0), "0:0"), ("0:1", TimeOffset(0, 1), "0:1"), ("-0:1", TimeOffset(0, 1, -1), "-0:1"), ("5", TimeOffset(5, 0), "5:0"), ("5:1", TimeOffset(5, 1), "5:1"), ("-5:1", TimeOffset(5, 1, -1), "-5:1"), ("5:999999999", TimeOffset(5, 999999999), "5:999999999")] for t in tests_ts: ts = TimeOffset.from_sec_nsec(t[0]) self.assertEqual(ts, t[1], msg="Called with {} {} {}".format( t[0], t[1], t[2])) ts_str = ts.to_sec_nsec() self.assertEqual(ts_str, t[2], msg="Called with {} {} {}".format( t[0], t[1], t[2]))
def test_interval_frac(self): """This tests that TimeOffsets can be converted to interval fractions.""" tests_ts = [(TimeOffset.get_interval_fraction(50, 1, 1), TimeOffset(0, 20000000)), (TimeOffset.get_interval_fraction(50, 1, 2), TimeOffset(0, 10000000))] for t in tests_ts: self.assertEqual(t[0], t[1]) bad_params = [(0, 1, 1), (50, 0, 1), (50, 1, 0)] for params in bad_params: with self.assertRaises(TsValueError): TimeOffset.get_interval_fraction(*params)
def test_multdiv(self): """This tests multiplication and division on timestamps.""" with mock.patch('__builtin__.__import__', side_effect=import_mock("pyipputils.ipptimestamp", False)): reload(nmoscommon.timestamp) from nmoscommon.timestamp import Timestamp, TimeOffset, IPP_UTILS, TsValueError self.assertFalse(IPP_UTILS) tests_ts = [ (TimeOffset(10, 10) * 0, TimeOffset(0, 0)), (TimeOffset(10, 10) * 10, TimeOffset(100, 100)), (10 * TimeOffset(10, 10), TimeOffset(100, 100)), (TimeOffset(10, 10) * (-10), TimeOffset(100, 100, -1)), (TimeOffset(10, 10, -1) * 10, TimeOffset(100, 100, -1)), (TimeOffset(100, 100) / 10, TimeOffset(10, 10)), (TimeOffset(100, 100) / -10, TimeOffset(10, 10, -1)), (TimeOffset(100, 100, -1) / 10, TimeOffset(10, 10, -1)), (TimeOffset(281474976710654, 0) / 281474976710655, TimeOffset(0, 999999999)), (Timestamp(100, 100) / 10, Timestamp(10, 10)), (Timestamp(10, 10) * 10, Timestamp(100, 100)), (10 * Timestamp(10, 10), Timestamp(100, 100)), ] count = 0 for t in tests_ts: self.assertEqual(t[0], t[1]) self.assertEqual( isinstance(t[0], Timestamp), isinstance(t[1], Timestamp), "Failed on itteration {}, {}, {}".format( count, type(t[0]), type(t[1]))) count = count + 1
def test_addsub(self): """This tests addition and subtraction on timestamps.""" with mock.patch('__builtin__.__import__', side_effect=import_mock("pyipputils.ipptimestamp", False)): reload(nmoscommon.timestamp) from nmoscommon.timestamp import Timestamp, TimeOffset, IPP_UTILS, TsValueError self.assertFalse(IPP_UTILS) tests_ts = [ (Timestamp(10, 0) + TimeOffset(1, 2), Timestamp(11, 2)), (Timestamp(11, 2) - TimeOffset(1, 2), Timestamp(10, 0)), (TimeOffset(11, 2) - TimeOffset(1, 2), TimeOffset(10, 0)), (Timestamp(10, 0) - TimeOffset(11, 2), Timestamp(0, 0)), (TimeOffset(10, 0) - TimeOffset(11, 2), TimeOffset(1, 2, -1)), (TimeOffset(10, 0) - Timestamp(11, 2), TimeOffset(1, 2, -1)), (Timestamp(10, 0) - Timestamp(11, 2), TimeOffset(1, 2, -1)), (Timestamp(11, 2) - Timestamp(10, 0), TimeOffset(1, 2, 1)), ] for t in tests_ts: self.assertEqual(t[0], t[1]) self.assertEqual(isinstance(t[0], Timestamp), isinstance(t[1], Timestamp))
def test_iaddsub(self): """This tests integer addition and subtraction on timestamps.""" with mock.patch('__builtin__.__import__', side_effect=import_mock("pyipputils.ipptimestamp", False)): reload(nmoscommon.timestamp) from nmoscommon.timestamp import Timestamp, TimeOffset, IPP_UTILS, TsValueError self.assertFalse(IPP_UTILS) ts = Timestamp(10, 0) ts += TimeOffset(1, 2) self.assertEqual(ts, Timestamp(11, 2)) ts -= TimeOffset(1, 2) self.assertEqual(ts, Timestamp(10, 0)) ts -= TimeOffset(100, 5) self.assertTrue(ts.is_null()) ts = Timestamp(281474976710655, 999999999) ts += TimeOffset(0, 1) self.assertEqual(ts, Timestamp(281474976710655, 999999999)) toff = TimeOffset(10, 0) toff -= TimeOffset(100, 0) self.assertEqual(toff, TimeOffset(90, 0, -1)) toff = TimeOffset(10, 0) toff -= TimeOffset(0, 1) self.assertEqual(toff, TimeOffset(9, 999999999)) toff = TimeOffset(10, 500000000) toff += TimeOffset(0, 500000000) self.assertEqual(toff, TimeOffset(11, 0)) toff = TimeOffset(10, 500000000, -1) toff -= TimeOffset(0, 500000000) self.assertEqual(toff, TimeOffset(11, 0, -1)) toff = TimeOffset(10, 0, -1) toff += TimeOffset(0, 500000000) self.assertEqual(toff, TimeOffset(9, 500000000, -1))
def test_multdiv(self): """This tests multiplication and division on timestamps.""" tests_ts = [ (TimeOffset(10, 10) * 0, TimeOffset(0, 0)), (TimeOffset(10, 10) * 10, TimeOffset(100, 100)), (10 * TimeOffset(10, 10), TimeOffset(100, 100)), (TimeOffset(10, 10) * (-10), TimeOffset(100, 100, -1)), (TimeOffset(10, 10, -1) * 10, TimeOffset(100, 100, -1)), (TimeOffset(100, 100) // 10, TimeOffset(10, 10)), (TimeOffset(100, 100) // -10, TimeOffset(10, 10, -1)), (TimeOffset(100, 100, -1) // 10, TimeOffset(10, 10, -1)), (TimeOffset(281474976710654, 0) // 281474976710655, TimeOffset(0, 999999999)), (Timestamp(100, 100) // 10, Timestamp(10, 10)), (Timestamp(10, 10) * 10, Timestamp(100, 100)), (10 * Timestamp(10, 10), Timestamp(100, 100)), ] count = 0 for t in tests_ts: self.assertEqual(t[0], t[1]) self.assertEqual( isinstance(t[0], Timestamp), isinstance(t[1], Timestamp), "Failed on itteration {}, {}, {}".format( count, type(t[0]), type(t[1]))) count = count + 1
def test_cast(self): """This tests that addition and subtraction of TimeOffsets and integers or floats works as expected.""" tests_ts = [ (TimeOffset(10, 1) + 1, TimeOffset(11, 1)), (TimeOffset(10, 1) - 1, TimeOffset(9, 1)), (TimeOffset(10, 1) + 1.5, TimeOffset(11, 500000001)), (TimeOffset(10, 1) - 1.5, TimeOffset(8, 500000001)), (TimeOffset(8, 500000000) == 8.5, True), (TimeOffset(8, 500000000) > 8, True), (TimeOffset(8, 500000000) < 8.6, True), (TimeOffset(8, 500000000) != 8.6, True), ] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_convert_sec_frac(self): """This tests that the conversion to and from TAI seconds with fractional parts works as expected.""" tests_ts = [("0.0", TimeOffset(0, 0), "0.0"), ("0.1", TimeOffset(0, 1000000000 // 10), "0.1"), ("-0.1", TimeOffset(0, 1000000000 // 10, -1), "-0.1"), ("5", TimeOffset(5, 0), "5.0"), ("5.1", TimeOffset(5, 1000000000 // 10), "5.1"), ("-5.1", TimeOffset(5, 1000000000 // 10, -1), "-5.1"), ("5.10000000", TimeOffset(5, 1000000000 // 10), "5.1"), ("5.123456789", TimeOffset(5, 123456789), "5.123456789"), ("5.000000001", TimeOffset(5, 1), "5.000000001"), ("5.0000000001", TimeOffset(5, 0), "5.0")] for t in tests_ts: ts = TimeOffset.from_sec_frac(t[0]) self.assertEqual(ts, t[1], msg="Called with {} {} {}".format( t[0], t[1], t[2])) ts_str = ts.to_sec_frac() self.assertEqual(ts_str, t[2], msg="Called with {} {} {}".format( t[0], t[1], t[2]))
def test_to_microsec(self): """This tests that time offsets can be converted to microsecond values.""" tests_ts = [ (TimeOffset(0, 1000).to_microsec(), 1), (TimeOffset(1, 1000000).to_microsec(), 1001000), (TimeOffset(1, 1000000, -1).to_microsec(), -1001000), (TimeOffset(100, 1499).to_microsec(), 100 * 1000000 + 1), # below .5 us (TimeOffset(100, 1500).to_microsec(), 100 * 1000000 + 2), # at .5 us (TimeOffset(100, 1501).to_microsec(), 100 * 1000000 + 2), # above .5 us (TimeOffset(100, 1499).to_microsec(TimeOffset.ROUND_UP), 100 * 1000000 + 2), # below .5 us, round up (TimeOffset(100, 1500).to_microsec(TimeOffset.ROUND_UP), 100 * 1000000 + 2), # at .5 us, round up (TimeOffset(100, 1501).to_microsec(TimeOffset.ROUND_UP), 100 * 1000000 + 2), # above .5 us, round up (TimeOffset(100, 1499).to_microsec(TimeOffset.ROUND_DOWN), 100 * 1000000 + 1), # below .5 us, round down (TimeOffset(100, 1500).to_microsec(TimeOffset.ROUND_DOWN), 100 * 1000000 + 1), # at .5 us, round down (TimeOffset(100, 1501).to_microsec(TimeOffset.ROUND_DOWN), 100 * 1000000 + 1), # above .5 us, round down (TimeOffset(100, 1499, -1).to_microsec(TimeOffset.ROUND_DOWN), -100 * 1000000 - 2), # below .5 us, round down (TimeOffset(100, 1500, -1).to_microsec(TimeOffset.ROUND_DOWN), -100 * 1000000 - 2), # at .5 us, round down (TimeOffset(100, 1501, -1).to_microsec(TimeOffset.ROUND_DOWN), -100 * 1000000 - 2), # above .5 us, round down (TimeOffset(100, 1499, -1).to_microsec(TimeOffset.ROUND_UP), -100 * 1000000 - 1), # below .5 us, round up (TimeOffset(100, 1500, -1).to_microsec(TimeOffset.ROUND_UP), -100 * 1000000 - 1), # at .5 us, round up (TimeOffset(100, 1501, -1).to_microsec(TimeOffset.ROUND_UP), -100 * 1000000 - 1), # above .5 us, round up ] n = 0 for t in tests_ts: self.assertEqual(t[0], t[1], msg="failed on check %d, %d != %d" % (n, t[0], t[1])) n += 1
def test_to_count(self): """This tests that time offsets can be converted to counts at particular frequencies.""" tests_ts = [ (TimeOffset(0, 20000000).to_count(50, 1), 1), (TimeOffset(1, 500000000).to_count(50, 1), 75), (TimeOffset(1, 500000000, -1).to_count(50, 1), -75), (TimeOffset(100, 29999999).to_count(50, 1), 100 * 50 + 1), # below .5 frame (TimeOffset(100, 30000000).to_count(50, 1), 100 * 50 + 2), # at .5 frame (TimeOffset(100, 30000001).to_count(50, 1), 100 * 50 + 2), # above .5 frame (TimeOffset(100, 9999999).to_count(50, 1), 100 * 50), # below negative .5 frame (TimeOffset(100, 10000000).to_count(50, 1), 100 * 50 + 1), # at negative .5 frame (TimeOffset(100, 10000001).to_count(50, 1), 100 * 50 + 1), # above negative .5 frame (TimeOffset(100, 29999999).to_count(50, 1, TimeOffset.ROUND_UP), 100 * 50 + 2), # below .5 frame, round up (TimeOffset(100, 30000000).to_count(50, 1, TimeOffset.ROUND_DOWN), 100 * 50 + 1), # at .5 frame, round down (TimeOffset(100, 30000001).to_count(50, 1, TimeOffset.ROUND_DOWN), 100 * 50 + 1), # above .5 frame, round down (TimeOffset(100, 29999999, -1).to_count(50, 1, TimeOffset.ROUND_DOWN), -100 * 50 - 2), # below .5 frame, round up (TimeOffset(100, 30000000, -1).to_count(50, 1, TimeOffset.ROUND_UP), -100 * 50 - 1), # at .5 frame, round down (TimeOffset(100, 30000001, -1).to_count(50, 1, TimeOffset.ROUND_UP), -100 * 50 - 1), # above .5 frame, round down ] for t in tests_ts: self.assertEqual(t[0], t[1]) bad_params = [(1, 0), (0, 1)] for params in bad_params: with self.assertRaises(TsValueError): TimeOffset(0, 0).to_count(*params)
def test_addsub(self): """This tests addition and subtraction on timestamps.""" tests_ts = [ (Timestamp(10, 0) + TimeOffset(1, 2), Timestamp(11, 2)), (Timestamp(11, 2) - TimeOffset(1, 2), Timestamp(10, 0)), (TimeOffset(11, 2) - TimeOffset(1, 2), TimeOffset(10, 0)), (Timestamp(10, 0) - TimeOffset(11, 2), Timestamp(0, 0)), (TimeOffset(10, 0) - TimeOffset(11, 2), TimeOffset(1, 2, -1)), (TimeOffset(10, 0) - Timestamp(11, 2), TimeOffset(1, 2, -1)), (Timestamp(10, 0) - Timestamp(11, 2), TimeOffset(1, 2, -1)), (Timestamp(11, 2) - Timestamp(10, 0), TimeOffset(1, 2, 1)), ] for t in tests_ts: self.assertEqual(t[0], t[1]) self.assertEqual(isinstance(t[0], Timestamp), isinstance(t[1], Timestamp))
def test_hash(self): self.assertEqual(hash(TimeOffset(0, 0)), hash(TimeOffset(0, 0))) self.assertNotEqual(hash(TimeOffset(0, 0)), hash(TimeOffset(0, 1)))
def test_subsec(self): """This tests that TimeOffsets can be converted to millisec, nanosec, and microsec values.""" tests_ts = [ (TimeOffset(1, 1000000).to_millisec(), 1001), (TimeOffset(1, 1000).to_microsec(), 1000001), (TimeOffset(1, 1).to_nanosec(), 1000000001), (TimeOffset.from_millisec(1001), TimeOffset(1, 1000000)), (TimeOffset.from_microsec(1000001), TimeOffset(1, 1000)), (TimeOffset.from_nanosec(1000000001), TimeOffset(1, 1)), (TimeOffset(1, 500000).to_millisec(TimeOffset.ROUND_DOWN), 1000), (TimeOffset(1, 500000).to_millisec(TimeOffset.ROUND_NEAREST), 1001), (TimeOffset(1, 499999).to_millisec(TimeOffset.ROUND_NEAREST), 1000), (TimeOffset(1, 500000).to_millisec(TimeOffset.ROUND_UP), 1001), (TimeOffset(1, 500000, -1).to_millisec(TimeOffset.ROUND_DOWN), -1001), (TimeOffset(1, 500000, -1).to_millisec(TimeOffset.ROUND_NEAREST), -1001), (TimeOffset(1, 499999, -1).to_millisec(TimeOffset.ROUND_NEAREST), -1000), (TimeOffset(1, 500000, -1).to_millisec(TimeOffset.ROUND_UP), -1000) ] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_normalise(self): tests_ts = [ (TimeOffset(0, 0).normalise(30000, 1001), TimeOffset(0, 0)), (TimeOffset(1001, 0).normalise(30000, 1001), TimeOffset(1001, 0)), (TimeOffset(1001, 1001.0 / 30000 / 2 * 1000000000).normalise( 30000, 1001), TimeOffset(1001, 0)), (TimeOffset(1001, 1001.0 / 30000 / 2 * 1000000000 + 1).normalise( 30000, 1001), TimeOffset(1001, 1001.0 / 30000 * 1000000000)), (TimeOffset(1001, 1001.0 / 30000 / 2 * 1000000000, -1).normalise(30000, 1001), TimeOffset(1001, 0, -1)), (TimeOffset(1001, 1001.0 / 30000 / 2 * 1000000000 + 1, -1).normalise(30000, 1001), TimeOffset(1001, 1001.0 / 30000 * 1000000000, -1)) ] for t in tests_ts: self.assertEqual(t[0], t[1])
def test_iaddsub(self): """This tests integer addition and subtraction on timestamps.""" ts = Timestamp(10, 0) ts += TimeOffset(1, 2) self.assertEqual(ts, Timestamp(11, 2)) ts -= TimeOffset(1, 2) self.assertEqual(ts, Timestamp(10, 0)) ts -= TimeOffset(100, 5) self.assertTrue(ts.is_null()) ts = Timestamp(281474976710655, 999999999) ts += TimeOffset(0, 1) self.assertEqual(ts, Timestamp(281474976710655, 999999999)) toff = TimeOffset(10, 0) toff -= TimeOffset(100, 0) self.assertEqual(toff, TimeOffset(90, 0, -1)) toff = TimeOffset(10, 0) toff -= TimeOffset(0, 1) self.assertEqual(toff, TimeOffset(9, 999999999)) toff = TimeOffset(10, 500000000) toff += TimeOffset(0, 500000000) self.assertEqual(toff, TimeOffset(11, 0)) toff = TimeOffset(10, 500000000, -1) toff -= TimeOffset(0, 500000000) self.assertEqual(toff, TimeOffset(11, 0, -1)) toff = TimeOffset(10, 0, -1) toff += TimeOffset(0, 500000000) self.assertEqual(toff, TimeOffset(9, 500000000, -1))