OSDN Git Service

532764ca7d3bfa7534b47fa40993bc9faf57fa87
[stigmata/stigmata-plugins.git] / cflib / src / test / java / jp / sourceforge / stigmata / birthmarks / ControlFlowGraphTest.java
1 package jp.sourceforge.stigmata.birthmarks;\r
2 \r
3 import java.util.Iterator;\r
4 \r
5 import junit.framework.Assert;\r
6 \r
7 import org.junit.Before;\r
8 import org.junit.Test;\r
9 import org.objectweb.asm.ClassReader;\r
10 import org.objectweb.asm.ClassWriter;\r
11 \r
12 public class ControlFlowGraphTest {\r
13     private ControlFlowGraphExtractVisitor cfVisitor;\r
14     private ControlFlowGraphExtractVisitor cfVisitor2;\r
15 \r
16     @Before\r
17     public void setUp() throws Exception{\r
18         ClassReader reader1 = new ClassReader(getClass().getResource("/resources/MyServer.class").openStream());\r
19         cfVisitor = new ControlFlowGraphExtractVisitor(new ClassWriter(0));\r
20         reader1.accept(cfVisitor, 0);\r
21 \r
22         ClassReader reader2 = new ClassReader(getClass().getResource("/resources/MyServer2.class").openStream());\r
23         cfVisitor2 = new ControlFlowGraphExtractVisitor(new ClassWriter(0));\r
24         reader2.accept(cfVisitor2, 0);\r
25     }\r
26 \r
27     @Test\r
28     public void testBasic() throws Exception{\r
29         Iterator<String> iterator = cfVisitor.getMethodNames();\r
30 \r
31         Assert.assertTrue(iterator.hasNext());\r
32         Assert.assertEquals("<init>(I)V", iterator.next());\r
33         Assert.assertFalse(iterator.hasNext());\r
34 \r
35         ControlFlowGraph graph = cfVisitor.getGraph("<init>(I)V");\r
36         Assert.assertEquals(6, graph.getBasicBlockSize());\r
37         graph.setIncludingExceptionFlow(true);\r
38 \r
39         Assert.assertEquals(6, graph.getBasicBlockSize());\r
40     }\r
41 \r
42     @Test\r
43     public void testGraph() throws Exception{\r
44         int[][] graph = cfVisitor.getGraph("<init>(I)V").getGraphMatrix();\r
45 \r
46         Assert.assertEquals(6, graph.length);\r
47 \r
48         Assert.assertEquals(0, graph[0][0]);\r
49         Assert.assertEquals(1, graph[0][1]);\r
50         Assert.assertEquals(1, graph[0][2]);\r
51         Assert.assertEquals(0, graph[0][3]);\r
52         Assert.assertEquals(0, graph[0][4]);\r
53         Assert.assertEquals(0, graph[0][5]);\r
54 \r
55         Assert.assertEquals(0, graph[1][0]);\r
56         Assert.assertEquals(0, graph[1][1]);\r
57         Assert.assertEquals(0, graph[1][2]);\r
58         Assert.assertEquals(1, graph[1][3]);\r
59         Assert.assertEquals(0, graph[1][4]);\r
60         Assert.assertEquals(0, graph[1][5]);\r
61 \r
62         Assert.assertEquals(0, graph[2][0]);\r
63         Assert.assertEquals(0, graph[2][1]);\r
64         Assert.assertEquals(0, graph[2][2]);\r
65         Assert.assertEquals(1, graph[2][3]);\r
66         Assert.assertEquals(0, graph[2][4]);\r
67         Assert.assertEquals(0, graph[2][5]);\r
68 \r
69         Assert.assertEquals(0, graph[3][0]);\r
70         Assert.assertEquals(0, graph[3][1]);\r
71         Assert.assertEquals(0, graph[3][2]);\r
72         Assert.assertEquals(0, graph[3][3]);\r
73         Assert.assertEquals(0, graph[3][4]);\r
74         Assert.assertEquals(1, graph[3][5]);\r
75 \r
76         Assert.assertEquals(0, graph[4][0]);\r
77         Assert.assertEquals(0, graph[4][1]);\r
78         Assert.assertEquals(0, graph[4][2]);\r
79         Assert.assertEquals(0, graph[4][3]);\r
80         Assert.assertEquals(0, graph[4][4]);\r
81         Assert.assertEquals(1, graph[4][5]);\r
82 \r
83         Assert.assertEquals(0, graph[5][0]);\r
84         Assert.assertEquals(0, graph[5][1]);\r
85         Assert.assertEquals(0, graph[5][2]);\r
86         Assert.assertEquals(0, graph[5][3]);\r
87         Assert.assertEquals(0, graph[5][4]);\r
88         Assert.assertEquals(0, graph[5][5]);\r
89     }\r
90 \r
91     @Test\r
92     public void testExceptionGraph() throws Exception{\r
93         ControlFlowGraph cfgraph = cfVisitor.getGraph("<init>(I)V");\r
94         cfgraph.setIncludingExceptionFlow(true);\r
95         int[][] graph = cfgraph.getGraphMatrix();\r
96 \r
97         Assert.assertEquals(6, graph.length);\r
98 \r
99         Assert.assertEquals(0, graph[0][0]);\r
100         Assert.assertEquals(1, graph[0][1]);\r
101         Assert.assertEquals(1, graph[0][2]);\r
102         Assert.assertEquals(0, graph[0][3]);\r
103         Assert.assertEquals(1, graph[0][4]);\r
104         Assert.assertEquals(0, graph[0][5]);\r
105 \r
106         Assert.assertEquals(0, graph[1][0]);\r
107         Assert.assertEquals(0, graph[1][1]);\r
108         Assert.assertEquals(0, graph[1][2]);\r
109         Assert.assertEquals(1, graph[1][3]);\r
110         Assert.assertEquals(1, graph[1][4]);\r
111         Assert.assertEquals(0, graph[1][5]);\r
112 \r
113         Assert.assertEquals(0, graph[2][0]);\r
114         Assert.assertEquals(0, graph[2][1]);\r
115         Assert.assertEquals(0, graph[2][2]);\r
116         Assert.assertEquals(1, graph[2][3]);\r
117         Assert.assertEquals(1, graph[2][4]);\r
118         Assert.assertEquals(0, graph[2][5]);\r
119 \r
120         Assert.assertEquals(0, graph[3][0]);\r
121         Assert.assertEquals(0, graph[3][1]);\r
122         Assert.assertEquals(0, graph[3][2]);\r
123         Assert.assertEquals(0, graph[3][3]);\r
124         Assert.assertEquals(1, graph[3][4]);\r
125         Assert.assertEquals(1, graph[3][5]);\r
126 \r
127         Assert.assertEquals(0, graph[4][0]);\r
128         Assert.assertEquals(0, graph[4][1]);\r
129         Assert.assertEquals(0, graph[4][2]);\r
130         Assert.assertEquals(0, graph[4][3]);\r
131         Assert.assertEquals(0, graph[4][4]);\r
132         Assert.assertEquals(1, graph[4][5]);\r
133 \r
134         Assert.assertEquals(0, graph[5][0]);\r
135         Assert.assertEquals(0, graph[5][1]);\r
136         Assert.assertEquals(0, graph[5][2]);\r
137         Assert.assertEquals(0, graph[5][3]);\r
138         Assert.assertEquals(0, graph[5][4]);\r
139         Assert.assertEquals(0, graph[5][5]);\r
140     }\r
141 \r
142     @Test\r
143     public void testBasic2() throws Exception{\r
144         Iterator<String> iterator = cfVisitor2.getMethodNames();\r
145 \r
146         Assert.assertTrue(iterator.hasNext());\r
147         Assert.assertEquals("<init>(I)V", iterator.next());\r
148         Assert.assertFalse(iterator.hasNext());\r
149 \r
150         ControlFlowGraph graph = cfVisitor2.getGraph("<init>(I)V");\r
151 \r
152         Assert.assertEquals(6, graph.getBasicBlockSize());\r
153 \r
154         // graph.setIncludingExceptionFlow(true);\r
155         // Assert.assertEquals(6, graph.getBasicBlockSize());\r
156     }\r
157 \r
158     @Test\r
159     public void testGraph2() throws Exception{\r
160         ControlFlowGraph graph = cfVisitor2.getGraph("<init>(I)V");\r
161         int[][] graphMatrix = graph.getGraphMatrix();\r
162 \r
163         Assert.assertEquals(6, graphMatrix.length);\r
164 \r
165         Assert.assertEquals(0, graphMatrix[0][0]);\r
166         Assert.assertEquals(1, graphMatrix[0][1]);\r
167         Assert.assertEquals(1, graphMatrix[0][2]);\r
168         Assert.assertEquals(0, graphMatrix[0][3]);\r
169         Assert.assertEquals(0, graphMatrix[0][4]);\r
170         Assert.assertEquals(0, graphMatrix[0][5]);\r
171 \r
172         Assert.assertEquals(0, graphMatrix[1][0]);\r
173         Assert.assertEquals(0, graphMatrix[1][1]);\r
174         Assert.assertEquals(0, graphMatrix[1][2]);\r
175         Assert.assertEquals(1, graphMatrix[1][3]);\r
176         Assert.assertEquals(0, graphMatrix[1][4]);\r
177         Assert.assertEquals(0, graphMatrix[1][5]);\r
178 \r
179         Assert.assertEquals(0, graphMatrix[2][0]);\r
180         Assert.assertEquals(0, graphMatrix[2][1]);\r
181         Assert.assertEquals(0, graphMatrix[2][2]);\r
182         Assert.assertEquals(1, graphMatrix[2][3]);\r
183         Assert.assertEquals(0, graphMatrix[2][4]);\r
184         Assert.assertEquals(0, graphMatrix[2][5]);\r
185 \r
186         Assert.assertEquals(0, graphMatrix[3][0]);\r
187         Assert.assertEquals(0, graphMatrix[3][1]);\r
188         Assert.assertEquals(0, graphMatrix[3][2]);\r
189         Assert.assertEquals(0, graphMatrix[3][3]);\r
190         Assert.assertEquals(0, graphMatrix[3][4]);\r
191         Assert.assertEquals(1, graphMatrix[3][5]);\r
192 \r
193         Assert.assertEquals(0, graphMatrix[4][0]);\r
194         Assert.assertEquals(0, graphMatrix[4][1]);\r
195         Assert.assertEquals(0, graphMatrix[4][2]);\r
196         Assert.assertEquals(0, graphMatrix[4][3]);\r
197         Assert.assertEquals(0, graphMatrix[4][4]);\r
198         Assert.assertEquals(1, graphMatrix[4][5]);\r
199 \r
200         Assert.assertEquals(0, graphMatrix[5][0]);\r
201         Assert.assertEquals(0, graphMatrix[5][1]);\r
202         Assert.assertEquals(0, graphMatrix[5][2]);\r
203         Assert.assertEquals(0, graphMatrix[5][3]);\r
204         Assert.assertEquals(0, graphMatrix[5][4]);\r
205         Assert.assertEquals(0, graphMatrix[5][5]);\r
206     }\r
207 }\r