| 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(packet_context_field_class=fc, |
| 62 | supports_packets=True) |
| 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(default_clock_class=self._cc, |
| 150 | supports_packets=True) |
| 151 | self.assertTrue(sc.supports_packets) |
| 152 | self.assertFalse(sc.packets_have_beginning_default_clock_snapshot) |
| 153 | self.assertFalse(sc.packets_have_end_default_clock_snapshot) |
| 154 | |
| 155 | def test_supports_packets_with_begin_cs(self): |
| 156 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 157 | supports_packets=True, |
| 158 | packets_have_beginning_default_clock_snapshot=True) |
| 159 | self.assertTrue(sc.supports_packets) |
| 160 | self.assertTrue(sc.packets_have_beginning_default_clock_snapshot) |
| 161 | self.assertFalse(sc.packets_have_end_default_clock_snapshot) |
| 162 | |
| 163 | def test_supports_packets_with_end_cs(self): |
| 164 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 165 | supports_packets=True, |
| 166 | packets_have_end_default_clock_snapshot=True) |
| 167 | self.assertTrue(sc.supports_packets) |
| 168 | self.assertFalse(sc.packets_have_beginning_default_clock_snapshot) |
| 169 | self.assertTrue(sc.packets_have_end_default_clock_snapshot) |
| 170 | |
| 171 | def test_supports_packets_raises_type_error(self): |
| 172 | with self.assertRaises(TypeError): |
| 173 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 174 | supports_packets=23) |
| 175 | |
| 176 | def test_packets_have_begin_default_cs_raises_type_error(self): |
| 177 | with self.assertRaises(TypeError): |
| 178 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 179 | packets_have_beginning_default_clock_snapshot=23) |
| 180 | |
| 181 | def test_packets_have_end_default_cs_raises_type_error(self): |
| 182 | with self.assertRaises(TypeError): |
| 183 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 184 | packets_have_end_default_clock_snapshot=23) |
| 185 | |
| 186 | def test_does_not_support_packets_raises_with_begin_cs(self): |
| 187 | with self.assertRaises(ValueError): |
| 188 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 189 | packets_have_beginning_default_clock_snapshot=True) |
| 190 | |
| 191 | def test_does_not_support_packets_raises_with_end_cs(self): |
| 192 | with self.assertRaises(ValueError): |
| 193 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 194 | packets_have_end_default_clock_snapshot=True) |
| 195 | |
| 196 | def test_supports_discarded_events_without_cs(self): |
| 197 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 198 | supports_discarded_events=True) |
| 199 | self.assertTrue(sc.supports_discarded_events) |
| 200 | self.assertFalse(sc.discarded_events_have_default_clock_snapshots) |
| 201 | |
| 202 | def test_supports_discarded_events_with_cs(self): |
| 203 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 204 | supports_discarded_events=True, |
| 205 | discarded_events_have_default_clock_snapshots=True) |
| 206 | self.assertTrue(sc.supports_discarded_events) |
| 207 | self.assertTrue(sc.discarded_events_have_default_clock_snapshots) |
| 208 | |
| 209 | def test_supports_discarded_events_raises_type_error(self): |
| 210 | with self.assertRaises(TypeError): |
| 211 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 212 | supports_discarded_events=23) |
| 213 | |
| 214 | def test_discarded_events_have_default_cs_raises_type_error(self): |
| 215 | with self.assertRaises(TypeError): |
| 216 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 217 | discarded_events_have_default_clock_snapshots=23) |
| 218 | |
| 219 | def test_does_not_support_discarded_events_raises_with_cs(self): |
| 220 | with self.assertRaises(ValueError): |
| 221 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 222 | discarded_events_have_default_clock_snapshots=True) |
| 223 | |
| 224 | def test_supports_discarded_packets_without_cs(self): |
| 225 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 226 | supports_discarded_packets=True, |
| 227 | supports_packets=True) |
| 228 | self.assertTrue(sc.supports_discarded_packets) |
| 229 | self.assertFalse(sc.discarded_packets_have_default_clock_snapshots) |
| 230 | |
| 231 | def test_supports_discarded_packets_with_cs(self): |
| 232 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 233 | supports_discarded_packets=True, |
| 234 | discarded_packets_have_default_clock_snapshots=True, |
| 235 | supports_packets=True) |
| 236 | self.assertTrue(sc.supports_discarded_packets) |
| 237 | self.assertTrue(sc.discarded_packets_have_default_clock_snapshots) |
| 238 | |
| 239 | def test_supports_discarded_packets_raises_without_packet_support(self): |
| 240 | with self.assertRaises(ValueError): |
| 241 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 242 | supports_discarded_packets=True) |
| 243 | |
| 244 | def test_supports_discarded_packets_raises_type_error(self): |
| 245 | with self.assertRaises(TypeError): |
| 246 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 247 | supports_discarded_packets=23, |
| 248 | supports_packets=True) |
| 249 | |
| 250 | def test_discarded_packets_have_default_cs_raises_type_error(self): |
| 251 | with self.assertRaises(TypeError): |
| 252 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 253 | discarded_packets_have_default_clock_snapshots=23, |
| 254 | supports_packets=True) |
| 255 | |
| 256 | def test_does_not_support_discarded_packets_raises_with_cs(self): |
| 257 | with self.assertRaises(ValueError): |
| 258 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 259 | discarded_packets_have_default_clock_snapshots=True, |
| 260 | supports_packets=True) |
| 261 | |
| 262 | def test_trace_class(self): |
| 263 | sc = self._tc.create_stream_class() |
| 264 | self.assertEqual(sc.trace_class.addr, self._tc.addr) |
| 265 | |
| 266 | def _create_stream_class_with_event_classes(self): |
| 267 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 268 | ec1 = sc.create_event_class(id=23) |
| 269 | ec2 = sc.create_event_class(id=17) |
| 270 | return sc, ec1, ec2 |
| 271 | |
| 272 | def test_getitem(self): |
| 273 | sc, ec1, ec2 = self._create_stream_class_with_event_classes() |
| 274 | |
| 275 | self.assertEqual(sc[23].addr, ec1.addr) |
| 276 | self.assertEqual(sc[17].addr, ec2.addr) |
| 277 | |
| 278 | def test_getitem_wrong_key_type(self): |
| 279 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 280 | |
| 281 | with self.assertRaises(TypeError): |
| 282 | sc['event23'] |
| 283 | |
| 284 | def test_getitem_wrong_key(self): |
| 285 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 286 | |
| 287 | with self.assertRaises(KeyError): |
| 288 | sc[19] |
| 289 | |
| 290 | def test_len(self): |
| 291 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 292 | |
| 293 | self.assertEqual(len(sc), 2) |
| 294 | |
| 295 | def test_iter(self): |
| 296 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 297 | |
| 298 | ec_ids = sorted(sc) |
| 299 | self.assertEqual(ec_ids, [17, 23]) |