Skip to content
Snippets Groups Projects
Commit 64844704 authored by Martin's avatar Martin
Browse files

fixed the bug

parent 659d0545
No related branches found
No related tags found
1 merge request!37The integer memory table will not allow the user to inpu values larger than it can store
Pipeline #133055 failed
......@@ -17,6 +17,17 @@ class Base(Enum):
HEXADECIMAL = 4
class ValueToBig(Exception):
"""
A class representing the error of a user inputting a value that does not fit in the memory.
Used to differentiate between wrong input using the exception python raises by default (ValueError)
and the error of a value that does not fit.
"""
pass
class IntegerMemoryWindow(MemoryWindow):
"""
A class showing the contents of a memory module in a new window.
......@@ -187,6 +198,7 @@ class IntegerMemoryTable(MemoryTable):
item = self.item(row, col)
value = item.text()
max_value = 2**self._bit_length
# Turn every value into a positive int
# in base 10
......@@ -203,6 +215,10 @@ class IntegerMemoryTable(MemoryTable):
self._base == Base.DECIMAL_SIGNED or self._base == Base.DECIMAL_UNSIGNED
):
value = int(value)
if value > max_value:
raise ValueToBig
except ValueError:
msg = None
if self._base == Base.BINARY:
......@@ -217,12 +233,18 @@ class IntegerMemoryTable(MemoryTable):
self.update()
return
except ValueToBig:
self._errorMessageWidget.showMessage(
"Input value does not fit within the bit length."
)
self.update()
return
# Turn negative signed numbers into their positive unsigned
# counter part
# Ex 4 bits:
# -7 = 1001
# -7 + 16 % 16 = 9 = 1001 = -7
max_value = 2**self._bit_length
value = (value + max_value) % max_value
index = row * 4 + col
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment