Apply black code formatter on all Python code
[babeltrace.git] / tests / bindings / python / bt2 / test_stream_class.py
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
19 import unittest
20 import bt2
21 from utils import run_in_component_init
22
23
24 class StreamClassTestCase(unittest.TestCase):
25 def setUp(self):
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)
43 self.assertFalse(sc.supports_packets)
44 self.assertFalse(sc.packets_have_beginning_default_clock_snapshot)
45 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
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)
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):
56 with self.assertRaises(TypeError):
57 self._tc.create_stream_class(name=17)
58
59 def test_create_packet_context_field_class(self):
60 fc = self._tc.create_structure_field_class()
61 sc = self._tc.create_stream_class(
62 packet_context_field_class=fc, supports_packets=True
63 )
64 self.assertEqual(sc.packet_context_field_class, fc)
65
66 def test_create_invalid_packet_context_field_class(self):
67 with self.assertRaises(TypeError):
68 self._tc.create_stream_class(packet_context_field_class=22)
69
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
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)
80
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)
84
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)
88
89 def test_create_invalid_default_clock_class(self):
90 with self.assertRaises(TypeError):
91 self._tc.create_stream_class(default_clock_class=12)
92
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)
96
97 stream = self._trace.create_stream(sc)
98 self.assertIsNotNone(stream.id)
99
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
104 with self.assertRaises(ValueError):
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
118 with self.assertRaises(ValueError):
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
132 with self.assertRaises(ValueError):
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)
138
139 ec = sc.create_event_class(id=333)
140 self.assertEqual(ec.id, 333)
141
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)
145
146 with self.assertRaises(ValueError):
147 sc.create_event_class()
148
149 def test_supports_packets_without_cs(self):
150 sc = self._tc.create_stream_class(
151 default_clock_class=self._cc, supports_packets=True
152 )
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):
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 )
163 self.assertTrue(sc.supports_packets)
164 self.assertTrue(sc.packets_have_beginning_default_clock_snapshot)
165 self.assertFalse(sc.packets_have_end_default_clock_snapshot)
166
167 def test_supports_packets_with_end_cs(self):
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 )
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):
178 with self.assertRaises(TypeError):
179 sc = self._tc.create_stream_class(
180 default_clock_class=self._cc, supports_packets=23
181 )
182
183 def test_packets_have_begin_default_cs_raises_type_error(self):
184 with self.assertRaises(TypeError):
185 sc = self._tc.create_stream_class(
186 default_clock_class=self._cc,
187 packets_have_beginning_default_clock_snapshot=23,
188 )
189
190 def test_packets_have_end_default_cs_raises_type_error(self):
191 with self.assertRaises(TypeError):
192 sc = self._tc.create_stream_class(
193 default_clock_class=self._cc, packets_have_end_default_clock_snapshot=23
194 )
195
196 def test_does_not_support_packets_raises_with_begin_cs(self):
197 with self.assertRaises(ValueError):
198 sc = self._tc.create_stream_class(
199 default_clock_class=self._cc,
200 packets_have_beginning_default_clock_snapshot=True,
201 )
202
203 def test_does_not_support_packets_raises_with_end_cs(self):
204 with self.assertRaises(ValueError):
205 sc = self._tc.create_stream_class(
206 default_clock_class=self._cc,
207 packets_have_end_default_clock_snapshot=True,
208 )
209
210 def test_supports_discarded_events_without_cs(self):
211 sc = self._tc.create_stream_class(
212 default_clock_class=self._cc, supports_discarded_events=True
213 )
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):
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 )
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):
228 sc = self._tc.create_stream_class(
229 default_clock_class=self._cc, supports_discarded_events=23
230 )
231
232 def test_discarded_events_have_default_cs_raises_type_error(self):
233 with self.assertRaises(TypeError):
234 sc = self._tc.create_stream_class(
235 default_clock_class=self._cc,
236 discarded_events_have_default_clock_snapshots=23,
237 )
238
239 def test_does_not_support_discarded_events_raises_with_cs(self):
240 with self.assertRaises(ValueError):
241 sc = self._tc.create_stream_class(
242 default_clock_class=self._cc,
243 discarded_events_have_default_clock_snapshots=True,
244 )
245
246 def test_supports_discarded_packets_without_cs(self):
247 sc = self._tc.create_stream_class(
248 default_clock_class=self._cc,
249 supports_discarded_packets=True,
250 supports_packets=True,
251 )
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):
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 )
262 self.assertTrue(sc.supports_discarded_packets)
263 self.assertTrue(sc.discarded_packets_have_default_clock_snapshots)
264
265 def test_supports_discarded_packets_raises_without_packet_support(self):
266 with self.assertRaises(ValueError):
267 sc = self._tc.create_stream_class(
268 default_clock_class=self._cc, supports_discarded_packets=True
269 )
270
271 def test_supports_discarded_packets_raises_type_error(self):
272 with self.assertRaises(TypeError):
273 sc = self._tc.create_stream_class(
274 default_clock_class=self._cc,
275 supports_discarded_packets=23,
276 supports_packets=True,
277 )
278
279 def test_discarded_packets_have_default_cs_raises_type_error(self):
280 with self.assertRaises(TypeError):
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 )
286
287 def test_does_not_support_discarded_packets_raises_with_cs(self):
288 with self.assertRaises(ValueError):
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 )
294
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
304
305 def test_getitem(self):
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)
310
311 def test_getitem_wrong_key_type(self):
312 sc, _, _ = self._create_stream_class_with_event_classes()
313
314 with self.assertRaises(TypeError):
315 sc['event23']
316
317 def test_getitem_wrong_key(self):
318 sc, _, _ = self._create_stream_class_with_event_classes()
319
320 with self.assertRaises(KeyError):
321 sc[19]
322
323 def test_len(self):
324 sc, _, _ = self._create_stream_class_with_event_classes()
325
326 self.assertEqual(len(sc), 2)
327
328 def test_iter(self):
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.036677 seconds and 5 git commands to generate.