Commit | Line | Data |
---|---|---|
9cf643d1 | 1 | import unittest |
9cf643d1 | 2 | import bt2 |
060aee52 | 3 | from utils import run_in_component_init |
9cf643d1 PP |
4 | |
5 | ||
6 | class StreamClassTestCase(unittest.TestCase): | |
7 | def setUp(self): | |
060aee52 SM |
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) | |
77037b2b PP |
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) | |
060aee52 SM |
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): | |
9cf643d1 | 37 | with self.assertRaises(TypeError): |
060aee52 | 38 | self._tc.create_stream_class(name=17) |
9cf643d1 | 39 | |
060aee52 SM |
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) | |
9cf643d1 | 44 | |
060aee52 | 45 | def test_create_invalid_packet_context_field_class(self): |
9cf643d1 | 46 | with self.assertRaises(TypeError): |
060aee52 | 47 | self._tc.create_stream_class(packet_context_field_class=22) |
9cf643d1 | 48 | |
060aee52 SM |
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) | |
f6a5e476 | 53 | |
060aee52 SM |
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) | |
9cf643d1 | 57 | |
060aee52 SM |
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) | |
9cf643d1 | 61 | |
060aee52 | 62 | def test_create_invalid_default_clock_class(self): |
9cf643d1 | 63 | with self.assertRaises(TypeError): |
060aee52 | 64 | self._tc.create_stream_class(default_clock_class=12) |
9cf643d1 | 65 | |
060aee52 SM |
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) | |
9cf643d1 | 69 | |
060aee52 SM |
70 | stream = self._trace.create_stream(sc) |
71 | self.assertIsNotNone(stream.id) | |
9cf643d1 | 72 | |
060aee52 SM |
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 | ||
116779e3 | 77 | with self.assertRaises(ValueError): |
060aee52 SM |
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 | ||
116779e3 | 91 | with self.assertRaises(ValueError): |
060aee52 SM |
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 | ||
116779e3 | 105 | with self.assertRaises(ValueError): |
060aee52 SM |
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) | |
9cf643d1 | 111 | |
060aee52 SM |
112 | ec = sc.create_event_class(id=333) |
113 | self.assertEqual(ec.id, 333) | |
9cf643d1 | 114 | |
060aee52 SM |
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) | |
9cf643d1 | 118 | |
116779e3 | 119 | with self.assertRaises(ValueError): |
060aee52 SM |
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): | |
9cf643d1 | 135 | with self.assertRaises(TypeError): |
060aee52 SM |
136 | sc = self._tc.create_stream_class(packets_have_default_end_clock_snapshot="something") |
137 | ||
77037b2b PP |
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 | ||
060aee52 SM |
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 | |
9cf643d1 PP |
203 | |
204 | def test_getitem(self): | |
060aee52 SM |
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) | |
9cf643d1 PP |
209 | |
210 | def test_getitem_wrong_key_type(self): | |
060aee52 SM |
211 | sc, _, _ = self._create_stream_class_with_event_classes() |
212 | ||
9cf643d1 | 213 | with self.assertRaises(TypeError): |
060aee52 | 214 | sc['event23'] |
9cf643d1 PP |
215 | |
216 | def test_getitem_wrong_key(self): | |
060aee52 SM |
217 | sc, _, _ = self._create_stream_class_with_event_classes() |
218 | ||
9cf643d1 | 219 | with self.assertRaises(KeyError): |
060aee52 | 220 | sc[19] |
9cf643d1 PP |
221 | |
222 | def test_len(self): | |
060aee52 SM |
223 | sc, _, _ = self._create_stream_class_with_event_classes() |
224 | ||
225 | self.assertEqual(len(sc), 2) | |
9cf643d1 PP |
226 | |
227 | def test_iter(self): | |
060aee52 SM |
228 | sc, _, _ = self._create_stream_class_with_event_classes() |
229 | ||
230 | ec_ids = sorted(sc) | |
231 | self.assertEqual(ec_ids, [17, 23]) |