Skip to content

Commit

Permalink
[matter_yamltests] Add single/double constraint type supports
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jun 6, 2023
1 parent 4f6923f commit bc8efc7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ class FloatConverter(BaseConverter):

def maybe_convert(self, typename, value):
if typename == 'single':
value = float('%g' % value)
float_representation = float("%.16f" % value)
value = float('%g' % float_representation)
return value


Expand Down
14 changes: 13 additions & 1 deletion scripts/py_matter_yamltests/matter_yamltests/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ def check_response(self, value, value_type_name) -> bool:
success = value >= -36028797018963967 and value <= 36028797018963967
elif self._type == 'nullable_int64s' and type(value) is int:
success = value >= -9223372036854775807 and value <= 9223372036854775807
elif self._type == 'single' and type(value) is float:
success = (value >= -3.402823466E+38 and value <= -1.175494351E-38) or value == 0.0 or (
value >= 1.175494351E-38 and value <= 3.402823466E+38)
elif self._type == 'double' and type(value) is float:
success = (value >= -1.7976931348623157E+308 and value <= -2.2250738585072014E-308) or value == 0.0 or (
value >= 2.2250738585072014E-308 and value <= 1.7976931348623157E+308)
else:
success = self._type == value_type_name
return success
Expand Down Expand Up @@ -502,14 +508,20 @@ def get_reason(self, value, value_type_name) -> str:
if value >= -9223372036854775807 and value <= 9223372036854775807:
types.append('nullable_int64s')

if (value >= -3.402823466E+38 and value <= -1.175494351E-38) or value == 0.0 or (value >= 1.175494351E-38 and value <= 3.402823466E+38):
types.append('single')

if (value >= -1.7976931348623157E+308 and value <= -2.2250738585072014E-308) or value == 0.0 or (value >= 2.2250738585072014E-308 and value <= 1.7976931348623157E+308):
types.append('double')

types.sort(key=lambda input_type: [int(c) if c.isdigit(
) else c for c in re.split('([0-9]+)', input_type)])

if value_type_name not in types:
types.append(value_type_name)

if len(types) == 1:
reason = f'The response type {types[0]}) does not match the constraint.'
reason = f'The response type ({types[0]}) does not match the constraint.'
else:
reason = f'The response value ({value}) is of one of those types: {types}.'
return reason
Expand Down

0 comments on commit bc8efc7

Please sign in to comment.