Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hex support for msg constants #559

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions rosidl_adapter/rosidl_adapter/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,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 @@ -737,7 +741,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