xref: /aosp_15_r20/external/javassist/src/test/test/javassist/bytecode/analysis/DomTreeTest.java (revision f1fbf3c2ab775ce834e0af96b7a85bdc7a0eac65)
1 package test.javassist.bytecode.analysis;
2 
3 import javassist.ClassPool;
4 import javassist.bytecode.analysis.ControlFlow;
5 import javassist.bytecode.analysis.ControlFlow.Block;
6 import javassist.bytecode.analysis.ControlFlow.Node;
7 import junit.framework.TestCase;
8 
9 public class DomTreeTest extends TestCase {
10     private ClassPool pool = ClassPool.getDefault();
11 
testDomtree()12     public void testDomtree() throws Exception {
13         ControlFlow cf = new ControlFlow(pool.get(DomTreeTest.class.getName()).getDeclaredMethod("test1"));
14         Block[] blocks = cf.basicBlocks();
15         // for (int i = 0; i < blocks.length; i++)
16         //    System.out.println(i + ": " + blocks[i]);
17         testBlock(blocks[0], new int[] {}, new int[] { 11, 6 } );
18         testBlock(blocks[1], new int[] { 0 }, new int[] { 17, 11 } );
19         testBlock(blocks[2], new int[] { 0, 6 }, new int[] { 19, 17 });
20         testBlock(blocks[3], new int[] { 6, 11 }, new int[] { 19 });
21         testBlock(blocks[4], new int[] { 11, 17 }, new int[] {});
22 
23         Node[] dom = cf.dominatorTree();
24         assertNull(dom[0].parent());
25         assertEquals(0, dom[1].parent().block().position());
26         assertEquals(0, dom[2].parent().block().position());
27         assertEquals(0, dom[3].parent().block().position());
28         assertEquals(0, dom[4].parent().block().position());
29 
30         Node[] pdom = cf.postDominatorTree();
31         assertEquals(19, pdom[0].parent().block().position());
32         assertEquals(19, pdom[1].parent().block().position());
33         assertEquals(19, pdom[2].parent().block().position());
34         assertEquals(19, pdom[3].parent().block().position());
35         assertNull(pdom[4].parent());
36     }
37 
testBlock(Block b, int[] incoming, int[] outgoing)38     private void testBlock(Block b, int[] incoming, int[] outgoing) {
39         assertEquals(incoming.length, b.incomings());
40         int i = 0;
41         for (int index: incoming)
42             assertEquals(index, b.incoming(i++).position());
43         i = 0;
44         assertEquals(outgoing.length, b.exits());
45         for (int index: outgoing)
46             assertEquals(index, b.exit(i++).position());
47     }
48 
testNode(Node n, int[] incoming, int[] outgoing)49     private void testNode(Node n, int[] incoming, int[] outgoing) {
50         int i = 0;
51         for (int index: incoming)
52             assertEquals(index, n.parent().block().index());
53     }
54 
test1()55     public void test1(){
56         int k=0;
57         if (k != 0 && k!=2 || k < 7) {
58             k = 3 ;
59         }
60     }
61 
testDomtree2()62     public void testDomtree2() throws Exception {
63         ControlFlow cf = new ControlFlow(pool.get(DomTreeTest.class.getName()).getDeclaredMethod("test2"));
64         Block[] blocks = cf.basicBlocks();
65         // for (int i = 0; i < blocks.length; i++)
66         //    System.out.println(i + ": " + blocks[i]);
67         testBlock(blocks[0], new int[] { 7 }, new int[] { 14, 7 } );
68         testBlock(blocks[1], new int[] { 0 }, new int[] { 0, 12 } );
69         testBlock(blocks[2], new int[] { 7 }, new int[] {});
70         testBlock(blocks[3], new int[] { 0 }, new int[] {});
71 
72         Node[] dom = cf.dominatorTree();
73         assertNull(dom[0].parent());
74         assertEquals(0, dom[1].parent().block().position());
75         assertEquals(7, dom[2].parent().block().position());
76         assertEquals(0, dom[3].parent().block().position());
77 
78         Node[] pdom = cf.postDominatorTree();
79         assertNull(pdom[0].parent());
80         assertNull(pdom[1].parent());
81         assertNull(pdom[2].parent());
82         assertNull(pdom[3].parent());
83     }
84 
test2(int i)85     public int test2(int i){
86         while (i-- > 0)
87             if (i == 3)
88                 return 1;
89 
90         return i + 3;
91     }
92 
testDomtree3()93     public void testDomtree3() throws Exception {
94         ControlFlow cf = new ControlFlow(pool.get(DomTreeTest.class.getName()).getDeclaredMethod("test3"));
95         Block[] blocks = cf.basicBlocks();
96         for (int i = 0; i < blocks.length; i++)
97             System.out.println(blocks[i]);
98     }
99 
test3(int i, int j)100     public int test3(int i, int j) {
101         while (i > 0) {
102             try {
103                 j++;
104             }
105             catch (Throwable t) {
106                 j = 0;
107             }
108             i--;
109         }
110 
111         return j;
112     }
113 }
114