Debug tests

This commit is contained in:
Loic GUEGAN 2018-09-03 07:27:15 +02:00
parent 041e5ba04d
commit f947f82892
3 changed files with 32 additions and 29 deletions

View file

@ -28,17 +28,9 @@ class Caretaker:
def __setitem__(self,key,value):
if key!="RAM":
if ((-(2**7-1))>value or value>(2**8)-1) and (key=="MBR" or key=="MBRU"):
raise RuntimeError("Value {} cannot fit in MBR register (-2^7 minimum value)".format(value))
elif value>(2**32-1) and (key!="MBR" and key!="MBRU"):
raise RuntimeError("Value {} cannot fit in MBR register (2^32 minimum value)".format(value))
# if key!="RAM":
# if (value > (2**32) or value <(2**32)/2) and key!="MBR" and key!="MBRU": # Check value fit in word
# print("Warning word overflow: value {} on register {}".format(value,key))
# value=value%(2**32) # Force to fit in word
# elif (value > (2**8) or value <126) and key=="MBR" or key=="MBRU": # Check value fit in byte
# print("Warning byte overflow: value {} on register {}".format(value,key))
# value=abs(value)%256 # Force to fit in byte
raise RuntimeError("Value {} cannot fit in {} register (-(2^7-1) minimum value and (2^7-1) maximum value)".format(value,key))
elif (-(2**31-1)>value or value>(2**32)-1) and (key!="MBR" and key!="MBRU"):
raise RuntimeError("Value {} cannot fit in {} register (2^32-1 maximum value)".format(value,key))
self.objects[key]=value
def items(self):

View file

@ -30,6 +30,16 @@ class CaretakerTest(unittest.TestCase):
with self.assertRaises(KeyError): # Check it returns a KeyError
self.c["kjhkjhkoih"+str(randint(0,7698))]
# Check outside bound values
with self.assertRaises(Exception):
self.c["MBR"]=-128
with self.assertRaises(Exception):
self.c["MBR"]=256
with self.assertRaises(Exception):
self.c["PC"]=2**32
with self.assertRaises(Exception):
self.c["PC"]=-(2**31)
def test___setitem__(self):
"""
Test if getitem operation follow Mic-1 rules

View file

@ -72,13 +72,14 @@ class RamTest(unittest.TestCase):
self.caretaker["MAR"]=self.ramSize
ram.read()
with self.assertRaises(Exception):
self.caretaker["MAR"]=-1*randint(0,self.ramSize-1)
self.caretaker["MAR"]=-1*randint(1,self.ramSize-1)
ram.read()
def test_fetch(self):
"""
Test fetch method
"""
for q in range(0,1999):
# Test classical fetch
ram=Ram(self.caretaker,self.ramSize)
data=dict()
@ -94,7 +95,7 @@ class RamTest(unittest.TestCase):
self.caretaker["PC"]=self.ramSize
ram.fetch()
with self.assertRaises(Exception):
self.caretaker["PC"]=-1*randint(0,self.ramSize-1)
self.caretaker["PC"]=-1*randint(1,self.ramSize-1)
ram.fetch()
if __name__ == "__main__":