Commit | Line | Data |
---|---|---|
32d2d479 MJ |
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 | ||
32656995 | 19 | import unittest |
32656995 | 20 | import bt2 |
9cbe0c59 FD |
21 | from utils import get_default_trace_class, TestOutputPortMessageIterator |
22 | from bt2 import value as bt2_value | |
23 | from bt2 import field_class as bt2_field_class | |
24 | ||
25 | ||
26 | def _create_stream(tc, ctx_field_classes): | |
27 | packet_context_fc = tc.create_structure_field_class() | |
28 | for name, fc in ctx_field_classes: | |
29 | packet_context_fc.append_member(name, fc) | |
30 | ||
31 | trace = tc() | |
32 | stream_class = tc.create_stream_class( | |
33 | packet_context_field_class=packet_context_fc, supports_packets=True | |
34 | ) | |
35 | ||
36 | stream = trace.create_stream(stream_class) | |
37 | return stream | |
38 | ||
39 | ||
40 | def _create_const_field_class(tc, field_class, value_setter_fn): | |
41 | field_name = 'const field' | |
42 | ||
43 | class MyIter(bt2._UserMessageIterator): | |
9415de1c | 44 | def __init__(self, config, self_port_output): |
9cbe0c59 FD |
45 | nonlocal field_class |
46 | nonlocal value_setter_fn | |
47 | stream = _create_stream(tc, [(field_name, field_class)]) | |
48 | packet = stream.create_packet() | |
49 | ||
50 | value_setter_fn(packet.context_field[field_name]) | |
51 | ||
52 | self._msgs = [ | |
53 | self._create_stream_beginning_message(stream), | |
54 | self._create_packet_beginning_message(packet), | |
55 | ] | |
56 | ||
57 | def __next__(self): | |
58 | if len(self._msgs) == 0: | |
59 | raise StopIteration | |
60 | ||
61 | return self._msgs.pop(0) | |
62 | ||
63 | class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter): | |
e3250e61 | 64 | def __init__(self, config, params, obj): |
9cbe0c59 FD |
65 | self._add_output_port('out', params) |
66 | ||
67 | graph = bt2.Graph() | |
68 | src_comp = graph.add_component(MySrc, 'my_source', None) | |
69 | msg_iter = TestOutputPortMessageIterator(graph, src_comp.output_ports['out']) | |
70 | ||
71 | # Ignore first message, stream beginning | |
72 | _ = next(msg_iter) | |
73 | packet_beg_msg = next(msg_iter) | |
74 | ||
75 | return packet_beg_msg.packet.context_field[field_name].cls | |
32656995 SM |
76 | |
77 | ||
b2df5780 PP |
78 | class _TestFieldClass: |
79 | def test_create_user_attributes(self): | |
80 | fc = self._create_default_field_class(user_attributes={'salut': 23}) | |
81 | self.assertEqual(fc.user_attributes, {'salut': 23}) | |
9cbe0c59 FD |
82 | self.assertIs(type(fc.user_attributes), bt2_value.MapValue) |
83 | ||
84 | def test_const_create_user_attributes(self): | |
85 | fc = self._create_default_const_field_class(user_attributes={'salut': 23}) | |
86 | self.assertEqual(fc.user_attributes, {'salut': 23}) | |
87 | self.assertIs(type(fc.user_attributes), bt2_value._MapValueConst) | |
b2df5780 PP |
88 | |
89 | def test_create_invalid_user_attributes(self): | |
90 | with self.assertRaises(TypeError): | |
91 | self._create_default_field_class(user_attributes=object()) | |
92 | ||
93 | def test_create_invalid_user_attributes_value_type(self): | |
94 | with self.assertRaises(TypeError): | |
95 | self._create_default_field_class(user_attributes=23) | |
96 | ||
97 | ||
98 | class BoolFieldClassTestCase(_TestFieldClass, unittest.TestCase): | |
9cbe0c59 FD |
99 | @staticmethod |
100 | def _const_value_setter(field): | |
c82e5122 | 101 | field.value = False |
9cbe0c59 | 102 | |
b2df5780 | 103 | def _create_default_field_class(self, **kwargs): |
a07f15cb | 104 | tc = get_default_trace_class() |
b2df5780 PP |
105 | return tc.create_bool_field_class(**kwargs) |
106 | ||
9cbe0c59 FD |
107 | def _create_default_const_field_class(self, *args, **kwargs): |
108 | tc = get_default_trace_class() | |
109 | fc = tc.create_bool_field_class(*args, **kwargs) | |
110 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
111 | ||
b2df5780 PP |
112 | def setUp(self): |
113 | self._fc = self._create_default_field_class() | |
9cbe0c59 | 114 | self._fc_const = self._create_default_const_field_class() |
a07f15cb PP |
115 | |
116 | def test_create_default(self): | |
117 | self.assertIsNotNone(self._fc) | |
b2df5780 | 118 | self.assertEqual(len(self._fc.user_attributes), 0) |
a07f15cb PP |
119 | |
120 | ||
b2df5780 | 121 | class BitArrayFieldClassTestCase(_TestFieldClass, unittest.TestCase): |
9cbe0c59 FD |
122 | @staticmethod |
123 | def _const_value_setter(field): | |
c82e5122 | 124 | field.value = [] |
9cbe0c59 | 125 | |
b2df5780 PP |
126 | def _create_field_class(self, *args, **kwargs): |
127 | tc = get_default_trace_class() | |
128 | return tc.create_bit_array_field_class(*args, **kwargs) | |
129 | ||
130 | def _create_default_field_class(self, **kwargs): | |
131 | return self._create_field_class(17, **kwargs) | |
132 | ||
9cbe0c59 FD |
133 | def _create_default_const_field_class(self, *args, **kwargs): |
134 | tc = get_default_trace_class() | |
135 | fc = tc.create_bit_array_field_class(17, **kwargs) | |
136 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
137 | ||
6b29f2d4 | 138 | def setUp(self): |
b2df5780 | 139 | self._fc = self._create_default_field_class() |
6b29f2d4 PP |
140 | |
141 | def test_create_default(self): | |
142 | self.assertIsNotNone(self._fc) | |
b2df5780 | 143 | self.assertEqual(len(self._fc.user_attributes), 0) |
6b29f2d4 PP |
144 | |
145 | def test_create_length_out_of_range(self): | |
146 | with self.assertRaises(ValueError): | |
b2df5780 | 147 | self._create_field_class(65) |
6b29f2d4 PP |
148 | |
149 | def test_create_length_zero(self): | |
150 | with self.assertRaises(ValueError): | |
b2df5780 | 151 | self._create_field_class(0) |
6b29f2d4 PP |
152 | |
153 | def test_create_length_invalid_type(self): | |
154 | with self.assertRaises(TypeError): | |
b2df5780 | 155 | self._create_field_class('lel') |
6b29f2d4 PP |
156 | |
157 | def test_length_prop(self): | |
158 | self.assertEqual(self._fc.length, 17) | |
159 | ||
160 | ||
c8820b76 SM |
161 | class _TestIntegerFieldClassProps: |
162 | def test_create_default(self): | |
b2df5780 | 163 | fc = self._create_default_field_class() |
c8820b76 SM |
164 | self.assertEqual(fc.field_value_range, 64) |
165 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL) | |
b2df5780 | 166 | self.assertEqual(len(fc.user_attributes), 0) |
32656995 | 167 | |
c8820b76 | 168 | def test_create_range(self): |
b2df5780 | 169 | fc = self._create_field_class(field_value_range=35) |
c8820b76 | 170 | self.assertEqual(fc.field_value_range, 35) |
32656995 | 171 | |
b2df5780 | 172 | fc = self._create_field_class(36) |
c8820b76 | 173 | self.assertEqual(fc.field_value_range, 36) |
32656995 | 174 | |
c8820b76 SM |
175 | def test_create_invalid_range(self): |
176 | with self.assertRaises(TypeError): | |
b2df5780 | 177 | self._create_field_class('yes') |
32656995 | 178 | |
c8820b76 | 179 | with self.assertRaises(TypeError): |
b2df5780 | 180 | self._create_field_class(field_value_range='yes') |
32656995 | 181 | |
32656995 | 182 | with self.assertRaises(ValueError): |
b2df5780 | 183 | self._create_field_class(field_value_range=-2) |
32656995 | 184 | |
c8820b76 | 185 | with self.assertRaises(ValueError): |
b2df5780 | 186 | self._create_field_class(field_value_range=0) |
32656995 | 187 | |
c8820b76 | 188 | def test_create_base(self): |
b2df5780 | 189 | fc = self._create_field_class( |
61d96b89 FD |
190 | preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL |
191 | ) | |
c8820b76 | 192 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL) |
32656995 | 193 | |
c8820b76 | 194 | def test_create_invalid_base_type(self): |
32656995 | 195 | with self.assertRaises(TypeError): |
b2df5780 | 196 | self._create_field_class(preferred_display_base='yes') |
32656995 | 197 | |
c8820b76 SM |
198 | def test_create_invalid_base_value(self): |
199 | with self.assertRaises(ValueError): | |
b2df5780 | 200 | self._create_field_class(preferred_display_base=444) |
32656995 | 201 | |
c8820b76 | 202 | def test_create_full(self): |
b2df5780 PP |
203 | fc = self._create_field_class( |
204 | 24, preferred_display_base=bt2.IntegerDisplayBase.OCTAL | |
205 | ) | |
c8820b76 SM |
206 | self.assertEqual(fc.field_value_range, 24) |
207 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.OCTAL) | |
32656995 SM |
208 | |
209 | ||
b2df5780 PP |
210 | class SignedIntegerFieldClassTestCase( |
211 | _TestIntegerFieldClassProps, _TestFieldClass, unittest.TestCase | |
212 | ): | |
9cbe0c59 FD |
213 | @staticmethod |
214 | def _const_value_setter(field): | |
c82e5122 | 215 | field.value = -18 |
9cbe0c59 | 216 | |
b2df5780 PP |
217 | def _create_field_class(self, *args, **kwargs): |
218 | tc = get_default_trace_class() | |
219 | return tc.create_signed_integer_field_class(*args, **kwargs) | |
32656995 | 220 | |
9cbe0c59 FD |
221 | def _create_default_const_field_class(self, *args, **kwargs): |
222 | tc = get_default_trace_class() | |
223 | fc = tc.create_signed_integer_field_class(*args, **kwargs) | |
224 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
225 | ||
b2df5780 PP |
226 | _create_default_field_class = _create_field_class |
227 | ||
228 | ||
229 | class UnsignedIntegerFieldClassTestCase( | |
230 | _TestIntegerFieldClassProps, _TestFieldClass, unittest.TestCase | |
231 | ): | |
9cbe0c59 FD |
232 | @staticmethod |
233 | def _const_value_setter(field): | |
c82e5122 | 234 | field.value = 18 |
9cbe0c59 | 235 | |
b2df5780 PP |
236 | def _create_field_class(self, *args, **kwargs): |
237 | tc = get_default_trace_class() | |
238 | return tc.create_unsigned_integer_field_class(*args, **kwargs) | |
239 | ||
9cbe0c59 FD |
240 | def _create_default_const_field_class(self, *args, **kwargs): |
241 | tc = get_default_trace_class() | |
242 | fc = tc.create_signed_integer_field_class(*args, **kwargs) | |
243 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
244 | ||
b2df5780 | 245 | _create_default_field_class = _create_field_class |
32656995 | 246 | |
b2df5780 | 247 | |
76276a81 | 248 | class SingleRealFieldClassTestCase(_TestFieldClass, unittest.TestCase): |
9cbe0c59 FD |
249 | @staticmethod |
250 | def _const_value_setter(field): | |
76276a81 | 251 | field.value = -18.0 |
9cbe0c59 | 252 | |
b2df5780 PP |
253 | def _create_field_class(self, *args, **kwargs): |
254 | tc = get_default_trace_class() | |
76276a81 | 255 | return tc.create_single_precision_real_field_class(*args, **kwargs) |
b2df5780 | 256 | |
9cbe0c59 FD |
257 | def _create_default_const_field_class(self, *args, **kwargs): |
258 | tc = get_default_trace_class() | |
76276a81 | 259 | fc = tc.create_single_precision_real_field_class(*args, **kwargs) |
9cbe0c59 FD |
260 | return _create_const_field_class(tc, fc, self._const_value_setter) |
261 | ||
b2df5780 | 262 | _create_default_field_class = _create_field_class |
32656995 | 263 | |
c8820b76 | 264 | def test_create_default(self): |
b2df5780 | 265 | fc = self._create_field_class() |
b2df5780 | 266 | self.assertEqual(len(fc.user_attributes), 0) |
32656995 | 267 | |
c8820b76 | 268 | |
76276a81 FD |
269 | class DoubleRealFieldClassTestCase(_TestFieldClass, unittest.TestCase): |
270 | @staticmethod | |
271 | def _const_value_setter(field): | |
272 | field.value = -18.0 | |
273 | ||
274 | def _create_field_class(self, *args, **kwargs): | |
275 | tc = get_default_trace_class() | |
276 | return tc.create_double_precision_real_field_class(*args, **kwargs) | |
277 | ||
278 | def _create_default_const_field_class(self, *args, **kwargs): | |
279 | tc = get_default_trace_class() | |
280 | fc = tc.create_double_precision_real_field_class(*args, **kwargs) | |
281 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
282 | ||
283 | _create_default_field_class = _create_field_class | |
284 | ||
285 | def test_create_default(self): | |
286 | fc = self._create_field_class() | |
287 | self.assertEqual(len(fc.user_attributes), 0) | |
32656995 | 288 | |
32656995 | 289 | |
c8820b76 SM |
290 | # Converts an _EnumerationFieldClassMapping to a list of ranges: |
291 | # | |
292 | # [(lower0, upper0), (lower1, upper1), ...] | |
293 | ||
61d96b89 | 294 | |
02b61fe0 PP |
295 | def enum_mapping_to_set(mapping): |
296 | return {(x.lower, x.upper) for x in mapping.ranges} | |
32656995 | 297 | |
32656995 | 298 | |
02b61fe0 | 299 | class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): |
c8820b76 | 300 | def setUp(self): |
02b61fe0 | 301 | self._spec_set_up() |
b2df5780 | 302 | self._fc = self._create_default_field_class() |
9cbe0c59 | 303 | self._fc_const = self._create_default_const_field_class() |
c8820b76 SM |
304 | |
305 | def test_create_from_invalid_type(self): | |
32656995 | 306 | with self.assertRaises(TypeError): |
b2df5780 | 307 | self._create_field_class('coucou') |
32656995 | 308 | |
c8820b76 | 309 | def test_add_mapping_simple(self): |
02b61fe0 | 310 | self._fc.add_mapping('hello', self._ranges1) |
c8820b76 SM |
311 | mapping = self._fc['hello'] |
312 | self.assertEqual(mapping.label, 'hello') | |
02b61fe0 | 313 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 314 | |
9cbe0c59 FD |
315 | def test_const_add_mapping(self): |
316 | with self.assertRaises(AttributeError): | |
317 | self._fc_const.add_mapping('hello', self._ranges1) | |
318 | ||
c8820b76 | 319 | def test_add_mapping_simple_kwargs(self): |
02b61fe0 | 320 | self._fc.add_mapping(label='hello', ranges=self._ranges1) |
c8820b76 SM |
321 | mapping = self._fc['hello'] |
322 | self.assertEqual(mapping.label, 'hello') | |
02b61fe0 | 323 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 324 | |
02b61fe0 PP |
325 | def test_add_mapping_invalid_name(self): |
326 | with self.assertRaises(TypeError): | |
327 | self._fc.add_mapping(17, self._ranges1) | |
32656995 | 328 | |
02b61fe0 PP |
329 | def test_add_mapping_invalid_range(self): |
330 | with self.assertRaises(TypeError): | |
331 | self._fc.add_mapping('allo', 'meow') | |
c8820b76 | 332 | |
02b61fe0 | 333 | def test_add_mapping_dup_label(self): |
3b2be708 | 334 | with self.assertRaises(ValueError): |
02b61fe0 PP |
335 | self._fc.add_mapping('a', self._ranges1) |
336 | self._fc.add_mapping('a', self._ranges2) | |
c8820b76 | 337 | |
02b61fe0 | 338 | def test_add_mapping_invalid_ranges_signedness(self): |
32656995 | 339 | with self.assertRaises(TypeError): |
02b61fe0 | 340 | self._fc.add_mapping('allo', self._inval_ranges) |
32656995 | 341 | |
c8820b76 | 342 | def test_iadd(self): |
02b61fe0 | 343 | self._fc.add_mapping('c', self._ranges1) |
32656995 | 344 | |
61d96b89 | 345 | self._fc += [('d', self._ranges2), ('e', self._ranges3)] |
32656995 | 346 | |
02b61fe0 | 347 | self.assertEqual(len(self._fc), 3) |
c8820b76 | 348 | self.assertEqual(self._fc['c'].label, 'c') |
02b61fe0 | 349 | self.assertEqual(self._fc['c'].ranges, self._ranges1) |
c8820b76 | 350 | self.assertEqual(self._fc['d'].label, 'd') |
02b61fe0 | 351 | self.assertEqual(self._fc['d'].ranges, self._ranges2) |
c8820b76 | 352 | self.assertEqual(self._fc['e'].label, 'e') |
02b61fe0 | 353 | self.assertEqual(self._fc['e'].ranges, self._ranges3) |
32656995 | 354 | |
9cbe0c59 FD |
355 | def test_const_iadd(self): |
356 | with self.assertRaises(TypeError): | |
357 | self._fc_const += [('d', self._ranges2), ('e', self._ranges3)] | |
358 | ||
c8820b76 SM |
359 | def test_bool_op(self): |
360 | self.assertFalse(self._fc) | |
02b61fe0 | 361 | self._fc.add_mapping('a', self._ranges1) |
c8820b76 | 362 | self.assertTrue(self._fc) |
32656995 | 363 | |
c8820b76 | 364 | def test_len(self): |
02b61fe0 PP |
365 | self._fc.add_mapping('a', self._ranges1) |
366 | self._fc.add_mapping('b', self._ranges2) | |
367 | self._fc.add_mapping('c', self._ranges3) | |
c8820b76 | 368 | self.assertEqual(len(self._fc), 3) |
32656995 | 369 | |
c8820b76 | 370 | def test_getitem(self): |
02b61fe0 PP |
371 | self._fc.add_mapping('a', self._ranges1) |
372 | self._fc.add_mapping('b', self._ranges2) | |
373 | self._fc.add_mapping('c', self._ranges3) | |
c8820b76 | 374 | mapping = self._fc['a'] |
c8820b76 | 375 | self.assertEqual(mapping.label, 'a') |
02b61fe0 | 376 | self.assertEqual(mapping.ranges, self._ranges1) |
8d99efe6 FD |
377 | self.assertIs(type(mapping), self._MAPPING_CLASS) |
378 | self.assertIs(type(mapping.ranges), self._CONST_RANGE_SET_CLASS) | |
32656995 | 379 | |
02b61fe0 | 380 | def test_getitem_nonexistent(self): |
c8820b76 SM |
381 | with self.assertRaises(KeyError): |
382 | self._fc['doesnotexist'] | |
32656995 | 383 | |
c8820b76 | 384 | def test_iter(self): |
02b61fe0 PP |
385 | self._fc.add_mapping('a', self._ranges1) |
386 | self._fc.add_mapping('b', self._ranges2) | |
387 | self._fc.add_mapping('c', self._ranges3) | |
32656995 | 388 | |
c8820b76 SM |
389 | # This exercises iteration. |
390 | labels = sorted(self._fc) | |
32656995 | 391 | |
02b61fe0 | 392 | self.assertEqual(labels, ['a', 'b', 'c']) |
32656995 | 393 | |
c8820b76 | 394 | def test_find_by_value(self): |
02b61fe0 PP |
395 | self._fc.add_mapping('a', self._ranges1) |
396 | self._fc.add_mapping('b', self._ranges2) | |
397 | self._fc.add_mapping('c', self._ranges3) | |
398 | mappings = self._fc.mappings_for_value(self._value_in_range_1_and_3) | |
399 | labels = set([mapping.label for mapping in mappings]) | |
400 | expected_labels = set(['a', 'c']) | |
401 | self.assertEqual(labels, expected_labels) | |
402 | ||
403 | ||
61d96b89 | 404 | class UnsignedEnumerationFieldClassTestCase( |
b2df5780 | 405 | _EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase |
61d96b89 | 406 | ): |
8d99efe6 FD |
407 | _MAPPING_CLASS = bt2_field_class._UnsignedEnumerationFieldClassMappingConst |
408 | _RANGE_SET_CLASS = bt2.UnsignedIntegerRangeSet | |
409 | _CONST_RANGE_SET_CLASS = bt2._UnsignedIntegerRangeSetConst | |
410 | ||
02b61fe0 PP |
411 | def _spec_set_up(self): |
412 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) | |
413 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) | |
414 | self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 22), (48, 99)]) | |
415 | self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, -5), (48, 1928)]) | |
416 | self._value_in_range_1_and_3 = 20 | |
b2df5780 | 417 | |
9cbe0c59 FD |
418 | @staticmethod |
419 | def _const_value_setter(field): | |
c82e5122 | 420 | field.value = 0 |
9cbe0c59 | 421 | |
b2df5780 PP |
422 | def _create_field_class(self, *args, **kwargs): |
423 | tc = get_default_trace_class() | |
424 | return tc.create_unsigned_enumeration_field_class(*args, **kwargs) | |
425 | ||
9cbe0c59 FD |
426 | def _create_default_const_field_class(self, *args, **kwargs): |
427 | tc = get_default_trace_class() | |
428 | fc = tc.create_unsigned_enumeration_field_class(*args, **kwargs) | |
429 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
430 | ||
b2df5780 | 431 | _create_default_field_class = _create_field_class |
32656995 | 432 | |
32656995 | 433 | |
61d96b89 | 434 | class SignedEnumerationFieldClassTestCase( |
b2df5780 | 435 | _EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase |
61d96b89 | 436 | ): |
8d99efe6 FD |
437 | _MAPPING_CLASS = bt2_field_class._SignedEnumerationFieldClassMappingConst |
438 | _RANGE_SET_CLASS = bt2.SignedIntegerRangeSet | |
439 | _CONST_RANGE_SET_CLASS = bt2._SignedIntegerRangeSetConst | |
440 | ||
02b61fe0 PP |
441 | def _spec_set_up(self): |
442 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) | |
443 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) | |
444 | self._ranges3 = bt2.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)]) | |
445 | self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
446 | self._value_in_range_1_and_3 = -7 | |
32656995 | 447 | |
9cbe0c59 FD |
448 | @staticmethod |
449 | def _const_value_setter(field): | |
c82e5122 | 450 | field.value = 0 |
9cbe0c59 | 451 | |
b2df5780 PP |
452 | def _create_field_class(self, *args, **kwargs): |
453 | tc = get_default_trace_class() | |
454 | return tc.create_signed_enumeration_field_class(*args, **kwargs) | |
455 | ||
9cbe0c59 FD |
456 | def _create_default_const_field_class(self, *args, **kwargs): |
457 | tc = get_default_trace_class() | |
458 | fc = tc.create_signed_enumeration_field_class(*args, **kwargs) | |
459 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
460 | ||
b2df5780 | 461 | _create_default_field_class = _create_field_class |
32656995 | 462 | |
b2df5780 PP |
463 | |
464 | class StringFieldClassTestCase(_TestFieldClass, unittest.TestCase): | |
9cbe0c59 FD |
465 | @staticmethod |
466 | def _const_value_setter(field): | |
c82e5122 | 467 | field.value = 'chaine' |
9cbe0c59 | 468 | |
b2df5780 | 469 | def _create_field_class(self, *args, **kwargs): |
c8820b76 | 470 | tc = get_default_trace_class() |
b2df5780 PP |
471 | return tc.create_string_field_class(*args, **kwargs) |
472 | ||
9cbe0c59 FD |
473 | def _create_default_const_field_class(self, *args, **kwargs): |
474 | tc = get_default_trace_class() | |
475 | fc = tc.create_string_field_class(*args, **kwargs) | |
476 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
477 | ||
b2df5780 PP |
478 | _create_default_field_class = _create_field_class |
479 | ||
480 | def setUp(self): | |
481 | self._fc = self._create_default_field_class() | |
32656995 SM |
482 | |
483 | def test_create_default(self): | |
c8820b76 | 484 | self.assertIsNotNone(self._fc) |
b2df5780 | 485 | self.assertEqual(len(self._fc.user_attributes), 0) |
32656995 | 486 | |
32656995 | 487 | |
61d96b89 | 488 | class _TestElementContainer: |
02b61fe0 PP |
489 | def setUp(self): |
490 | self._tc = get_default_trace_class() | |
b2df5780 | 491 | self._fc = self._create_default_field_class() |
9cbe0c59 | 492 | self._fc_const = self._create_default_const_field_class() |
02b61fe0 PP |
493 | |
494 | def test_create_default(self): | |
495 | self.assertIsNotNone(self._fc) | |
b2df5780 | 496 | self.assertEqual(len(self._fc.user_attributes), 0) |
02b61fe0 | 497 | |
c8820b76 SM |
498 | def test_append_element(self): |
499 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
500 | self._append_element_method(self._fc, 'int32', int_field_class) | |
02b61fe0 | 501 | field_class = self._fc['int32'].field_class |
c8820b76 | 502 | self.assertEqual(field_class.addr, int_field_class.addr) |
32656995 | 503 | |
02b61fe0 | 504 | def test_append_element_kwargs(self): |
c8820b76 SM |
505 | int_field_class = self._tc.create_signed_integer_field_class(32) |
506 | self._append_element_method(self._fc, name='int32', field_class=int_field_class) | |
02b61fe0 | 507 | field_class = self._fc['int32'].field_class |
c8820b76 SM |
508 | self.assertEqual(field_class.addr, int_field_class.addr) |
509 | ||
510 | def test_append_element_invalid_name(self): | |
511 | sub_fc = self._tc.create_string_field_class() | |
32656995 | 512 | |
32656995 | 513 | with self.assertRaises(TypeError): |
c8820b76 | 514 | self._append_element_method(self._fc, 23, sub_fc) |
32656995 | 515 | |
c8820b76 | 516 | def test_append_element_invalid_field_class(self): |
32656995 | 517 | with self.assertRaises(TypeError): |
c8820b76 | 518 | self._append_element_method(self._fc, 'yes', object()) |
32656995 | 519 | |
02b61fe0 PP |
520 | def test_append_element_dup_name(self): |
521 | sub_fc1 = self._tc.create_string_field_class() | |
522 | sub_fc2 = self._tc.create_string_field_class() | |
523 | ||
3b2be708 | 524 | with self.assertRaises(ValueError): |
02b61fe0 PP |
525 | self._append_element_method(self._fc, 'yes', sub_fc1) |
526 | self._append_element_method(self._fc, 'yes', sub_fc2) | |
527 | ||
9cbe0c59 FD |
528 | def test_attr_field_class(self): |
529 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
530 | self._append_element_method(self._fc, 'int32', int_field_class) | |
531 | field_class = self._fc['int32'].field_class | |
532 | ||
533 | self.assertIs(type(field_class), bt2_field_class._SignedIntegerFieldClass) | |
534 | ||
535 | def test_const_attr_field_class(self): | |
536 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
537 | self._append_element_method(self._fc, 'int32', int_field_class) | |
538 | field_class = self._fc['int32'].field_class | |
539 | const_fc = _create_const_field_class( | |
540 | self._tc, self._fc, self._const_value_setter | |
541 | ) | |
542 | field_class = const_fc['int32'].field_class | |
543 | ||
544 | self.assertIs(type(field_class), bt2_field_class._SignedIntegerFieldClassConst) | |
545 | ||
32656995 | 546 | def test_iadd(self): |
76276a81 | 547 | a_field_class = self._tc.create_single_precision_real_field_class() |
c8820b76 SM |
548 | b_field_class = self._tc.create_signed_integer_field_class(17) |
549 | self._append_element_method(self._fc, 'a_float', a_field_class) | |
550 | self._append_element_method(self._fc, 'b_int', b_field_class) | |
02b61fe0 | 551 | c_field_class = self._tc.create_string_field_class() |
61d96b89 FD |
552 | d_field_class = self._tc.create_signed_enumeration_field_class( |
553 | field_value_range=32 | |
554 | ) | |
02b61fe0 PP |
555 | e_field_class = self._tc.create_structure_field_class() |
556 | self._fc += [ | |
557 | ('c_string', c_field_class), | |
558 | ('d_enum', d_field_class), | |
559 | ('e_struct', e_field_class), | |
560 | ] | |
561 | self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr) | |
562 | self.assertEqual(self._fc['a_float'].name, 'a_float') | |
563 | self.assertEqual(self._fc['b_int'].field_class.addr, b_field_class.addr) | |
564 | self.assertEqual(self._fc['b_int'].name, 'b_int') | |
565 | self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr) | |
566 | self.assertEqual(self._fc['c_string'].name, 'c_string') | |
567 | self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr) | |
568 | self.assertEqual(self._fc['d_enum'].name, 'd_enum') | |
569 | self.assertEqual(self._fc['e_struct'].field_class.addr, e_field_class.addr) | |
570 | self.assertEqual(self._fc['e_struct'].name, 'e_struct') | |
32656995 | 571 | |
9cbe0c59 | 572 | def test_const_iadd(self): |
76276a81 | 573 | a_field_class = self._tc.create_single_precision_real_field_class() |
9cbe0c59 FD |
574 | with self.assertRaises(TypeError): |
575 | self._fc_const += a_field_class | |
576 | ||
32656995 SM |
577 | def test_bool_op(self): |
578 | self.assertFalse(self._fc) | |
c8820b76 | 579 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
32656995 SM |
580 | self.assertTrue(self._fc) |
581 | ||
582 | def test_len(self): | |
02b61fe0 PP |
583 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
584 | self._append_element_method(self._fc, 'b', self._tc.create_string_field_class()) | |
585 | self._append_element_method(self._fc, 'c', self._tc.create_string_field_class()) | |
32656995 SM |
586 | self.assertEqual(len(self._fc), 3) |
587 | ||
588 | def test_getitem(self): | |
c8820b76 SM |
589 | a_fc = self._tc.create_signed_integer_field_class(32) |
590 | b_fc = self._tc.create_string_field_class() | |
76276a81 | 591 | c_fc = self._tc.create_single_precision_real_field_class() |
c8820b76 SM |
592 | self._append_element_method(self._fc, 'a', a_fc) |
593 | self._append_element_method(self._fc, 'b', b_fc) | |
594 | self._append_element_method(self._fc, 'c', c_fc) | |
02b61fe0 PP |
595 | self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr) |
596 | self.assertEqual(self._fc['b'].name, 'b') | |
32656995 SM |
597 | |
598 | def test_getitem_invalid_key_type(self): | |
599 | with self.assertRaises(TypeError): | |
600 | self._fc[0] | |
601 | ||
602 | def test_getitem_invalid_key(self): | |
603 | with self.assertRaises(KeyError): | |
604 | self._fc['no way'] | |
605 | ||
606 | def test_contains(self): | |
607 | self.assertFalse('a' in self._fc) | |
c8820b76 | 608 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
32656995 SM |
609 | self.assertTrue('a' in self._fc) |
610 | ||
611 | def test_iter(self): | |
c8820b76 SM |
612 | a_fc = self._tc.create_signed_integer_field_class(32) |
613 | b_fc = self._tc.create_string_field_class() | |
76276a81 | 614 | c_fc = self._tc.create_single_precision_real_field_class() |
61d96b89 | 615 | elements = (('a', a_fc), ('b', b_fc), ('c', c_fc)) |
32656995 | 616 | |
02b61fe0 PP |
617 | for elem in elements: |
618 | self._append_element_method(self._fc, *elem) | |
32656995 | 619 | |
02b61fe0 PP |
620 | for (name, element), test_elem in zip(self._fc.items(), elements): |
621 | self.assertEqual(element.name, test_elem[0]) | |
622 | self.assertEqual(name, element.name) | |
623 | self.assertEqual(element.field_class.addr, test_elem[1].addr) | |
b2df5780 | 624 | self.assertEqual(len(element.user_attributes), 0) |
32656995 SM |
625 | |
626 | def test_at_index(self): | |
c8820b76 SM |
627 | a_fc = self._tc.create_signed_integer_field_class(32) |
628 | b_fc = self._tc.create_string_field_class() | |
76276a81 | 629 | c_fc = self._tc.create_single_precision_real_field_class() |
c8820b76 SM |
630 | self._append_element_method(self._fc, 'c', c_fc) |
631 | self._append_element_method(self._fc, 'a', a_fc) | |
632 | self._append_element_method(self._fc, 'b', b_fc) | |
02b61fe0 PP |
633 | elem = self._at_index_method(self._fc, 1) |
634 | self.assertEqual(elem.field_class.addr, a_fc.addr) | |
635 | self.assertEqual(elem.name, 'a') | |
32656995 SM |
636 | |
637 | def test_at_index_invalid(self): | |
61d96b89 FD |
638 | self._append_element_method( |
639 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) | |
640 | ) | |
32656995 SM |
641 | |
642 | with self.assertRaises(TypeError): | |
c8820b76 | 643 | self._at_index_method(self._fc, 'yes') |
32656995 SM |
644 | |
645 | def test_at_index_out_of_bounds_after(self): | |
61d96b89 FD |
646 | self._append_element_method( |
647 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) | |
648 | ) | |
32656995 SM |
649 | |
650 | with self.assertRaises(IndexError): | |
c8820b76 | 651 | self._at_index_method(self._fc, len(self._fc)) |
32656995 | 652 | |
b2df5780 PP |
653 | def test_user_attributes(self): |
654 | self._append_element_method( | |
655 | self._fc, | |
656 | 'c', | |
657 | self._tc.create_string_field_class(), | |
658 | user_attributes={'salut': 23}, | |
659 | ) | |
660 | self.assertEqual(self._fc['c'].user_attributes, {'salut': 23}) | |
9cbe0c59 FD |
661 | self.assertIs(type(self._fc.user_attributes), bt2_value.MapValue) |
662 | self.assertIs(type(self._fc['c'].user_attributes), bt2_value.MapValue) | |
32656995 | 663 | |
b2df5780 PP |
664 | def test_invalid_user_attributes(self): |
665 | with self.assertRaises(TypeError): | |
666 | self._append_element_method( | |
667 | self._fc, | |
668 | 'c', | |
669 | self._tc.create_string_field_class(), | |
670 | user_attributes=object(), | |
671 | ) | |
672 | ||
673 | def test_invalid_user_attributes_value_type(self): | |
674 | with self.assertRaises(TypeError): | |
675 | self._append_element_method( | |
676 | self._fc, 'c', self._tc.create_string_field_class(), user_attributes=23 | |
677 | ) | |
678 | ||
679 | ||
680 | class StructureFieldClassTestCase( | |
681 | _TestFieldClass, _TestElementContainer, unittest.TestCase | |
682 | ): | |
c946c9de PP |
683 | _append_element_method = staticmethod(bt2._StructureFieldClass.append_member) |
684 | _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index) | |
02b61fe0 | 685 | |
9cbe0c59 FD |
686 | @staticmethod |
687 | def _const_value_setter(field): | |
688 | field.value = {} | |
689 | ||
b2df5780 PP |
690 | def _create_field_class(self, *args, **kwargs): |
691 | tc = get_default_trace_class() | |
692 | return tc.create_structure_field_class(*args, **kwargs) | |
02b61fe0 | 693 | |
9cbe0c59 FD |
694 | def _create_default_const_field_class(self, *args, **kwargs): |
695 | tc = get_default_trace_class() | |
696 | fc = tc.create_structure_field_class(*args, **kwargs) | |
697 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
698 | ||
b2df5780 PP |
699 | _create_default_field_class = _create_field_class |
700 | ||
9cbe0c59 FD |
701 | def test_const_member_field_class(self): |
702 | def _real_value_setter(field): | |
703 | field.value = {'real': 0} | |
704 | ||
705 | tc = get_default_trace_class() | |
706 | fc = tc.create_structure_field_class() | |
76276a81 | 707 | member_fc = self._tc.create_single_precision_real_field_class() |
9cbe0c59 FD |
708 | fc.append_member('real', member_fc) |
709 | const_fc = _create_const_field_class(tc, fc, _real_value_setter) | |
710 | ||
711 | self.assertIs( | |
76276a81 FD |
712 | type(const_fc['real'].field_class), |
713 | bt2_field_class._SinglePrecisionRealFieldClassConst, | |
9cbe0c59 FD |
714 | ) |
715 | ||
716 | def test_member_field_class(self): | |
717 | tc = get_default_trace_class() | |
718 | fc = tc.create_structure_field_class() | |
76276a81 | 719 | member_fc = self._tc.create_single_precision_real_field_class() |
9cbe0c59 FD |
720 | fc.append_member('real', member_fc) |
721 | ||
76276a81 FD |
722 | self.assertIs( |
723 | type(fc['real'].field_class), bt2_field_class._SinglePrecisionRealFieldClass | |
724 | ) | |
9cbe0c59 | 725 | |
b2df5780 | 726 | |
467673c1 | 727 | class OptionWithoutSelectorFieldClassTestCase(_TestFieldClass, unittest.TestCase): |
9cbe0c59 FD |
728 | @staticmethod |
729 | def _const_value_setter(field): | |
730 | field.has_field = True | |
731 | field.value = 12 | |
732 | ||
b2df5780 | 733 | def _create_default_field_class(self, *args, **kwargs): |
467673c1 PP |
734 | return self._tc.create_option_without_selector_field_class( |
735 | self._content_fc, *args, **kwargs | |
736 | ) | |
02b61fe0 | 737 | |
9cbe0c59 | 738 | def _create_default_const_field_class(self, *args, **kwargs): |
467673c1 PP |
739 | fc = self._tc.create_option_without_selector_field_class( |
740 | self._content_fc, *args, **kwargs | |
741 | ) | |
9cbe0c59 FD |
742 | return _create_const_field_class(self._tc, fc, self._const_value_setter) |
743 | ||
84eba0d9 PP |
744 | def setUp(self): |
745 | self._tc = get_default_trace_class() | |
746 | self._content_fc = self._tc.create_signed_integer_field_class(23) | |
84eba0d9 PP |
747 | |
748 | def test_create_default(self): | |
b2df5780 | 749 | fc = self._create_default_field_class() |
84eba0d9 | 750 | self.assertEqual(fc.field_class.addr, self._content_fc.addr) |
b2df5780 | 751 | self.assertEqual(len(fc.user_attributes), 0) |
84eba0d9 | 752 | |
467673c1 PP |
753 | def test_create_invalid_field_class(self): |
754 | with self.assertRaises(TypeError): | |
755 | self._tc.create_option_without_selector_field_class(object()) | |
756 | ||
9cbe0c59 FD |
757 | def test_attr_field_class(self): |
758 | fc = self._create_default_field_class() | |
759 | self.assertIs(type(fc.field_class), bt2_field_class._SignedIntegerFieldClass) | |
760 | ||
761 | def test_const_attr_field_class(self): | |
762 | fc = self._create_default_const_field_class() | |
763 | self.assertIs( | |
764 | type(fc.field_class), bt2_field_class._SignedIntegerFieldClassConst | |
765 | ) | |
766 | ||
467673c1 PP |
767 | |
768 | class _OptionWithSelectorFieldClassTestCase(_TestFieldClass): | |
769 | @staticmethod | |
770 | def _const_value_setter(field): | |
771 | field['opt'].has_field = True | |
772 | field['opt'].value = 12 | |
773 | ||
774 | def _create_default_const_field_class(self, *args, **kwargs): | |
775 | # Create a struct to contain the option and its selector else we can't | |
776 | # create the non-const field necessary to get the the const field_class | |
777 | struct_fc = self._tc.create_structure_field_class() | |
778 | struct_fc.append_member('selecteux', self._tag_fc) | |
779 | opt_fc = self._create_default_field_class(*args, **kwargs) | |
780 | struct_fc.append_member('opt', opt_fc) | |
781 | ||
782 | return _create_const_field_class(self._tc, struct_fc, self._const_value_setter)[ | |
783 | 'opt' | |
784 | ].field_class | |
785 | ||
786 | def setUp(self): | |
787 | self._tc = get_default_trace_class() | |
788 | self._content_fc = self._tc.create_signed_integer_field_class(23) | |
789 | self._tag_fc = self._create_tag_fc() | |
790 | ||
84eba0d9 | 791 | def _create_field_class_for_field_path_test(self): |
467673c1 | 792 | fc = self._create_default_field_class() |
84eba0d9 | 793 | |
76276a81 | 794 | foo_fc = self._tc.create_single_precision_real_field_class() |
84eba0d9 PP |
795 | bar_fc = self._tc.create_string_field_class() |
796 | baz_fc = self._tc.create_string_field_class() | |
797 | ||
798 | inner_struct_fc = self._tc.create_structure_field_class() | |
799 | inner_struct_fc.append_member('bar', bar_fc) | |
800 | inner_struct_fc.append_member('baz', baz_fc) | |
801 | inner_struct_fc.append_member('tag', self._tag_fc) | |
802 | inner_struct_fc.append_member('opt', fc) | |
803 | ||
467673c1 PP |
804 | opt_struct_array_fc = self._tc.create_option_without_selector_field_class( |
805 | inner_struct_fc | |
806 | ) | |
84eba0d9 PP |
807 | |
808 | outer_struct_fc = self._tc.create_structure_field_class() | |
809 | outer_struct_fc.append_member('foo', foo_fc) | |
810 | outer_struct_fc.append_member('inner_opt', opt_struct_array_fc) | |
811 | ||
812 | # The path to the selector field class is resolved when the | |
813 | # option field class is actually used, for example in a packet | |
814 | # context. | |
815 | self._tc.create_stream_class( | |
816 | packet_context_field_class=outer_struct_fc, supports_packets=True | |
817 | ) | |
818 | ||
819 | return fc | |
820 | ||
821 | def test_field_path_len(self): | |
822 | fc = self._create_field_class_for_field_path_test() | |
823 | self.assertEqual(len(fc.selector_field_path), 3) | |
824 | ||
825 | def test_field_path_iter(self): | |
826 | fc = self._create_field_class_for_field_path_test() | |
827 | path_items = list(fc.selector_field_path) | |
828 | ||
829 | self.assertEqual(len(path_items), 3) | |
830 | ||
831 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) | |
832 | self.assertEqual(path_items[0].index, 1) | |
833 | ||
834 | self.assertIsInstance(path_items[1], bt2._CurrentOptionContentFieldPathItem) | |
835 | ||
836 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) | |
837 | self.assertEqual(path_items[2].index, 2) | |
838 | ||
839 | def test_field_path_root_scope(self): | |
840 | fc = self._create_field_class_for_field_path_test() | |
841 | self.assertEqual( | |
842 | fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT | |
843 | ) | |
844 | ||
467673c1 PP |
845 | |
846 | class OptionWithBoolSelectorFieldClassTestCase( | |
847 | _OptionWithSelectorFieldClassTestCase, unittest.TestCase | |
848 | ): | |
849 | def _create_default_field_class(self, *args, **kwargs): | |
850 | return self._tc.create_option_with_bool_selector_field_class( | |
851 | self._content_fc, self._tag_fc, *args, **kwargs | |
852 | ) | |
853 | ||
854 | def _create_tag_fc(self): | |
855 | return self._tc.create_bool_field_class() | |
856 | ||
857 | def test_create_default(self): | |
858 | fc = self._create_default_field_class() | |
859 | self.assertEqual(fc.field_class.addr, self._content_fc.addr) | |
860 | self.assertFalse(fc.selector_is_reversed) | |
861 | self.assertEqual(len(fc.user_attributes), 0) | |
862 | ||
863 | def test_create_selector_is_reversed_wrong_type(self): | |
84eba0d9 | 864 | with self.assertRaises(TypeError): |
467673c1 | 865 | self._create_default_field_class(selector_is_reversed=23) |
84eba0d9 PP |
866 | |
867 | def test_create_invalid_selector_type(self): | |
868 | with self.assertRaises(TypeError): | |
467673c1 PP |
869 | self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17) |
870 | ||
871 | def test_attr_selector_is_reversed(self): | |
872 | fc = self._create_default_field_class(selector_is_reversed=True) | |
873 | self.assertTrue(fc.selector_is_reversed) | |
874 | ||
875 | def test_const_attr_selector_is_reversed(self): | |
876 | fc = self._create_default_const_field_class(selector_is_reversed=True) | |
877 | self.assertTrue(fc.selector_is_reversed) | |
878 | ||
879 | ||
880 | class _OptionWithIntegerSelectorFieldClassTestCase( | |
881 | _OptionWithSelectorFieldClassTestCase | |
882 | ): | |
883 | def _create_default_field_class(self, *args, **kwargs): | |
884 | return self._tc.create_option_with_integer_selector_field_class( | |
885 | self._content_fc, self._tag_fc, self._ranges, *args, **kwargs | |
886 | ) | |
887 | ||
888 | def test_create_default(self): | |
889 | fc = self._create_default_field_class() | |
890 | self.assertEqual(fc.field_class.addr, self._content_fc.addr) | |
891 | self.assertEqual(fc.ranges, self._ranges) | |
892 | self.assertEqual(len(fc.user_attributes), 0) | |
893 | ||
894 | def test_create_ranges_wrong_type(self): | |
895 | with self.assertRaises(TypeError): | |
896 | self._tc.create_option_with_integer_selector_field_class( | |
897 | self._content_fc, self._tag_fc, 23 | |
898 | ) | |
899 | ||
900 | def test_create_ranges_empty(self): | |
901 | with self.assertRaises(ValueError): | |
902 | self._tc.create_option_with_integer_selector_field_class( | |
903 | self._content_fc, self._tag_fc, type(self._ranges)() | |
904 | ) | |
905 | ||
906 | def test_create_invalid_selector_type(self): | |
907 | with self.assertRaises(TypeError): | |
908 | self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17) | |
909 | ||
910 | def test_attr_ranges(self): | |
911 | fc = self._create_default_field_class() | |
912 | print(type(fc.ranges), type(self._ranges)) | |
913 | self.assertEqual(fc.ranges, self._ranges) | |
914 | ||
915 | def test_const_attr_ranges(self): | |
916 | fc = self._create_default_const_field_class() | |
917 | self.assertEqual(fc.ranges, self._ranges) | |
918 | ||
919 | ||
920 | class OptionWithUnsignedIntegerSelectorFieldClassTestCase( | |
921 | _OptionWithIntegerSelectorFieldClassTestCase, unittest.TestCase | |
922 | ): | |
923 | def setUp(self): | |
924 | self._ranges = bt2.UnsignedIntegerRangeSet([(1, 3), (18, 44)]) | |
925 | super().setUp() | |
926 | ||
927 | def _create_tag_fc(self): | |
928 | return self._tc.create_unsigned_integer_field_class() | |
84eba0d9 PP |
929 | |
930 | ||
61d96b89 | 931 | class VariantFieldClassWithoutSelectorTestCase( |
b2df5780 | 932 | _TestFieldClass, _TestElementContainer, unittest.TestCase |
61d96b89 FD |
933 | ): |
934 | _append_element_method = staticmethod( | |
c946c9de | 935 | bt2._VariantFieldClassWithoutSelector.append_option |
61d96b89 FD |
936 | ) |
937 | _at_index_method = staticmethod( | |
c946c9de | 938 | bt2._VariantFieldClassWithoutSelector.option_at_index |
61d96b89 | 939 | ) |
02b61fe0 | 940 | |
9cbe0c59 FD |
941 | @staticmethod |
942 | def _const_value_setter(variant_field): | |
943 | variant_field.selected_option_index = 0 | |
944 | variant_field.value = 12 | |
945 | ||
b2df5780 PP |
946 | def _create_field_class(self, *args, **kwargs): |
947 | tc = get_default_trace_class() | |
948 | return tc.create_variant_field_class(*args, **kwargs) | |
949 | ||
9cbe0c59 FD |
950 | def _create_default_const_field_class(self, *args, **kwargs): |
951 | tc = get_default_trace_class() | |
952 | fc = tc.create_variant_field_class(*args, **kwargs) | |
953 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
954 | fc.append_option('int32', int_field_class) | |
955 | ||
956 | return _create_const_field_class(tc, fc, self._const_value_setter) | |
957 | ||
b2df5780 | 958 | _create_default_field_class = _create_field_class |
02b61fe0 PP |
959 | |
960 | ||
0822832b | 961 | class _VariantFieldClassWithIntegerSelectorTestCase: |
9cbe0c59 FD |
962 | @staticmethod |
963 | def _const_value_setter(field): | |
964 | field['variant'].selected_option_index = 0 | |
965 | field['variant'] = 12 | |
966 | ||
b2df5780 PP |
967 | def _create_default_field_class(self, *args, **kwargs): |
968 | return self._tc.create_variant_field_class( | |
969 | *args, selector_fc=self._selector_fc, **kwargs | |
970 | ) | |
971 | ||
9cbe0c59 FD |
972 | def _create_default_const_field_class(self, *args, **kwargs): |
973 | # Create a struct to contain the variant and its selector else we can't | |
974 | # create the non-const field necessary to get the the const field_class | |
975 | struct_fc = self._tc.create_structure_field_class() | |
976 | struct_fc.append_member('selecteux', self._selector_fc) | |
977 | variant_fc = self._tc.create_variant_field_class( | |
978 | *args, selector_fc=self._selector_fc | |
979 | ) | |
980 | variant_fc.append_option( | |
981 | 'a', self._tc.create_signed_integer_field_class(32), self._ranges1 | |
982 | ) | |
983 | struct_fc.append_member('variant', variant_fc, **kwargs) | |
984 | ||
985 | return _create_const_field_class(self._tc, struct_fc, self._const_value_setter)[ | |
986 | 'variant' | |
987 | ].field_class | |
988 | ||
32656995 | 989 | def setUp(self): |
c8820b76 | 990 | self._tc = get_default_trace_class() |
02b61fe0 | 991 | self._spec_set_up() |
b2df5780 | 992 | self._fc = self._create_default_field_class() |
c8820b76 SM |
993 | |
994 | def test_create_default(self): | |
995 | self.assertIsNotNone(self._fc) | |
b2df5780 | 996 | self.assertEqual(len(self._fc.user_attributes), 0) |
32656995 | 997 | |
02b61fe0 PP |
998 | def test_append_element(self): |
999 | str_field_class = self._tc.create_string_field_class() | |
1000 | self._fc.append_option('str', str_field_class, self._ranges1) | |
1001 | opt = self._fc['str'] | |
1002 | self.assertEqual(opt.field_class.addr, str_field_class.addr) | |
1003 | self.assertEqual(opt.name, 'str') | |
1004 | self.assertEqual(opt.ranges.addr, self._ranges1.addr) | |
1005 | ||
9cbe0c59 FD |
1006 | def test_const_append(self): |
1007 | fc_const = self._create_default_const_field_class() | |
dda7ee0a | 1008 | str_field_class = self._tc.create_string_field_class() |
9cbe0c59 FD |
1009 | with self.assertRaises(AttributeError): |
1010 | fc_const.append_option('str', str_field_class, self._ranges1) | |
1011 | ||
02b61fe0 PP |
1012 | def test_append_element_kwargs(self): |
1013 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
61d96b89 FD |
1014 | self._fc.append_option( |
1015 | name='int32', field_class=int_field_class, ranges=self._ranges1 | |
1016 | ) | |
02b61fe0 PP |
1017 | opt = self._fc['int32'] |
1018 | self.assertEqual(opt.field_class.addr, int_field_class.addr) | |
1019 | self.assertEqual(opt.name, 'int32') | |
1020 | self.assertEqual(opt.ranges.addr, self._ranges1.addr) | |
c8820b76 | 1021 | |
02b61fe0 PP |
1022 | def test_append_element_invalid_name(self): |
1023 | sub_fc = self._tc.create_string_field_class() | |
32656995 | 1024 | |
02b61fe0 PP |
1025 | with self.assertRaises(TypeError): |
1026 | self._fc.append_option(self._fc, 23, sub_fc) | |
32656995 | 1027 | |
02b61fe0 PP |
1028 | def test_append_element_invalid_field_class(self): |
1029 | with self.assertRaises(TypeError): | |
1030 | self._fc.append_option(self._fc, 'yes', object()) | |
1031 | ||
1032 | def test_append_element_invalid_ranges(self): | |
1033 | sub_fc = self._tc.create_string_field_class() | |
1034 | ||
1035 | with self.assertRaises(TypeError): | |
1036 | self._fc.append_option(self._fc, sub_fc, 'lel') | |
1037 | ||
1038 | def test_append_element_dup_name(self): | |
1039 | sub_fc1 = self._tc.create_string_field_class() | |
1040 | sub_fc2 = self._tc.create_string_field_class() | |
1041 | ||
3b2be708 | 1042 | with self.assertRaises(ValueError): |
02b61fe0 PP |
1043 | self._fc.append_option('yes', sub_fc1, self._ranges1) |
1044 | self._fc.append_option('yes', sub_fc2, self._ranges2) | |
1045 | ||
1046 | def test_append_element_invalid_ranges_signedness(self): | |
1047 | sub_fc = self._tc.create_string_field_class() | |
1048 | ||
1049 | with self.assertRaises(TypeError): | |
1050 | self._fc.append_option(self._fc, sub_fc, self._inval_ranges) | |
1051 | ||
b2df5780 PP |
1052 | def test_user_attributes(self): |
1053 | self._fc.append_option( | |
1054 | 'c', | |
1055 | self._tc.create_string_field_class(), | |
1056 | self._ranges1, | |
1057 | user_attributes={'salut': 23}, | |
1058 | ) | |
1059 | self.assertEqual(self._fc['c'].user_attributes, {'salut': 23}) | |
9cbe0c59 FD |
1060 | self.assertIs(type(self._fc.user_attributes), bt2_value.MapValue) |
1061 | ||
1062 | def test_const_user_attributes(self): | |
1063 | fc_const = self._create_default_const_field_class() | |
1064 | self.assertIs(type(fc_const.user_attributes), bt2_value._MapValueConst) | |
b2df5780 PP |
1065 | |
1066 | def test_invalid_user_attributes(self): | |
1067 | with self.assertRaises(TypeError): | |
1068 | self._fc.append_option( | |
1069 | 'c', | |
1070 | self._tc.create_string_field_class(), | |
1071 | self._ranges1, | |
1072 | user_attributes=object(), | |
1073 | ) | |
1074 | ||
1075 | def test_invalid_user_attributes_value_type(self): | |
1076 | with self.assertRaises(TypeError): | |
1077 | self._fc.append_option( | |
1078 | 'c', | |
1079 | self._tc.create_string_field_class(), | |
1080 | self._ranges1, | |
1081 | user_attributes=23, | |
1082 | ) | |
1083 | ||
02b61fe0 | 1084 | def test_iadd(self): |
76276a81 | 1085 | a_field_class = self._tc.create_single_precision_real_field_class() |
02b61fe0 PP |
1086 | self._fc.append_option('a_float', a_field_class, self._ranges1) |
1087 | c_field_class = self._tc.create_string_field_class() | |
61d96b89 FD |
1088 | d_field_class = self._tc.create_signed_enumeration_field_class( |
1089 | field_value_range=32 | |
1090 | ) | |
02b61fe0 PP |
1091 | self._fc += [ |
1092 | ('c_string', c_field_class, self._ranges2), | |
1093 | ('d_enum', d_field_class, self._ranges3), | |
1094 | ] | |
1095 | self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr) | |
1096 | self.assertEqual(self._fc['a_float'].name, 'a_float') | |
1097 | self.assertEqual(self._fc['a_float'].ranges, self._ranges1) | |
1098 | self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr) | |
1099 | self.assertEqual(self._fc['c_string'].name, 'c_string') | |
1100 | self.assertEqual(self._fc['c_string'].ranges, self._ranges2) | |
1101 | self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr) | |
1102 | self.assertEqual(self._fc['d_enum'].name, 'd_enum') | |
1103 | self.assertEqual(self._fc['d_enum'].ranges, self._ranges3) | |
1104 | ||
9cbe0c59 FD |
1105 | def test_const_iadd(self): |
1106 | fc_const = self._create_default_const_field_class() | |
76276a81 | 1107 | a_field_class = self._tc.create_single_precision_real_field_class() |
9cbe0c59 FD |
1108 | with self.assertRaises(TypeError): |
1109 | fc_const += [('a_float', a_field_class, self._ranges1)] | |
1110 | ||
02b61fe0 PP |
1111 | def test_bool_op(self): |
1112 | self.assertFalse(self._fc) | |
1113 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
1114 | self.assertTrue(self._fc) | |
1115 | ||
1116 | def test_len(self): | |
1117 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
1118 | self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2) | |
1119 | self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3) | |
1120 | self.assertEqual(len(self._fc), 3) | |
1121 | ||
1122 | def test_getitem(self): | |
1123 | a_fc = self._tc.create_signed_integer_field_class(32) | |
1124 | b_fc = self._tc.create_string_field_class() | |
76276a81 | 1125 | c_fc = self._tc.create_single_precision_real_field_class() |
02b61fe0 PP |
1126 | self._fc.append_option('a', a_fc, self._ranges1) |
1127 | self._fc.append_option('b', b_fc, self._ranges2) | |
1128 | self._fc.append_option('c', c_fc, self._ranges3) | |
1129 | self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr) | |
1130 | self.assertEqual(self._fc['b'].name, 'b') | |
1131 | self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr) | |
1132 | ||
9cbe0c59 FD |
1133 | def test_option_field_class(self): |
1134 | a_fc = self._tc.create_signed_integer_field_class(32) | |
1135 | self._fc.append_option('a', a_fc, self._ranges1) | |
1136 | self.assertIs( | |
1137 | type(self._fc['a'].field_class), bt2_field_class._SignedIntegerFieldClass | |
1138 | ) | |
1139 | ||
1140 | def test_const_option_field_class(self): | |
1141 | fc_const = self._create_default_const_field_class() | |
1142 | self.assertIs( | |
1143 | type(fc_const['a'].field_class), | |
1144 | bt2_field_class._SignedIntegerFieldClassConst, | |
1145 | ) | |
1146 | ||
02b61fe0 PP |
1147 | def test_getitem_invalid_key_type(self): |
1148 | with self.assertRaises(TypeError): | |
1149 | self._fc[0] | |
1150 | ||
1151 | def test_getitem_invalid_key(self): | |
1152 | with self.assertRaises(KeyError): | |
1153 | self._fc['no way'] | |
1154 | ||
1155 | def test_contains(self): | |
1156 | self.assertFalse('a' in self._fc) | |
1157 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
1158 | self.assertTrue('a' in self._fc) | |
1159 | ||
1160 | def test_iter(self): | |
1161 | a_fc = self._tc.create_signed_integer_field_class(32) | |
1162 | b_fc = self._tc.create_string_field_class() | |
76276a81 | 1163 | c_fc = self._tc.create_single_precision_real_field_class() |
02b61fe0 PP |
1164 | opts = ( |
1165 | ('a', a_fc, self._ranges1), | |
1166 | ('b', b_fc, self._ranges2), | |
1167 | ('c', c_fc, self._ranges3), | |
1168 | ) | |
1169 | ||
1170 | for opt in opts: | |
1171 | self._fc.append_option(*opt) | |
32656995 | 1172 | |
02b61fe0 PP |
1173 | for (name, opt), test_opt in zip(self._fc.items(), opts): |
1174 | self.assertEqual(opt.name, test_opt[0]) | |
1175 | self.assertEqual(name, opt.name) | |
1176 | self.assertEqual(opt.field_class.addr, test_opt[1].addr) | |
1177 | self.assertEqual(opt.ranges.addr, test_opt[2].addr) | |
1178 | ||
1179 | def test_at_index(self): | |
1180 | a_fc = self._tc.create_signed_integer_field_class(32) | |
1181 | b_fc = self._tc.create_string_field_class() | |
76276a81 | 1182 | c_fc = self._tc.create_single_precision_real_field_class() |
02b61fe0 PP |
1183 | self._fc.append_option('c', c_fc, self._ranges1) |
1184 | self._fc.append_option('a', a_fc, self._ranges2) | |
1185 | self._fc.append_option('b', b_fc, self._ranges3) | |
1186 | self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr) | |
1187 | self.assertEqual(self._fc.option_at_index(1).name, 'a') | |
1188 | self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr) | |
1189 | ||
1190 | def test_at_index_invalid(self): | |
61d96b89 FD |
1191 | self._fc.append_option( |
1192 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 | |
1193 | ) | |
02b61fe0 PP |
1194 | |
1195 | with self.assertRaises(TypeError): | |
1196 | self._fc.option_at_index('yes') | |
1197 | ||
1198 | def test_at_index_out_of_bounds_after(self): | |
61d96b89 FD |
1199 | self._fc.append_option( |
1200 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 | |
1201 | ) | |
02b61fe0 PP |
1202 | |
1203 | with self.assertRaises(IndexError): | |
1204 | self._fc.option_at_index(len(self._fc)) | |
1205 | ||
1206 | def _fill_default_fc_for_field_path_test(self): | |
c8820b76 SM |
1207 | # Create something equivalent to: |
1208 | # | |
1209 | # struct outer_struct_fc { | |
1210 | # real foo; | |
1211 | # struct inner_struct_fc { | |
02b61fe0 | 1212 | # [u]int64_t selector; |
c8820b76 SM |
1213 | # string bar; |
1214 | # string baz; | |
02b61fe0 PP |
1215 | # variant <selector> { |
1216 | # real a; // selected with self._ranges1 | |
1217 | # int21_t b; // selected with self._ranges2 | |
1218 | # uint34_t c; // selected with self._ranges3 | |
c8820b76 SM |
1219 | # } variant; |
1220 | # } inner_struct[2]; | |
1221 | # }; | |
76276a81 FD |
1222 | self._fc.append_option( |
1223 | 'a', self._tc.create_single_precision_real_field_class(), self._ranges1 | |
1224 | ) | |
61d96b89 FD |
1225 | self._fc.append_option( |
1226 | 'b', self._tc.create_signed_integer_field_class(21), self._ranges2 | |
1227 | ) | |
1228 | self._fc.append_option( | |
1229 | 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3 | |
1230 | ) | |
32656995 | 1231 | |
76276a81 | 1232 | foo_fc = self._tc.create_single_precision_real_field_class() |
c8820b76 SM |
1233 | bar_fc = self._tc.create_string_field_class() |
1234 | baz_fc = self._tc.create_string_field_class() | |
32656995 | 1235 | |
c8820b76 | 1236 | inner_struct_fc = self._tc.create_structure_field_class() |
02b61fe0 | 1237 | inner_struct_fc.append_member('selector', self._selector_fc) |
c8820b76 SM |
1238 | inner_struct_fc.append_member('bar', bar_fc) |
1239 | inner_struct_fc.append_member('baz', baz_fc) | |
02b61fe0 | 1240 | inner_struct_fc.append_member('variant', self._fc) |
32656995 | 1241 | |
61d96b89 FD |
1242 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
1243 | inner_struct_fc, 2 | |
1244 | ) | |
32656995 | 1245 | |
c8820b76 SM |
1246 | outer_struct_fc = self._tc.create_structure_field_class() |
1247 | outer_struct_fc.append_member('foo', foo_fc) | |
1248 | outer_struct_fc.append_member('inner_struct', inner_struct_array_fc) | |
32656995 | 1249 | |
c8820b76 SM |
1250 | # The path to the selector field is resolved when the sequence is |
1251 | # actually used, for example in a packet context. | |
61d96b89 FD |
1252 | self._tc.create_stream_class( |
1253 | supports_packets=True, packet_context_field_class=outer_struct_fc | |
1254 | ) | |
32656995 | 1255 | |
c8820b76 | 1256 | def test_selector_field_path_length(self): |
02b61fe0 PP |
1257 | self._fill_default_fc_for_field_path_test() |
1258 | self.assertEqual(len(self._fc.selector_field_path), 3) | |
32656995 | 1259 | |
c8820b76 | 1260 | def test_selector_field_path_iter(self): |
02b61fe0 PP |
1261 | self._fill_default_fc_for_field_path_test() |
1262 | path_items = list(self._fc.selector_field_path) | |
32656995 | 1263 | |
c8820b76 | 1264 | self.assertEqual(len(path_items), 3) |
32656995 | 1265 | |
c946c9de | 1266 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
c8820b76 | 1267 | self.assertEqual(path_items[0].index, 1) |
32656995 | 1268 | |
c946c9de | 1269 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
32656995 | 1270 | |
c946c9de | 1271 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
c8820b76 | 1272 | self.assertEqual(path_items[2].index, 0) |
32656995 | 1273 | |
c8820b76 | 1274 | def test_selector_field_path_root_scope(self): |
02b61fe0 | 1275 | self._fill_default_fc_for_field_path_test() |
61d96b89 | 1276 | self.assertEqual( |
056995e5 | 1277 | self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT |
61d96b89 | 1278 | ) |
02b61fe0 PP |
1279 | |
1280 | ||
61d96b89 | 1281 | class VariantFieldClassWithUnsignedSelectorTestCase( |
0822832b | 1282 | _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase |
61d96b89 | 1283 | ): |
02b61fe0 PP |
1284 | def _spec_set_up(self): |
1285 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) | |
1286 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) | |
1287 | self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
1288 | self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)]) | |
1289 | self._selector_fc = self._tc.create_unsigned_integer_field_class() | |
1290 | ||
1291 | ||
61d96b89 | 1292 | class VariantFieldClassWithSignedSelectorTestCase( |
0822832b | 1293 | _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase |
61d96b89 | 1294 | ): |
02b61fe0 PP |
1295 | def _spec_set_up(self): |
1296 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) | |
1297 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) | |
1298 | self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)]) | |
1299 | self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
1300 | self._selector_fc = self._tc.create_signed_integer_field_class() | |
32656995 | 1301 | |
c8820b76 | 1302 | |
9cbe0c59 FD |
1303 | class _ArrayFieldClassTestCase: |
1304 | def test_attr_element_field_class(self): | |
1305 | fc = self._create_array() | |
1306 | self.assertIs( | |
1307 | type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClass | |
1308 | ) | |
1309 | ||
b8ddb4f0 PP |
1310 | |
1311 | class _ArrayFieldClassConstTestCase: | |
9cbe0c59 FD |
1312 | def test_const_attr_element_field_class(self): |
1313 | fc = self._create_const_array() | |
1314 | self.assertIs( | |
1315 | type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClassConst | |
1316 | ) | |
1317 | ||
1318 | ||
b8ddb4f0 PP |
1319 | class StaticArrayFieldClassTestCase( |
1320 | _ArrayFieldClassTestCase, _ArrayFieldClassConstTestCase, unittest.TestCase | |
1321 | ): | |
9cbe0c59 FD |
1322 | @staticmethod |
1323 | def _const_value_setter(field): | |
c82e5122 | 1324 | field.value = [9] * 45 |
9cbe0c59 FD |
1325 | |
1326 | def _create_array(self): | |
1327 | return self._tc.create_static_array_field_class(self._elem_fc, 45) | |
1328 | ||
1329 | def _create_const_array(self): | |
1330 | fc = self._tc.create_static_array_field_class(self._elem_fc, 45) | |
1331 | return _create_const_field_class(self._tc, fc, self._const_value_setter) | |
1332 | ||
c8820b76 SM |
1333 | def setUp(self): |
1334 | self._tc = get_default_trace_class() | |
1335 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
32656995 SM |
1336 | |
1337 | def test_create_default(self): | |
c8820b76 SM |
1338 | fc = self._tc.create_static_array_field_class(self._elem_fc, 45) |
1339 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
1340 | self.assertEqual(fc.length, 45) | |
b2df5780 | 1341 | self.assertEqual(len(fc.user_attributes), 0) |
32656995 | 1342 | |
c8820b76 | 1343 | def test_create_invalid_elem_field_class(self): |
32656995 | 1344 | with self.assertRaises(TypeError): |
c8820b76 | 1345 | self._tc.create_static_array_field_class(object(), 45) |
32656995 SM |
1346 | |
1347 | def test_create_invalid_length(self): | |
1348 | with self.assertRaises(ValueError): | |
61d96b89 FD |
1349 | self._tc.create_static_array_field_class( |
1350 | self._tc.create_string_field_class(), -17 | |
1351 | ) | |
32656995 SM |
1352 | |
1353 | def test_create_invalid_length_type(self): | |
1354 | with self.assertRaises(TypeError): | |
61d96b89 FD |
1355 | self._tc.create_static_array_field_class( |
1356 | self._tc.create_string_field_class(), 'the length' | |
1357 | ) | |
c8820b76 SM |
1358 | |
1359 | ||
b8ddb4f0 PP |
1360 | class DynamicArrayFieldClassTestCase( |
1361 | _ArrayFieldClassTestCase, _ArrayFieldClassConstTestCase, unittest.TestCase | |
1362 | ): | |
9cbe0c59 FD |
1363 | @staticmethod |
1364 | def _const_value_setter(field): | |
c82e5122 | 1365 | field.value = [] |
9cbe0c59 FD |
1366 | |
1367 | def _create_array(self): | |
1368 | return self._tc.create_dynamic_array_field_class(self._elem_fc) | |
1369 | ||
1370 | def _create_const_array(self): | |
1371 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc) | |
1372 | return _create_const_field_class(self._tc, fc, self._const_value_setter) | |
1373 | ||
c8820b76 SM |
1374 | def setUp(self): |
1375 | self._tc = get_default_trace_class() | |
1376 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
32656995 | 1377 | |
c8820b76 SM |
1378 | def test_create_default(self): |
1379 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc) | |
1380 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
b8ddb4f0 PP |
1381 | self.assertEqual(len(fc.user_attributes), 0) |
1382 | ||
1383 | def test_create_invalid_field_class(self): | |
1384 | with self.assertRaises(TypeError): | |
1385 | self._tc.create_dynamic_array_field_class(object()) | |
1386 | ||
1387 | ||
1388 | class DynamicArrayWithLengthFieldFieldClassTestCase( | |
1389 | _ArrayFieldClassTestCase, unittest.TestCase | |
1390 | ): | |
1391 | @staticmethod | |
1392 | def _const_value_setter(field): | |
1393 | field.value = [] | |
1394 | ||
1395 | def _create_array(self): | |
1396 | return self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) | |
1397 | ||
1398 | def setUp(self): | |
1399 | self._tc = get_default_trace_class() | |
1400 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
1401 | self._len_fc = self._tc.create_unsigned_integer_field_class(12) | |
1402 | ||
1403 | def test_create_default(self): | |
1404 | fc = self._create_array() | |
1405 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
c8820b76 | 1406 | self.assertIsNone(fc.length_field_path, None) |
b2df5780 | 1407 | self.assertEqual(len(fc.user_attributes), 0) |
32656995 | 1408 | |
c8820b76 SM |
1409 | def _create_field_class_for_field_path_test(self): |
1410 | # Create something a field class that is equivalent to: | |
1411 | # | |
1412 | # struct outer_struct_fc { | |
1413 | # real foo; | |
1414 | # struct inner_struct_fc { | |
1415 | # string bar; | |
1416 | # string baz; | |
1417 | # uint12_t len; | |
1418 | # uint23_t dyn_array[len]; | |
1419 | # } inner_struct[2]; | |
1420 | # }; | |
32656995 | 1421 | |
b8ddb4f0 | 1422 | fc = self._create_array() |
32656995 | 1423 | |
76276a81 | 1424 | foo_fc = self._tc.create_single_precision_real_field_class() |
c8820b76 SM |
1425 | bar_fc = self._tc.create_string_field_class() |
1426 | baz_fc = self._tc.create_string_field_class() | |
32656995 | 1427 | |
c8820b76 SM |
1428 | inner_struct_fc = self._tc.create_structure_field_class() |
1429 | inner_struct_fc.append_member('bar', bar_fc) | |
1430 | inner_struct_fc.append_member('baz', baz_fc) | |
1431 | inner_struct_fc.append_member('len', self._len_fc) | |
1432 | inner_struct_fc.append_member('dyn_array', fc) | |
32656995 | 1433 | |
61d96b89 FD |
1434 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
1435 | inner_struct_fc, 2 | |
1436 | ) | |
c8820b76 SM |
1437 | |
1438 | outer_struct_fc = self._tc.create_structure_field_class() | |
1439 | outer_struct_fc.append_member('foo', foo_fc) | |
1440 | outer_struct_fc.append_member('inner_struct', inner_struct_array_fc) | |
1441 | ||
1442 | # The path to the length field is resolved when the sequence is | |
1443 | # actually used, for example in a packet context. | |
61d96b89 FD |
1444 | self._tc.create_stream_class( |
1445 | packet_context_field_class=outer_struct_fc, supports_packets=True | |
1446 | ) | |
c8820b76 SM |
1447 | |
1448 | return fc | |
1449 | ||
1450 | def test_field_path_len(self): | |
1451 | fc = self._create_field_class_for_field_path_test() | |
1452 | self.assertEqual(len(fc.length_field_path), 3) | |
1453 | ||
1454 | def test_field_path_iter(self): | |
1455 | fc = self._create_field_class_for_field_path_test() | |
1456 | path_items = list(fc.length_field_path) | |
1457 | ||
1458 | self.assertEqual(len(path_items), 3) | |
1459 | ||
c946c9de | 1460 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
c8820b76 SM |
1461 | self.assertEqual(path_items[0].index, 1) |
1462 | ||
c946c9de | 1463 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
c8820b76 | 1464 | |
c946c9de | 1465 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
c8820b76 SM |
1466 | self.assertEqual(path_items[2].index, 2) |
1467 | ||
1468 | def test_field_path_root_scope(self): | |
1469 | fc = self._create_field_class_for_field_path_test() | |
056995e5 PP |
1470 | self.assertEqual( |
1471 | fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT | |
1472 | ) | |
32656995 | 1473 | |
32656995 SM |
1474 | def test_create_invalid_length_type(self): |
1475 | with self.assertRaises(TypeError): | |
61d96b89 FD |
1476 | self._tc.create_dynamic_array_field_class( |
1477 | self._tc.create_string_field_class(), 17 | |
1478 | ) | |
32656995 | 1479 | |
32656995 | 1480 | |
c8820b76 SM |
1481 | if __name__ == "__main__": |
1482 | unittest.main() |