| 1 | import unittest |
| 2 | import bt2 |
| 3 | from utils import run_in_component_init |
| 4 | |
| 5 | |
| 6 | class StreamClassTestCase(unittest.TestCase): |
| 7 | def setUp(self): |
| 8 | def f(comp_self): |
| 9 | tc = comp_self._create_trace_class(assigns_automatic_stream_class_id=True) |
| 10 | cc = comp_self._create_clock_class() |
| 11 | return tc, cc |
| 12 | |
| 13 | self._tc, self._cc = run_in_component_init(f) |
| 14 | self._trace = self._tc() |
| 15 | |
| 16 | def test_create_default(self): |
| 17 | sc = self._tc.create_stream_class() |
| 18 | |
| 19 | self.assertIsNone(sc.name) |
| 20 | self.assertIsNone(sc.packet_context_field_class) |
| 21 | self.assertIsNone(sc.event_common_context_field_class) |
| 22 | self.assertIsNone(sc.default_clock_class) |
| 23 | self.assertTrue(sc.assigns_automatic_event_class_id) |
| 24 | self.assertTrue(sc.assigns_automatic_stream_id) |
| 25 | self.assertFalse(sc.packets_have_default_beginning_clock_snapshot) |
| 26 | self.assertFalse(sc.packets_have_default_end_clock_snapshot) |
| 27 | self.assertFalse(sc.supports_discarded_events) |
| 28 | self.assertFalse(sc.discarded_events_have_default_clock_snapshots) |
| 29 | self.assertFalse(sc.supports_discarded_packets) |
| 30 | self.assertFalse(sc.discarded_packets_have_default_clock_snapshots) |
| 31 | |
| 32 | def test_create_name(self): |
| 33 | sc = self._tc.create_stream_class(name='bozo') |
| 34 | self.assertEqual(sc.name, 'bozo') |
| 35 | |
| 36 | def test_create_invalid_name(self): |
| 37 | with self.assertRaises(TypeError): |
| 38 | self._tc.create_stream_class(name=17) |
| 39 | |
| 40 | def test_create_packet_context_field_class(self): |
| 41 | fc = self._tc.create_structure_field_class() |
| 42 | sc = self._tc.create_stream_class(packet_context_field_class=fc) |
| 43 | self.assertEqual(sc.packet_context_field_class, fc) |
| 44 | |
| 45 | def test_create_invalid_packet_context_field_class(self): |
| 46 | with self.assertRaises(TypeError): |
| 47 | self._tc.create_stream_class(packet_context_field_class=22) |
| 48 | |
| 49 | def test_create_event_common_context_field_class(self): |
| 50 | fc = self._tc.create_structure_field_class() |
| 51 | sc = self._tc.create_stream_class(event_common_context_field_class=fc) |
| 52 | self.assertEqual(sc.event_common_context_field_class, fc) |
| 53 | |
| 54 | def test_create_invalid_event_common_context_field_class(self): |
| 55 | with self.assertRaises(TypeError): |
| 56 | self._tc.create_stream_class(event_common_context_field_class=22) |
| 57 | |
| 58 | def test_create_default_clock_class(self): |
| 59 | sc = self._tc.create_stream_class(default_clock_class=self._cc) |
| 60 | self.assertEqual(sc.default_clock_class.addr, self._cc.addr) |
| 61 | |
| 62 | def test_create_invalid_default_clock_class(self): |
| 63 | with self.assertRaises(TypeError): |
| 64 | self._tc.create_stream_class(default_clock_class=12) |
| 65 | |
| 66 | def test_automatic_stream_ids(self): |
| 67 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=True) |
| 68 | self.assertTrue(sc.assigns_automatic_stream_id) |
| 69 | |
| 70 | stream = self._trace.create_stream(sc) |
| 71 | self.assertIsNotNone(stream.id) |
| 72 | |
| 73 | def test_automatic_stream_ids_raises(self): |
| 74 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=True) |
| 75 | self.assertTrue(sc.assigns_automatic_stream_id) |
| 76 | |
| 77 | with self.assertRaises(ValueError): |
| 78 | self._trace.create_stream(sc, id=123) |
| 79 | |
| 80 | def test_no_automatic_stream_ids(self): |
| 81 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) |
| 82 | self.assertFalse(sc.assigns_automatic_stream_id) |
| 83 | |
| 84 | stream = self._trace.create_stream(sc, id=333) |
| 85 | self.assertEqual(stream.id, 333) |
| 86 | |
| 87 | def test_no_automatic_stream_ids_raises(self): |
| 88 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) |
| 89 | self.assertFalse(sc.assigns_automatic_stream_id) |
| 90 | |
| 91 | with self.assertRaises(ValueError): |
| 92 | self._trace.create_stream(sc) |
| 93 | |
| 94 | def test_automatic_event_class_ids(self): |
| 95 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True) |
| 96 | self.assertTrue(sc.assigns_automatic_event_class_id) |
| 97 | |
| 98 | ec = sc.create_event_class() |
| 99 | self.assertIsNotNone(ec.id) |
| 100 | |
| 101 | def test_automatic_event_class_ids_raises(self): |
| 102 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=True) |
| 103 | self.assertTrue(sc.assigns_automatic_event_class_id) |
| 104 | |
| 105 | with self.assertRaises(ValueError): |
| 106 | sc.create_event_class(id=123) |
| 107 | |
| 108 | def test_no_automatic_event_class_ids(self): |
| 109 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 110 | self.assertFalse(sc.assigns_automatic_event_class_id) |
| 111 | |
| 112 | ec = sc.create_event_class(id=333) |
| 113 | self.assertEqual(ec.id, 333) |
| 114 | |
| 115 | def test_no_automatic_event_class_ids_raises(self): |
| 116 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 117 | self.assertFalse(sc.assigns_automatic_event_class_id) |
| 118 | |
| 119 | with self.assertRaises(ValueError): |
| 120 | sc.create_event_class() |
| 121 | |
| 122 | def test_packets_have_default_beginning_clock_snapshot(self): |
| 123 | sc = self._tc.create_stream_class(default_clock_class=self._cc, packets_have_default_beginning_clock_snapshot=True) |
| 124 | self.assertTrue(sc.packets_have_default_beginning_clock_snapshot) |
| 125 | |
| 126 | def test_packets_have_default_beginning_clock_snapshot_raises(self): |
| 127 | with self.assertRaises(TypeError): |
| 128 | sc = self._tc.create_stream_class(packets_have_default_beginning_clock_snapshot="something") |
| 129 | |
| 130 | def test_packets_have_default_end_clock_snapshot(self): |
| 131 | sc = self._tc.create_stream_class(default_clock_class=self._cc, packets_have_default_end_clock_snapshot=True) |
| 132 | self.assertTrue(sc.packets_have_default_end_clock_snapshot) |
| 133 | |
| 134 | def test_packets_have_default_end_clock_snapshot_raises(self): |
| 135 | with self.assertRaises(TypeError): |
| 136 | sc = self._tc.create_stream_class(packets_have_default_end_clock_snapshot="something") |
| 137 | |
| 138 | def test_supports_discarded_events_without_cs(self): |
| 139 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 140 | supports_discarded_events=True) |
| 141 | self.assertTrue(sc.supports_discarded_events) |
| 142 | self.assertFalse(sc.discarded_events_have_default_clock_snapshots) |
| 143 | |
| 144 | def test_supports_discarded_events_with_cs(self): |
| 145 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 146 | supports_discarded_events=True, |
| 147 | discarded_events_have_default_clock_snapshots=True) |
| 148 | self.assertTrue(sc.supports_discarded_events) |
| 149 | self.assertTrue(sc.discarded_events_have_default_clock_snapshots) |
| 150 | |
| 151 | def test_supports_discarded_events_raises_type_error(self): |
| 152 | with self.assertRaises(TypeError): |
| 153 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 154 | supports_discarded_events=23) |
| 155 | |
| 156 | def test_discarded_events_have_default_cs_raises_type_error(self): |
| 157 | with self.assertRaises(TypeError): |
| 158 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 159 | discarded_events_have_default_clock_snapshots=23) |
| 160 | |
| 161 | def test_does_not_support_discarded_events_raises_with_cs(self): |
| 162 | with self.assertRaises(ValueError): |
| 163 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 164 | discarded_events_have_default_clock_snapshots=True) |
| 165 | |
| 166 | def test_supports_discarded_packets_without_cs(self): |
| 167 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 168 | supports_discarded_packets=True) |
| 169 | self.assertTrue(sc.supports_discarded_packets) |
| 170 | self.assertFalse(sc.discarded_packets_have_default_clock_snapshots) |
| 171 | |
| 172 | def test_supports_discarded_packets_with_cs(self): |
| 173 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 174 | supports_discarded_packets=True, |
| 175 | discarded_packets_have_default_clock_snapshots=True) |
| 176 | self.assertTrue(sc.supports_discarded_packets) |
| 177 | self.assertTrue(sc.discarded_packets_have_default_clock_snapshots) |
| 178 | |
| 179 | def test_supports_discarded_packets_raises_type_error(self): |
| 180 | with self.assertRaises(TypeError): |
| 181 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 182 | supports_discarded_packets=23) |
| 183 | |
| 184 | def test_discarded_packets_have_default_cs_raises_type_error(self): |
| 185 | with self.assertRaises(TypeError): |
| 186 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 187 | discarded_packets_have_default_clock_snapshots=23) |
| 188 | |
| 189 | def test_does_not_support_discarded_packets_raises_with_cs(self): |
| 190 | with self.assertRaises(ValueError): |
| 191 | sc = self._tc.create_stream_class(default_clock_class=self._cc, |
| 192 | discarded_packets_have_default_clock_snapshots=True) |
| 193 | |
| 194 | def test_trace_class(self): |
| 195 | sc = self._tc.create_stream_class() |
| 196 | self.assertEqual(sc.trace_class.addr, self._tc.addr) |
| 197 | |
| 198 | def _create_stream_class_with_event_classes(self): |
| 199 | sc = self._tc.create_stream_class(assigns_automatic_event_class_id=False) |
| 200 | ec1 = sc.create_event_class(id=23) |
| 201 | ec2 = sc.create_event_class(id=17) |
| 202 | return sc, ec1, ec2 |
| 203 | |
| 204 | def test_getitem(self): |
| 205 | sc, ec1, ec2 = self._create_stream_class_with_event_classes() |
| 206 | |
| 207 | self.assertEqual(sc[23].addr, ec1.addr) |
| 208 | self.assertEqual(sc[17].addr, ec2.addr) |
| 209 | |
| 210 | def test_getitem_wrong_key_type(self): |
| 211 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 212 | |
| 213 | with self.assertRaises(TypeError): |
| 214 | sc['event23'] |
| 215 | |
| 216 | def test_getitem_wrong_key(self): |
| 217 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 218 | |
| 219 | with self.assertRaises(KeyError): |
| 220 | sc[19] |
| 221 | |
| 222 | def test_len(self): |
| 223 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 224 | |
| 225 | self.assertEqual(len(sc), 2) |
| 226 | |
| 227 | def test_iter(self): |
| 228 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 229 | |
| 230 | ec_ids = sorted(sc) |
| 231 | self.assertEqual(ec_ids, [17, 23]) |