Skip to content

Commit 46c6603

Browse files
committed
Program complexity measure & analysis
1 parent d66ee91 commit 46c6603

15 files changed

+1745
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package software_testing_meaures;
8+
9+
import java.io.BufferedReader;
10+
import java.io.FileNotFoundException;
11+
import java.io.FileReader;
12+
import java.io.IOException;
13+
import java.util.Scanner;
14+
15+
/**
16+
*
17+
* @author Suraj Singh
18+
*/
19+
class Automated_Calculation_Cyclomatic_Complexity {
20+
static String[] matching_Expression = new String[]{""};
21+
public static void main(String arfs[]) throws IOException
22+
{
23+
Scanner s = new Scanner(System.in);
24+
String line,path;
25+
int counter=0,isSwitch=0;// store the number of decision nodes in the DD graph
26+
char choice;
27+
StringBuffer inputFile, matcherFile;
28+
do
29+
{
30+
counter = 0;
31+
System.out.print("Enter file name : ");
32+
path = "C:\\Users\\Suraj Singh\\Desktop\\"+s.next()+".java";
33+
System.out.print("Do you want source code : ");
34+
choice = s.next().charAt(0);
35+
try
36+
{
37+
BufferedReader r = new BufferedReader(new FileReader(path));
38+
if(choice=='y'||choice=='Y')
39+
System.out.println("\n*************************************SOURCE CODE*************************************");
40+
while( (line=r.readLine())!=null)
41+
{
42+
if(choice=='y'||choice=='Y') System.out.println(line);
43+
if(line.matches("(^\\s*)(.*)(if|for|while)(\\(.*)"))
44+
counter++;
45+
else if(line.matches("(^\\s*)switch(\\(.*)"))
46+
isSwitch++; // Beginning of switch statement
47+
else if(line.matches("(^\\s*)case(\\s+)(\\w+):(.*)") && isSwitch>0) // support nested sw
48+
counter++;
49+
else if(line.matches("(^\\s*)default(\\s*):(.*)") && isSwitch>0)//end of switch further case will not be cosidered
50+
{
51+
isSwitch--;
52+
counter++;
53+
}
54+
}
55+
}
56+
catch(FileNotFoundException e)
57+
{
58+
System.out.println(e.getMessage());
59+
return;
60+
}
61+
if(choice=='y'||choice=='Y') System.out.println("**************************************************************************************");
62+
System.out.println("\nCYCLOMATIC COMPLEXITY => "+counter);
63+
System.out.print("\nDo you want to continue (y/n)? ");
64+
choice = s.next().charAt(0);
65+
System.out.println();
66+
}while(choice=='Y'||choice=='y');
67+
}
68+
}

CyclomaticComplexity.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package software_testing_meaures;
8+
9+
import java.util.Scanner;
10+
11+
/**
12+
*
13+
* @author Suraj Singh
14+
*/
15+
//class CyclomaticComplexity {
16+
// public static void main(String arfs[])
17+
// {
18+
// Scanner s = new Scanner(System.in);
19+
// int i;
20+
// String input = s.next();
21+
// for(i =0;i<input.length()/2;i++)
22+
// {
23+
// if(input.charAt(i)!=input.charAt(input.length()-i-1))
24+
// break;
25+
// }
26+
// if(i==input.length()/2)
27+
// System.out.println("Palindrome");
28+
// else
29+
// System.out.println("Not a Palindrome");
30+
// }
31+
//}
32+
/*
33+
Program to find the result of student
34+
*/
35+
class CyclomaticComplexity
36+
{
37+
public static void main(String arfs[])
38+
{
39+
Scanner s = new Scanner(System.in);
40+
int a,sum = 5;
41+
System.out.println("Enter marks ");
42+
int n = s.nextInt();
43+
if(n < 100 && n > 0)
44+
{
45+
System.out.println("Marks obtained "+n);
46+
if(n>50)
47+
{
48+
System.out.println("Student Passed ");
49+
}
50+
else
51+
{
52+
System.out.println("Student Failed");
53+
}
54+
}
55+
System.out.println("The End !!!");
56+
}
57+
}
+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package software_testing_meaures;
8+
9+
import java.io.BufferedReader;
10+
import java.io.FileReader;
11+
import java.util.ArrayList;
12+
import java.util.Scanner;
13+
import java.util.Stack;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
/**
18+
*
19+
* @author Guarav Shah
20+
*/
21+
class graph
22+
{
23+
graph[] adjacents = new graph[2]; // each node can have two outgoing branches out of it.
24+
String type; // will store the line numbers which this node comprise
25+
int start;
26+
int end;
27+
int index=0; // will represent the number of outgoing edges out of a node.
28+
public String toString()
29+
{
30+
return " N"+this.index+" "+this.start+" "+this.end;
31+
}
32+
}
33+
class CyclomaticComplexity_Using_GraphMatrix {
34+
static ArrayList<graph> nodes;
35+
static int number;
36+
public static void print_Graph_Matrix()
37+
{
38+
graph node,left,right;
39+
int graphMatrix[][] = new int[nodes.size()][nodes.size()],value[] = new int[nodes.size()],verticalSum=0;
40+
System.out.println("\n");
41+
System.out.println("******************** CONNECTION MATRIX ********************");
42+
System.out.print(" ");
43+
for(int i=0;i<nodes.size();i++)
44+
{
45+
System.out.print("N"+(i+1)+" ");
46+
}
47+
System.out.println();
48+
for(int i=0;i<nodes.size();i++)
49+
{
50+
value[i]=0;
51+
node = nodes.get(i);
52+
// left link is not null
53+
if(node.adjacents[0]!=null)
54+
{
55+
left = node.adjacents[0];
56+
graphMatrix[i][left.index-1]++;
57+
value[i]++;
58+
}
59+
if(node.adjacents[1]!=null)
60+
{
61+
right = node.adjacents[1];
62+
graphMatrix[i][right.index-1]++;
63+
value[i]++;
64+
}
65+
}
66+
for(int i=0;i<nodes.size();i++)
67+
{
68+
System.out.print("N"+nodes.get(i).index+" ");
69+
for(int j=0;j<nodes.size();j++)
70+
{
71+
System.out.print(graphMatrix[i][j]+" ");
72+
}
73+
if(value[i]!=0)
74+
{
75+
verticalSum+= value[i]-1;
76+
System.out.print(value[i]+"-1="+(value[i]-1));
77+
}
78+
else
79+
{
80+
System.out.print(value[i]+"-0=0");
81+
}
82+
System.out.println();
83+
}
84+
System.out.println("\nCYCLOMATIC COMPLEXITY USING CONNECTION MATRIX "+verticalSum+"+1="+(verticalSum+1));
85+
}
86+
public static void print_Nodes_Lines()
87+
{
88+
graph node;
89+
System.out.println("\n********* Mapping Flow Graph Node To DD Graph Nodes *********\n");
90+
System.out.println("Program DD Path Adjacent Nodes");
91+
System.out.println("Graph Graph Left Right");
92+
System.out.println("Nodes Nodes Edge Edge");
93+
System.out.println("----------------------------------");
94+
for(int i=0;i<nodes.size();i++)
95+
{
96+
node = nodes.get(i);
97+
System.out.printf("%2s-%2s %6s",node.start,node.end,"N"+node.index);
98+
if(node.adjacents[0]!=null)
99+
//System.out.print(" "+node.adjacents[0].toString()+" ");
100+
System.out.printf("%10s","N"+node.adjacents[0].index);
101+
if(node.adjacents[0]!=null && node.adjacents[1]!=null)
102+
System.out.printf("%10s","N"+node.adjacents[1].index);
103+
else if(node.adjacents[1]!=null)
104+
System.out.print("Adjacent Nodes "+node.adjacents[1].toString()+" ");
105+
System.out.println();
106+
}
107+
}
108+
public static void main(String arfs[])
109+
{
110+
Scanner s = new Scanner(System.in);
111+
String line,path;
112+
// alternateNode will take care of statements which are part of one directional output of conditional statements.
113+
boolean conditionFound=false,alternateNode=false,mergeNode=false;
114+
StringBuffer sb = new StringBuffer();
115+
Stack<Integer> scope = new Stack<Integer>(); // 1 means top element is opening bracket, 2 means closing bracket
116+
Stack<graph> stack = new Stack<graph>();
117+
nodes = new ArrayList<graph>();
118+
119+
int counter=0,lastConditionPoint=1; // lastConditionPoint will store the last time before a condition node
120+
// Pattern definition section
121+
Pattern decision = Pattern.compile("\\b(if|for|while|do)\\b(.*)"); // decision
122+
// left node will store the node on left of conditional node, right node will store the node on right side of the conditional node.
123+
graph node,last=null,left=null,right=null,temp=null; // last node will store the last condition node found in order to trace the path along two outgoing edges.
124+
// Matcher definition section
125+
Matcher dM,bM;
126+
do
127+
{
128+
System.out.print("Enter file name : ");
129+
path = "C:\\Users\\Suraj Singh\\Desktop\\"+s.next()+".txt";
130+
System.out.println("\n\n******************** FILE CONTENT ********************");
131+
try
132+
{
133+
BufferedReader r = new BufferedReader(new FileReader(path));
134+
while( (line=r.readLine())!=null)
135+
{
136+
counter++;// store the line number in the sampled program.
137+
dM = decision.matcher(line);
138+
System.out.print(counter+" ");
139+
System.out.println(line);
140+
if(dM.find()) // We found a conditional node
141+
{
142+
// conditionFound = true;
143+
node = new graph();
144+
node.start=lastConditionPoint;
145+
node.end = counter;// will store the starting and ending line numbers for which this node is defined.
146+
lastConditionPoint = counter+1;
147+
node.index = ++number;
148+
if(stack.size()!=0) // nested conditional statements
149+
{
150+
temp = stack.peek();
151+
temp.adjacents[0] = node; // set the left outgoing edge
152+
}
153+
// last = node; // last node is stored in order to set the pointers when scope of this condition ends
154+
stack.push(node);
155+
nodes.add(node);
156+
conditionFound = true;
157+
//nodes.add(new graph())
158+
// Add code for this node
159+
}
160+
else if(line.contains("{") && conditionFound) // beginning of new block scope
161+
{
162+
conditionFound=false;
163+
scope.add(1);
164+
}
165+
else if(line.contains("else"))
166+
{
167+
alternateNode = true;
168+
}
169+
else if(line.contains("}") && scope.size()!=0 && !alternateNode) // we found the complete block scope with open and closing brackets
170+
{
171+
//inside the scope we need to create one single graph node
172+
node = new graph();
173+
node.start = lastConditionPoint;
174+
node.end = counter;
175+
node.index = ++number;
176+
last = stack.peek();
177+
if(left!=null && right!=null)
178+
left.adjacents[0] = right.adjacents[0] = node;
179+
if(last.adjacents[0]==null)
180+
last.adjacents[0] = node; // if condition is true then statements inside scope are executed and form the sibling of last node.
181+
left = node;
182+
nodes.add(node);
183+
mergeNode = false;
184+
lastConditionPoint = counter+1;
185+
scope.pop();
186+
}
187+
else if(line.contains("}") && alternateNode) // take care of else part
188+
{
189+
node = new graph();
190+
node.start = lastConditionPoint;
191+
node.end = counter;
192+
node.index = ++number;
193+
nodes.add(node);
194+
last = stack.pop();
195+
right = node;
196+
//last.adjacents[1] = new graph();
197+
if(last.adjacents[1]==null)
198+
last.adjacents[1] = node;
199+
lastConditionPoint = counter+1;
200+
alternateNode = false;
201+
mergeNode = true;
202+
last = null; // both left and right are set of the conditional node
203+
}
204+
} // end of while loop
205+
206+
// To handle the case when last statements are part of some condition statements
207+
node = new graph();
208+
node.start = lastConditionPoint;
209+
node.end = counter;
210+
node.index = ++number;
211+
nodes.add(node);
212+
if(!mergeNode) // alternateNode = true, both outgoing edges hasn't been taken care of
213+
{
214+
last.adjacents[1] = new graph();
215+
last.adjacents[1] = node;
216+
}
217+
else // if alternateNode is present, then this is the merge node
218+
{
219+
// set the link of the nodes inside the conditional statements.
220+
left.adjacents[0] = node;
221+
right.adjacents[0] = node;
222+
}
223+
}
224+
catch(Exception e)
225+
{
226+
e.printStackTrace();
227+
System.out.println(e.toString());
228+
}
229+
}while(false); // run only once :)
230+
print_Nodes_Lines();
231+
print_Graph_Matrix();
232+
}
233+
}
234+
235+
236+
237+
238+
239+

0 commit comments

Comments
 (0)