From f947f82892749e9972ee6cb77eb4c5d16ac59f21 Mon Sep 17 00:00:00 2001 From: Loic GUEGAN Date: Mon, 3 Sep 2018 07:27:15 +0200 Subject: [PATCH] Debug tests --- MicSim/components/caretaker.py | 14 +++---------- MicSim/test/test_caretaker.py | 10 +++++++++ MicSim/test/test_ram.py | 37 +++++++++++++++++----------------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/MicSim/components/caretaker.py b/MicSim/components/caretaker.py index b43b3e3..cd6d913 100644 --- a/MicSim/components/caretaker.py +++ b/MicSim/components/caretaker.py @@ -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): diff --git a/MicSim/test/test_caretaker.py b/MicSim/test/test_caretaker.py index 414bf37..cc8d2ef 100644 --- a/MicSim/test/test_caretaker.py +++ b/MicSim/test/test_caretaker.py @@ -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 diff --git a/MicSim/test/test_ram.py b/MicSim/test/test_ram.py index 6baf158..f755753 100644 --- a/MicSim/test/test_ram.py +++ b/MicSim/test/test_ram.py @@ -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()