+ self._def_const = create_const_field(
+ self._tc,
+ self._tc.create_single_precision_real_field_class(),
+ self._const_value_setter,
+ )
+ self._def.value = 52.0
+ self._def_value = 52.0
+ self._def_new_value = -17.0
+
+ def _test_invalid_op(self, cb):
+ with self.assertRaises(TypeError):
+ cb()
+
+ def test_assign_true(self):
+ self._def.value = True
+ self.assertTrue(self._def)
+
+ def test_assign_false(self):
+ self._def.value = False
+ self.assertFalse(self._def)
+
+ def test_assign_pos_int(self):
+ raw = 477
+ self._def.value = raw
+ self.assertEqual(self._def, float(raw))
+
+ def test_assign_neg_int(self):
+ raw = -13
+ self._def.value = raw
+ self.assertEqual(self._def, float(raw))
+
+ def test_assign_int_field(self):
+ int_fc = self._tc.create_signed_integer_field_class(32)
+ int_field = _create_field(self._tc, int_fc)
+ raw = 999
+ int_field.value = raw
+ self._def.value = int_field
+ self.assertEqual(self._def, float(raw))
+
+ def test_assign_float(self):
+ raw = -19.23
+ self._def.value = raw
+ # It's expected to have some lost of precision because of the field
+ # that is in single precision.
+ self.assertEqual(round(self._def, 5), raw)
+
+ def test_assign_float_field(self):
+ field = _create_field(self._tc, self._create_fc(self._tc))
+ raw = 101.32
+ field.value = raw
+ self._def.value = field
+ # It's expected to have some lost of precision because of the field
+ # that is in single precision.
+ self.assertEqual(round(self._def, 5), raw)
+
+ def test_assign_invalid_type(self):
+ with self.assertRaises(TypeError):
+ self._def.value = 'yes'
+
+ def test_invalid_lshift(self):
+ self._test_invalid_op(lambda: self._def << 23)
+
+ def test_invalid_rshift(self):
+ self._test_invalid_op(lambda: self._def >> 23)
+
+ def test_invalid_and(self):
+ self._test_invalid_op(lambda: self._def & 23)
+
+ def test_invalid_or(self):
+ self._test_invalid_op(lambda: self._def | 23)
+
+ def test_invalid_xor(self):
+ self._test_invalid_op(lambda: self._def ^ 23)
+
+ def test_invalid_invert(self):
+ self._test_invalid_op(lambda: ~self._def)
+
+ def test_str_op(self):
+ self.assertEqual(str(round(self._def, 5)), str(self._def_value))
+
+
+_inject_numeric_testing_methods(SingleRealFieldTestCase)
+
+
+class DoubleRealFieldTestCase(_TestNumericField, unittest.TestCase):
+ @staticmethod
+ def _const_value_setter(field):
+ field.value = 52.7
+
+ def _create_fc(self, tc):
+ return tc.create_double_precision_real_field_class()
+
+ def setUp(self):
+ 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_double_precision_real_field_class(),
+ self._const_value_setter,
+ )