test_dataflow.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import unittest
  2. from src.statement import Statement as S
  3. from src.program import Program as P
  4. from src.dataflow import BasicBlock as B, find_leaders, find_basic_blocks, \
  5. generate_flow_graph
  6. class TestDataflow(unittest.TestCase):
  7. def setUp(self):
  8. add = S('command', 'add', '$1', '$2', '$3')
  9. self.statements = [add, S('command', 'j', 'foo'), add, add, \
  10. S('label', 'foo')]
  11. def tearDown(self):
  12. del self.statements
  13. def test_find_leaders(self):
  14. self.assertEqual(find_leaders(self.statements), [0, 2, 4])
  15. def test_find_basic_blocks(self):
  16. s = self.statements
  17. statements = map(lambda b: b.statements, find_basic_blocks(s))
  18. self.assertEqual(statements, [s[:2], s[2:4], s[4:]])
  19. def test_generate_flow_graph_simple(self):
  20. b1 = B([S('command', 'foo'), S('command', 'j', 'b2')])
  21. b2 = B([S('label', 'b2'), S('command', 'bar')])
  22. generate_flow_graph([b1, b2])
  23. self.assertEqual(b1.edges_to, [b2])
  24. self.assertEqual(b2.edges_from, [b1])
  25. def test_generate_flow_graph_branch(self):
  26. b1 = B([S('command', 'foo'), S('command', 'beq', '$1', '$2', 'b3')])
  27. b2 = B([S('command', 'bar')])
  28. b3 = B([S('label', 'b3'), S('command', 'baz')])
  29. generate_flow_graph([b1, b2, b3])
  30. self.assertIn(b2, b1.edges_to)
  31. self.assertIn(b3, b1.edges_to)
  32. self.assertEqual(b2.edges_from, [b1])
  33. self.assertEqual(b2.edges_to, [b3])
  34. self.assertIn(b1, b3.edges_from)
  35. self.assertIn(b2, b3.edges_from)