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.
21 from utils
import run_in_component_init
24 class StreamClassTestCase(unittest
.TestCase
):
27 tc
= comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
28 cc
= comp_self
._create
_clock
_class
()
31 self
._tc
, self
._cc
= run_in_component_init(f
)
32 self
._trace
= self
._tc
()
34 def test_create_default(self
):
35 sc
= self
._tc
.create_stream_class()
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
.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
)
50 def test_create_name(self
):
51 sc
= self
._tc
.create_stream_class(name
='bozo')
52 self
.assertEqual(sc
.name
, 'bozo')
54 def test_create_invalid_name(self
):
55 with self
.assertRaises(TypeError):
56 self
._tc
.create_stream_class(name
=17)
58 def test_create_packet_context_field_class(self
):
59 fc
= self
._tc
.create_structure_field_class()
60 sc
= self
._tc
.create_stream_class(packet_context_field_class
=fc
)
61 self
.assertEqual(sc
.packet_context_field_class
, fc
)
63 def test_create_invalid_packet_context_field_class(self
):
64 with self
.assertRaises(TypeError):
65 self
._tc
.create_stream_class(packet_context_field_class
=22)
67 def test_create_event_common_context_field_class(self
):
68 fc
= self
._tc
.create_structure_field_class()
69 sc
= self
._tc
.create_stream_class(event_common_context_field_class
=fc
)
70 self
.assertEqual(sc
.event_common_context_field_class
, fc
)
72 def test_create_invalid_event_common_context_field_class(self
):
73 with self
.assertRaises(TypeError):
74 self
._tc
.create_stream_class(event_common_context_field_class
=22)
76 def test_create_default_clock_class(self
):
77 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
)
78 self
.assertEqual(sc
.default_clock_class
.addr
, self
._cc
.addr
)
80 def test_create_invalid_default_clock_class(self
):
81 with self
.assertRaises(TypeError):
82 self
._tc
.create_stream_class(default_clock_class
=12)
84 def test_automatic_stream_ids(self
):
85 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=True)
86 self
.assertTrue(sc
.assigns_automatic_stream_id
)
88 stream
= self
._trace
.create_stream(sc
)
89 self
.assertIsNotNone(stream
.id)
91 def test_automatic_stream_ids_raises(self
):
92 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=True)
93 self
.assertTrue(sc
.assigns_automatic_stream_id
)
95 with self
.assertRaises(ValueError):
96 self
._trace
.create_stream(sc
, id=123)
98 def test_no_automatic_stream_ids(self
):
99 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=False)
100 self
.assertFalse(sc
.assigns_automatic_stream_id
)
102 stream
= self
._trace
.create_stream(sc
, id=333)
103 self
.assertEqual(stream
.id, 333)
105 def test_no_automatic_stream_ids_raises(self
):
106 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=False)
107 self
.assertFalse(sc
.assigns_automatic_stream_id
)
109 with self
.assertRaises(ValueError):
110 self
._trace
.create_stream(sc
)
112 def test_automatic_event_class_ids(self
):
113 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=True)
114 self
.assertTrue(sc
.assigns_automatic_event_class_id
)
116 ec
= sc
.create_event_class()
117 self
.assertIsNotNone(ec
.id)
119 def test_automatic_event_class_ids_raises(self
):
120 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=True)
121 self
.assertTrue(sc
.assigns_automatic_event_class_id
)
123 with self
.assertRaises(ValueError):
124 sc
.create_event_class(id=123)
126 def test_no_automatic_event_class_ids(self
):
127 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=False)
128 self
.assertFalse(sc
.assigns_automatic_event_class_id
)
130 ec
= sc
.create_event_class(id=333)
131 self
.assertEqual(ec
.id, 333)
133 def test_no_automatic_event_class_ids_raises(self
):
134 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=False)
135 self
.assertFalse(sc
.assigns_automatic_event_class_id
)
137 with self
.assertRaises(ValueError):
138 sc
.create_event_class()
140 def test_packets_have_beginning_default_clock_snapshot(self
):
141 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
, packets_have_beginning_default_clock_snapshot
=True)
142 self
.assertTrue(sc
.packets_have_beginning_default_clock_snapshot
)
144 def test_packets_have_beginning_default_clock_snapshot_raises(self
):
145 with self
.assertRaises(TypeError):
146 sc
= self
._tc
.create_stream_class(packets_have_beginning_default_clock_snapshot
="something")
148 def test_packets_have_end_default_clock_snapshot(self
):
149 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
, packets_have_end_default_clock_snapshot
=True)
150 self
.assertTrue(sc
.packets_have_end_default_clock_snapshot
)
152 def test_packets_have_end_default_clock_snapshot_raises(self
):
153 with self
.assertRaises(TypeError):
154 sc
= self
._tc
.create_stream_class(packets_have_end_default_clock_snapshot
="something")
156 def test_supports_discarded_events_without_cs(self
):
157 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
158 supports_discarded_events
=True)
159 self
.assertTrue(sc
.supports_discarded_events
)
160 self
.assertFalse(sc
.discarded_events_have_default_clock_snapshots
)
162 def test_supports_discarded_events_with_cs(self
):
163 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
164 supports_discarded_events
=True,
165 discarded_events_have_default_clock_snapshots
=True)
166 self
.assertTrue(sc
.supports_discarded_events
)
167 self
.assertTrue(sc
.discarded_events_have_default_clock_snapshots
)
169 def test_supports_discarded_events_raises_type_error(self
):
170 with self
.assertRaises(TypeError):
171 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
172 supports_discarded_events
=23)
174 def test_discarded_events_have_default_cs_raises_type_error(self
):
175 with self
.assertRaises(TypeError):
176 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
177 discarded_events_have_default_clock_snapshots
=23)
179 def test_does_not_support_discarded_events_raises_with_cs(self
):
180 with self
.assertRaises(ValueError):
181 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
182 discarded_events_have_default_clock_snapshots
=True)
184 def test_supports_discarded_packets_without_cs(self
):
185 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
186 supports_discarded_packets
=True)
187 self
.assertTrue(sc
.supports_discarded_packets
)
188 self
.assertFalse(sc
.discarded_packets_have_default_clock_snapshots
)
190 def test_supports_discarded_packets_with_cs(self
):
191 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
192 supports_discarded_packets
=True,
193 discarded_packets_have_default_clock_snapshots
=True)
194 self
.assertTrue(sc
.supports_discarded_packets
)
195 self
.assertTrue(sc
.discarded_packets_have_default_clock_snapshots
)
197 def test_supports_discarded_packets_raises_type_error(self
):
198 with self
.assertRaises(TypeError):
199 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
200 supports_discarded_packets
=23)
202 def test_discarded_packets_have_default_cs_raises_type_error(self
):
203 with self
.assertRaises(TypeError):
204 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
205 discarded_packets_have_default_clock_snapshots
=23)
207 def test_does_not_support_discarded_packets_raises_with_cs(self
):
208 with self
.assertRaises(ValueError):
209 sc
= self
._tc
.create_stream_class(default_clock_class
=self
._cc
,
210 discarded_packets_have_default_clock_snapshots
=True)
212 def test_trace_class(self
):
213 sc
= self
._tc
.create_stream_class()
214 self
.assertEqual(sc
.trace_class
.addr
, self
._tc
.addr
)
216 def _create_stream_class_with_event_classes(self
):
217 sc
= self
._tc
.create_stream_class(assigns_automatic_event_class_id
=False)
218 ec1
= sc
.create_event_class(id=23)
219 ec2
= sc
.create_event_class(id=17)
222 def test_getitem(self
):
223 sc
, ec1
, ec2
= self
._create
_stream
_class
_with
_event
_classes
()
225 self
.assertEqual(sc
[23].addr
, ec1
.addr
)
226 self
.assertEqual(sc
[17].addr
, ec2
.addr
)
228 def test_getitem_wrong_key_type(self
):
229 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
231 with self
.assertRaises(TypeError):
234 def test_getitem_wrong_key(self
):
235 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
237 with self
.assertRaises(KeyError):
241 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
243 self
.assertEqual(len(sc
), 2)
246 sc
, _
, _
= self
._create
_stream
_class
_with
_event
_classes
()
249 self
.assertEqual(ec_ids
, [17, 23])