Apply black code formatter on all Python code
[babeltrace.git] / tests / bindings / python / bt2 / test_clock_class.py
CommitLineData
d2d857a8
MJ
1#
2# Copyright (C) 2019 EfficiOS Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; only version 2
7# of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17#
18
9cf643d1
PP
19import unittest
20import uuid
21import copy
22import bt2
be7bbff9 23from utils import run_in_component_init
9cf643d1
PP
24
25
26class ClockClassOffsetTestCase(unittest.TestCase):
27 def test_create_default(self):
28 cco = bt2.ClockClassOffset()
29 self.assertEqual(cco.seconds, 0)
30 self.assertEqual(cco.cycles, 0)
31
32 def test_create(self):
33 cco = bt2.ClockClassOffset(23, 4871232)
34 self.assertEqual(cco.seconds, 23)
35 self.assertEqual(cco.cycles, 4871232)
36
37 def test_create_kwargs(self):
38 cco = bt2.ClockClassOffset(seconds=23, cycles=4871232)
39 self.assertEqual(cco.seconds, 23)
40 self.assertEqual(cco.cycles, 4871232)
41
42 def test_create_invalid_seconds(self):
43 with self.assertRaises(TypeError):
44 bt2.ClockClassOffset('hello', 4871232)
45
46 def test_create_invalid_cycles(self):
47 with self.assertRaises(TypeError):
48 bt2.ClockClassOffset(23, 'hello')
49
50 def test_eq(self):
51 cco1 = bt2.ClockClassOffset(23, 42)
52 cco2 = bt2.ClockClassOffset(23, 42)
53 self.assertEqual(cco1, cco2)
54
55 def test_ne_seconds(self):
56 cco1 = bt2.ClockClassOffset(23, 42)
57 cco2 = bt2.ClockClassOffset(24, 42)
58 self.assertNotEqual(cco1, cco2)
59
60 def test_ne_cycles(self):
61 cco1 = bt2.ClockClassOffset(23, 42)
62 cco2 = bt2.ClockClassOffset(23, 43)
63 self.assertNotEqual(cco1, cco2)
64
65 def test_eq_invalid(self):
66 self.assertFalse(bt2.ClockClassOffset() == 23)
67
68
69class ClockClassTestCase(unittest.TestCase):
be7bbff9
SM
70 def assertRaisesInComponentInit(self, expected_exc_type, user_code):
71 def f(comp_self):
72 try:
73 user_code(comp_self)
74 except Exception as exc:
75 return type(exc)
811644b8 76
be7bbff9
SM
77 exc_type = run_in_component_init(f)
78 self.assertIsNotNone(exc_type)
79 self.assertEqual(exc_type, expected_exc_type)
9cf643d1
PP
80
81 def test_create_default(self):
be7bbff9 82 cc = run_in_component_init(lambda comp_self: comp_self._create_clock_class())
9cf643d1 83
be7bbff9
SM
84 self.assertIsNone(cc.name)
85 self.assertEqual(cc.frequency, 1000000000)
86 self.assertIsNone(cc.description)
87 self.assertEqual(cc.precision, 0)
88 self.assertEqual(cc.offset, bt2.ClockClassOffset())
89 self.assertTrue(cc.origin_is_unix_epoch)
90 self.assertIsNone(cc.uuid)
9cf643d1 91
be7bbff9
SM
92 def test_create_name(self):
93 def f(comp_self):
94 return comp_self._create_clock_class(name='the_clock')
9cf643d1 95
be7bbff9
SM
96 cc = run_in_component_init(f)
97 self.assertEqual(cc.name, 'the_clock')
9cf643d1 98
be7bbff9
SM
99 def test_create_invalid_name(self):
100 def f(comp_self):
101 comp_self._create_clock_class(name=23)
9cf643d1 102
be7bbff9 103 self.assertRaisesInComponentInit(TypeError, f)
9cf643d1 104
be7bbff9
SM
105 def test_create_description(self):
106 def f(comp_self):
107 return comp_self._create_clock_class(description='hi people')
9cf643d1 108
be7bbff9
SM
109 cc = run_in_component_init(f)
110 self.assertEqual(cc.description, 'hi people')
9cf643d1 111
be7bbff9
SM
112 def test_create_invalid_description(self):
113 def f(comp_self):
114 return comp_self._create_clock_class(description=23)
9cf643d1 115
be7bbff9 116 self.assertRaisesInComponentInit(TypeError, f)
9cf643d1 117
be7bbff9
SM
118 def test_create_frequency(self):
119 def f(comp_self):
120 return comp_self._create_clock_class(frequency=987654321)
9cf643d1 121
be7bbff9
SM
122 cc = run_in_component_init(f)
123 self.assertEqual(cc.frequency, 987654321)
9cf643d1 124
be7bbff9
SM
125 def test_create_invalid_frequency(self):
126 def f(comp_self):
127 return comp_self._create_clock_class(frequency='lel')
9cf643d1 128
be7bbff9 129 self.assertRaisesInComponentInit(TypeError, f)
9cf643d1 130
be7bbff9
SM
131 def test_create_precision(self):
132 def f(comp_self):
133 return comp_self._create_clock_class(precision=12)
9cf643d1 134
be7bbff9
SM
135 cc = run_in_component_init(f)
136 self.assertEqual(cc.precision, 12)
9cf643d1 137
be7bbff9
SM
138 def test_create_invalid_precision(self):
139 def f(comp_self):
140 return comp_self._create_clock_class(precision='lel')
9cf643d1 141
be7bbff9 142 self.assertRaisesInComponentInit(TypeError, f)
9cf643d1 143
be7bbff9
SM
144 def test_create_offset(self):
145 def f(comp_self):
146 return comp_self._create_clock_class(offset=bt2.ClockClassOffset(12, 56))
9cf643d1 147
be7bbff9
SM
148 cc = run_in_component_init(f)
149 self.assertEqual(cc.offset, bt2.ClockClassOffset(12, 56))
150
151 def test_create_invalid_offset(self):
152 def f(comp_self):
153 return comp_self._create_clock_class(offset=object())
154
155 self.assertRaisesInComponentInit(TypeError, f)
156
157 def test_create_origin_is_unix_epoch(self):
158 def f(comp_self):
159 return comp_self._create_clock_class(origin_is_unix_epoch=False)
160
161 cc = run_in_component_init(f)
162 self.assertEqual(cc.origin_is_unix_epoch, False)
163
164 def test_create_invalid_origin_is_unix_epoch(self):
165 def f(comp_self):
166 return comp_self._create_clock_class(origin_is_unix_epoch=23)
167
168 self.assertRaisesInComponentInit(TypeError, f)
169
170 def test_cycles_to_ns_from_origin(self):
171 def f(comp_self):
cfbd7cf3
FD
172 return comp_self._create_clock_class(
173 frequency=10 ** 8, origin_is_unix_epoch=True
174 )
be7bbff9
SM
175
176 cc = run_in_component_init(f)
177 self.assertEqual(cc.cycles_to_ns_from_origin(112), 1120)
178
179 def test_cycles_to_ns_from_origin_overflow(self):
180 def f(comp_self):
181 return comp_self._create_clock_class(frequency=1000)
182
183 cc = run_in_component_init(f)
d24d5663 184 with self.assertRaises(bt2.OverflowError):
cfbd7cf3 185 cc.cycles_to_ns_from_origin(2 ** 63)
be7bbff9
SM
186
187 def test_create_uuid(self):
188 def f(comp_self):
cfbd7cf3
FD
189 return comp_self._create_clock_class(
190 uuid=uuid.UUID('b43372c32ef0be28444dfc1c5cdafd33')
191 )
be7bbff9
SM
192
193 cc = run_in_component_init(f)
194 self.assertEqual(cc.uuid, uuid.UUID('b43372c32ef0be28444dfc1c5cdafd33'))
195
196 def test_create_invalid_uuid(self):
197 def f(comp_self):
198 return comp_self._create_clock_class(uuid=23)
199
200 self.assertRaisesInComponentInit(TypeError, f)
9cf643d1
PP
201
202
4b552f8b 203class ClockSnapshotTestCase(unittest.TestCase):
9cf643d1 204 def setUp(self):
be7bbff9 205 def f(comp_self):
cfbd7cf3
FD
206 cc = comp_self._create_clock_class(
207 1000, 'my_cc', offset=bt2.ClockClassOffset(45, 354)
208 )
be7bbff9
SM
209 tc = comp_self._create_trace_class()
210
211 return (cc, tc)
212
213 _cc, _tc = run_in_component_init(f)
214 _trace = _tc()
26fc5aed 215 _sc = _tc.create_stream_class(default_clock_class=_cc)
be7bbff9
SM
216 _ec = _sc.create_event_class(name='salut')
217 _stream = _trace.create_stream(_sc)
be7bbff9
SM
218 self._stream = _stream
219 self._ec = _ec
220 self._cc = _cc
221
222 class MyIter(bt2._UserMessageIterator):
c5f330cd 223 def __init__(self, self_port_output):
be7bbff9
SM
224 self._at = 0
225
226 def __next__(self):
227 if self._at == 0:
228 notif = self._create_stream_beginning_message(_stream)
229 elif self._at == 1:
26fc5aed 230 notif = self._create_event_message(_ec, _stream, 123)
be7bbff9 231 elif self._at == 2:
cfbd7cf3 232 notif = self._create_event_message(_ec, _stream, 2 ** 63)
be7bbff9 233 elif self._at == 3:
be7bbff9
SM
234 notif = self._create_stream_end_message(_stream)
235 else:
236 raise bt2.Stop
237
238 self._at += 1
239 return notif
240
241 class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
242 def __init__(self, params):
243 self._add_output_port('out')
244
245 self._graph = bt2.Graph()
246 self._src_comp = self._graph.add_component(MySrc, 'my_source')
247 self._msg_iter = self._graph.create_output_port_message_iterator(
cfbd7cf3
FD
248 self._src_comp.output_ports['out']
249 )
be7bbff9
SM
250
251 for i, msg in enumerate(self._msg_iter):
26fc5aed 252 if i == 1:
be7bbff9 253 self._msg = msg
26fc5aed 254 elif i == 2:
be7bbff9
SM
255 self._msg_clock_overflow = msg
256 break
811644b8
PP
257
258 def tearDown(self):
259 del self._cc
be7bbff9 260 del self._msg
9cf643d1
PP
261
262 def test_create_default(self):
be7bbff9 263 self.assertEqual(
cfbd7cf3
FD
264 self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr
265 )
be7bbff9 266 self.assertEqual(self._msg.default_clock_snapshot.value, 123)
9cf643d1 267
be7bbff9
SM
268 def test_clock_class(self):
269 self.assertEqual(
cfbd7cf3
FD
270 self._msg.default_clock_snapshot.clock_class.addr, self._cc.addr
271 )
9cf643d1 272
be7bbff9
SM
273 def test_ns_from_origin(self):
274 s_from_origin = 45 + ((354 + 123) / 1000)
275 ns_from_origin = int(s_from_origin * 1e9)
276 self.assertEqual(
cfbd7cf3
FD
277 self._msg.default_clock_snapshot.ns_from_origin, ns_from_origin
278 )
9cf643d1 279
be7bbff9 280 def test_ns_from_origin_overflow(self):
d24d5663 281 with self.assertRaises(bt2.OverflowError):
be7bbff9 282 self._msg_clock_overflow.default_clock_snapshot.ns_from_origin
9cf643d1 283
811644b8 284 def test_eq_int(self):
be7bbff9 285 self.assertEqual(self._msg.default_clock_snapshot, 123)
9cf643d1
PP
286
287 def test_eq_invalid(self):
be7bbff9
SM
288 self.assertFalse(self._msg.default_clock_snapshot == 23)
289
290 def test_comparison(self):
291 self.assertTrue(self._msg.default_clock_snapshot > 100)
292 self.assertFalse(self._msg.default_clock_snapshot > 200)
9cf643d1 293
be7bbff9
SM
294 self.assertTrue(self._msg.default_clock_snapshot >= 123)
295 self.assertFalse(self._msg.default_clock_snapshot >= 200)
9cf643d1 296
be7bbff9
SM
297 self.assertTrue(self._msg.default_clock_snapshot < 200)
298 self.assertFalse(self._msg.default_clock_snapshot < 100)
9cf643d1 299
be7bbff9
SM
300 self.assertTrue(self._msg.default_clock_snapshot <= 123)
301 self.assertFalse(self._msg.default_clock_snapshot <= 100)
This page took 0.055551 seconds and 4 git commands to generate.