From d0fc22d3e90e951ae6d9b0ec2ed6974a1e07a121 Mon Sep 17 00:00:00 2001 From: Loic GUEGAN Date: Sun, 2 Sep 2018 18:46:31 +0200 Subject: [PATCH] Debug test on ram --- MicSim/components/ram.py | 2 +- MicSim/test/test_ram.py | 38 +++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/MicSim/components/ram.py b/MicSim/components/ram.py index 62edb0c..a0f708e 100644 --- a/MicSim/components/ram.py +++ b/MicSim/components/ram.py @@ -69,7 +69,7 @@ class Ram: try: value=self.data[addr] except: - if addr>self.lastAddr: + if addr>self.lastAddr or addr<0: raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr)) if(value==None): return(0) diff --git a/MicSim/test/test_ram.py b/MicSim/test/test_ram.py index 2a9b932..a60cd4f 100644 --- a/MicSim/test/test_ram.py +++ b/MicSim/test/test_ram.py @@ -11,6 +11,7 @@ class RamTest(unittest.TestCase): Init test """ self.caretaker=dict({"MDR":0,"MAR":0,"MBR":0,"PC":0}) + self.ramSize=1000*4 # Ram size should be a multiple of 4 to guaranty test validity def test_write(self): """ @@ -20,9 +21,9 @@ class RamTest(unittest.TestCase): for i in range(0,32): # Test for n number toWrite=randint(0,2**i) # Pick a random number to write self.caretaker["MDR"]=toWrite - self.caretaker["MAR"]=randint(0,10000-1) + self.caretaker["MAR"]=randint(0,self.ramSize-1) - ram=Ram(self.caretaker,10000) + ram=Ram(self.caretaker,self.ramSize) ram.write() # Write a random number at address 0 data=ram.getData() # Dump ram @@ -47,13 +48,13 @@ class RamTest(unittest.TestCase): """ Test read method """ - ram=Ram(self.caretaker,10000) + ram=Ram(self.caretaker,self.ramSize) data=dict() toWrite=randint(0,256-1) - for i in range(0,10000): # Write in memory + for i in range(0,self.ramSize): # Write in memory data[i]=toWrite # Write the random byte ram.setData(data) - for i in range(0,int(10000/4)): # Read and check if its what we wrote + for i in range(0,int(self.ramSize/4)): # Read and check if its what we wrote self.caretaker["MAR"]=i*4 data=ram.read() # Read start at 0 addr self.assertEqual(toWrite,(data>>24)&0xFF) @@ -62,22 +63,33 @@ class RamTest(unittest.TestCase): self.assertEqual(toWrite,data&0xFF) # Try to read outside of the memory with self.assertRaises(Exception): - self.caretaker["MAR"]=10000 + self.caretaker["MAR"]=self.ramSize ram.read() with self.assertRaises(Exception): - self.caretaker["MAR"]=-10000 + self.caretaker["MAR"]=-1*randint(0,self.ramSize-1) ram.read() def test_fetch(self): """ Test fetch method """ - ram=Ram(self.caretaker,10000) - for i in range(1,10000): - self.caretaker["MDR"]=i - self.caretaker["MAR"]=i - ram.write() - + # 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()