- def test_iter(self):
- mappings = (
- ('a', 1, 5),
- ('b', 10, 17),
- ('c', 20, 1504),
- ('d', 22510, 99999),
- )
-
- for mapping in mappings:
- self._fc.add_mapping(*mapping)
-
- for fc_mapping, mapping in zip(self._fc, mappings):
- self.assertEqual(fc_mapping.name, mapping[0])
- self.assertEqual(fc_mapping.lower, mapping[1])
- self.assertEqual(fc_mapping.upper, mapping[2])
-
- def test_mapping_eq(self):
- enum1 = bt2.EnumerationFieldClass(size=32)
- enum2 = bt2.EnumerationFieldClass(size=16)
- enum1.add_mapping('b', 1, 3)
- enum2.add_mapping('b', 1, 3)
- self.assertEqual(enum1[0], enum2[0])
-
- def test_mapping_eq_invalid(self):
- enum1 = bt2.EnumerationFieldClass(size=32)
- enum1.add_mapping('b', 1, 3)
- self.assertNotEqual(enum1[0], 23)
-
- def _test_find_by_name(self, fc):
- fc.add_mapping('a', 0)
- fc.add_mapping('b', 1, 3)
- fc.add_mapping('a', 5)
- fc.add_mapping('a', 17, 144)
- fc.add_mapping('C', 5)
- mapping_iter = fc.mappings_by_name('a')
- mappings = list(mapping_iter)
- a0 = False
- a5 = False
- a17_144 = False
- i = 0
-
- for mapping in mappings:
- i += 1
- self.assertEqual(mapping.name, 'a')
-
- if mapping.lower == 0 and mapping.upper == 0:
- a0 = True
- elif mapping.lower == 5 and mapping.upper == 5:
- a5 = True
- elif mapping.lower == 17 and mapping.upper == 144:
- a17_144 = True
-
- self.assertEqual(i, 3)
- self.assertTrue(a0)
- self.assertTrue(a5)
- self.assertTrue(a17_144)
-
- def test_find_by_name_signed(self):
- self._test_find_by_name(bt2.EnumerationFieldClass(size=8, is_signed=True))
-
- def test_find_by_name_unsigned(self):
- self._test_find_by_name(bt2.EnumerationFieldClass(size=8))
-
- def _test_find_by_value(self, fc):
- fc.add_mapping('a', 0)
- fc.add_mapping('b', 1, 3)
- fc.add_mapping('c', 5, 19)
- fc.add_mapping('d', 8, 15)
- fc.add_mapping('e', 10, 21)
- fc.add_mapping('f', 0)
- fc.add_mapping('g', 14)
- mapping_iter = fc.mappings_by_value(14)
- mappings = list(mapping_iter)
- c = False
- d = False
- e = False
- g = False
- i = 0
-
- for mapping in mappings:
- i += 1
-
- if mapping.name == 'c':
- c = True
- elif mapping.name == 'd':
- d = True
- elif mapping.name == 'e':
- e = True
- elif mapping.name == 'g':
- g = True
-
- self.assertEqual(i, 4)
- self.assertTrue(c)
- self.assertTrue(d)
- self.assertTrue(e)
- self.assertTrue(g)
-
- def test_find_by_value_signed(self):
- self._test_find_by_value(bt2.EnumerationFieldClass(size=8, is_signed=True))
-
- def test_find_by_value_unsigned(self):
- self._test_find_by_value(bt2.EnumerationFieldClass(size=8))
-
- def test_create_field(self):
- self._fc.add_mapping('c', 4, 5)
- field = self._fc()
- self.assertIsInstance(field, bt2.field._EnumerationField)
-
- def test_create_field_init(self):
- self._fc.add_mapping('c', 4, 5)
- field = self._fc(4)
- self.assertEqual(field, 4)
-
-
-@unittest.skip("this is broken")
-class StringFieldClassTestCase(_TestCopySimple, _TestInvalidEq,
- unittest.TestCase):