bt2: field.py: raise ValueError when setting out of range value to Integer
[babeltrace.git] / tests / bindings / python / bt2 / test_field.py
index cdacd7ffa63971e462029877080ef744a58dc58a..ecf0194c3b7d589b036a82bf364f17e26a9e8be8 100644 (file)
@@ -660,6 +660,18 @@ class _TestNumericField:
     def test_str_op(self):
         self.assertEqual(str(self._def), str(self._def_value))
 
+    def test_hash_op(self):
+        with self.assertRaises(TypeError):
+            hash(self._def)
+
+    def test_const_hash_op(self):
+        self.assertEqual(hash(self._def_const), hash(self._def_value))
+
+    def test_const_hash_dict(self):
+        my_dict = {}
+        my_dict[self._def_const] = 'my_value'
+        self.assertEqual(my_dict[self._def_value], 'my_value')
+
     def test_eq_none(self):
         # Ignore this lint error:
         #   E711 comparison to None should be 'if cond is None:'
@@ -1272,12 +1284,35 @@ class _TestIntegerFieldCommon(_TestNumericField):
         field.value = (2 ** 53) + 1
         self.assertEqual(field, raw)
 
-    def test_assign_uint_invalid_neg(self):
-        uint_fc = self._tc.create_unsigned_integer_field_class(32)
+    def test_assign_uint_out_of_range(self):
+        uint_fc = self._tc.create_unsigned_integer_field_class(8)
         field = _create_field(self._tc, uint_fc)
 
-        with self.assertRaises(ValueError):
-            field.value = -23
+        with self.assertRaises(ValueError) as ctx:
+            field.value = 256
+        self.assertEqual(
+            str(ctx.exception), 'Value 256 is outside valid range [0, 255]'
+        )
+
+        with self.assertRaises(ValueError) as ctx:
+            field.value = -1
+        self.assertEqual(str(ctx.exception), 'Value -1 is outside valid range [0, 255]')
+
+    def test_assign_int_out_of_range(self):
+        int_fc = self._tc.create_signed_integer_field_class(8)
+        field = _create_field(self._tc, int_fc)
+
+        with self.assertRaises(ValueError) as ctx:
+            field.value = 128
+        self.assertEqual(
+            str(ctx.exception), 'Value 128 is outside valid range [-128, 127]'
+        )
+
+        with self.assertRaises(ValueError) as ctx:
+            field.value = -129
+        self.assertEqual(
+            str(ctx.exception), 'Value -129 is outside valid range [-128, 127]'
+        )
 
     def test_str_op(self):
         self.assertEqual(str(self._def), str(self._def_value))
@@ -1287,6 +1322,10 @@ _inject_numeric_testing_methods(_TestIntegerFieldCommon)
 
 
 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
+    @staticmethod
+    def _const_value_setter(field):
+        field.value = 17
+
     def _create_fc(self, tc):
         return tc.create_signed_integer_field_class(25)
 
@@ -1297,10 +1336,17 @@ class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
         self._def = _create_field(self._tc, self._create_fc(self._tc))
         self._def.value = 17
         self._def_value = 17
+        self._def_const = _create_const_field(
+            self._tc, self._create_fc(self._tc), self._const_value_setter
+        )
         self._def_new_value = -101
 
 
 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
+    @staticmethod
+    def _const_value_setter(field):
+        field.value = 17
+
     def _create_fc(self, tc):
         fc = tc.create_signed_enumeration_field_class(32)
         fc.add_mapping('something', bt2.SignedIntegerRangeSet([(17, 17)]))
@@ -1318,6 +1364,9 @@ class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase)
         self._def = _create_field(self._tc, self._create_fc(self._tc))
         self._def.value = 17
         self._def_value = 17
+        self._def_const = _create_const_field(
+            self._tc, self._create_fc(self._tc), self._const_value_setter
+        )
         self._def_new_value = -101
 
     def test_str_op(self):
@@ -1342,6 +1391,10 @@ class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase)
 
 
 class RealFieldTestCase(_TestNumericField, unittest.TestCase):
+    @staticmethod
+    def _const_value_setter(field):
+        field.value = 52.7
+
     def _create_fc(self, tc):
         return tc.create_real_field_class()
 
@@ -1349,6 +1402,9 @@ class RealFieldTestCase(_TestNumericField, unittest.TestCase):
         self._tc = get_default_trace_class()
         self._field = _create_field(self._tc, self._create_fc(self._tc))
         self._def = _create_field(self._tc, self._create_fc(self._tc))
+        self._def_const = _create_const_field(
+            self._tc, self._tc.create_real_field_class(), self._const_value_setter
+        )
         self._def.value = 52.7
         self._def_value = 52.7
         self._def_new_value = -17.164857
@@ -1541,6 +1597,18 @@ class StringFieldTestCase(unittest.TestCase):
         self._def_value += to_append
         self.assertEqual(self._def, self._def_value)
 
+    def test_hash_op(self):
+        with self.assertRaises(TypeError):
+            hash(self._def)
+
+    def test_const_hash_op(self):
+        self.assertEqual(hash(self._def_const), hash(self._def_value))
+
+    def test_const_hash_dict(self):
+        my_dict = {}
+        my_dict[self._def_const] = 'my_value'
+        self.assertEqual(my_dict[self._def_value], 'my_value')
+
 
 class _TestArrayFieldCommon:
     def _modify_def(self):
@@ -2293,3 +2361,7 @@ class VariantFieldTestCase(unittest.TestCase):
         other_field.selected_option_index = 2
         other_field.value = 14.4245
         self.assertEqual(str(field), str(other_field))
+
+
+if __name__ == '__main__':
+    unittest.main()
This page took 0.025472 seconds and 4 git commands to generate.