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