cli: fix "permission denied" test after g_dir_open
[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):
3fb99a22
PP
374 _append_element_method = staticmethod(bt2._StructureFieldClass.append_member)
375 _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index)
45c51519
PP
376
377 def _create_default_fc(self):
378 return self._tc.create_structure_field_class()
379
380
cfbd7cf3
FD
381class VariantFieldClassWithoutSelectorTestCase(
382 _TestElementContainer, unittest.TestCase
383):
384 _append_element_method = staticmethod(
3fb99a22 385 bt2._VariantFieldClassWithoutSelector.append_option
cfbd7cf3
FD
386 )
387 _at_index_method = staticmethod(
3fb99a22 388 bt2._VariantFieldClassWithoutSelector.option_at_index
cfbd7cf3 389 )
45c51519
PP
390
391 def _create_default_fc(self):
392 return self._tc.create_variant_field_class()
393
394
395class _VariantFieldClassWithSelectorTestCase:
b4f45851 396 def setUp(self):
d47b87ac 397 self._tc = get_default_trace_class()
45c51519
PP
398 self._spec_set_up()
399 self._fc = self._create_default_fc()
400
401 def _create_default_fc(self):
402 return self._tc.create_variant_field_class(self._selector_fc)
d47b87ac
SM
403
404 def test_create_default(self):
405 self.assertIsNotNone(self._fc)
b4f45851 406
45c51519
PP
407 def test_append_element(self):
408 str_field_class = self._tc.create_string_field_class()
409 self._fc.append_option('str', str_field_class, self._ranges1)
410 opt = self._fc['str']
411 self.assertEqual(opt.field_class.addr, str_field_class.addr)
412 self.assertEqual(opt.name, 'str')
413 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
414
415 def test_append_element_kwargs(self):
416 int_field_class = self._tc.create_signed_integer_field_class(32)
cfbd7cf3
FD
417 self._fc.append_option(
418 name='int32', field_class=int_field_class, ranges=self._ranges1
419 )
45c51519
PP
420 opt = self._fc['int32']
421 self.assertEqual(opt.field_class.addr, int_field_class.addr)
422 self.assertEqual(opt.name, 'int32')
423 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
d47b87ac 424
45c51519
PP
425 def test_append_element_invalid_name(self):
426 sub_fc = self._tc.create_string_field_class()
b4f45851 427
45c51519
PP
428 with self.assertRaises(TypeError):
429 self._fc.append_option(self._fc, 23, sub_fc)
b4f45851 430
45c51519
PP
431 def test_append_element_invalid_field_class(self):
432 with self.assertRaises(TypeError):
433 self._fc.append_option(self._fc, 'yes', object())
434
435 def test_append_element_invalid_ranges(self):
436 sub_fc = self._tc.create_string_field_class()
437
438 with self.assertRaises(TypeError):
439 self._fc.append_option(self._fc, sub_fc, 'lel')
440
441 def test_append_element_dup_name(self):
442 sub_fc1 = self._tc.create_string_field_class()
443 sub_fc2 = self._tc.create_string_field_class()
444
ce4923b0 445 with self.assertRaises(ValueError):
45c51519
PP
446 self._fc.append_option('yes', sub_fc1, self._ranges1)
447 self._fc.append_option('yes', sub_fc2, self._ranges2)
448
449 def test_append_element_invalid_ranges_signedness(self):
450 sub_fc = self._tc.create_string_field_class()
451
452 with self.assertRaises(TypeError):
453 self._fc.append_option(self._fc, sub_fc, self._inval_ranges)
454
455 def test_iadd(self):
456 other_fc = self._create_default_fc()
457 a_field_class = self._tc.create_real_field_class()
458 self._fc.append_option('a_float', a_field_class, self._ranges1)
459 c_field_class = self._tc.create_string_field_class()
cfbd7cf3
FD
460 d_field_class = self._tc.create_signed_enumeration_field_class(
461 field_value_range=32
462 )
45c51519
PP
463 self._fc += [
464 ('c_string', c_field_class, self._ranges2),
465 ('d_enum', d_field_class, self._ranges3),
466 ]
467 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
468 self.assertEqual(self._fc['a_float'].name, 'a_float')
469 self.assertEqual(self._fc['a_float'].ranges, self._ranges1)
470 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
471 self.assertEqual(self._fc['c_string'].name, 'c_string')
472 self.assertEqual(self._fc['c_string'].ranges, self._ranges2)
473 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
474 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
475 self.assertEqual(self._fc['d_enum'].ranges, self._ranges3)
476
477 def test_bool_op(self):
478 self.assertFalse(self._fc)
479 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
480 self.assertTrue(self._fc)
481
482 def test_len(self):
483 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
484 self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2)
485 self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3)
486 self.assertEqual(len(self._fc), 3)
487
488 def test_getitem(self):
489 a_fc = self._tc.create_signed_integer_field_class(32)
490 b_fc = self._tc.create_string_field_class()
491 c_fc = self._tc.create_real_field_class()
492 self._fc.append_option('a', a_fc, self._ranges1)
493 self._fc.append_option('b', b_fc, self._ranges2)
494 self._fc.append_option('c', c_fc, self._ranges3)
495 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
496 self.assertEqual(self._fc['b'].name, 'b')
497 self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr)
498
499 def test_getitem_invalid_key_type(self):
500 with self.assertRaises(TypeError):
501 self._fc[0]
502
503 def test_getitem_invalid_key(self):
504 with self.assertRaises(KeyError):
505 self._fc['no way']
506
507 def test_contains(self):
508 self.assertFalse('a' in self._fc)
509 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
510 self.assertTrue('a' in self._fc)
511
512 def test_iter(self):
513 a_fc = self._tc.create_signed_integer_field_class(32)
514 b_fc = self._tc.create_string_field_class()
515 c_fc = self._tc.create_real_field_class()
516 opts = (
517 ('a', a_fc, self._ranges1),
518 ('b', b_fc, self._ranges2),
519 ('c', c_fc, self._ranges3),
520 )
521
522 for opt in opts:
523 self._fc.append_option(*opt)
b4f45851 524
45c51519
PP
525 for (name, opt), test_opt in zip(self._fc.items(), opts):
526 self.assertEqual(opt.name, test_opt[0])
527 self.assertEqual(name, opt.name)
528 self.assertEqual(opt.field_class.addr, test_opt[1].addr)
529 self.assertEqual(opt.ranges.addr, test_opt[2].addr)
530
531 def test_at_index(self):
532 a_fc = self._tc.create_signed_integer_field_class(32)
533 b_fc = self._tc.create_string_field_class()
534 c_fc = self._tc.create_real_field_class()
535 self._fc.append_option('c', c_fc, self._ranges1)
536 self._fc.append_option('a', a_fc, self._ranges2)
537 self._fc.append_option('b', b_fc, self._ranges3)
538 self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr)
539 self.assertEqual(self._fc.option_at_index(1).name, 'a')
540 self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr)
541
542 def test_at_index_invalid(self):
cfbd7cf3
FD
543 self._fc.append_option(
544 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
545 )
45c51519
PP
546
547 with self.assertRaises(TypeError):
548 self._fc.option_at_index('yes')
549
550 def test_at_index_out_of_bounds_after(self):
cfbd7cf3
FD
551 self._fc.append_option(
552 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
553 )
45c51519
PP
554
555 with self.assertRaises(IndexError):
556 self._fc.option_at_index(len(self._fc))
557
558 def _fill_default_fc_for_field_path_test(self):
d47b87ac
SM
559 # Create something equivalent to:
560 #
561 # struct outer_struct_fc {
562 # real foo;
563 # struct inner_struct_fc {
45c51519 564 # [u]int64_t selector;
d47b87ac
SM
565 # string bar;
566 # string baz;
45c51519
PP
567 # variant <selector> {
568 # real a; // selected with self._ranges1
569 # int21_t b; // selected with self._ranges2
570 # uint34_t c; // selected with self._ranges3
d47b87ac
SM
571 # } variant;
572 # } inner_struct[2];
573 # };
45c51519 574 self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1)
cfbd7cf3
FD
575 self._fc.append_option(
576 'b', self._tc.create_signed_integer_field_class(21), self._ranges2
577 )
578 self._fc.append_option(
579 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3
580 )
b4f45851 581
d47b87ac
SM
582 foo_fc = self._tc.create_real_field_class()
583 bar_fc = self._tc.create_string_field_class()
584 baz_fc = self._tc.create_string_field_class()
b4f45851 585
d47b87ac 586 inner_struct_fc = self._tc.create_structure_field_class()
45c51519 587 inner_struct_fc.append_member('selector', self._selector_fc)
d47b87ac
SM
588 inner_struct_fc.append_member('bar', bar_fc)
589 inner_struct_fc.append_member('baz', baz_fc)
45c51519 590 inner_struct_fc.append_member('variant', self._fc)
b4f45851 591
cfbd7cf3
FD
592 inner_struct_array_fc = self._tc.create_static_array_field_class(
593 inner_struct_fc, 2
594 )
b4f45851 595
d47b87ac
SM
596 outer_struct_fc = self._tc.create_structure_field_class()
597 outer_struct_fc.append_member('foo', foo_fc)
598 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
b4f45851 599
d47b87ac
SM
600 # The path to the selector field is resolved when the sequence is
601 # actually used, for example in a packet context.
cfbd7cf3
FD
602 self._tc.create_stream_class(
603 supports_packets=True, packet_context_field_class=outer_struct_fc
604 )
b4f45851 605
d47b87ac 606 def test_selector_field_path_length(self):
45c51519
PP
607 self._fill_default_fc_for_field_path_test()
608 self.assertEqual(len(self._fc.selector_field_path), 3)
b4f45851 609
d47b87ac 610 def test_selector_field_path_iter(self):
45c51519
PP
611 self._fill_default_fc_for_field_path_test()
612 path_items = list(self._fc.selector_field_path)
b4f45851 613
d47b87ac 614 self.assertEqual(len(path_items), 3)
b4f45851 615
3fb99a22 616 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
d47b87ac 617 self.assertEqual(path_items[0].index, 1)
b4f45851 618
3fb99a22 619 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
b4f45851 620
3fb99a22 621 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
d47b87ac 622 self.assertEqual(path_items[2].index, 0)
b4f45851 623
d47b87ac 624 def test_selector_field_path_root_scope(self):
45c51519 625 self._fill_default_fc_for_field_path_test()
cfbd7cf3 626 self.assertEqual(
3fb99a22 627 self._fc.selector_field_path.root_scope, bt2.Scope.PACKET_CONTEXT
cfbd7cf3 628 )
45c51519
PP
629
630
cfbd7cf3
FD
631class VariantFieldClassWithUnsignedSelectorTestCase(
632 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
633):
45c51519
PP
634 def _spec_set_up(self):
635 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
636 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
637 self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
638 self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)])
639 self._selector_fc = self._tc.create_unsigned_integer_field_class()
640
641
cfbd7cf3
FD
642class VariantFieldClassWithSignedSelectorTestCase(
643 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
644):
45c51519
PP
645 def _spec_set_up(self):
646 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
647 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
648 self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)])
649 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
650 self._selector_fc = self._tc.create_signed_integer_field_class()
b4f45851 651
d47b87ac
SM
652
653class StaticArrayFieldClassTestCase(unittest.TestCase):
654 def setUp(self):
655 self._tc = get_default_trace_class()
656 self._elem_fc = self._tc.create_signed_integer_field_class(23)
b4f45851
SM
657
658 def test_create_default(self):
d47b87ac
SM
659 fc = self._tc.create_static_array_field_class(self._elem_fc, 45)
660 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
661 self.assertEqual(fc.length, 45)
b4f45851 662
d47b87ac 663 def test_create_invalid_elem_field_class(self):
b4f45851 664 with self.assertRaises(TypeError):
d47b87ac 665 self._tc.create_static_array_field_class(object(), 45)
b4f45851
SM
666
667 def test_create_invalid_length(self):
668 with self.assertRaises(ValueError):
cfbd7cf3
FD
669 self._tc.create_static_array_field_class(
670 self._tc.create_string_field_class(), -17
671 )
b4f45851
SM
672
673 def test_create_invalid_length_type(self):
674 with self.assertRaises(TypeError):
cfbd7cf3
FD
675 self._tc.create_static_array_field_class(
676 self._tc.create_string_field_class(), 'the length'
677 )
d47b87ac
SM
678
679
680class DynamicArrayFieldClassTestCase(unittest.TestCase):
681 def setUp(self):
682 self._tc = get_default_trace_class()
683 self._elem_fc = self._tc.create_signed_integer_field_class(23)
684 self._len_fc = self._tc.create_unsigned_integer_field_class(12)
b4f45851 685
d47b87ac
SM
686 def test_create_default(self):
687 fc = self._tc.create_dynamic_array_field_class(self._elem_fc)
688 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
689 self.assertIsNone(fc.length_field_path, None)
b4f45851 690
d47b87ac
SM
691 def _create_field_class_for_field_path_test(self):
692 # Create something a field class that is equivalent to:
693 #
694 # struct outer_struct_fc {
695 # real foo;
696 # struct inner_struct_fc {
697 # string bar;
698 # string baz;
699 # uint12_t len;
700 # uint23_t dyn_array[len];
701 # } inner_struct[2];
702 # };
b4f45851 703
d47b87ac 704 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
b4f45851 705
d47b87ac
SM
706 foo_fc = self._tc.create_real_field_class()
707 bar_fc = self._tc.create_string_field_class()
708 baz_fc = self._tc.create_string_field_class()
b4f45851 709
d47b87ac
SM
710 inner_struct_fc = self._tc.create_structure_field_class()
711 inner_struct_fc.append_member('bar', bar_fc)
712 inner_struct_fc.append_member('baz', baz_fc)
713 inner_struct_fc.append_member('len', self._len_fc)
714 inner_struct_fc.append_member('dyn_array', fc)
b4f45851 715
cfbd7cf3
FD
716 inner_struct_array_fc = self._tc.create_static_array_field_class(
717 inner_struct_fc, 2
718 )
d47b87ac
SM
719
720 outer_struct_fc = self._tc.create_structure_field_class()
721 outer_struct_fc.append_member('foo', foo_fc)
722 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
723
724 # The path to the length field is resolved when the sequence is
725 # actually used, for example in a packet context.
cfbd7cf3
FD
726 self._tc.create_stream_class(
727 packet_context_field_class=outer_struct_fc, supports_packets=True
728 )
d47b87ac
SM
729
730 return fc
731
732 def test_field_path_len(self):
733 fc = self._create_field_class_for_field_path_test()
734 self.assertEqual(len(fc.length_field_path), 3)
735
736 def test_field_path_iter(self):
737 fc = self._create_field_class_for_field_path_test()
738 path_items = list(fc.length_field_path)
739
740 self.assertEqual(len(path_items), 3)
741
3fb99a22 742 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
d47b87ac
SM
743 self.assertEqual(path_items[0].index, 1)
744
3fb99a22 745 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
d47b87ac 746
3fb99a22 747 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
d47b87ac
SM
748 self.assertEqual(path_items[2].index, 2)
749
750 def test_field_path_root_scope(self):
751 fc = self._create_field_class_for_field_path_test()
3fb99a22 752 self.assertEqual(fc.length_field_path.root_scope, bt2.Scope.PACKET_CONTEXT)
b4f45851
SM
753
754 def test_create_invalid_field_class(self):
755 with self.assertRaises(TypeError):
d47b87ac 756 self._tc.create_dynamic_array_field_class(object())
b4f45851
SM
757
758 def test_create_invalid_length_type(self):
759 with self.assertRaises(TypeError):
cfbd7cf3
FD
760 self._tc.create_dynamic_array_field_class(
761 self._tc.create_string_field_class(), 17
762 )
b4f45851 763
b4f45851 764
d47b87ac
SM
765if __name__ == "__main__":
766 unittest.main()
This page took 0.067709 seconds and 4 git commands to generate.