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