Skip to content

Commit

Permalink
Finished Memory tests.
Browse files Browse the repository at this point in the history
Added comment to memory class.
Added convenient method to memory class.
  • Loading branch information
lalo73 committed Jan 5, 2013
1 parent 2d70dbc commit 6e92d0b
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 33 deletions.
124 changes: 101 additions & 23 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 40 additions & 5 deletions hardware/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,53 @@
class Memory:
def __init__(self,length=1024):
self.cells = []
for i in length:
for i in range(length):
self.cells.append(Cell())

def length(self):
def __len__(self):
return len(self.cells)

def length(self):
#Return the size of the memory or number of cells
return len(self)

def cellAt(self,index):
#Returns the cell at position 'index'
try:
return self.cells[index]
except IndexError:
raise OutOfRange()

def write(self,index,data):
cell = self.cells[index]
#Writes the 'data' in cell at position 'index'
cell = self.cellAt(index)
cell.write(data)

def onUse(self,index):
#Sets on use the cell at position 'index'
cell = self.cellAt(index)
cell.onUse()

def release(self,index):
#See 'free' method
self.free(index)

def free(self,index):
#Release the cell at position 'index'
cell = self.cellAt(index)
cell.free()

def read(self,index):
cell = self.cells[index]
#Returns the data of the cell at positioin 'index'
cell = self.cellAt(index)
return cell.read()

def isInUse(self,index):
cell = self.cells[index]
"""
Returns True if the cell at position 'index' has been setted as 'onUse',
otherwise returns False
"""
cell = self.cellAt(index)
return cell.isInUse()

class Cell:
Expand All @@ -42,3 +72,8 @@ def onUse(self):
def free(self):
self.__inUse=False

#Exceptions

class OutOfRange(Exception):
def __str__(self):
return "there isn't a cell on this 'index'"
4 changes: 2 additions & 2 deletions test/cellTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mockito import *
from hardware.memory import Cell

class TestCell(unittest.TestCase):
class CellTest(unittest.TestCase):

def setUp(self):
self.cell = Cell()
Expand Down Expand Up @@ -40,5 +40,5 @@ def test_WhenSetFreeThenIsInUseReturnsFalse(self):

self.assertFalse(self.cell.isInUse())

suite = unittest.TestLoader().loadTestsFromTestCase(TestCell)
suite = unittest.TestLoader().loadTestsFromTestCase(CellTest)
unittest.TextTestRunner(verbosity=2).run(suite)
Loading

0 comments on commit 6e92d0b

Please sign in to comment.