1 /* TestTryStacks.java */
2 package org.quilt.cl;
3
4 import java.util.Comparator;
5 import junit.framework.*;
6 import org.apache.bcel.generic.*;
7 import org.quilt.graph.*;
8 /***
9 * Exercise quilt.cl.TryStacks.
10 *
11 * @author < a href="jddixon@users.sourceforge.net">Jim Dixon</a>
12 */
13 public class TestTryStacks extends TestCase {
14
15 private CodeExceptionGen[] handlers = null;
16 private ControlFlowGraph graph = null;
17 private InstructionList ilist = null;
18 private SortedBlocks blox = null;
19 private TryStacks ts = null;
20
21 private InstructionHandle
22 startMain, midMain, endMain,
23 tryStart2, tryEnd2, catch2A, catch2B,
24 tryMid1, tryEnd1, catch1A, catch1B, catch1C;
25 private CodeExceptionGen
26 handler1A, handler1B, handler1C, handler2A, handler2B;
27
28 public TestTryStacks (String name) {
29 super(name);
30 }
31
32 public void setUp () {
33 blox = new SortedBlocks();
34 graph = new ControlFlowGraph();
35 ilist = new InstructionList();
36
37 // a method with two try blocks starting at the same position
38 // A
39 // try {
40 // try {
41 // B
42 // } catch (...) {
43 // catch2A
44 // } catch (...) {
45 // catch2B
46 // }
47 // tryMid1
48 // } catch ( ...) {
49 // catch 1A
50 // } catch ( ...) {
51 // catch 1B
52 // } catch ( ...) {
53 // catch 1C
54 // }
55 // midMain
56 // endMain
57 startMain = ilist.append ( new NOP() ); // 0
58 ilist.append ( new NOP() );
59 tryStart2 = ilist.append ( new NOP() ); // 2
60 ilist.append ( new NOP() );
61 tryEnd2 = ilist.append ( new NOP() ); // 4
62 ilist.append ( new NOP() );
63 catch2A = ilist.append ( new NOP() ); // 6
64 ilist.append ( new NOP() );
65 catch2B = ilist.append ( new NOP() ); // 8
66 ilist.append ( new NOP() );
67 tryMid1 = ilist.append ( new NOP() ); // 10
68 ilist.append ( new NOP() );
69 tryEnd1 = ilist.append ( new NOP() ); // 12
70 ilist.append ( new NOP() );
71 catch1A = ilist.append ( new NOP() ); // 14
72 ilist.append ( new NOP() );
73 catch1B = ilist.append ( new NOP() ); // 16
74 ilist.append ( new NOP() );
75 catch1C = ilist.append ( new NOP() ); // 18
76 ilist.append ( new NOP() );
77 midMain = ilist.append ( new NOP() ); // 20
78 ilist.append ( new NOP() );
79 endMain = ilist.append ( new NOP() ); // 22
80
81 ilist.setPositions();
82 handler1A = new CodeExceptionGen (tryStart2, tryEnd1, catch1A,
83 new ObjectType("Exception"));
84 handler1B = new CodeExceptionGen (tryStart2, tryEnd1, catch1B,
85 new ObjectType("Exception"));
86 handler1C = new CodeExceptionGen (tryStart2, tryEnd1, catch1C,
87 new ObjectType("Exception"));
88
89 handler2A = new CodeExceptionGen (tryStart2, tryEnd2, catch2A,
90 new ObjectType("Exception"));
91 handler2B = new CodeExceptionGen (tryStart2, tryEnd2, catch2B,
92 new ObjectType("Exception"));
93 }
94
95 public void testPositions () {
96 assertEquals ("startMain offset wrong", 0, startMain.getPosition() );
97 assertEquals ("tryStart2 offset wrong", 2, tryStart2.getPosition() );
98 assertEquals ("tryEnd1 offset wrong", 12, tryEnd1.getPosition() );
99 assertEquals ("midMain offset wrong", 20, midMain.getPosition() );
100 assertEquals ("endMain offset wrong", 22, endMain.getPosition() );
101 }
102 public void testNewEmpty () {
103 assertEquals ("new graph has wrong number of vertices",
104 2, graph.size());
105 handlers = new CodeExceptionGen[0];
106 ts = new TryStacks (handlers, blox, graph);
107 assertEquals ("no handlers, but TryStacks constructor changed graph",
108 2, graph.size());
109 }
110 public void testComparator () {
111 CodeExceptionGen[] handlers = {
112 handler1A, handler1B, handler1C, handler2A, handler2B };
113 ts = new TryStacks (handlers, blox, graph);
114 Comparator cmp = ts.getComparator();
115 assertEquals ("handler1A doesn't equal itself ;-)",
116 0, cmp.compare(handler1A, handler1A) );
117 assertEquals ("handler1A doesn't sort before 1B",
118 -1, cmp.compare(handler1A, handler1B) );
119 assertEquals ("handler1C doesn't sort after 1B",
120 1, cmp.compare(handler1C, handler1B) );
121 assertEquals ("shorter try block doesn't sort after longer",
122 1, cmp.compare(handler2A, handler1B) );
123 assertEquals ("longer try block doesn't sort before shorter",
124 -1, cmp.compare(handler1C, handler2B) );
125 }
126 /***
127 * Graph should look like so (E = Entry, X = Exit, Tk = try block
128 * e0, nA/B/C = catch block, Vn = code vertex)
129 * E
130 * T1 T2 V2
131 * 2A 2B
132 * X
133 * 1A 1B 1C
134 * X
135 * X
136 */
137 private void checkScrambledHandlers (int n) {
138 Entry e0, e1, e2; // (sub)graph entry vertices
139 Connector connE0, connE1, connE2;//their connectors
140 Edge e0out, e1out, e2out; // their preferred edges
141 // connector multi-edges
142 Edge connE1out0, connE1out1, connE1out2;
143 Edge connE2out0, connE2out1;
144
145 Exit x0, x1, x2; // (sub)graph exit vertices
146 Connector connX0, connX1, connX2;
147 Edge x0out, x1out, x2out;
148
149 CodeVertex v1A, v1B, v1C, v2A, v2B; // catch vertices
150 Edge v1Aout, v1Bout, v1Cout, v2Aout, v2Bout;
151
152 CodeVertex v2; // code vertex
153 Connector connV2;
154 Edge v2out;
155
156 GraphTalker talker = new GraphTalker();
157 talker.xform (null, null, graph);
158 System.out.print (ts);
159
160 e0 = graph.getEntry(); // 1
161 connE0 = e0.getConnector();
162 e0out = e0.getEdge();
163 assertEquals ("graph Entry should have 1 edge out",
164 1, connE0.size() );
165
166 e1 = (Entry)e0out.getTarget(); // 2
167 connE1 = e1.getConnector();
168 e1out = e1.getEdge();
169 assertEquals ("first try block should have 3 edges out",
170 3, connE1.size() );
171
172 e2 = (Entry)e1out.getTarget(); // 3
173 connE2 = e2.getConnector();
174 e2out = e2.getEdge();
175 assertEquals ("second try block should have 2 edges out",
176 2, connE2.size() );
177
178 v2 = (CodeVertex)e2out.getTarget(); // 4
179 connV2 = v2.getConnector();
180 e2out = v2.getEdge();
181 assertEquals ("code vertex should have 1 edge out",
182 1, connV2.size() );
183 assertEquals ("catch block has wrong offset",
184 tryStart2.getPosition(), v2.getPosition() );
185
186 assertEquals ("graph Entry has wrong index", 0, e0.getIndex());
187 assertEquals ("graph Entry has wrong index", 0, e1.getIndex());
188 assertEquals ("graph Entry has wrong index", 0, e2.getIndex());
189
190 assertEquals ("e0's target should be e1", e1, e0.getTarget());
191 assertEquals ("e1's target should be e2", e2, e1.getTarget());
192 assertEquals ("e2's target should be v2", v2, e2.getTarget());
193
194 assertEquals ("v2 should be in graph 2", 2, v2.getGraph().getIndex());
195 // handlers are 2.2 and 2.3
196 assertEquals ("v2 has wrong index", 4, v2.getIndex() );
197
198 x2 = (Exit)e2out.getTarget(); // 5
199 connX2 = x2.getConnector();
200 x2out = x2.getEdge();
201 assertEquals ("second subgraph exit should have 1 edge out",
202 1, connX2.size() );
203
204 x1 = (Exit)x2out.getTarget(); // 6
205 connX1 = x1.getConnector();
206 x1out = x1.getEdge();
207 assertEquals ("first subgraph exit should have 1 edge out",
208 1, connX1.size() );
209
210 x0 = (Exit)x1out.getTarget(); // 7
211 connX0 = x0.getConnector();
212 x0out = x0.getEdge();
213 assertEquals ("main graph exit should have 1 edge out",
214 1, connX0.size() );
215 assertEquals ("graph exit doesn't loop back to graph e0",
216 e0, x0out.getTarget() );
217
218 assertEquals ("v2's target should be x2", x2, v2.getTarget());
219 assertEquals ("x2's target should be x1", x1, x2.getTarget());
220 assertEquals ("x2's target should be x0", x0, x1.getTarget());
221
222 assertEquals ("graph exit has wrong index", 1, x2.getIndex());
223 assertEquals ("graph exit has wrong index", 1, x1.getIndex());
224 assertEquals ("graph exit has wrong index", 1, x0.getIndex());
225
226 // CATCH BLOCKS (1)
227 ComplexConnector cc = (ComplexConnector) connE1;
228 connE1out0 = cc.getEdge(0);
229 v1A = (CodeVertex)connE1out0.getTarget(); // 8
230 v1Aout = v1A.getEdge();
231 assertEquals ("catch block edge doesn't go to exit",
232 v1Aout.getTarget(), x1 );
233 assertEquals ("catch block has wrong offset",
234 catch1A.getPosition(), v1A.getPosition() );
235 assertEquals ("catch block has wrong index", 2, v1A.getIndex());
236
237 connE1out1 = cc.getEdge(1);
238 v1B = (CodeVertex)connE1out1.getTarget(); // 9
239 v1Bout = v1B.getEdge();
240 assertEquals ("catch block edge doesn't go to exit",
241 v1Bout.getTarget(), x1 );
242 assertEquals ("catch block has wrong offset",
243 catch1B.getPosition(), v1B.getPosition() );
244 assertEquals ("catch block has wrong index", 3, v1B.getIndex());
245
246 connE1out2 = cc.getEdge(2);
247 v1C = (CodeVertex)connE1out2.getTarget(); // 10
248 v1Cout = v1C.getEdge();
249 assertEquals ("catch block edge doesn't go to exit",
250 v1Cout.getTarget(), x1 );
251 assertEquals ("catch block has wrong offset",
252 catch1C.getPosition(), v1C.getPosition() );
253 assertEquals ("catch block has wrong index", 4, v1C.getIndex());
254
255 // CATCH BLOCKS (2)
256 cc = (ComplexConnector) e2.getConnector();
257 connE2out0 = cc.getEdge(0);
258 v2A = (CodeVertex)connE2out0.getTarget(); // 11
259 v2Aout = v2A.getEdge();
260 assertEquals ("catch block edge doesn't go to exit",
261 v2Aout.getTarget(), x2 );
262 assertEquals ("catch block has wrong offset",
263 catch2A.getPosition(), v2A.getPosition() );
264 assertEquals ("catch block has wrong index", 2, v2A.getIndex());
265
266 connE2out1 = cc.getEdge(1);
267 v2B = (CodeVertex)connE2out1.getTarget(); // 12
268 v2Bout = v2B.getEdge();
269 assertEquals ("catch block edge doesn't go to exit",
270 v2Bout.getTarget(), x2 );
271 assertEquals ("catch block has wrong offset",
272 catch2B.getPosition(), v2B.getPosition() );
273 assertEquals ("catch block has wrong index", 3, v2B.getIndex());
274
275 }
276 public void testGraphSize() {
277 CodeExceptionGen[] handlers = {
278 handler1B, handler2B, handler1C, handler2A, handler1A};
279 ts = new TryStacks (handlers, blox, graph);
280 // // DEBUG
281 // System.out.print(ts);
282 // GraphTalker talker = new GraphTalker();
283 // talker.xform (null, null, graph);
284 // // END
285 assertEquals ("wrong number of vertices in complex graph ",
286 12, graph.size() );
287 }
288 public void testScrambledHandlers1 () {
289 CodeExceptionGen[] handlers = {
290 handler1B, handler2B, handler1C, handler2A, handler1A};
291 ts = new TryStacks (handlers, blox, graph);
292 checkScrambledHandlers (1);
293 }
294 public void testScrambledHandlers2 () {
295 CodeExceptionGen[] handlers = {
296 handler2B, handler1C, handler2A, handler1A, handler1B};
297 ts = new TryStacks (handlers, blox, graph);
298 checkScrambledHandlers (2);
299 }
300 public void testScrambledHandlers3 () {
301 CodeExceptionGen[] handlers = {
302 handler2B, handler2A, handler1C, handler1B, handler1A};
303 ts = new TryStacks (handlers, blox, graph);
304 checkScrambledHandlers (3);
305 }
306 }
This page was automatically generated by Maven