1 package junit.quilt.cover.state;
2
3 import junit.quilt.framework.QuiltCollectorImpl;
4 import junit.quilt.framework.CoverageSegmentImpl;
5
6 import junit.quilt.cover.generic.*;
7
8 import java.util.List;
9
10 public class SimpleStateMachine
11 extends QuiltCollectorImpl
12 implements StateMachineCollector
13 {
14 private static String capabilities[] = {
15 STATEMENT_COVERAGE,
16 BRANCH_COVERAGE };
17
18 private class StateCollector
19 extends StatementSegment
20 {
21 private int state = -1;
22 private int data[][];
23
24 public StateCollector(String sourceFile,
25 BlockVertex bv,
26 int state,
27 int data[][]) {
28 super( sourceFile, bv );
29 this.state = state;
30 this.data = data;
31 }
32
33 public int getNumVisits() {
34 int RC = 0;
35 for (int i = 0; i < data[state].length; i++) {
36 RC += data[state][i];
37 }
38 return RC;
39 }
40 }
41
42 private class TransitionCollector
43 extends CoverageSegmentImpl
44 {
45 private int start = -1;
46 private int end = -1;
47 private int data[][];
48
49 public TransitionCollector(int start,
50 int end,
51 int data[][]) {
52 this.start = start;
53 this.end = end;
54 this.data = data;
55 }
56
57 public int getNumVisits() {
58 return data[start][end];
59 }
60 }
61
62 private String sourceFile = null;
63
64 private boolean valid[][];
65 private int visits[][];
66
67 private StateCollector[] allstates;
68 private TransitionCollector[][] alltrans;
69
70 private int last = -1;
71 private int numStates = -1;
72
73 public SimpleStateMachine( String sourcefile,
74 int numStates ) {
75 super( capabilities );
76 this.sourceFile = sourceFile;
77 valid = new boolean[numStates][numStates];
78 visits = new int[numStates][numStates];
79 this.numStates = numStates;
80
81 allstates = new StateCollector[numStates];
82 alltrans = new TransitionCollector[numStates][numStates];
83
84 }
85
86 public void visit( int state ) {
87 if (last > 0)
88 visits[last][state]++;
89 last = state;
90 }
91
92 public void addState( int state, BlockVertex bv ) {
93 if (bv instanceof CallVisitVertex) {
94 System.err.println("WARNING! Adding instrumentation to State Machine.");
95 }
96
97 if (bv == null) {
98 System.err.println("WARNING! Adding NULL to State Machine.");
99 }
100
101 if (allstates[state] == null) {
102 allstates[state] =
103 new StateCollector( sourceFile,
104 bv,
105 state,
106 visits );
107 addSegment( STATEMENT_COVERAGE, allstates[state] );
108 }
109 }
110
111 public void addTransition( int start, int end ) {
112 if (alltrans[start][end] == null) {
113 valid[start][end] = true;
114 alltrans[start][end] =
115 new TransitionCollector( start, end, visits );
116 addSegment( BRANCH_COVERAGE, alltrans[start][end] );
117 }
118 }
119
120 public void start() {}
121
122 public void reset() {
123 for (int i = 0; i < numStates; i++) {
124 for (int j = 0; j < numStates; j++) {
125 visits[i][j] = 0;
126 }
127 }
128 }
129 }
130
131
132
133
134
135
This page was automatically generated by Maven