Skip to content

Commit

Permalink
Increase maximum size of scalar types to 64 bit
Browse files Browse the repository at this point in the history
Ref. #205, #238
  • Loading branch information
treiher committed Jun 22, 2020
1 parent 9127ca7 commit 49c800e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
14 changes: 8 additions & 6 deletions rflx/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def __init__(self, identifier: StrID, modulus: Expr) -> None:

modulus_int = int(modulus_num)

if modulus_int > 2 ** 57: # ISSUE: Componolit/RecordFlux#238
raise ModelError(f'modulus of "{self.name}" exceeds limit (2**57)')
if modulus_int > 2 ** 64:
raise ModelError(f'modulus of "{self.name}" exceeds limit (2**64)')
if modulus_int == 0 or (modulus_int & (modulus_int - 1)) != 0:
raise ModelError(f'modulus of "{self.name}" not power of two')

Expand Down Expand Up @@ -150,6 +150,8 @@ def __init__(self, identifier: StrID, first: Expr, last: Expr, size: Expr) -> No

if not isinstance(last_num, Number):
raise ModelError(f'last of "{self.name}" contains variable')
if int(last_num) >= 2 ** 63:
raise ModelError(f'last of "{self.name}" exceeds limit (2**63 - 1)')
if first_num < Number(0):
raise ModelError(f'first of "{self.name}" negative')
if first_num > last_num:
Expand All @@ -161,8 +163,8 @@ def __init__(self, identifier: StrID, first: Expr, last: Expr, size: Expr) -> No
raise ModelError(f'size of "{self.name}" contains variable')
if int(last_num).bit_length() > int(size_num):
raise ModelError(f'size for "{self.name}" too small')
if int(size_num) > 57: # ISSUE: Componolit/RecordFlux#238
raise ModelError(f'size of "{self.name}" exceeds limit (2**57)')
if int(size_num) > 64:
raise ModelError(f'size of "{self.name}" exceeds limit (2**64)')

self.__first = first
self.__last = last
Expand Down Expand Up @@ -211,8 +213,8 @@ def __init__(
raise ModelError(f'size of "{self.name}" contains variable')
if max(map(int, literals.values())).bit_length() > int(size_num):
raise ModelError(f'size for "{self.name}" too small')
if int(size_num) > 57: # ISSUE: Componolit/RecordFlux#238
raise ModelError(f'size of "{self.name}" exceeds limit (2**57)')
if int(size_num) > 64:
raise ModelError(f'size of "{self.name}" exceeds limit (2**64)')
if len(set(literals.values())) < len(literals.values()):
raise ModelError(f'"{self.name}" contains elements with same value')
for l in literals:
Expand Down
14 changes: 8 additions & 6 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def test_modular_invalid_modulus_variable() -> None:


def test_modular_invalid_modulus_limit() -> None:
# ISSUE: Componolit/RecordFlux#238
with pytest.raises(ModelError, match=r'^modulus of "T" exceeds limit \(2\*\*57\)$'):
with pytest.raises(ModelError, match=r'^modulus of "T" exceeds limit \(2\*\*64\)$'):
ModularInteger("P.T", Pow(Number(2), Number(128)))


Expand All @@ -196,6 +195,11 @@ def test_range_invalid_last_variable() -> None:
RangeInteger("P.T", Number(1), Add(Number(1), Variable("X")), Number(4))


def test_range_invalid_last_exceeds_limit() -> None:
with pytest.raises(ModelError, match=r'^last of "T" exceeds limit \(2\*\*63 - 1\)$'):
RangeInteger("P.T", Number(1), Pow(Number(2), Number(63)), Number(64))


def test_range_invalid_first_negative() -> None:
with pytest.raises(ModelError, match=r'^first of "T" negative$'):
RangeInteger("P.T", Number(-1), Number(0), Number(1))
Expand All @@ -217,8 +221,7 @@ def test_range_invalid_size_too_small() -> None:


def test_range_invalid_size_exceeds_limit() -> None:
# ISSUE: Componolit/RecordFlux#238
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*57\)$'):
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*64\)$'):
RangeInteger("P.T", Number(0), Number(256), Number(128))


Expand All @@ -233,8 +236,7 @@ def test_enumeration_invalid_size_too_small() -> None:


def test_enumeration_invalid_size_exceeds_limit() -> None:
# ISSUE: Componolit/RecordFlux#238
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*57\)$'):
with pytest.raises(ModelError, match=r'^size of "T" exceeds limit \(2\*\*64\)$'):
Enumeration("P.T", {"A": Number(256)}, Number(128), False)


Expand Down
3 changes: 1 addition & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,13 @@ def test_illegal_redefinition() -> None:


def test_invalid_modular_type() -> None:
# ISSUE: Componolit/RecordFlux#238
assert_parse_exception_string(
"""
package Test is
type T is mod 2**128;
end Test;
""",
r'^modulus of "T" exceeds limit \(2\*\*57\)',
r'^modulus of "T" exceeds limit \(2\*\*64\)',
)


Expand Down

0 comments on commit 49c800e

Please sign in to comment.