Skip to content
Snippets Groups Projects
Commit a61ebd64 authored by Martin Högstedt's avatar Martin Högstedt
Browse files

Merge branch '57-restrict-input-values-into-memory' into 'main'

The integer memory table will not allow the user to inpu values larger than it can store

Closes #57

See merge request !37
parents e5a0b932 f8af7c03
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 #133078 failed
......@@ -17,6 +17,17 @@ class Base(Enum):
HEXADECIMAL = 4
class ValueTooBig(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,8 @@ class IntegerMemoryTable(MemoryTable):
item = self.item(row, col)
value = item.text()
max_value = 2**self._bit_length
min_value = -(2 ** (self._bit_length - 1))
# Turn every value into a positive int
# in base 10
......@@ -203,6 +216,10 @@ class IntegerMemoryTable(MemoryTable):
self._base == Base.DECIMAL_SIGNED or self._base == Base.DECIMAL_UNSIGNED
):
value = int(value)
if value > max_value or value < min_value:
raise ValueTooBig
except ValueError:
msg = None
if self._base == Base.BINARY:
......@@ -217,12 +234,18 @@ class IntegerMemoryTable(MemoryTable):
self.update()
return
except ValueTooBig:
self._errorMessageWidget.showMessage(
f"Input value does not fit within the bit length of {self._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.
Finish editing this message first!
Please register or to comment