lib: strictly type function return status enumerations
[babeltrace.git] / tests / bindings / python / bt2 / test_clock_class.py
index 436b2b9ec7c0c03d936356404a238f1588edf46c..1f3959127208b274f23b0a3615ee05ac9f436631 100644 (file)
@@ -1,7 +1,26 @@
+#
+# Copyright (C) 2019 EfficiOS Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; only version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+
 import unittest
 import uuid
 import copy
 import bt2
+from utils import run_in_component_init
 
 
 class ClockClassOffsetTestCase(unittest.TestCase):
@@ -48,252 +67,234 @@ class ClockClassOffsetTestCase(unittest.TestCase):
 
 
 class ClockClassTestCase(unittest.TestCase):
-    def setUp(self):
-        self._cc = bt2.ClockClass('salut')
+    def assertRaisesInComponentInit(self, expected_exc_type, user_code):
+        def f(comp_self):
+            try:
+                user_code(comp_self)
+            except Exception as exc:
+                return type(exc)
+
+        exc_type = run_in_component_init(f)
+        self.assertIsNotNone(exc_type)
+        self.assertEqual(exc_type, expected_exc_type)
 
     def test_create_default(self):
-        self.assertEqual(self._cc.name, 'salut')
+        cc = run_in_component_init(lambda comp_self: comp_self._create_clock_class())
 
-    def test_create_invalid_no_name(self):
-        with self.assertRaises(TypeError):
-            bt2.ClockClass()
-
-    def test_create_full(self):
-        my_uuid = uuid.uuid1()
-        cc = bt2.ClockClass(name='name', description='some description',
-                            frequency=1001, precision=176,
-                            offset=bt2.ClockClassOffset(45, 3003),
-                            is_absolute=True, uuid=my_uuid)
-        self.assertEqual(cc.name, 'name')
-        self.assertEqual(cc.description, 'some description')
-        self.assertEqual(cc.frequency, 1001)
-        self.assertEqual(cc.precision, 176)
-        self.assertEqual(cc.offset, bt2.ClockClassOffset(45, 3003))
-        self.assertEqual(cc.is_absolute, True)
-        self.assertEqual(cc.uuid, copy.deepcopy(my_uuid))
-
-    def test_assign_name(self):
-        self._cc.name = 'the_clock'
-        self.assertEqual(self._cc.name, 'the_clock')
-
-    def test_assign_invalid_name(self):
-        with self.assertRaises(TypeError):
-            self._cc.name = 23
+        self.assertIsNone(cc.name)
+        self.assertEqual(cc.frequency, 1000000000)
+        self.assertIsNone(cc.description)
+        self.assertEqual(cc.precision, 0)
+        self.assertEqual(cc.offset, bt2.ClockClassOffset())
+        self.assertTrue(cc.origin_is_unix_epoch)
+        self.assertIsNone(cc.uuid)
 
-    def test_assign_description(self):
-        self._cc.description = 'hi people'
-        self.assertEqual(self._cc.description, 'hi people')
+    def test_create_name(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(name='the_clock')
 
-    def test_assign_invalid_description(self):
-        with self.assertRaises(TypeError):
-            self._cc.description = 23
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.name, 'the_clock')
 
-    def test_assign_frequency(self):
-        self._cc.frequency = 987654321
-        self.assertEqual(self._cc.frequency, 987654321)
+    def test_create_invalid_name(self):
+        def f(comp_self):
+            comp_self._create_clock_class(name=23)
 
-    def test_assign_invalid_frequency(self):
-        with self.assertRaises(TypeError):
-            self._cc.frequency = 'lel'
+        self.assertRaisesInComponentInit(TypeError, f)
 
-    def test_assign_precision(self):
-        self._cc.precision = 12
-        self.assertEqual(self._cc.precision, 12)
+    def test_create_description(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(description='hi people')
 
-    def test_assign_invalid_precision(self):
-        with self.assertRaises(TypeError):
-            self._cc.precision = 'lel'
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.description, 'hi people')
 
-    def test_assign_offset(self):
-        self._cc.offset = bt2.ClockClassOffset(12, 56)
-        self.assertEqual(self._cc.offset, bt2.ClockClassOffset(12, 56))
+    def test_create_invalid_description(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(description=23)
 
-    def test_assign_invalid_offset(self):
-        with self.assertRaises(TypeError):
-            self._cc.offset = object()
+        self.assertRaisesInComponentInit(TypeError, f)
 
-    def test_assign_absolute(self):
-        self._cc.is_absolute = True
-        self.assertTrue(self._cc.is_absolute)
+    def test_create_frequency(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(frequency=987654321)
 
-    def test_assign_invalid_absolute(self):
-        with self.assertRaises(TypeError):
-            self._cc.is_absolute = 23
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.frequency, 987654321)
 
-    def test_assign_uuid(self):
-        the_uuid = uuid.uuid1()
-        self._cc.uuid = the_uuid
-        self.assertEqual(self._cc.uuid, the_uuid)
+    def test_create_invalid_frequency(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(frequency='lel')
 
-    def test_assign_invalid_uuid(self):
-        with self.assertRaises(TypeError):
-            self._cc.uuid = object()
+        self.assertRaisesInComponentInit(TypeError, f)
 
-    def test_create_clock_value(self):
-        cv = self._cc.create_clock_value(756)
-        self.assertEqual(cv.clock_class.addr, self._cc.addr)
+    def test_create_precision(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(precision=12)
 
-    def _test_copy(self, cpy):
-        self.assertIsNot(cpy, self._cc)
-        self.assertNotEqual(cpy.addr, self._cc.addr)
-        self.assertEqual(cpy, self._cc)
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.precision, 12)
 
-    def test_copy(self):
-        cpy = copy.copy(self._cc)
-        self._test_copy(cpy)
+    def test_create_invalid_precision(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(precision='lel')
 
-    def test_deepcopy(self):
-        cpy = copy.deepcopy(self._cc)
-        self._test_copy(cpy)
+        self.assertRaisesInComponentInit(TypeError, f)
 
-    def test_eq(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        self.assertEqual(cc1, cc2)
-
-    def test_ne_name(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='mane', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        self.assertNotEqual(cc1, cc2)
-
-    def test_ne_description(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='name', description='some descripti2',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        self.assertNotEqual(cc1, cc2)
-
-    def test_ne_frequency(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1003, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        self.assertNotEqual(cc1, cc2)
-
-    def test_ne_precision(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=171,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        self.assertNotEqual(cc1, cc2)
-
-    def test_ne_offset(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3001),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        self.assertNotEqual(cc1, cc2)
-
-    def test_ne_absolute(self):
-        my_uuid = uuid.uuid1()
-        cc1 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=my_uuid)
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=False, uuid=my_uuid)
-        self.assertNotEqual(cc1, cc2)
-
-    def test_ne_uuid(self):
-        cc1 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=uuid.uuid1())
-        cc2 = bt2.ClockClass(name='name', description='some description',
-                             frequency=1001, precision=176,
-                             offset=bt2.ClockClassOffset(45, 3003),
-                             is_absolute=True, uuid=uuid.uuid1())
-        self.assertNotEqual(cc1, cc2)
+    def test_create_offset(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(offset=bt2.ClockClassOffset(12, 56))
 
-    def test_eq_invalid(self):
-        self.assertFalse(self._cc == 23)
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.offset, bt2.ClockClassOffset(12, 56))
+
+    def test_create_invalid_offset(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(offset=object())
+
+        self.assertRaisesInComponentInit(TypeError, f)
+
+    def test_create_origin_is_unix_epoch(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(origin_is_unix_epoch=False)
 
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.origin_is_unix_epoch, False)
 
-class ClockClassValueTestCase(unittest.TestCase):
+    def test_create_invalid_origin_is_unix_epoch(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(origin_is_unix_epoch=23)
+
+        self.assertRaisesInComponentInit(TypeError, f)
+
+    def test_cycles_to_ns_from_origin(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(frequency=10**8, origin_is_unix_epoch=True)
+
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.cycles_to_ns_from_origin(112), 1120)
+
+    def test_cycles_to_ns_from_origin_overflow(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(frequency=1000)
+
+        cc = run_in_component_init(f)
+        with self.assertRaises(bt2.OverflowError):
+            cc.cycles_to_ns_from_origin(2**63)
+
+    def test_create_uuid(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(uuid=uuid.UUID('b43372c32ef0be28444dfc1c5cdafd33'))
+
+        cc = run_in_component_init(f)
+        self.assertEqual(cc.uuid, uuid.UUID('b43372c32ef0be28444dfc1c5cdafd33'))
+
+    def test_create_invalid_uuid(self):
+        def f(comp_self):
+            return comp_self._create_clock_class(uuid=23)
+
+        self.assertRaisesInComponentInit(TypeError, f)
+
+
+class ClockSnapshotTestCase(unittest.TestCase):
     def setUp(self):
-        self._cc = bt2.ClockClass('salut')
-        self._cv = self._cc.create_clock_value(123)
+        def f(comp_self):
+            cc = comp_self._create_clock_class(1000, 'my_cc',
+                                               offset=bt2.ClockClassOffset(45, 354))
+            tc = comp_self._create_trace_class()
+
+            return (cc, tc)
+
+        _cc, _tc = run_in_component_init(f)
+        _trace = _tc()
+        _sc = _tc.create_stream_class(default_clock_class=_cc,
+                                      packets_have_beginning_default_clock_snapshot=True,
+                                      packets_have_end_default_clock_snapshot=True)
+        _ec = _sc.create_event_class(name='salut')
+        _stream = _trace.create_stream(_sc)
+        _packet = _stream.create_packet()
+        self._packet = _packet
+        self._stream = _stream
+        self._ec = _ec
+        self._cc = _cc
+
+        class MyIter(bt2._UserMessageIterator):
+            def __init__(self, self_port_output):
+                self._at = 0
+
+            def __next__(self):
+                if self._at == 0:
+                    notif = self._create_stream_beginning_message(_stream)
+                elif self._at == 1:
+                    notif = self._create_packet_beginning_message(_packet, 100)
+                elif self._at == 2:
+                    notif = self._create_event_message(_ec, _packet, 123)
+                elif self._at == 3:
+                    notif = self._create_event_message(_ec, _packet, 2**63)
+                elif self._at == 4:
+                    notif = self._create_packet_end_message(_packet)
+                elif self._at == 5:
+                    notif = self._create_stream_end_message(_stream)
+                else:
+                    raise bt2.Stop
+
+                self._at += 1
+                return notif
+
+        class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
+            def __init__(self, params):
+                self._add_output_port('out')
+
+        self._graph = bt2.Graph()
+        self._src_comp = self._graph.add_component(MySrc, 'my_source')
+        self._msg_iter = self._graph.create_output_port_message_iterator(
+            self._src_comp.output_ports['out'])
+
+        for i, msg in enumerate(self._msg_iter):
+            if i == 2:
+                self._msg = msg
+            elif i == 3:
+                self._msg_clock_overflow = msg
+                break
+
+    def tearDown(self):
+        del self._cc
+        del self._msg
 
     def test_create_default(self):
-        self.assertEqual(self._cv.clock_class.addr, self._cc.addr)
-        self.assertEqual(self._cv.cycles, 123)
-
-    def test_create_invalid_cycles_type(self):
-        with self.assertRaises(TypeError):
-            self._cc.create_clock_value('yes')
+        self.assertEqual(
+            self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr)
+        self.assertEqual(self._msg.default_clock_snapshot.value, 123)
 
-    def test_ns_from_epoch(self):
-        self._cv.clock_class.frequency = 1000
-        self._cv.clock_class.offset = bt2.ClockClassOffset(45, 354)
-        s_from_epoch = 45 + ((354 + 123) / 1000)
-        ns_from_epoch = int(s_from_epoch * 1e9)
-        self.assertEqual(self._cv.ns_from_epoch, ns_from_epoch)
+    def test_clock_class(self):
+        self.assertEqual(
+            self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr)
 
-    def test_eq(self):
-        cv1 = self._cc.create_clock_value(123)
-        cv2 = self._cc.create_clock_value(123)
-        self.assertEqual(cv1, cv2)
+    def test_ns_from_origin(self):
+        s_from_origin = 45 + ((354 + 123) / 1000)
+        ns_from_origin = int(s_from_origin * 1e9)
+        self.assertEqual(
+            self._msg.default_clock_snapshot.ns_from_origin, ns_from_origin)
 
-    def test_ne_clock_class(self):
-        cc1 = bt2.ClockClass('yes')
-        cc2 = bt2.ClockClass('yes')
-        cv1 = cc1.create_clock_value(123)
-        cv2 = cc2.create_clock_value(123)
-        self.assertNotEqual(cv1, cv2)
+    def test_ns_from_origin_overflow(self):
+        with self.assertRaises(bt2.OverflowError):
+            self._msg_clock_overflow.default_clock_snapshot.ns_from_origin
 
-    def test_ne_cycles(self):
-        cv1 = self._cc.create_clock_value(123)
-        cv2 = self._cc.create_clock_value(125)
-        self.assertNotEqual(cv1, cv2)
+    def test_eq_int(self):
+        self.assertEqual(self._msg.default_clock_snapshot, 123)
 
     def test_eq_invalid(self):
-        self.assertFalse(self._cv == 23)
+        self.assertFalse(self._msg.default_clock_snapshot == 23)
+
+    def test_comparison(self):
+        self.assertTrue(self._msg.default_clock_snapshot > 100)
+        self.assertFalse(self._msg.default_clock_snapshot > 200)
 
-    def _test_copy(self, cpy):
-        self.assertIsNot(cpy, self._cv)
-        self.assertNotEqual(cpy.addr, self._cv.addr)
-        self.assertEqual(cpy, self._cv)
+        self.assertTrue(self._msg.default_clock_snapshot >= 123)
+        self.assertFalse(self._msg.default_clock_snapshot >= 200)
 
-    def test_copy(self):
-        cpy = copy.copy(self._cv)
-        self._test_copy(cpy)
+        self.assertTrue(self._msg.default_clock_snapshot < 200)
+        self.assertFalse(self._msg.default_clock_snapshot < 100)
 
-    def test_deepcopy(self):
-        cpy = copy.deepcopy(self._cv)
-        self._test_copy(cpy)
+        self.assertTrue(self._msg.default_clock_snapshot <= 123)
+        self.assertFalse(self._msg.default_clock_snapshot <= 100)
This page took 0.034685 seconds and 4 git commands to generate.