Skip to content

Commit

Permalink
Support hex constants in msg files (ros2#559)
Browse files Browse the repository at this point in the history
Signed-off-by: Dereck Wonnacott <dereck@gmail.com>
  • Loading branch information
dawonn committed Feb 9, 2021
1 parent 6bae09b commit c175a0b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
11 changes: 9 additions & 2 deletions rosidl_adapter/rosidl_adapter/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,11 @@ def parse_primitive_value_string(type_, value_string):
try:
value = int(value_string)
except ValueError:
raise ex
try:
value = int(value_string, 0)
except ValueError:
raise ex

if value < 0 or value > 255:
raise ex
return value
Expand Down Expand Up @@ -740,7 +744,10 @@ def parse_primitive_value_string(type_, value_string):
try:
value = int(value_string)
except ValueError:
raise ex
try:
value = int(value_string, 0)
except ValueError:
raise ex

# check that value is in valid range
if value < lower_bound or value > upper_bound:
Expand Down
114 changes: 114 additions & 0 deletions rosidl_adapter/test/test_parse_primitive_value_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,120 @@ def test_parse_primitive_value_string_integer():
Type(integer_type), str(upper_bound + 1))


def test_parse_primitive_value_string_hex():
integer_types = {
'byte': [8, True],
'char': [8, True],
'int8': [8, False],
'uint8': [8, True],
'int16': [16, False],
'uint16': [16, True],
'int32': [32, False],
'uint32': [32, True],
'int64': [64, False],
'uint64': [64, True],
}
for integer_type, (bits, is_unsigned) in integer_types.items():
lower_bound = 0 if is_unsigned else -(2 ** (bits - 1))
upper_bound = (2 ** (bits if is_unsigned else (bits - 1))) - 1

value = parse_primitive_value_string(
Type(integer_type), hex(lower_bound))
assert value == lower_bound
value = parse_primitive_value_string(
Type(integer_type), hex(0))
assert value == 0
value = parse_primitive_value_string(
Type(integer_type), hex(upper_bound))
assert value == upper_bound

with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), 'value')
with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), hex(lower_bound - 1))
with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), hex(upper_bound + 1))


def test_parse_primitive_value_string_oct():
integer_types = {
'byte': [8, True],
'char': [8, True],
'int8': [8, False],
'uint8': [8, True],
'int16': [16, False],
'uint16': [16, True],
'int32': [32, False],
'uint32': [32, True],
'int64': [64, False],
'uint64': [64, True],
}
for integer_type, (bits, is_unsigned) in integer_types.items():
lower_bound = 0 if is_unsigned else -(2 ** (bits - 1))
upper_bound = (2 ** (bits if is_unsigned else (bits - 1))) - 1

value = parse_primitive_value_string(
Type(integer_type), oct(lower_bound))
assert value == lower_bound
value = parse_primitive_value_string(
Type(integer_type), oct(0))
assert value == 0
value = parse_primitive_value_string(
Type(integer_type), oct(upper_bound))
assert value == upper_bound

with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), 'value')
with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), oct(lower_bound - 1))
with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), oct(upper_bound + 1))


def test_parse_primitive_value_string_bin():
integer_types = {
'byte': [8, True],
'char': [8, True],
'int8': [8, False],
'uint8': [8, True],
'int16': [16, False],
'uint16': [16, True],
'int32': [32, False],
'uint32': [32, True],
'int64': [64, False],
'uint64': [64, True],
}
for integer_type, (bits, is_unsigned) in integer_types.items():
lower_bound = 0 if is_unsigned else -(2 ** (bits - 1))
upper_bound = (2 ** (bits if is_unsigned else (bits - 1))) - 1

value = parse_primitive_value_string(
Type(integer_type), bin(lower_bound))
assert value == lower_bound
value = parse_primitive_value_string(
Type(integer_type), bin(0))
assert value == 0
value = parse_primitive_value_string(
Type(integer_type), bin(upper_bound))
assert value == upper_bound

with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), 'value')
with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), bin(lower_bound - 1))
with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type(integer_type), bin(upper_bound + 1))


def test_parse_primitive_value_string_float():
for float_type in ['float32', 'float64']:
value = parse_primitive_value_string(
Expand Down

0 comments on commit c175a0b

Please sign in to comment.