KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > cl > TestGraphTransformer


1 /* TestGraphTransformer.java */
2 package org.quilt.cl;
3
4 import java.util.List JavaDoc;
5 import java.util.Vector JavaDoc;
6 import junit.framework.*;
7 import org.apache.bcel.classfile.Method;
8 import org.apache.bcel.generic.*;
9
10 import org.quilt.graph.*;
11
12 /**
13  * Test GraphTransformer using runTest methods from classes synthesized
14  * by ClassFactory.
15  *
16  * @author <a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
17  */

18 public class TestGraphTransformer extends TestCase {
19
20     private ClassFactory factory = ClassFactory.getInstance();
21
22     private ClassGen clazz;
23
24     private GraphTransformer xformer;
25
26     private GraphSpy spy;
27
28     private GraphTalker talker;
29
30     private ControlFlowGraph theGraph;
31
32     public TestGraphTransformer (String JavaDoc name) {
33         super(name);
34         GraphSpy.setName("the spy");
35     }
36
37     public void setUp () {
38         List JavaDoc gxf = new Vector JavaDoc();
39         spy = new GraphSpy();
40         talker = new GraphTalker();
41         gxf.add ( spy );
42         // uncomment if things stop working
43
// gxf.add ( talker );
44
xformer = new GraphTransformer ( gxf );
45     }
46
47     private MethodGen loadAndExtractRunTest (String JavaDoc name) {
48         clazz = factory.makeClass(name, name);
49         Method [] methods = clazz.getMethods();
50         for (int i = 0 ; i < methods.length; i++) {
51             if ( methods[i].getName().equals("runTest") ) {
52                 return new MethodGen (methods[i], clazz.getClassName(),
53                         clazz.getConstantPool());
54             }
55         }
56         return null; // not found
57
}
58     public void testDefault () {
59         MethodGen
60         mgDefault = loadAndExtractRunTest ("test.data.TestDefault");
61         assertNotNull ("extracted method is null", mgDefault);
62
63         // build the graph, get the instruction list from it
64
mgDefault.setInstructionList(xformer.xform ( clazz, mgDefault));
65         mgDefault.removeNOPs();
66         InstructionList ilist = mgDefault.getInstructionList();
67         Instruction [] inst = ilist.getInstructions();
68         assertEquals ("wrong number of instructions", 2, ilist.getLength() );
69         assertTrue ("", inst[0] instanceof ICONST);
70         assertTrue ("", inst[1] instanceof IRETURN);
71
72         theGraph = GraphSpy.getTheGraph();
73         // DEBUG
74
//GraphTalker talker = new GraphTalker();
75
//talker.xform (null, null, theGraph);
76
// END
77
assertNotNull ("failed to get reference to method CFG", theGraph);
78         assertNotNull ("graph exit vertex is null", theGraph.getExit());
79         assertEquals ("TestDefault graph has wrong size", 3, theGraph.size() );
80     }
81
82     public void testIfThen () {
83         MethodGen
84         mgIfThen = loadAndExtractRunTest ("test.data.TestIfThen");
85         assertNotNull ("extracted method is null", mgIfThen);
86
87         // build the graph, get the instruction list from it
88
InstructionList ilist = xformer.xform ( clazz, mgIfThen);
89         assertEquals ("wrong number of instructions", 6, ilist.getLength() );
90         Instruction [] inst = ilist.getInstructions();
91         assertTrue ("should be ILOAD: " + inst[0],
92                                         inst[0] instanceof ILOAD);
93         assertTrue ("should be IFGT: " + inst[1],
94                                         inst[1] instanceof IFGT);
95         assertTrue ("should be ICONST: " + inst[2],
96                                         inst[2] instanceof ICONST);
97         assertTrue ("should be IRETURN: " + inst[3],
98                                         inst[3] instanceof IRETURN);
99         assertTrue ("should be ICONST: " + inst[4],
100                                         inst[4] instanceof ICONST);
101         assertTrue ("should be IRETURN: " + inst[5],
102                                         inst[5] instanceof IRETURN);
103
104         // EXAMINE GRAPH
105
theGraph = GraphSpy.getTheGraph();
106         assertNotNull ("failed to get reference to method CFG", theGraph);
107         assertNotNull ("graph exit vertex is null", theGraph.getExit());
108         assertEquals ("TestIfThen graph has wrong size", 5, theGraph.size() );
109     }
110
111     public void testWhile () {
112         MethodGen
113         mgWhile = loadAndExtractRunTest ("test.data.TestWhile");
114         assertNotNull ("extracted method is null", mgWhile);
115
116         // build the graph, get the instruction list from it
117
mgWhile.setInstructionList(xformer.xform ( clazz, mgWhile));
118         mgWhile.removeNOPs();
119         InstructionList ilist = mgWhile.getInstructionList();
120         assertEquals ("wrong number of instructions", 7, ilist.getLength() );
121         Instruction [] inst = ilist.getInstructions();
122         assertTrue ("", inst[0] instanceof ILOAD);
123         assertTrue ("", inst[1] instanceof DUP);
124         assertTrue ("", inst[2] instanceof IFLE);
125         assertTrue ("", inst[3] instanceof ICONST);
126         assertTrue ("", inst[4] instanceof ISUB);
127         assertTrue ("", inst[5] instanceof GOTO);
128         assertTrue ("", inst[6] instanceof IRETURN);
129
130         // EXAMINE GRAPH
131
theGraph = GraphSpy.getTheGraph();
132         assertNotNull ("failed to get reference to method CFG", theGraph);
133         assertNotNull ("graph exit vertex is null", theGraph.getExit());
134         assertEquals ("TestWhile graph has wrong size", 6, theGraph.size() );
135     } // GEEP
136

137     private void dumpInstructions (Instruction [] ilist) {
138         System.out.println("Instructions returned:");
139         for (int i = 0; i < ilist.length; i++) {
140             System.out.println (" " + ilist[i] );
141         }
142     }
143     public void testNPEWithCatch () {
144         MethodGen
145         mgNPEWithCatch = loadAndExtractRunTest ("test.data.TestNPEWithCatch");
146         assertNotNull ("extracted method is null", mgNPEWithCatch);
147
148         // build the graph, get the instruction list from it
149
mgNPEWithCatch.setInstructionList(xformer.xform ( clazz,
150                                                             mgNPEWithCatch));
151         mgNPEWithCatch.removeNOPs();
152         InstructionList ilist = mgNPEWithCatch.getInstructionList();
153         Instruction [] inst = ilist.getInstructions();
154
155         theGraph = GraphSpy.getTheGraph();
156         // talker.xform (null, null, theGraph);
157

158         assertNotNull ("failed to get reference to method CFG", theGraph);
159         assertNotNull ("graph exit vertex is null", theGraph.getExit());
160         assertEquals ("TestNPEWithCatch graph has wrong size",
161                                                     8, theGraph.size() );
162
163         assertEquals ("wrong number of instructions", 7, ilist.getLength() );
164         assertTrue ("expected ACONST_NULL", inst[0] instanceof ACONST_NULL);
165         assertTrue ("expected ICONST", inst[1] instanceof ICONST);
166         assertTrue ("expected INVOKEVIRTUAL", inst[2] instanceof INVOKEVIRTUAL);
167         assertTrue ("expected ICONST", inst[3] instanceof ICONST);
168         assertTrue ("expected IRETURN", inst[4] instanceof IRETURN);
169         assertTrue ("expected ICONST", inst[5] instanceof ICONST);
170         assertTrue ("expected IRETURN", inst[6] instanceof IRETURN);
171     }
172
173     public void testNPENoCatch () {
174         MethodGen
175         mgNPENoCatch = loadAndExtractRunTest ("test.data.TestNPENoCatch");
176         assertNotNull ("extracted method is null", mgNPENoCatch);
177
178         // build the graph, get the instruction list from it
179
InstructionList ilist = xformer.xform ( clazz, mgNPENoCatch);
180         assertEquals ("wrong number of instructions", 5, ilist.getLength() );
181         Instruction [] inst = ilist.getInstructions();
182         assertTrue ("expected ACONST_NULL", inst[0] instanceof ACONST_NULL);
183         assertTrue ("expected ICONST", inst[1] instanceof ICONST);
184         assertTrue ("expected INVOKEVIRTUAL", inst[2] instanceof INVOKEVIRTUAL);
185         assertTrue ("expected ICONST", inst[3] instanceof ICONST);
186         assertTrue ("expected IRETURN", inst[4] instanceof IRETURN);
187
188         theGraph = GraphSpy.getTheGraph();
189         // DEBUG
190
//GraphTalker talker = new GraphTalker();
191
//talker.xform (null, null, theGraph);
192
// END
193
assertNotNull ("failed to get reference to method CFG", theGraph);
194         assertNotNull ("graph exit vertex is null", theGraph.getExit());
195         assertEquals ("TestNPENoCatch graph has wrong size",
196                                                     4, theGraph.size() );
197     }
198
199     public void testSelect () {
200         MethodGen
201         mgSelect = loadAndExtractRunTest ("test.data.TestSelect");
202         assertNotNull ("extracted method is null", mgSelect);
203
204         // build the graph, get the instruction list from it
205
mgSelect.setInstructionList(xformer.xform ( clazz, mgSelect));
206         mgSelect.removeNOPs();
207         InstructionList ilist = mgSelect.getInstructionList();
208         Instruction [] inst = ilist.getInstructions();
209         // DEBUG
210
//dumpInstructions(inst);
211
// END
212
assertEquals ("wrong number of instructions", 10, inst.length );
213         assertTrue ("expected ILOAD", inst[0] instanceof ILOAD);
214         assertTrue ("expected LOOKUPSWITCH", inst[1] instanceof LOOKUPSWITCH);
215         assertTrue ("expected SIPUSH", inst[2] instanceof SIPUSH);
216         assertTrue ("expected IRETURN", inst[3] instanceof IRETURN);
217         assertTrue ("expected SIPUSH", inst[4] instanceof SIPUSH);
218         assertTrue ("expected IRETURN", inst[5] instanceof IRETURN);
219         assertTrue ("expected SIPUSH", inst[6] instanceof SIPUSH);
220         assertTrue ("expected IRETURN", inst[7] instanceof IRETURN);
221         assertTrue ("expected SIPUSH", inst[8] instanceof SIPUSH);
222         assertTrue ("expected IRETURN", inst[9] instanceof IRETURN);
223
224         theGraph = GraphSpy.getTheGraph();
225         // DEBUG
226
//GraphTalker talker = new GraphTalker();
227
//talker.xform (null, null, theGraph);
228
// END
229
assertNotNull ("failed to get reference to method CFG", theGraph);
230         assertNotNull ("graph exit vertex is null", theGraph.getExit());
231         assertEquals ("TestSelect graph has wrong size", 7, theGraph.size() );
232    }
233 }
234
Popular Tags