09303363698e2f66b772c1f64bcc3315c066a845
[babeltrace.git] / tests / bindings / python / bt2 / test_field_class.py
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
19 import bt2.field
20 import unittest
21 import bt2
22 import collections
23 from utils import get_default_trace_class
24
25
26 class _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)
31
32 def test_create_range(self):
33 fc = self._create_func(field_value_range=35)
34 self.assertEqual(fc.field_value_range, 35)
35
36 fc = self._create_func(36)
37 self.assertEqual(fc.field_value_range, 36)
38
39 def test_create_invalid_range(self):
40 with self.assertRaises(TypeError):
41 self._create_func('yes')
42
43 with self.assertRaises(TypeError):
44 self._create_func(field_value_range='yes')
45
46 with self.assertRaises(ValueError):
47 self._create_func(field_value_range=-2)
48
49 with self.assertRaises(ValueError):
50 self._create_func(field_value_range=0)
51
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)
55
56 def test_create_invalid_base_type(self):
57 with self.assertRaises(TypeError):
58 self._create_func(preferred_display_base='yes')
59
60 def test_create_invalid_base_value(self):
61 with self.assertRaises(ValueError):
62 self._create_func(preferred_display_base=444)
63
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)
68
69
70 class 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
74
75
76 class RealFieldClassTestCase(unittest.TestCase):
77 def setUp(self):
78 self._tc = get_default_trace_class()
79
80 def test_create_default(self):
81 fc = self._tc.create_real_field_class()
82 self.assertFalse(fc.is_single_precision)
83
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):
89 with self.assertRaises(TypeError):
90 self._tc.create_real_field_class(is_single_precision='hohoho')
91
92
93 # Converts an _EnumerationFieldClassMapping to a list of ranges:
94 #
95 # [(lower0, upper0), (lower1, upper1), ...]
96
97 def enum_mapping_to_set(mapping):
98 return {(x.lower, x.upper) for x in mapping.ranges}
99
100
101 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
102 def setUp(self):
103 self._tc = get_default_trace_class()
104 self._spec_set_up()
105 self._fc = self._create_func()
106
107 def test_create_from_invalid_type(self):
108 with self.assertRaises(TypeError):
109 self._create_func('coucou')
110
111 def test_add_mapping_simple(self):
112 self._fc.add_mapping('hello', self._ranges1)
113 mapping = self._fc['hello']
114 self.assertEqual(mapping.label, 'hello')
115 self.assertEqual(mapping.ranges, self._ranges1)
116
117 def test_add_mapping_simple_kwargs(self):
118 self._fc.add_mapping(label='hello', ranges=self._ranges1)
119 mapping = self._fc['hello']
120 self.assertEqual(mapping.label, 'hello')
121 self.assertEqual(mapping.ranges, self._ranges1)
122
123 def test_add_mapping_invalid_name(self):
124 with self.assertRaises(TypeError):
125 self._fc.add_mapping(17, self._ranges1)
126
127 def test_add_mapping_invalid_range(self):
128 with self.assertRaises(TypeError):
129 self._fc.add_mapping('allo', 'meow')
130
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)
135
136 def test_add_mapping_invalid_ranges_signedness(self):
137 with self.assertRaises(TypeError):
138 self._fc.add_mapping('allo', self._inval_ranges)
139
140 def test_iadd(self):
141 self._fc.add_mapping('c', self._ranges1)
142
143 self._fc += [
144 ('d', self._ranges2),
145 ('e', self._ranges3),
146 ]
147
148 self.assertEqual(len(self._fc), 3)
149 self.assertEqual(self._fc['c'].label, 'c')
150 self.assertEqual(self._fc['c'].ranges, self._ranges1)
151 self.assertEqual(self._fc['d'].label, 'd')
152 self.assertEqual(self._fc['d'].ranges, self._ranges2)
153 self.assertEqual(self._fc['e'].label, 'e')
154 self.assertEqual(self._fc['e'].ranges, self._ranges3)
155
156 def test_bool_op(self):
157 self.assertFalse(self._fc)
158 self._fc.add_mapping('a', self._ranges1)
159 self.assertTrue(self._fc)
160
161 def test_len(self):
162 self._fc.add_mapping('a', self._ranges1)
163 self._fc.add_mapping('b', self._ranges2)
164 self._fc.add_mapping('c', self._ranges3)
165 self.assertEqual(len(self._fc), 3)
166
167 def test_getitem(self):
168 self._fc.add_mapping('a', self._ranges1)
169 self._fc.add_mapping('b', self._ranges2)
170 self._fc.add_mapping('c', self._ranges3)
171 mapping = self._fc['a']
172 self.assertEqual(mapping.label, 'a')
173 self.assertEqual(mapping.ranges, self._ranges1)
174
175 def test_getitem_nonexistent(self):
176 with self.assertRaises(KeyError):
177 self._fc['doesnotexist']
178
179 def test_iter(self):
180 self._fc.add_mapping('a', self._ranges1)
181 self._fc.add_mapping('b', self._ranges2)
182 self._fc.add_mapping('c', self._ranges3)
183
184 # This exercises iteration.
185 labels = sorted(self._fc)
186
187 self.assertEqual(labels, ['a', 'b', 'c'])
188
189 def test_find_by_value(self):
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
199 class 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
206 self._create_func = self._tc.create_unsigned_enumeration_field_class
207
208
209 class 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
216 self._create_func = self._tc.create_signed_enumeration_field_class
217
218
219 class StringFieldClassTestCase(unittest.TestCase):
220 def setUp(self):
221 tc = get_default_trace_class()
222 self._fc = tc.create_string_field_class()
223
224 def test_create_default(self):
225 self.assertIsNotNone(self._fc)
226
227
228 class _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
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)
239 field_class = self._fc['int32'].field_class
240 self.assertEqual(field_class.addr, int_field_class.addr)
241
242 def test_append_element_kwargs(self):
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)
245 field_class = self._fc['int32'].field_class
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()
250
251 with self.assertRaises(TypeError):
252 self._append_element_method(self._fc, 23, sub_fc)
253
254 def test_append_element_invalid_field_class(self):
255 with self.assertRaises(TypeError):
256 self._append_element_method(self._fc, 'yes', object())
257
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
266 def test_iadd(self):
267 other_fc = self._create_default_fc()
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)
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')
290
291 def test_bool_op(self):
292 self.assertFalse(self._fc)
293 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
294 self.assertTrue(self._fc)
295
296 def test_len(self):
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())
300 self.assertEqual(len(self._fc), 3)
301
302 def test_getitem(self):
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)
309 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
310 self.assertEqual(self._fc['b'].name, 'b')
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)
322 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
323 self.assertTrue('a' in self._fc)
324
325 def test_iter(self):
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()
329 elements = (
330 ('a', a_fc),
331 ('b', b_fc),
332 ('c', c_fc),
333 )
334
335 for elem in elements:
336 self._append_element_method(self._fc, *elem)
337
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)
342
343 def test_at_index(self):
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)
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')
353
354 def test_at_index_invalid(self):
355 self._append_element_method(self._fc, 'c', self._tc.create_signed_integer_field_class(32))
356
357 with self.assertRaises(TypeError):
358 self._at_index_method(self._fc, 'yes')
359
360 def test_at_index_out_of_bounds_after(self):
361 self._append_element_method(self._fc, 'c', self._tc.create_signed_integer_field_class(32))
362
363 with self.assertRaises(IndexError):
364 self._at_index_method(self._fc, len(self._fc))
365
366
367 class 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
375 class 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
383 class _VariantFieldClassWithSelectorTestCase:
384 def setUp(self):
385 self._tc = get_default_trace_class()
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)
391
392 def test_create_default(self):
393 self.assertIsNotNone(self._fc)
394
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)
411
412 def test_append_element_invalid_name(self):
413 sub_fc = self._tc.create_string_field_class()
414
415 with self.assertRaises(TypeError):
416 self._fc.append_option(self._fc, 23, sub_fc)
417
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)
509
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):
540 # Create something equivalent to:
541 #
542 # struct outer_struct_fc {
543 # real foo;
544 # struct inner_struct_fc {
545 # [u]int64_t selector;
546 # string bar;
547 # string baz;
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
552 # } variant;
553 # } inner_struct[2];
554 # };
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)
558
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()
562
563 inner_struct_fc = self._tc.create_structure_field_class()
564 inner_struct_fc.append_member('selector', self._selector_fc)
565 inner_struct_fc.append_member('bar', bar_fc)
566 inner_struct_fc.append_member('baz', baz_fc)
567 inner_struct_fc.append_member('variant', self._fc)
568
569 inner_struct_array_fc = self._tc.create_static_array_field_class(inner_struct_fc, 2)
570
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)
574
575 # The path to the selector field is resolved when the sequence is
576 # actually used, for example in a packet context.
577 self._tc.create_stream_class(supports_packets=True,
578 packet_context_field_class=outer_struct_fc)
579
580 def test_selector_field_path_length(self):
581 self._fill_default_fc_for_field_path_test()
582 self.assertEqual(len(self._fc.selector_field_path), 3)
583
584 def test_selector_field_path_iter(self):
585 self._fill_default_fc_for_field_path_test()
586 path_items = list(self._fc.selector_field_path)
587
588 self.assertEqual(len(path_items), 3)
589
590 self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
591 self.assertEqual(path_items[0].index, 1)
592
593 self.assertIsInstance(path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem)
594
595 self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
596 self.assertEqual(path_items[2].index, 0)
597
598 def test_selector_field_path_root_scope(self):
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
603 class 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
612 class 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()
619
620
621 class 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)
625
626 def test_create_default(self):
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)
630
631 def test_create_invalid_elem_field_class(self):
632 with self.assertRaises(TypeError):
633 self._tc.create_static_array_field_class(object(), 45)
634
635 def test_create_invalid_length(self):
636 with self.assertRaises(ValueError):
637 self._tc.create_static_array_field_class(self._tc.create_string_field_class(), -17)
638
639 def test_create_invalid_length_type(self):
640 with self.assertRaises(TypeError):
641 self._tc.create_static_array_field_class(self._tc.create_string_field_class(), 'the length')
642
643
644 class 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)
649
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)
654
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 # };
667
668 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
669
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()
673
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)
679
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.
688 self._tc.create_stream_class(packet_context_field_class=outer_struct_fc,
689 supports_packets=True)
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)
714
715 def test_create_invalid_field_class(self):
716 with self.assertRaises(TypeError):
717 self._tc.create_dynamic_array_field_class(object())
718
719 def test_create_invalid_length_type(self):
720 with self.assertRaises(TypeError):
721 self._tc.create_dynamic_array_field_class(self._tc.create_string_field_class(), 17)
722
723
724 if __name__ == "__main__":
725 unittest.main()
This page took 0.047454 seconds and 3 git commands to generate.