Skip to content
Snippets Groups Projects
Commit 945d7583 authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Created som unit tests for statement class.

parent ed760ad8
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ class Statement: ...@@ -5,7 +5,7 @@ class Statement:
def __init__(self, stype, name, *args, **kwargs): def __init__(self, stype, name, *args, **kwargs):
self.stype = stype self.stype = stype
self.name = name self.name = name
self.args = args self.args = list(args)
self.options = kwargs self.options = kwargs
def __getitem__(self, n): def __getitem__(self, n):
...@@ -52,6 +52,12 @@ class Statement: ...@@ -52,6 +52,12 @@ class Statement:
and re.match('^j|jal|beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \ and re.match('^j|jal|beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
self.name) self.name)
def is_branch(self):
"""Check if the statement is a branch."""
return self.is_command() \
and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
self.name)
def is_shift(self): def is_shift(self):
"""Check if the statement is a shift operation.""" """Check if the statement is a shift operation."""
return self.is_command() and re.match('^s(ll|la|rl|ra)$', self.name) return self.is_command() and re.match('^s(ll|la|rl|ra)$', self.name)
...@@ -81,6 +87,9 @@ class Block: ...@@ -81,6 +87,9 @@ class Block:
def __iter__(self): def __iter__(self):
return iter(self.statements) return iter(self.statements)
def __getitem__(self, n):
return self.statements[n]
def __len__(self): def __len__(self):
return len(self.statements) return len(self.statements)
......
...@@ -6,6 +6,7 @@ from src.statement import Statement as S, Block as B ...@@ -6,6 +6,7 @@ from src.statement import Statement as S, Block as B
class TestStatement(unittest.TestCase): class TestStatement(unittest.TestCase):
def setUp(self): def setUp(self):
self.statement = S('command', 'foo', '$1')
self.block = B([S('command', 'foo'), \ self.block = B([S('command', 'foo'), \
S('comment', 'bar'), S('comment', 'bar'),
S('command', 'baz')]) S('command', 'baz')])
...@@ -13,6 +14,13 @@ class TestStatement(unittest.TestCase): ...@@ -13,6 +14,13 @@ class TestStatement(unittest.TestCase):
def tearDown(self): def tearDown(self):
del self.block del self.block
def test_getitem(self):
self.assertEqual(self.statement[0], '$1')
def test_setitem(self):
self.statement[0] = '$2'
self.assertEqual(self.statement[0], '$2')
def test_eq(self): def test_eq(self):
self.assertTrue(S('command', 'foo') == S('command', 'foo')) self.assertTrue(S('command', 'foo') == S('command', 'foo'))
self.assertFalse(S('command', 'foo') == S('command', 'bar')) self.assertFalse(S('command', 'foo') == S('command', 'bar'))
...@@ -69,3 +77,18 @@ class TestStatement(unittest.TestCase): ...@@ -69,3 +77,18 @@ class TestStatement(unittest.TestCase):
self.block.apply_filter(lambda s: s.is_command()) self.block.apply_filter(lambda s: s.is_command())
self.assertEqual(self.block.statements, [S('command', 'foo'), \ self.assertEqual(self.block.statements, [S('command', 'foo'), \
S('command', 'baz')]) S('command', 'baz')])
def test_is_shift(self):
self.assertTrue(S('command', 'sll').is_shift())
self.assertFalse(S('command', 'foo').is_shift())
self.assertFalse(S('label', 'sll').is_shift())
def test_is_load(self):
self.assertTrue(S('command', 'lw').is_load())
self.assertFalse(S('command', 'foo').is_load())
self.assertFalse(S('label', 'lw').is_load())
def test_is_arith(self):
self.assertTrue(S('command', 'add', '$1', '$2', '$3').is_arith())
self.assertFalse(S('command', 'foo').is_arith())
self.assertFalse(S('label', 'add').is_arith())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment