Apply black code formatter on all Python code
[babeltrace.git] / tests / bindings / python / bt2 / test_stream_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 19import unittest
9cf643d1 20import bt2
3cdfbaea 21from utils import run_in_component_init
9cf643d1
PP
22
23
24class StreamClassTestCase(unittest.TestCase):
25 def setUp(self):
3cdfbaea
SM
26 def f(comp_self):
27 tc = comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
28 cc = comp_self._create_clock_class()
29 return tc, cc
30
31 self._tc, self._cc = run_in_component_init(f)
32 self._trace = self._tc()
33
34 def test_create_default(self):
35 sc = self._tc.create_stream_class()
36
37 self.assertIsNone(sc.name)
38 self.assertIsNone(sc.packet_context_field_class)
39 self.assertIsNone(sc.event_common_context_field_class)
40 self.assertIsNone(sc.default_clock_class)
41 self.assertTrue(sc.assigns_automatic_event_class_id)
42 self.assertTrue(sc.assigns_automatic_stream_id)
26fc5aed 43 self.assertFalse(sc.supports_packets)
9b24b6aa
PP
44 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
45 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
2e90378a
PP
46 self.assertFalse(sc.supports_discarded_events)
47 self.assertFalse(sc.discarded_events_have_default_clock_snapshots)
48 self.assertFalse(sc.supports_discarded_packets)
49 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
3cdfbaea
SM
50
51 def test_create_name(self):
52 sc = self._tc.create_stream_class(name='bozo')
53 self.assertEqual(sc.name, 'bozo')
54
55 def test_create_invalid_name(self):
9cf643d1 56 with self.assertRaises(TypeError):
3cdfbaea 57 self._tc.create_stream_class(name=17)
9cf643d1 58
3cdfbaea
SM
59 def test_create_packet_context_field_class(self):
60 fc = self._tc.create_structure_field_class()
cfbd7cf3
FD
61 sc = self._tc.create_stream_class(
62 packet_context_field_class=fc, supports_packets=True
63 )
3cdfbaea 64 self.assertEqual(sc.packet_context_field_class, fc)
9cf643d1 65
3cdfbaea 66 def test_create_invalid_packet_context_field_class(self):
9cf643d1 67 with self.assertRaises(TypeError):
3cdfbaea 68 self._tc.create_stream_class(packet_context_field_class=22)
9cf643d1 69
26fc5aed
PP
70 def test_create_invalid_packet_context_field_class_no_packets(self):
71 fc = self._tc.create_structure_field_class()
72
73 with self.assertRaises(ValueError):
74 self._tc.create_stream_class(packet_context_field_class=fc)
75
3cdfbaea
SM
76 def test_create_event_common_context_field_class(self):
77 fc = self._tc.create_structure_field_class()
78 sc = self._tc.create_stream_class(event_common_context_field_class=fc)
79 self.assertEqual(sc.event_common_context_field_class, fc)
811644b8 80
3cdfbaea
SM
81 def test_create_invalid_event_common_context_field_class(self):
82 with self.assertRaises(TypeError):
83 self._tc.create_stream_class(event_common_context_field_class=22)
9cf643d1 84
3cdfbaea
SM
85 def test_create_default_clock_class(self):
86 sc = self._tc.create_stream_class(default_clock_class=self._cc)
87 self.assertEqual(sc.default_clock_class.addr, self._cc.addr)
9cf643d1 88
3cdfbaea 89 def test_create_invalid_default_clock_class(self):
9cf643d1 90 with self.assertRaises(TypeError):
3cdfbaea 91 self._tc.create_stream_class(default_clock_class=12)
9cf643d1 92
3cdfbaea
SM
93 def test_automatic_stream_ids(self):
94 sc = self._tc.create_stream_class(assigns_automatic_stream_id=True)
95 self.assertTrue(sc.assigns_automatic_stream_id)
9cf643d1 96
3cdfbaea
SM
97 stream = self._trace.create_stream(sc)
98 self.assertIsNotNone(stream.id)
9cf643d1 99
3cdfbaea
SM
100 def test_automatic_stream_ids_raises(self):
101 sc = self._tc.create_stream_class(assigns_automatic_stream_id=True)
102 self.assertTrue(sc.assigns_automatic_stream_id)
103
4430bc80 104 with self.assertRaises(ValueError):
3cdfbaea
SM
105 self._trace.create_stream(sc, id=123)
106
107 def test_no_automatic_stream_ids(self):
108 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
109 self.assertFalse(sc.assigns_automatic_stream_id)
110
111 stream = self._trace.create_stream(sc, id=333)
112 self.assertEqual(stream.id, 333)
113
114 def test_no_automatic_stream_ids_raises(self):
115 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
116 self.assertFalse(sc.assigns_automatic_stream_id)
117
4430bc80 118 with self.assertRaises(ValueError):
3cdfbaea
SM
119 self._trace.create_stream(sc)
120
121 def test_automatic_event_class_ids(self):
122 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True)
123 self.assertTrue(sc.assigns_automatic_event_class_id)
124
125 ec = sc.create_event_class()
126 self.assertIsNotNone(ec.id)
127
128 def test_automatic_event_class_ids_raises(self):
129 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True)
130 self.assertTrue(sc.assigns_automatic_event_class_id)
131
4430bc80 132 with self.assertRaises(ValueError):
3cdfbaea
SM
133 sc.create_event_class(id=123)
134
135 def test_no_automatic_event_class_ids(self):
136 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
137 self.assertFalse(sc.assigns_automatic_event_class_id)
9cf643d1 138
3cdfbaea
SM
139 ec = sc.create_event_class(id=333)
140 self.assertEqual(ec.id, 333)
9cf643d1 141
3cdfbaea
SM
142 def test_no_automatic_event_class_ids_raises(self):
143 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
144 self.assertFalse(sc.assigns_automatic_event_class_id)
9cf643d1 145
4430bc80 146 with self.assertRaises(ValueError):
3cdfbaea
SM
147 sc.create_event_class()
148
26fc5aed 149 def test_supports_packets_without_cs(self):
cfbd7cf3
FD
150 sc = self._tc.create_stream_class(
151 default_clock_class=self._cc, supports_packets=True
152 )
26fc5aed
PP
153 self.assertTrue(sc.supports_packets)
154 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
155 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
156
157 def test_supports_packets_with_begin_cs(self):
cfbd7cf3
FD
158 sc = self._tc.create_stream_class(
159 default_clock_class=self._cc,
160 supports_packets=True,
161 packets_have_beginning_default_clock_snapshot=True,
162 )
26fc5aed 163 self.assertTrue(sc.supports_packets)
9b24b6aa 164 self.assertTrue(sc.packets_have_beginning_default_clock_snapshot)
26fc5aed 165 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
3cdfbaea 166
26fc5aed 167 def test_supports_packets_with_end_cs(self):
cfbd7cf3
FD
168 sc = self._tc.create_stream_class(
169 default_clock_class=self._cc,
170 supports_packets=True,
171 packets_have_end_default_clock_snapshot=True,
172 )
26fc5aed
PP
173 self.assertTrue(sc.supports_packets)
174 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
175 self.assertTrue(sc.packets_have_end_default_clock_snapshot)
176
177 def test_supports_packets_raises_type_error(self):
3cdfbaea 178 with self.assertRaises(TypeError):
cfbd7cf3
FD
179 sc = self._tc.create_stream_class(
180 default_clock_class=self._cc, supports_packets=23
181 )
3cdfbaea 182
26fc5aed
PP
183 def test_packets_have_begin_default_cs_raises_type_error(self):
184 with self.assertRaises(TypeError):
cfbd7cf3
FD
185 sc = self._tc.create_stream_class(
186 default_clock_class=self._cc,
187 packets_have_beginning_default_clock_snapshot=23,
188 )
3cdfbaea 189
26fc5aed 190 def test_packets_have_end_default_cs_raises_type_error(self):
9cf643d1 191 with self.assertRaises(TypeError):
cfbd7cf3
FD
192 sc = self._tc.create_stream_class(
193 default_clock_class=self._cc, packets_have_end_default_clock_snapshot=23
194 )
26fc5aed
PP
195
196 def test_does_not_support_packets_raises_with_begin_cs(self):
197 with self.assertRaises(ValueError):
cfbd7cf3
FD
198 sc = self._tc.create_stream_class(
199 default_clock_class=self._cc,
200 packets_have_beginning_default_clock_snapshot=True,
201 )
26fc5aed
PP
202
203 def test_does_not_support_packets_raises_with_end_cs(self):
204 with self.assertRaises(ValueError):
cfbd7cf3
FD
205 sc = self._tc.create_stream_class(
206 default_clock_class=self._cc,
207 packets_have_end_default_clock_snapshot=True,
208 )
3cdfbaea 209
2e90378a 210 def test_supports_discarded_events_without_cs(self):
cfbd7cf3
FD
211 sc = self._tc.create_stream_class(
212 default_clock_class=self._cc, supports_discarded_events=True
213 )
2e90378a
PP
214 self.assertTrue(sc.supports_discarded_events)
215 self.assertFalse(sc.discarded_events_have_default_clock_snapshots)
216
217 def test_supports_discarded_events_with_cs(self):
cfbd7cf3
FD
218 sc = self._tc.create_stream_class(
219 default_clock_class=self._cc,
220 supports_discarded_events=True,
221 discarded_events_have_default_clock_snapshots=True,
222 )
2e90378a
PP
223 self.assertTrue(sc.supports_discarded_events)
224 self.assertTrue(sc.discarded_events_have_default_clock_snapshots)
225
226 def test_supports_discarded_events_raises_type_error(self):
227 with self.assertRaises(TypeError):
cfbd7cf3
FD
228 sc = self._tc.create_stream_class(
229 default_clock_class=self._cc, supports_discarded_events=23
230 )
2e90378a
PP
231
232 def test_discarded_events_have_default_cs_raises_type_error(self):
233 with self.assertRaises(TypeError):
cfbd7cf3
FD
234 sc = self._tc.create_stream_class(
235 default_clock_class=self._cc,
236 discarded_events_have_default_clock_snapshots=23,
237 )
2e90378a
PP
238
239 def test_does_not_support_discarded_events_raises_with_cs(self):
240 with self.assertRaises(ValueError):
cfbd7cf3
FD
241 sc = self._tc.create_stream_class(
242 default_clock_class=self._cc,
243 discarded_events_have_default_clock_snapshots=True,
244 )
2e90378a
PP
245
246 def test_supports_discarded_packets_without_cs(self):
cfbd7cf3
FD
247 sc = self._tc.create_stream_class(
248 default_clock_class=self._cc,
249 supports_discarded_packets=True,
250 supports_packets=True,
251 )
2e90378a
PP
252 self.assertTrue(sc.supports_discarded_packets)
253 self.assertFalse(sc.discarded_packets_have_default_clock_snapshots)
254
255 def test_supports_discarded_packets_with_cs(self):
cfbd7cf3
FD
256 sc = self._tc.create_stream_class(
257 default_clock_class=self._cc,
258 supports_discarded_packets=True,
259 discarded_packets_have_default_clock_snapshots=True,
260 supports_packets=True,
261 )
2e90378a
PP
262 self.assertTrue(sc.supports_discarded_packets)
263 self.assertTrue(sc.discarded_packets_have_default_clock_snapshots)
264
26fc5aed
PP
265 def test_supports_discarded_packets_raises_without_packet_support(self):
266 with self.assertRaises(ValueError):
cfbd7cf3
FD
267 sc = self._tc.create_stream_class(
268 default_clock_class=self._cc, supports_discarded_packets=True
269 )
26fc5aed 270
2e90378a
PP
271 def test_supports_discarded_packets_raises_type_error(self):
272 with self.assertRaises(TypeError):
cfbd7cf3
FD
273 sc = self._tc.create_stream_class(
274 default_clock_class=self._cc,
275 supports_discarded_packets=23,
276 supports_packets=True,
277 )
2e90378a
PP
278
279 def test_discarded_packets_have_default_cs_raises_type_error(self):
280 with self.assertRaises(TypeError):
cfbd7cf3
FD
281 sc = self._tc.create_stream_class(
282 default_clock_class=self._cc,
283 discarded_packets_have_default_clock_snapshots=23,
284 supports_packets=True,
285 )
2e90378a
PP
286
287 def test_does_not_support_discarded_packets_raises_with_cs(self):
288 with self.assertRaises(ValueError):
cfbd7cf3
FD
289 sc = self._tc.create_stream_class(
290 default_clock_class=self._cc,
291 discarded_packets_have_default_clock_snapshots=True,
292 supports_packets=True,
293 )
2e90378a 294
3cdfbaea
SM
295 def test_trace_class(self):
296 sc = self._tc.create_stream_class()
297 self.assertEqual(sc.trace_class.addr, self._tc.addr)
298
299 def _create_stream_class_with_event_classes(self):
300 sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False)
301 ec1 = sc.create_event_class(id=23)
302 ec2 = sc.create_event_class(id=17)
303 return sc, ec1, ec2
9cf643d1
PP
304
305 def test_getitem(self):
3cdfbaea
SM
306 sc, ec1, ec2 = self._create_stream_class_with_event_classes()
307
308 self.assertEqual(sc[23].addr, ec1.addr)
309 self.assertEqual(sc[17].addr, ec2.addr)
9cf643d1
PP
310
311 def test_getitem_wrong_key_type(self):
3cdfbaea
SM
312 sc, _, _ = self._create_stream_class_with_event_classes()
313
9cf643d1 314 with self.assertRaises(TypeError):
3cdfbaea 315 sc['event23']
9cf643d1
PP
316
317 def test_getitem_wrong_key(self):
3cdfbaea
SM
318 sc, _, _ = self._create_stream_class_with_event_classes()
319
9cf643d1 320 with self.assertRaises(KeyError):
3cdfbaea 321 sc[19]
9cf643d1
PP
322
323 def test_len(self):
3cdfbaea
SM
324 sc, _, _ = self._create_stream_class_with_event_classes()
325
326 self.assertEqual(len(sc), 2)
9cf643d1
PP
327
328 def test_iter(self):
3cdfbaea
SM
329 sc, _, _ = self._create_stream_class_with_event_classes()
330
331 ec_ids = sorted(sc)
332 self.assertEqual(ec_ids, [17, 23])
This page took 0.05774 seconds and 4 git commands to generate.