bt2: make bt2.Error wrap current thread's error
[babeltrace.git] / tests / bindings / python / bt2 / test_field_class.py
CommitLineData
d2d857a8
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
c4239792 19import bt2.field
b4f45851 20import unittest
b4f45851 21import bt2
45c51519 22import collections
d47b87ac 23from utils import get_default_trace_class
b4f45851
SM
24
25
d47b87ac
SM
26class _TestIntegerFieldClassProps:
27 def test_create_default(self):
28 fc = self._create_func()
29 self.assertEqual(fc.field_value_range, 64)
30 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL)
b4f45851 31
d47b87ac
SM
32 def test_create_range(self):
33 fc = self._create_func(field_value_range=35)
34 self.assertEqual(fc.field_value_range, 35)
b4f45851 35
d47b87ac
SM
36 fc = self._create_func(36)
37 self.assertEqual(fc.field_value_range, 36)
b4f45851 38
d47b87ac
SM
39 def test_create_invalid_range(self):
40 with self.assertRaises(TypeError):
41 self._create_func('yes')
b4f45851 42
d47b87ac
SM
43 with self.assertRaises(TypeError):
44 self._create_func(field_value_range='yes')
b4f45851 45
b4f45851 46 with self.assertRaises(ValueError):
d47b87ac 47 self._create_func(field_value_range=-2)
b4f45851 48
d47b87ac
SM
49 with self.assertRaises(ValueError):
50 self._create_func(field_value_range=0)
b4f45851 51
d47b87ac 52 def test_create_base(self):
cfbd7cf3
FD
53 fc = self._create_func(
54 preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL
55 )
d47b87ac 56 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL)
b4f45851 57
d47b87ac 58 def test_create_invalid_base_type(self):
b4f45851 59 with self.assertRaises(TypeError):
d47b87ac 60 self._create_func(preferred_display_base='yes')
b4f45851 61
d47b87ac
SM
62 def test_create_invalid_base_value(self):
63 with self.assertRaises(ValueError):
64 self._create_func(preferred_display_base=444)
b4f45851 65
d47b87ac
SM
66 def test_create_full(self):
67 fc = self._create_func(24, preferred_display_base=bt2.IntegerDisplayBase.OCTAL)
68 self.assertEqual(fc.field_value_range, 24)
69 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.OCTAL)
b4f45851
SM
70
71
d47b87ac
SM
72class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase):
73 def setUp(self):
74 self._tc = get_default_trace_class()
75 self._create_func = self._tc.create_signed_integer_field_class
b4f45851 76
b4f45851 77
d47b87ac
SM
78class RealFieldClassTestCase(unittest.TestCase):
79 def setUp(self):
80 self._tc = get_default_trace_class()
b4f45851 81
d47b87ac
SM
82 def test_create_default(self):
83 fc = self._tc.create_real_field_class()
84 self.assertFalse(fc.is_single_precision)
b4f45851 85
d47b87ac
SM
86 def test_create_is_single_precision(self):
87 fc = self._tc.create_real_field_class(is_single_precision=True)
88 self.assertTrue(fc.is_single_precision)
89
90 def test_create_invalid_is_single_precision(self):
b4f45851 91 with self.assertRaises(TypeError):
d47b87ac 92 self._tc.create_real_field_class(is_single_precision='hohoho')
b4f45851 93
b4f45851 94
d47b87ac
SM
95# Converts an _EnumerationFieldClassMapping to a list of ranges:
96#
97# [(lower0, upper0), (lower1, upper1), ...]
98
cfbd7cf3 99
45c51519
PP
100def enum_mapping_to_set(mapping):
101 return {(x.lower, x.upper) for x in mapping.ranges}
b4f45851 102
b4f45851 103
45c51519 104class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
d47b87ac
SM
105 def setUp(self):
106 self._tc = get_default_trace_class()
45c51519
PP
107 self._spec_set_up()
108 self._fc = self._create_func()
d47b87ac
SM
109
110 def test_create_from_invalid_type(self):
b4f45851 111 with self.assertRaises(TypeError):
d47b87ac 112 self._create_func('coucou')
b4f45851 113
d47b87ac 114 def test_add_mapping_simple(self):
45c51519 115 self._fc.add_mapping('hello', self._ranges1)
d47b87ac
SM
116 mapping = self._fc['hello']
117 self.assertEqual(mapping.label, 'hello')
45c51519 118 self.assertEqual(mapping.ranges, self._ranges1)
b4f45851 119
d47b87ac 120 def test_add_mapping_simple_kwargs(self):
45c51519 121 self._fc.add_mapping(label='hello', ranges=self._ranges1)
d47b87ac
SM
122 mapping = self._fc['hello']
123 self.assertEqual(mapping.label, 'hello')
45c51519 124 self.assertEqual(mapping.ranges, self._ranges1)
b4f45851 125
45c51519
PP
126 def test_add_mapping_invalid_name(self):
127 with self.assertRaises(TypeError):
128 self._fc.add_mapping(17, self._ranges1)
b4f45851 129
45c51519
PP
130 def test_add_mapping_invalid_range(self):
131 with self.assertRaises(TypeError):
132 self._fc.add_mapping('allo', 'meow')
d47b87ac 133
45c51519 134 def test_add_mapping_dup_label(self):
ce4923b0 135 with self.assertRaises(ValueError):
45c51519
PP
136 self._fc.add_mapping('a', self._ranges1)
137 self._fc.add_mapping('a', self._ranges2)
d47b87ac 138
45c51519 139 def test_add_mapping_invalid_ranges_signedness(self):
b4f45851 140 with self.assertRaises(TypeError):
45c51519 141 self._fc.add_mapping('allo', self._inval_ranges)
b4f45851 142
d47b87ac 143 def test_iadd(self):
45c51519 144 self._fc.add_mapping('c', self._ranges1)
b4f45851 145
cfbd7cf3 146 self._fc += [('d', self._ranges2), ('e', self._ranges3)]
b4f45851 147
45c51519 148 self.assertEqual(len(self._fc), 3)
d47b87ac 149 self.assertEqual(self._fc['c'].label, 'c')
45c51519 150 self.assertEqual(self._fc['c'].ranges, self._ranges1)
d47b87ac 151 self.assertEqual(self._fc['d'].label, 'd')
45c51519 152 self.assertEqual(self._fc['d'].ranges, self._ranges2)
d47b87ac 153 self.assertEqual(self._fc['e'].label, 'e')
45c51519 154 self.assertEqual(self._fc['e'].ranges, self._ranges3)
b4f45851 155
d47b87ac
SM
156 def test_bool_op(self):
157 self.assertFalse(self._fc)
45c51519 158 self._fc.add_mapping('a', self._ranges1)
d47b87ac 159 self.assertTrue(self._fc)
b4f45851 160
d47b87ac 161 def test_len(self):
45c51519
PP
162 self._fc.add_mapping('a', self._ranges1)
163 self._fc.add_mapping('b', self._ranges2)
164 self._fc.add_mapping('c', self._ranges3)
d47b87ac 165 self.assertEqual(len(self._fc), 3)
b4f45851 166
d47b87ac 167 def test_getitem(self):
45c51519
PP
168 self._fc.add_mapping('a', self._ranges1)
169 self._fc.add_mapping('b', self._ranges2)
170 self._fc.add_mapping('c', self._ranges3)
d47b87ac 171 mapping = self._fc['a']
d47b87ac 172 self.assertEqual(mapping.label, 'a')
45c51519 173 self.assertEqual(mapping.ranges, self._ranges1)
b4f45851 174
45c51519 175 def test_getitem_nonexistent(self):
d47b87ac
SM
176 with self.assertRaises(KeyError):
177 self._fc['doesnotexist']
b4f45851 178
d47b87ac 179 def test_iter(self):
45c51519
PP
180 self._fc.add_mapping('a', self._ranges1)
181 self._fc.add_mapping('b', self._ranges2)
182 self._fc.add_mapping('c', self._ranges3)
b4f45851 183
d47b87ac
SM
184 # This exercises iteration.
185 labels = sorted(self._fc)
b4f45851 186
45c51519 187 self.assertEqual(labels, ['a', 'b', 'c'])
b4f45851 188
d47b87ac 189 def test_find_by_value(self):
45c51519
PP
190 self._fc.add_mapping('a', self._ranges1)
191 self._fc.add_mapping('b', self._ranges2)
192 self._fc.add_mapping('c', self._ranges3)
193 mappings = self._fc.mappings_for_value(self._value_in_range_1_and_3)
194 labels = set([mapping.label for mapping in mappings])
195 expected_labels = set(['a', 'c'])
196 self.assertEqual(labels, expected_labels)
197
198
cfbd7cf3
FD
199class UnsignedEnumerationFieldClassTestCase(
200 _EnumerationFieldClassTestCase, unittest.TestCase
201):
45c51519
PP
202 def _spec_set_up(self):
203 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
204 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
205 self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
206 self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
207 self._value_in_range_1_and_3 = 20
d47b87ac 208 self._create_func = self._tc.create_unsigned_enumeration_field_class
b4f45851 209
b4f45851 210
cfbd7cf3
FD
211class SignedEnumerationFieldClassTestCase(
212 _EnumerationFieldClassTestCase, unittest.TestCase
213):
45c51519
PP
214 def _spec_set_up(self):
215 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
216 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
217 self._ranges3 = bt2.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
218 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
219 self._value_in_range_1_and_3 = -7
d47b87ac 220 self._create_func = self._tc.create_signed_enumeration_field_class
b4f45851 221
b4f45851 222
d47b87ac 223class StringFieldClassTestCase(unittest.TestCase):
b4f45851 224 def setUp(self):
d47b87ac
SM
225 tc = get_default_trace_class()
226 self._fc = tc.create_string_field_class()
b4f45851
SM
227
228 def test_create_default(self):
d47b87ac 229 self.assertIsNotNone(self._fc)
b4f45851 230
b4f45851 231
cfbd7cf3 232class _TestElementContainer:
45c51519
PP
233 def setUp(self):
234 self._tc = get_default_trace_class()
235 self._fc = self._create_default_fc()
236
237 def test_create_default(self):
238 self.assertIsNotNone(self._fc)
239
d47b87ac
SM
240 def test_append_element(self):
241 int_field_class = self._tc.create_signed_integer_field_class(32)
242 self._append_element_method(self._fc, 'int32', int_field_class)
45c51519 243 field_class = self._fc['int32'].field_class
d47b87ac 244 self.assertEqual(field_class.addr, int_field_class.addr)
b4f45851 245
45c51519 246 def test_append_element_kwargs(self):
d47b87ac
SM
247 int_field_class = self._tc.create_signed_integer_field_class(32)
248 self._append_element_method(self._fc, name='int32', field_class=int_field_class)
45c51519 249 field_class = self._fc['int32'].field_class
d47b87ac
SM
250 self.assertEqual(field_class.addr, int_field_class.addr)
251
252 def test_append_element_invalid_name(self):
253 sub_fc = self._tc.create_string_field_class()
b4f45851 254
b4f45851 255 with self.assertRaises(TypeError):
d47b87ac 256 self._append_element_method(self._fc, 23, sub_fc)
b4f45851 257
d47b87ac 258 def test_append_element_invalid_field_class(self):
b4f45851 259 with self.assertRaises(TypeError):
d47b87ac 260 self._append_element_method(self._fc, 'yes', object())
b4f45851 261
45c51519
PP
262 def test_append_element_dup_name(self):
263 sub_fc1 = self._tc.create_string_field_class()
264 sub_fc2 = self._tc.create_string_field_class()
265
ce4923b0 266 with self.assertRaises(ValueError):
45c51519
PP
267 self._append_element_method(self._fc, 'yes', sub_fc1)
268 self._append_element_method(self._fc, 'yes', sub_fc2)
269
b4f45851 270 def test_iadd(self):
45c51519 271 other_fc = self._create_default_fc()
d47b87ac
SM
272 a_field_class = self._tc.create_real_field_class()
273 b_field_class = self._tc.create_signed_integer_field_class(17)
274 self._append_element_method(self._fc, 'a_float', a_field_class)
275 self._append_element_method(self._fc, 'b_int', b_field_class)
45c51519 276 c_field_class = self._tc.create_string_field_class()
cfbd7cf3
FD
277 d_field_class = self._tc.create_signed_enumeration_field_class(
278 field_value_range=32
279 )
45c51519
PP
280 e_field_class = self._tc.create_structure_field_class()
281 self._fc += [
282 ('c_string', c_field_class),
283 ('d_enum', d_field_class),
284 ('e_struct', e_field_class),
285 ]
286 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
287 self.assertEqual(self._fc['a_float'].name, 'a_float')
288 self.assertEqual(self._fc['b_int'].field_class.addr, b_field_class.addr)
289 self.assertEqual(self._fc['b_int'].name, 'b_int')
290 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
291 self.assertEqual(self._fc['c_string'].name, 'c_string')
292 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
293 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
294 self.assertEqual(self._fc['e_struct'].field_class.addr, e_field_class.addr)
295 self.assertEqual(self._fc['e_struct'].name, 'e_struct')
b4f45851
SM
296
297 def test_bool_op(self):
298 self.assertFalse(self._fc)
d47b87ac 299 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
b4f45851
SM
300 self.assertTrue(self._fc)
301
302 def test_len(self):
45c51519
PP
303 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
304 self._append_element_method(self._fc, 'b', self._tc.create_string_field_class())
305 self._append_element_method(self._fc, 'c', self._tc.create_string_field_class())
b4f45851
SM
306 self.assertEqual(len(self._fc), 3)
307
308 def test_getitem(self):
d47b87ac
SM
309 a_fc = self._tc.create_signed_integer_field_class(32)
310 b_fc = self._tc.create_string_field_class()
311 c_fc = self._tc.create_real_field_class()
312 self._append_element_method(self._fc, 'a', a_fc)
313 self._append_element_method(self._fc, 'b', b_fc)
314 self._append_element_method(self._fc, 'c', c_fc)
45c51519
PP
315 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
316 self.assertEqual(self._fc['b'].name, 'b')
b4f45851
SM
317
318 def test_getitem_invalid_key_type(self):
319 with self.assertRaises(TypeError):
320 self._fc[0]
321
322 def test_getitem_invalid_key(self):
323 with self.assertRaises(KeyError):
324 self._fc['no way']
325
326 def test_contains(self):
327 self.assertFalse('a' in self._fc)
d47b87ac 328 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
b4f45851
SM
329 self.assertTrue('a' in self._fc)
330
331 def test_iter(self):
d47b87ac
SM
332 a_fc = self._tc.create_signed_integer_field_class(32)
333 b_fc = self._tc.create_string_field_class()
334 c_fc = self._tc.create_real_field_class()
cfbd7cf3 335 elements = (('a', a_fc), ('b', b_fc), ('c', c_fc))
b4f45851 336
45c51519
PP
337 for elem in elements:
338 self._append_element_method(self._fc, *elem)
b4f45851 339
45c51519
PP
340 for (name, element), test_elem in zip(self._fc.items(), elements):
341 self.assertEqual(element.name, test_elem[0])
342 self.assertEqual(name, element.name)
343 self.assertEqual(element.field_class.addr, test_elem[1].addr)
b4f45851
SM
344
345 def test_at_index(self):
d47b87ac
SM
346 a_fc = self._tc.create_signed_integer_field_class(32)
347 b_fc = self._tc.create_string_field_class()
348 c_fc = self._tc.create_real_field_class()
349 self._append_element_method(self._fc, 'c', c_fc)
350 self._append_element_method(self._fc, 'a', a_fc)
351 self._append_element_method(self._fc, 'b', b_fc)
45c51519
PP
352 elem = self._at_index_method(self._fc, 1)
353 self.assertEqual(elem.field_class.addr, a_fc.addr)
354 self.assertEqual(elem.name, 'a')
b4f45851
SM
355
356 def test_at_index_invalid(self):
cfbd7cf3
FD
357 self._append_element_method(
358 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
359 )
b4f45851
SM
360
361 with self.assertRaises(TypeError):
d47b87ac 362 self._at_index_method(self._fc, 'yes')
b4f45851
SM
363
364 def test_at_index_out_of_bounds_after(self):
cfbd7cf3
FD
365 self._append_element_method(
366 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
367 )
b4f45851
SM
368
369 with self.assertRaises(IndexError):
d47b87ac 370 self._at_index_method(self._fc, len(self._fc))
b4f45851
SM
371
372
45c51519 373class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase):
cfbd7cf3
FD
374 _append_element_method = staticmethod(
375 bt2.field_class._StructureFieldClass.append_member
376 )
377 _at_index_method = staticmethod(
378 bt2.field_class._StructureFieldClass.member_at_index
379 )
45c51519
PP
380
381 def _create_default_fc(self):
382 return self._tc.create_structure_field_class()
383
384
cfbd7cf3
FD
385class VariantFieldClassWithoutSelectorTestCase(
386 _TestElementContainer, unittest.TestCase
387):
388 _append_element_method = staticmethod(
389 bt2.field_class._VariantFieldClassWithoutSelector.append_option
390 )
391 _at_index_method = staticmethod(
392 bt2.field_class._VariantFieldClassWithoutSelector.option_at_index
393 )
45c51519
PP
394
395 def _create_default_fc(self):
396 return self._tc.create_variant_field_class()
397
398
399class _VariantFieldClassWithSelectorTestCase:
b4f45851 400 def setUp(self):
d47b87ac 401 self._tc = get_default_trace_class()
45c51519
PP
402 self._spec_set_up()
403 self._fc = self._create_default_fc()
404
405 def _create_default_fc(self):
406 return self._tc.create_variant_field_class(self._selector_fc)
d47b87ac
SM
407
408 def test_create_default(self):
409 self.assertIsNotNone(self._fc)
b4f45851 410
45c51519
PP
411 def test_append_element(self):
412 str_field_class = self._tc.create_string_field_class()
413 self._fc.append_option('str', str_field_class, self._ranges1)
414 opt = self._fc['str']
415 self.assertEqual(opt.field_class.addr, str_field_class.addr)
416 self.assertEqual(opt.name, 'str')
417 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
418
419 def test_append_element_kwargs(self):
420 int_field_class = self._tc.create_signed_integer_field_class(32)
cfbd7cf3
FD
421 self._fc.append_option(
422 name='int32', field_class=int_field_class, ranges=self._ranges1
423 )
45c51519
PP
424 opt = self._fc['int32']
425 self.assertEqual(opt.field_class.addr, int_field_class.addr)
426 self.assertEqual(opt.name, 'int32')
427 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
d47b87ac 428
45c51519
PP
429 def test_append_element_invalid_name(self):
430 sub_fc = self._tc.create_string_field_class()
b4f45851 431
45c51519
PP
432 with self.assertRaises(TypeError):
433 self._fc.append_option(self._fc, 23, sub_fc)
b4f45851 434
45c51519
PP
435 def test_append_element_invalid_field_class(self):
436 with self.assertRaises(TypeError):
437 self._fc.append_option(self._fc, 'yes', object())
438
439 def test_append_element_invalid_ranges(self):
440 sub_fc = self._tc.create_string_field_class()
441
442 with self.assertRaises(TypeError):
443 self._fc.append_option(self._fc, sub_fc, 'lel')
444
445 def test_append_element_dup_name(self):
446 sub_fc1 = self._tc.create_string_field_class()
447 sub_fc2 = self._tc.create_string_field_class()
448
ce4923b0 449 with self.assertRaises(ValueError):
45c51519
PP
450 self._fc.append_option('yes', sub_fc1, self._ranges1)
451 self._fc.append_option('yes', sub_fc2, self._ranges2)
452
453 def test_append_element_invalid_ranges_signedness(self):
454 sub_fc = self._tc.create_string_field_class()
455
456 with self.assertRaises(TypeError):
457 self._fc.append_option(self._fc, sub_fc, self._inval_ranges)
458
459 def test_iadd(self):
460 other_fc = self._create_default_fc()
461 a_field_class = self._tc.create_real_field_class()
462 self._fc.append_option('a_float', a_field_class, self._ranges1)
463 c_field_class = self._tc.create_string_field_class()
cfbd7cf3
FD
464 d_field_class = self._tc.create_signed_enumeration_field_class(
465 field_value_range=32
466 )
45c51519
PP
467 self._fc += [
468 ('c_string', c_field_class, self._ranges2),
469 ('d_enum', d_field_class, self._ranges3),
470 ]
471 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
472 self.assertEqual(self._fc['a_float'].name, 'a_float')
473 self.assertEqual(self._fc['a_float'].ranges, self._ranges1)
474 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
475 self.assertEqual(self._fc['c_string'].name, 'c_string')
476 self.assertEqual(self._fc['c_string'].ranges, self._ranges2)
477 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
478 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
479 self.assertEqual(self._fc['d_enum'].ranges, self._ranges3)
480
481 def test_bool_op(self):
482 self.assertFalse(self._fc)
483 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
484 self.assertTrue(self._fc)
485
486 def test_len(self):
487 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
488 self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2)
489 self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3)
490 self.assertEqual(len(self._fc), 3)
491
492 def test_getitem(self):
493 a_fc = self._tc.create_signed_integer_field_class(32)
494 b_fc = self._tc.create_string_field_class()
495 c_fc = self._tc.create_real_field_class()
496 self._fc.append_option('a', a_fc, self._ranges1)
497 self._fc.append_option('b', b_fc, self._ranges2)
498 self._fc.append_option('c', c_fc, self._ranges3)
499 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
500 self.assertEqual(self._fc['b'].name, 'b')
501 self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr)
502
503 def test_getitem_invalid_key_type(self):
504 with self.assertRaises(TypeError):
505 self._fc[0]
506
507 def test_getitem_invalid_key(self):
508 with self.assertRaises(KeyError):
509 self._fc['no way']
510
511 def test_contains(self):
512 self.assertFalse('a' in self._fc)
513 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
514 self.assertTrue('a' in self._fc)
515
516 def test_iter(self):
517 a_fc = self._tc.create_signed_integer_field_class(32)
518 b_fc = self._tc.create_string_field_class()
519 c_fc = self._tc.create_real_field_class()
520 opts = (
521 ('a', a_fc, self._ranges1),
522 ('b', b_fc, self._ranges2),
523 ('c', c_fc, self._ranges3),
524 )
525
526 for opt in opts:
527 self._fc.append_option(*opt)
b4f45851 528
45c51519
PP
529 for (name, opt), test_opt in zip(self._fc.items(), opts):
530 self.assertEqual(opt.name, test_opt[0])
531 self.assertEqual(name, opt.name)
532 self.assertEqual(opt.field_class.addr, test_opt[1].addr)
533 self.assertEqual(opt.ranges.addr, test_opt[2].addr)
534
535 def test_at_index(self):
536 a_fc = self._tc.create_signed_integer_field_class(32)
537 b_fc = self._tc.create_string_field_class()
538 c_fc = self._tc.create_real_field_class()
539 self._fc.append_option('c', c_fc, self._ranges1)
540 self._fc.append_option('a', a_fc, self._ranges2)
541 self._fc.append_option('b', b_fc, self._ranges3)
542 self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr)
543 self.assertEqual(self._fc.option_at_index(1).name, 'a')
544 self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr)
545
546 def test_at_index_invalid(self):
cfbd7cf3
FD
547 self._fc.append_option(
548 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
549 )
45c51519
PP
550
551 with self.assertRaises(TypeError):
552 self._fc.option_at_index('yes')
553
554 def test_at_index_out_of_bounds_after(self):
cfbd7cf3
FD
555 self._fc.append_option(
556 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
557 )
45c51519
PP
558
559 with self.assertRaises(IndexError):
560 self._fc.option_at_index(len(self._fc))
561
562 def _fill_default_fc_for_field_path_test(self):
d47b87ac
SM
563 # Create something equivalent to:
564 #
565 # struct outer_struct_fc {
566 # real foo;
567 # struct inner_struct_fc {
45c51519 568 # [u]int64_t selector;
d47b87ac
SM
569 # string bar;
570 # string baz;
45c51519
PP
571 # variant <selector> {
572 # real a; // selected with self._ranges1
573 # int21_t b; // selected with self._ranges2
574 # uint34_t c; // selected with self._ranges3
d47b87ac
SM
575 # } variant;
576 # } inner_struct[2];
577 # };
45c51519 578 self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1)
cfbd7cf3
FD
579 self._fc.append_option(
580 'b', self._tc.create_signed_integer_field_class(21), self._ranges2
581 )
582 self._fc.append_option(
583 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3
584 )
b4f45851 585
d47b87ac
SM
586 foo_fc = self._tc.create_real_field_class()
587 bar_fc = self._tc.create_string_field_class()
588 baz_fc = self._tc.create_string_field_class()
b4f45851 589
d47b87ac 590 inner_struct_fc = self._tc.create_structure_field_class()
45c51519 591 inner_struct_fc.append_member('selector', self._selector_fc)
d47b87ac
SM
592 inner_struct_fc.append_member('bar', bar_fc)
593 inner_struct_fc.append_member('baz', baz_fc)
45c51519 594 inner_struct_fc.append_member('variant', self._fc)
b4f45851 595
cfbd7cf3
FD
596 inner_struct_array_fc = self._tc.create_static_array_field_class(
597 inner_struct_fc, 2
598 )
b4f45851 599
d47b87ac
SM
600 outer_struct_fc = self._tc.create_structure_field_class()
601 outer_struct_fc.append_member('foo', foo_fc)
602 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
b4f45851 603
d47b87ac
SM
604 # The path to the selector field is resolved when the sequence is
605 # actually used, for example in a packet context.
cfbd7cf3
FD
606 self._tc.create_stream_class(
607 supports_packets=True, packet_context_field_class=outer_struct_fc
608 )
b4f45851 609
d47b87ac 610 def test_selector_field_path_length(self):
45c51519
PP
611 self._fill_default_fc_for_field_path_test()
612 self.assertEqual(len(self._fc.selector_field_path), 3)
b4f45851 613
d47b87ac 614 def test_selector_field_path_iter(self):
45c51519
PP
615 self._fill_default_fc_for_field_path_test()
616 path_items = list(self._fc.selector_field_path)
b4f45851 617
d47b87ac 618 self.assertEqual(len(path_items), 3)
b4f45851 619
d47b87ac
SM
620 self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
621 self.assertEqual(path_items[0].index, 1)
b4f45851 622
cfbd7cf3
FD
623 self.assertIsInstance(
624 path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem
625 )
b4f45851 626
d47b87ac
SM
627 self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
628 self.assertEqual(path_items[2].index, 0)
b4f45851 629
d47b87ac 630 def test_selector_field_path_root_scope(self):
45c51519 631 self._fill_default_fc_for_field_path_test()
cfbd7cf3
FD
632 self.assertEqual(
633 self._fc.selector_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT
634 )
45c51519
PP
635
636
cfbd7cf3
FD
637class VariantFieldClassWithUnsignedSelectorTestCase(
638 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
639):
45c51519
PP
640 def _spec_set_up(self):
641 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
642 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
643 self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
644 self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)])
645 self._selector_fc = self._tc.create_unsigned_integer_field_class()
646
647
cfbd7cf3
FD
648class VariantFieldClassWithSignedSelectorTestCase(
649 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
650):
45c51519
PP
651 def _spec_set_up(self):
652 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
653 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
654 self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)])
655 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
656 self._selector_fc = self._tc.create_signed_integer_field_class()
b4f45851 657
d47b87ac
SM
658
659class StaticArrayFieldClassTestCase(unittest.TestCase):
660 def setUp(self):
661 self._tc = get_default_trace_class()
662 self._elem_fc = self._tc.create_signed_integer_field_class(23)
b4f45851
SM
663
664 def test_create_default(self):
d47b87ac
SM
665 fc = self._tc.create_static_array_field_class(self._elem_fc, 45)
666 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
667 self.assertEqual(fc.length, 45)
b4f45851 668
d47b87ac 669 def test_create_invalid_elem_field_class(self):
b4f45851 670 with self.assertRaises(TypeError):
d47b87ac 671 self._tc.create_static_array_field_class(object(), 45)
b4f45851
SM
672
673 def test_create_invalid_length(self):
674 with self.assertRaises(ValueError):
cfbd7cf3
FD
675 self._tc.create_static_array_field_class(
676 self._tc.create_string_field_class(), -17
677 )
b4f45851
SM
678
679 def test_create_invalid_length_type(self):
680 with self.assertRaises(TypeError):
cfbd7cf3
FD
681 self._tc.create_static_array_field_class(
682 self._tc.create_string_field_class(), 'the length'
683 )
d47b87ac
SM
684
685
686class DynamicArrayFieldClassTestCase(unittest.TestCase):
687 def setUp(self):
688 self._tc = get_default_trace_class()
689 self._elem_fc = self._tc.create_signed_integer_field_class(23)
690 self._len_fc = self._tc.create_unsigned_integer_field_class(12)
b4f45851 691
d47b87ac
SM
692 def test_create_default(self):
693 fc = self._tc.create_dynamic_array_field_class(self._elem_fc)
694 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
695 self.assertIsNone(fc.length_field_path, None)
b4f45851 696
d47b87ac
SM
697 def _create_field_class_for_field_path_test(self):
698 # Create something a field class that is equivalent to:
699 #
700 # struct outer_struct_fc {
701 # real foo;
702 # struct inner_struct_fc {
703 # string bar;
704 # string baz;
705 # uint12_t len;
706 # uint23_t dyn_array[len];
707 # } inner_struct[2];
708 # };
b4f45851 709
d47b87ac 710 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
b4f45851 711
d47b87ac
SM
712 foo_fc = self._tc.create_real_field_class()
713 bar_fc = self._tc.create_string_field_class()
714 baz_fc = self._tc.create_string_field_class()
b4f45851 715
d47b87ac
SM
716 inner_struct_fc = self._tc.create_structure_field_class()
717 inner_struct_fc.append_member('bar', bar_fc)
718 inner_struct_fc.append_member('baz', baz_fc)
719 inner_struct_fc.append_member('len', self._len_fc)
720 inner_struct_fc.append_member('dyn_array', fc)
b4f45851 721
cfbd7cf3
FD
722 inner_struct_array_fc = self._tc.create_static_array_field_class(
723 inner_struct_fc, 2
724 )
d47b87ac
SM
725
726 outer_struct_fc = self._tc.create_structure_field_class()
727 outer_struct_fc.append_member('foo', foo_fc)
728 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
729
730 # The path to the length field is resolved when the sequence is
731 # actually used, for example in a packet context.
cfbd7cf3
FD
732 self._tc.create_stream_class(
733 packet_context_field_class=outer_struct_fc, supports_packets=True
734 )
d47b87ac
SM
735
736 return fc
737
738 def test_field_path_len(self):
739 fc = self._create_field_class_for_field_path_test()
740 self.assertEqual(len(fc.length_field_path), 3)
741
742 def test_field_path_iter(self):
743 fc = self._create_field_class_for_field_path_test()
744 path_items = list(fc.length_field_path)
745
746 self.assertEqual(len(path_items), 3)
747
748 self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
749 self.assertEqual(path_items[0].index, 1)
750
cfbd7cf3
FD
751 self.assertIsInstance(
752 path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem
753 )
d47b87ac
SM
754
755 self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
756 self.assertEqual(path_items[2].index, 2)
757
758 def test_field_path_root_scope(self):
759 fc = self._create_field_class_for_field_path_test()
cfbd7cf3
FD
760 self.assertEqual(
761 fc.length_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT
762 )
b4f45851
SM
763
764 def test_create_invalid_field_class(self):
765 with self.assertRaises(TypeError):
d47b87ac 766 self._tc.create_dynamic_array_field_class(object())
b4f45851
SM
767
768 def test_create_invalid_length_type(self):
769 with self.assertRaises(TypeError):
cfbd7cf3
FD
770 self._tc.create_dynamic_array_field_class(
771 self._tc.create_string_field_class(), 17
772 )
b4f45851 773
b4f45851 774
d47b87ac
SM
775if __name__ == "__main__":
776 unittest.main()
This page took 0.067431 seconds and 4 git commands to generate.