KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* TestVertexData.java */
2 package org.quilt.cl;
3
4 import junit.framework.*;
5 import org.apache.bcel.generic.*;
6 import org.quilt.graph.*;
7
8 /**
9  * Vertex data is the additional data differentiating a CodeVertex
10  * from a simple Vertex. It consists of a bytecode position, a
11  * label, and the InstructionList.
12  *
13  * XXX This code tested a now-obsolete data structure; it should be
14  * beefed up or dropped.
15  *
16  * @author <a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
17  */

18 public class TestVertexData extends TestCase {
19
20     private ControlFlowGraph cfg;
21     
22     private CodeVertex node;
23     
24     public TestVertexData (String JavaDoc name) {
25         super(name);
26     }
27
28     public void setUp () {
29         cfg = new ControlFlowGraph();
30         node = new CodeVertex (cfg, 3); // position of no significance
31
}
32
33     public void testNewNode() {
34         assertEquals ("empty node has wrong position",
35                                         3, node.getPosition() );
36         assertTrue ("empty node has something in it",
37                                         node.getInstructionList().isEmpty() );
38         assertEquals("empty node has something in instruction list",
39                                         0, node.getInstructionList().size() );
40     }
41     // in fact this would become several nodes, but for testing ...
42
public void testWhileNode() {
43         InstructionList ilist = node.getInstructionList();
44         ilist.append ( new ILOAD (1) );
45         InstructionHandle ifHandle = ilist.append ( new DUP () );
46         InstructionHandle loopHandle = ilist.append ( new ICONST( 1 ) );
47         ilist.append( new ISUB() );
48         ilist.append( new GOTO( ifHandle ));
49         InstructionHandle retHandle = ilist.append ( new IRETURN() );
50         ilist.insert( loopHandle, new IFLE( retHandle ));
51
52         assertEquals ("'while' node has wrong position",
53                                         3, node.getPosition() );
54         assertTrue ("'while' node has nothing in it",
55                                         !node.getInstructionList().isEmpty() );
56         assertEquals("'while' node has wrong size instruction list",
57                                         7, node.getInstructionList().size() );
58
59         ControlFlowGraph graph = new ControlFlowGraph();
60         CodeVertex v = graph.insertCodeVertex ( graph.getEntry().getEdge() );
61         v.setPos(node.getPosition());
62         assertEquals ("new vertex has wrong bytecode position",
63                                     node.getPosition(),
64                                     v.getPosition() );
65     }
66 }
67
Popular Tags