test_dataflow.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import unittest
  2. from src.statement import Statement as S
  3. from src.dataflow import BasicBlock as B, find_leaders, find_basic_blocks, \
  4. generate_flow_graph, Dag
  5. class TestDataflow(unittest.TestCase):
  6. def setUp(self):
  7. add = S('command', 'add', '$1', '$2', '$3')
  8. self.statements = [add, S('command', 'j', 'foo'), add, add, \
  9. S('label', 'foo')]
  10. def tearDown(self):
  11. del self.statements
  12. def test_find_leaders(self):
  13. self.assertEqual(find_leaders(self.statements), [0, 2, 4])
  14. def test_find_basic_blocks(self):
  15. s = self.statements
  16. self.assertEqual(map(lambda b: b.statements, find_basic_blocks(s)), \
  17. [B(s[:2]).statements, B(s[2:4]).statements, \
  18. B(s[4:]).statements])
  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)
  36. def test_dag(self):
  37. pass