Debug tests
This commit is contained in:
parent
041e5ba04d
commit
f947f82892
3 changed files with 32 additions and 29 deletions
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -72,30 +72,31 @@ 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
|
||||
"""
|
||||
# Test classical fetch
|
||||
ram=Ram(self.caretaker,self.ramSize)
|
||||
data=dict()
|
||||
toWrite=randint(0,256-1)
|
||||
for i in range(0,self.ramSize):
|
||||
data[i]=toWrite
|
||||
ram.setData(data)
|
||||
for i in range(0,self.ramSize):
|
||||
self.caretaker["PC"]=i
|
||||
self.assertEqual(toWrite,ram.fetch())
|
||||
# Test fetch outside of memory
|
||||
with self.assertRaises(Exception):
|
||||
self.caretaker["PC"]=self.ramSize
|
||||
ram.fetch()
|
||||
with self.assertRaises(Exception):
|
||||
self.caretaker["PC"]=-1*randint(0,self.ramSize-1)
|
||||
ram.fetch()
|
||||
for q in range(0,1999):
|
||||
# Test classical fetch
|
||||
ram=Ram(self.caretaker,self.ramSize)
|
||||
data=dict()
|
||||
toWrite=randint(0,256-1)
|
||||
for i in range(0,self.ramSize):
|
||||
data[i]=toWrite
|
||||
ram.setData(data)
|
||||
for i in range(0,self.ramSize):
|
||||
self.caretaker["PC"]=i
|
||||
self.assertEqual(toWrite,ram.fetch())
|
||||
# Test fetch outside of memory
|
||||
with self.assertRaises(Exception):
|
||||
self.caretaker["PC"]=self.ramSize
|
||||
ram.fetch()
|
||||
with self.assertRaises(Exception):
|
||||
self.caretaker["PC"]=-1*randint(1,self.ramSize-1)
|
||||
ram.fetch()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Reference in a new issue