Commit 7b1f59f5 authored by Taddeus Kroes's avatar Taddeus Kroes

Tested and debugged utility classes.

parent 60f1e223
......@@ -90,33 +90,35 @@ class Block:
def __len__(self):
return len(self.statements)
def replace(self, start, end, replacement):
"""Replace the given range start-end with the given statement list, and
move the pointer to the first statement after the replacement."""
before = self.statements[:start]
after = self.statements[end:]
self.statements = before + replacement + after
self.pointer = start + len(replacement)
def read(self, count=1):
"""Read the statement at the current pointer position and move the
pointer one position to the right."""
s = statements[self.pointer]
s = self.statements[self.pointer]
self.pointer += 1
return s
def end(self):
"""Check if the pointer is at the end of the statement list."""
return self.pointer == len(self)
def peek(self, count=1):
"""Read the statements until an offset from the current pointer
position."""
i = self.pointer + offset
return self.statements[self.pointer] if count == 1 \
else self.statements[self.pointer:self.pointer + count]
if i < len(self.statements):
return self.statements[self.pointer:i]
def replace(self, count, replacement, start=None):
"""Replace the given range start-(start + count) with the given
statement list, and move the pointer to the first statement after the
replacement."""
if start == None:
start = self.pointer
def end(self):
"""Check if the pointer is at the end of the statement list."""
return self.pointer == len(self.statements) - 1
before = self.statements[:start]
after = self.statements[start + count:]
self.statements = before + replacement + after
self.pointer = start + len(replacement)
def find_leaders(statements):
......
......@@ -7,16 +7,18 @@ from src.utils import Statement as S, Block as B, find_leaders, \
class TestUtils(unittest.TestCase):
def setUp(self):
pass
add = S('command', 'add', '$1', '$2', '$3')
self.statements = [add, S('command', 'j', 'foo'), add, add, \
S('label', 'foo')]
self.block = B([S('command', 'foo'), \
S('command', 'bar'),
S('command', 'baz')])
def test_find_leaders(self):
add = S('command', 'add', '$1', '$2', '$3')
s = [add, S('command', 'j', 'foo'), add, add, S('label', 'foo')]
self.assertEqual(find_leaders(s), [0, 2, 4])
self.assertEqual(find_leaders(self.statements), [0, 2, 4])
def test_find_basic_blocks(self):
add = S('command', 'add', '$1', '$2', '$3')
s = [add, S('command', 'j', 'foo'), add, add, S('label', 'foo')]
s = self.statements
self.assertEqual(map(lambda b: b.statements, find_basic_blocks(s)), \
[B(s[:2]).statements, B(s[2:4]).statements, \
B(s[4:]).statements])
......@@ -43,3 +45,29 @@ class TestUtils(unittest.TestCase):
def test_jump_target(self):
self.assertEqual(S('command', 'j', 'foo').jump_target(), 'foo')
self.assertRaises(Exception, S('command', 'foo').jump_target)
def test_read(self):
self.assertEqual(self.block.read(), S('command', 'foo'))
self.assertEqual(self.block.read(), S('command', 'bar'))
self.assertEqual(self.block.read(), S('command', 'baz'))
def test_end(self):
self.assertFalse(self.block.end())
self.block.read()
self.block.read()
self.block.read()
self.assertTrue(self.block.end())
def test_peek(self):
self.assertEqual(self.block.peek(), S('command', 'foo'))
self.assertEqual(self.block.peek(2), [S('command', 'foo'), \
S('command', 'bar')])
self.block.read()
self.assertEqual(self.block.peek(), S('command', 'bar'))
def test_replace(self):
self.block.replace(1, [S('command', 'foobar')])
self.assertEqual(self.block.pointer, 1)
self.assertEqual(self.block.statements, [S('command', 'foobar'), \
S('command', 'bar'), \
S('command', 'baz')])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment