| 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.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(packet_context_field_class=fc) |
| 61 | self.assertEqual(sc.packet_context_field_class, fc) |
| 62 | |
| 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) |
| 66 | |
| 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) |
| 71 | |
| 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) |
| 75 | |
| 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) |
| 79 | |
| 80 | def test_create_invalid_default_clock_class(self): |
| 81 | with self.assertRaises(TypeError): |
| 82 | self._tc.create_stream_class(default_clock_class=12) |
| 83 | |
| 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) |
| 87 | |
| 88 | stream = self._trace.create_stream(sc) |
| 89 | self.assertIsNotNone(stream.id) |
| 90 | |
| 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) |
| 94 | |
| 95 | with self.assertRaises(ValueError): |
| 96 | self._trace.create_stream(sc, id=123) |
| 97 | |
| 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) |
| 101 | |
| 102 | stream = self._trace.create_stream(sc, id=333) |
| 103 | self.assertEqual(stream.id, 333) |
| 104 | |
| 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) |
| 108 | |
| 109 | with self.assertRaises(ValueError): |
| 110 | self._trace.create_stream(sc) |
| 111 | |
| 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) |
| 115 | |
| 116 | ec = sc.create_event_class() |
| 117 | self.assertIsNotNone(ec.id) |
| 118 | |
| 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) |
| 122 | |
| 123 | with self.assertRaises(ValueError): |
| 124 | sc.create_event_class(id=123) |
| 125 | |
| 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) |
| 129 | |
| 130 | ec = sc.create_event_class(id=333) |
| 131 | self.assertEqual(ec.id, 333) |
| 132 | |
| 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) |
| 136 | |
| 137 | with self.assertRaises(ValueError): |
| 138 | sc.create_event_class() |
| 139 | |
| 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) |
| 143 | |
| 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") |
| 147 | |
| 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) |
| 151 | |
| 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") |
| 155 | |
| 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) |
| 161 | |
| 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) |
| 168 | |
| 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) |
| 173 | |
| 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) |
| 178 | |
| 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) |
| 183 | |
| 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) |
| 189 | |
| 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) |
| 196 | |
| 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) |
| 201 | |
| 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) |
| 206 | |
| 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) |
| 211 | |
| 212 | def test_trace_class(self): |
| 213 | sc = self._tc.create_stream_class() |
| 214 | self.assertEqual(sc.trace_class.addr, self._tc.addr) |
| 215 | |
| 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) |
| 220 | return sc, ec1, ec2 |
| 221 | |
| 222 | def test_getitem(self): |
| 223 | sc, ec1, ec2 = self._create_stream_class_with_event_classes() |
| 224 | |
| 225 | self.assertEqual(sc[23].addr, ec1.addr) |
| 226 | self.assertEqual(sc[17].addr, ec2.addr) |
| 227 | |
| 228 | def test_getitem_wrong_key_type(self): |
| 229 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 230 | |
| 231 | with self.assertRaises(TypeError): |
| 232 | sc['event23'] |
| 233 | |
| 234 | def test_getitem_wrong_key(self): |
| 235 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 236 | |
| 237 | with self.assertRaises(KeyError): |
| 238 | sc[19] |
| 239 | |
| 240 | def test_len(self): |
| 241 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 242 | |
| 243 | self.assertEqual(len(sc), 2) |
| 244 | |
| 245 | def test_iter(self): |
| 246 | sc, _, _ = self._create_stream_class_with_event_classes() |
| 247 | |
| 248 | ec_ids = sorted(sc) |
| 249 | self.assertEqual(ec_ids, [17, 23]) |