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