2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
20 from utils
import run_in_component_init
23 class StreamClassTestCase(unittest
.TestCase
):
26 tc
= comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
27 cc
= comp_self
._create
_clock
_class
()
30 self
._tc
, self
._cc
= run_in_component_init(f
)
31 self
._trace
= self
._tc
()
33 def test_create_default(self
):
34 sc
= self
._tc
.create_stream_class()
36 self
.assertIsNone(sc
.name
)
37 self
.assertIsNone(sc
.packet_context_field_class
)
38 self
.assertIsNone(sc
.event_common_context_field_class
)
39 self
.assertIsNone(sc
.default_clock_class
)
40 self
.assertTrue(sc
.assigns_automatic_event_class_id
)
41 self
.assertTrue(sc
.assigns_automatic_stream_id
)
42 self
.assertFalse(sc
.supports_packets
)
43 self
.assertFalse(sc
.packets_have_beginning_default_clock_snapshot
)
44 self
.assertFalse(sc
.packets_have_end_default_clock_snapshot
)
45 self
.assertFalse(sc
.supports_discarded_events
)
46 self
.assertFalse(sc
.discarded_events_have_default_clock_snapshots
)
47 self
.assertFalse(sc
.supports_discarded_packets
)
48 self
.assertFalse(sc
.discarded_packets_have_default_clock_snapshots
)
49 self
.assertEqual(len(sc
.user_attributes
), 0)
51 def test_create_name(self
):
52 sc
= self
._tc
.create_stream_class(name
='bozo')
53 self
.assertEqual(sc
.name
, 'bozo')
55 def test_create_invalid_name(self
):
56 with self
.assertRaises(TypeError):
57 self
._tc
.create_stream_class(name
=17)
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
64 self
.assertEqual(sc
.packet_context_field_class
, fc
)
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)
70 def test_create_invalid_packet_context_field_class_no_packets(self
):
71 fc
= self
._tc
.create_structure_field_class()
73 with self
.assertRaises(ValueError):
74 self
._tc
.create_stream_class(packet_context_field_class
=fc
)
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
)
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)
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
)
89 def test_create_invalid_default_clock_class(self
):
90 with self
.assertRaises(TypeError):
91 self
._tc
.create_stream_class(default_clock_class
=12)
93 def test_create_user_attributes(self
):
94 sc
= self
._tc
.create_stream_class(user_attributes
={'salut': 23})
95 self
.assertEqual(sc
.user_attributes
, {'salut': 23})
97 def test_create_invalid_user_attributes(self
):
98 with self
.assertRaises(TypeError):
99 self
._tc
.create_stream_class(user_attributes
=object())
101 def test_create_invalid_user_attributes_value_type(self
):
102 with self
.assertRaises(TypeError):
103 self
._tc
.create_stream_class(user_attributes
=23)
105 def test_automatic_stream_ids(self
):
106 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=True)
107 self
.assertTrue(sc
.assigns_automatic_stream_id
)
109 stream
= self
._trace
.create_stream(sc
)
110 self
.assertIsNotNone(stream
.id)
112 def test_automatic_stream_ids_raises(self
):
113 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=True)
114 self
.assertTrue(sc
.assigns_automatic_stream_id
)
116 with self
.assertRaises(ValueError):
117 self
._trace
.create_stream(sc
, id=123)
119 def test_no_automatic_stream_ids(self
):
120 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=False)
121 self
.assertFalse(sc
.assigns_automatic_stream_id
)
123 stream
= self
._trace
.create_stream(sc
, id=333)
124 self
.assertEqual(stream
.id, 333)
126 def test_no_automatic_stream_ids_raises(self
):
127 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=False)
128 self
.assertFalse(sc
.assigns_automatic_stream_id
)
130 with self
.assertRaises(ValueError):
131 self
._trace
.create_stream(sc
)
133 def test_automatic_event_class_ids(self
):
134 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=True)
135 self
.assertTrue(sc
.assigns_automatic_event_class_id
)
137 ec
= sc
.create_event_class()
138 self
.assertIsNotNone(ec
.id)
140 def test_automatic_event_class_ids_raises(self
):
141 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=True)
142 self
.assertTrue(sc
.assigns_automatic_event_class_id
)
144 with self
.assertRaises(ValueError):
145 sc
.create_event_class(id=123)
147 def test_no_automatic_event_class_ids(self
):
148 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=False)
149 self
.assertFalse(sc
.assigns_automatic_event_class_id
)
151 ec
= sc
.create_event_class(id=333)
152 self
.assertEqual(ec
.id, 333)
154 def test_no_automatic_event_class_ids_raises(self
):
155 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=False)
156 self
.assertFalse(sc
.assigns_automatic_event_class_id
)
158 with self
.assertRaises(ValueError):
159 sc
.create_event_class()
161 def test_supports_packets_without_cs(self
):
162 sc
= self
._tc
.create_stream_class(
163 default_clock_class
=self
._cc
, supports_packets
=True
165 self
.assertTrue(sc
.supports_packets
)
166 self
.assertFalse(sc
.packets_have_beginning_default_clock_snapshot
)
167 self
.assertFalse(sc
.packets_have_end_default_clock_snapshot
)
169 def test_supports_packets_with_begin_cs(self
):
170 sc
= self
._tc
.create_stream_class(
171 default_clock_class
=self
._cc
,
172 supports_packets
=True,
173 packets_have_beginning_default_clock_snapshot
=True,
175 self
.assertTrue(sc
.supports_packets
)
176 self
.assertTrue(sc
.packets_have_beginning_default_clock_snapshot
)
177 self
.assertFalse(sc
.packets_have_end_default_clock_snapshot
)
179 def test_supports_packets_with_end_cs(self
):
180 sc
= self
._tc
.create_stream_class(
181 default_clock_class
=self
._cc
,
182 supports_packets
=True,
183 packets_have_end_default_clock_snapshot
=True,
185 self
.assertTrue(sc
.supports_packets
)
186 self
.assertFalse(sc
.packets_have_beginning_default_clock_snapshot
)
187 self
.assertTrue(sc
.packets_have_end_default_clock_snapshot
)
189 def test_supports_packets_raises_type_error(self
):
190 with self
.assertRaises(TypeError):
191 self
._tc
.create_stream_class(
192 default_clock_class
=self
._cc
, supports_packets
=23
195 def test_packets_have_begin_default_cs_raises_type_error(self
):
196 with self
.assertRaises(TypeError):
197 self
._tc
.create_stream_class(
198 default_clock_class
=self
._cc
,
199 packets_have_beginning_default_clock_snapshot
=23,
202 def test_packets_have_end_default_cs_raises_type_error(self
):
203 with self
.assertRaises(TypeError):
204 self
._tc
.create_stream_class(
205 default_clock_class
=self
._cc
, packets_have_end_default_clock_snapshot
=23
208 def test_does_not_support_packets_raises_with_begin_cs(self
):
209 with self
.assertRaises(ValueError):
210 self
._tc
.create_stream_class(
211 default_clock_class
=self
._cc
,
212 packets_have_beginning_default_clock_snapshot
=True,
215 def test_does_not_support_packets_raises_with_end_cs(self
):
216 with self
.assertRaises(ValueError):
217 self
._tc
.create_stream_class(
218 default_clock_class
=self
._cc
,
219 packets_have_end_default_clock_snapshot
=True,
222 def test_supports_discarded_events_without_cs(self
):
223 sc
= self
._tc
.create_stream_class(
224 default_clock_class
=self
._cc
, supports_discarded_events
=True
226 self
.assertTrue(sc
.supports_discarded_events
)
227 self
.assertFalse(sc
.discarded_events_have_default_clock_snapshots
)
229 def test_supports_discarded_events_with_cs(self
):
230 sc
= self
._tc
.create_stream_class(
231 default_clock_class
=self
._cc
,
232 supports_discarded_events
=True,
233 discarded_events_have_default_clock_snapshots
=True,
235 self
.assertTrue(sc
.supports_discarded_events
)
236 self
.assertTrue(sc
.discarded_events_have_default_clock_snapshots
)
238 def test_supports_discarded_events_raises_type_error(self
):
239 with self
.assertRaises(TypeError):
240 self
._tc
.create_stream_class(
241 default_clock_class
=self
._cc
, supports_discarded_events
=23
244 def test_discarded_events_have_default_cs_raises_type_error(self
):
245 with self
.assertRaises(TypeError):
246 self
._tc
.create_stream_class(
247 default_clock_class
=self
._cc
,
248 discarded_events_have_default_clock_snapshots
=23,
251 def test_does_not_support_discarded_events_raises_with_cs(self
):
252 with self
.assertRaises(ValueError):
253 self
._tc
.create_stream_class(
254 default_clock_class
=self
._cc
,
255 discarded_events_have_default_clock_snapshots
=True,
258 def test_supports_discarded_packets_without_cs(self
):
259 sc
= self
._tc
.create_stream_class(
260 default_clock_class
=self
._cc
,
261 supports_discarded_packets
=True,
262 supports_packets
=True,
264 self
.assertTrue(sc
.supports_discarded_packets
)
265 self
.assertFalse(sc
.discarded_packets_have_default_clock_snapshots
)
267 def test_supports_discarded_packets_with_cs(self
):
268 sc
= self
._tc
.create_stream_class(
269 default_clock_class
=self
._cc
,
270 supports_discarded_packets
=True,
271 discarded_packets_have_default_clock_snapshots
=True,
272 supports_packets
=True,
274 self
.assertTrue(sc
.supports_discarded_packets
)
275 self
.assertTrue(sc
.discarded_packets_have_default_clock_snapshots
)
277 def test_supports_discarded_packets_raises_without_packet_support(self
):
278 with self
.assertRaises(ValueError):
279 self
._tc
.create_stream_class(
280 default_clock_class
=self
._cc
, supports_discarded_packets
=True
283 def test_supports_discarded_packets_raises_type_error(self
):
284 with self
.assertRaises(TypeError):
285 self
._tc
.create_stream_class(
286 default_clock_class
=self
._cc
,
287 supports_discarded_packets
=23,
288 supports_packets
=True,
291 def test_discarded_packets_have_default_cs_raises_type_error(self
):
292 with self
.assertRaises(TypeError):
293 self
._tc
.create_stream_class(
294 default_clock_class
=self
._cc
,
295 discarded_packets_have_default_clock_snapshots
=23,
296 supports_packets
=True,
299 def test_does_not_support_discarded_packets_raises_with_cs(self
):
300 with self
.assertRaises(ValueError):
301 self
._tc
.create_stream_class(
302 default_clock_class
=self
._cc
,
303 discarded_packets_have_default_clock_snapshots
=True,
304 supports_packets
=True,
307 def test_trace_class(self
):
308 sc
= self
._tc
.create_stream_class()
309 self
.assertEqual(sc
.trace_class
.addr
, self
._tc
.addr
)
311 def _create_stream_class_with_event_classes(self
):
312 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=False)
313 ec1
= sc
.create_event_class(id=23)
314 ec2
= sc
.create_event_class(id=17)
317 def test_getitem(self
):
318 sc
, ec1
, ec2
= self
._create
_stream
_class
_with
_event
_classes
()
320 self
.assertEqual(sc
[23].addr
, ec1
.addr
)
321 self
.assertEqual(sc
[17].addr
, ec2
.addr
)
323 def test_getitem_wrong_key_type(self
):
324 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
326 with self
.assertRaises(TypeError):
329 def test_getitem_wrong_key(self
):
330 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
332 with self
.assertRaises(KeyError):
336 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
338 self
.assertEqual(len(sc
), 2)
341 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
344 self
.assertEqual(ec_ids
, [17, 23])