test_dataflow.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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
  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(
  17. map(lambda b: b.statements, find_basic_blocks(s)[:-1]),
  18. [B(s[:2]).statements, B(s[2:4]).statements,
  19. B(s[4:]).statements]
  20. )
  21. def test_generate_flow_graph_simple(self):
  22. b1 = B([S('command', 'foo'), S('command', 'j', 'b2')])
  23. b2 = B([S('label', 'b2'), S('command', 'bar')])
  24. generate_flow_graph([b1, b2])
  25. self.assertEqual(b1.edges_to, [b2])
  26. self.assertEqual(b2.edges_from, [b1])
  27. def test_generate_flow_graph_branch(self):
  28. b1 = B([S('command', 'foo'), S('command', 'beq', '$1', '$2', 'b3')])
  29. b2 = B([S('command', 'bar')])
  30. b3 = B([S('label', 'b3'), S('command', 'baz')])
  31. generate_flow_graph([b1, b2, b3])
  32. self.assertIn(b2, b1.edges_to)
  33. self.assertIn(b3, b1.edges_to)
  34. self.assertEqual(b2.edges_from, [b1])
  35. self.assertEqual(b2.edges_to, [b3])
  36. self.assertIn(b1, b3.edges_from)
  37. self.assertIn(b2, b3.edges_from)