Mercurial > lcfOS
annotate test/testgraph.py @ 355:c2ddc8a36f5e
Enabled optimization
author | Windel Bouwman |
---|---|
date | Fri, 14 Mar 2014 10:30:13 +0100 |
parents | 86b02c98a717 |
children |
rev | line source |
---|---|
277 | 1 #!/usr/bin/python |
2 | |
3 import unittest | |
323 | 4 from ppci.codegen.graph import Graph, Node, DiGraph, DiNode |
301 | 5 from ppci.codegen.interferencegraph import InterferenceGraph |
6 from ppci.codegen.flowgraph import FlowGraph | |
7 from ppci import ir | |
8 from ppci.irmach import AbstractInstruction as AI | |
342 | 9 from ppci.target import Nop |
279 | 10 |
277 | 11 |
12 class GraphTestCase(unittest.TestCase): | |
13 def testEdge(self): | |
296 | 14 g = Graph() |
15 n1 = Node(g) | |
337 | 16 g.add_node(n1) |
296 | 17 n2 = Node(g) |
337 | 18 g.add_node(n2) |
277 | 19 g.addEdge(n1, n2) |
20 self.assertTrue(g.hasEdge(n2, n1)) | |
21 self.assertTrue(g.hasEdge(n1, n2)) | |
22 g.delNode(n1) | |
23 g.delNode(n2) | |
24 | |
25 def testDegree(self): | |
296 | 26 g = Graph() |
27 n1 = Node(g) | |
337 | 28 g.add_node(n1) |
296 | 29 n2 = Node(g) |
337 | 30 g.add_node(n2) |
296 | 31 n3 = Node(g) |
337 | 32 g.add_node(n3) |
277 | 33 g.addEdge(n1, n2) |
34 g.addEdge(n1, n3) | |
35 self.assertEqual(2, n1.Degree) | |
36 self.assertEqual(1, n2.Degree) | |
37 g.delNode(n2) | |
38 self.assertEqual(1, n1.Degree) | |
39 | |
279 | 40 |
277 | 41 class DigraphTestCase(unittest.TestCase): |
323 | 42 def testSuccessor(self): |
43 g = DiGraph() | |
44 a = DiNode(g) | |
45 b = DiNode(g) | |
46 c = DiNode(g) | |
337 | 47 g.add_node(a) |
48 g.add_node(b) | |
49 g.add_node(c) | |
323 | 50 g.addEdge(a, b) |
51 g.addEdge(b, c) | |
52 self.assertEqual({b}, a.Succ) | |
53 self.assertEqual({b}, c.Pred) | |
54 g.delNode(c) | |
55 self.assertEqual(set(), b.Succ) | |
290 | 56 |
277 | 57 |
279 | 58 class InterferenceGraphTestCase(unittest.TestCase): |
59 def testNormalUse(self): | |
60 t1 = ir.Temp('t1') | |
61 t2 = ir.Temp('t2') | |
62 t3 = ir.Temp('t3') | |
63 t4 = ir.Temp('t4') | |
64 t5 = ir.Temp('t5') | |
65 t6 = ir.Temp('t6') | |
66 instrs = [] | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
67 instrs.append(AI(Nop, dst=[t1])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
68 instrs.append(AI(Nop, dst=[t2])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
69 instrs.append(AI(Nop, dst=[t3])) |
296 | 70 cfg = FlowGraph(instrs) |
71 ig = InterferenceGraph(cfg) | |
279 | 72 |
73 def testCombine(self): | |
74 t1 = ir.Temp('t1') | |
75 t2 = ir.Temp('t2') | |
76 t3 = ir.Temp('t3') | |
77 t4 = ir.Temp('t4') | |
78 instrs = [] | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
79 instrs.append(AI(Nop, dst=[t1])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
80 instrs.append(AI(Nop, dst=[t2])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
81 instrs.append(AI(Nop, dst=[t3])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
82 instrs.append(AI(Nop, dst=[t4], src=[t3])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
83 instrs.append(AI(Nop, src=[t4])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
84 instrs.append(AI(Nop, src=[t1])) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
279
diff
changeset
|
85 instrs.append(AI(Nop, src=[t2])) |
296 | 86 cfg = FlowGraph(instrs) |
87 ig = InterferenceGraph(cfg) | |
279 | 88 ig.Combine(ig.getNode(t4), ig.getNode(t3)) |
89 self.assertIs(ig.getNode(t4), ig.getNode(t3)) | |
90 | |
91 | |
277 | 92 if __name__ == '__main__': |
93 unittest.main() |