0% found this document useful (0 votes)
255 views30 pages

Path Coverage

The document discusses path coverage testing and cyclomatic complexity. It defines path coverage as representing the flow of execution from the start to exit of a method, with a method having 2^N possible paths for N decisions. Cyclomatic complexity is a quantitative measure of logical complexity, calculated using edges, nodes and predicate nodes in a flow graph. Two examples applying path coverage testing to programs determining odd/even and largest of two numbers are provided, with flow graphs and source code.

Uploaded by

Mallika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views30 pages

Path Coverage

The document discusses path coverage testing and cyclomatic complexity. It defines path coverage as representing the flow of execution from the start to exit of a method, with a method having 2^N possible paths for N decisions. Cyclomatic complexity is a quantitative measure of logical complexity, calculated using edges, nodes and predicate nodes in a flow graph. Two examples applying path coverage testing to programs determining odd/even and largest of two numbers are provided, with flow graphs and source code.

Uploaded by

Mallika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5.

PATH COVERAGE

PATH COVERAGE TESTING

 To implement the path coverage testing using various rule based approach.

PATH COVERAGE

 A path represents the flow of execution from the start of a method to its exit

 A method with „N‟ decisions (branches) has 2^N possible paths

CYCLOMATIC COMPLEXITY

 It gives a quantitative measures of the logical complexity

 The cyclomatic complexity is calculated using three ways

1. V(G) = E-N+2

Where

Edges  Number of edges of the flow graph

Nodes Number of nodes of the flow graph

2. V(G) = P+1

Where,

P  Predicate Nodes in the flow graph

3. V(G) = C+1

Where,

C  Closed region in the flow graph

1
TERMS USED IN CYCLOMATIC COMPLEXITY

1. NODES

 It represents entries, exists, decisions and each statement of the program

2. EDGES

 It represents branching and non-branching links between nodes in the flow graph

3. PREDICATE NODES

 Containing a condition of nodes in the flow graph

4. REGION

 Areas bounded by edges and nodes called regions.

FLOW GRAPH NOTATION

SEQUENCE SELECTION: IF

WHILE SWICTH CASE

2
POINTS IN FLOWGRAPH

 Arrows called edges represent flow of control

 Circles called nodes represent one or more actions

 Areas bounded by edges and nodes called regions

 A predicate node is a node containing a one or more conditions

RULE 1: simple if-else

if
if (marks>35)
return “Pass”;
true false else
return “Fail”‟;

end-if

Fig: if-else

Total number of Nodes :4

Total number of Edges :4

RULE 2: nested single if-else

if
if ([Link](“TN”))
{
if If(code==1)
false
return “Chennai”;
else
true false return “Salem”;
}
else
end-if return “Puducherry”‟;

end-if

Fig: Nested-single-if-else

Total number of Nodes :7

Total number of Edges :8

3
RULE 3: single if-else-if ladder

if

true else-if

true false

end-if

Fig: Single-if-else-if-else

Total number of Nodes :6

Total number of Edges :7

e.g:

if((a>b)&&(a>c))

return a;

else if(b>c)

return b;

else

return c;

4
RULE 4: switch case labels-4

case

case 1 case 2 case 3 case 4 default

end-case

Fig: Switch case labels 4

Total number of Nodes :7

Total number of Edges : 10

RULE 5: for or while loop with only true statements

While or for

true state

end-while

Fig: for loop / while loop

Total number of Nodes :3

Total number of Edges :3

5
RULE 6: for / while loop with one if-else statement

for

if

true false

end-if

end-while

Fig: for loop / while loop

Total number of Nodes :6

Total number of Edges :7

6
I. EXAMPLE OF PATH COVERGAE TESTING-SELECTION STATEMENTS

(PATH COVERAGE FOR ODD EVEN PROGRAM)

1. FLOWGRAPH

Enter the Number 1

Read Number ‘a’ 2

if (a%2==0) 3
6

‘a’ is odd ‘a’ is even 4 5

6
end-if

CYCLOMATIC COMPLEXITY

Total No of Nodes (N) : 6


Total No of Edges (E) : 6
Total Number of Independent Paths
V(G)  E-N+2
 6-6+2
2

7
2. SOURCE CODE
package example;
import [Link].*;
public class OddEven {
DataInputStream ds=new DataInputStream([Link]);
int no,f=1;
int nodes=0,edges=0,CC=0;
public void findOddEven()throws Exception
{
[Link]("Enter a number : ");
// increment the node by 1 & edge by 1
++nodes;
++edges;
no=[Link]([Link]());
// increment the node by 1 & edge by 1
++nodes;
++edges;
if(no%2==0)
{
[Link]("Given Number is Even");
}
else
[Link]("Given Number is Odd");
nodes+=4;
edges+=4;
}
public void findPathOE()
{
// calculate path coverage result
CC=(edges-nodes)+2;
[Link]("===================================");
[Link]("\t\tPath Coverage Report");
[Link]("===================================");
[Link]("\t*** Cyclomatic Complexity ***");
[Link]("Total Edges in the program : "+edges);
[Link]("Total Nodes in the program : "+nodes);
[Link]("Total Independent Paths from Start to End : "+CC);
}
public static void main(String[] args)throws Exception {
OddEven obj=new OddEven();
[Link]();
[Link]();
}
}

8
3. OUTPUT

9
II. EXAMPLE OF PATH COVERGAE TESTING-SELECTION STATEMENTS

(PATH COVERAGE FOR BIGGEST OF TWO NUMBERS)

1. FLOWGRAPH

Enter 1st Number 1

Read Number ‘a’ 2

Enter 2nd Number 3

4
Read Number ‘b’
6

5
if (a>b) 6

‘a’ is big ‘b’ is big 6 7

end-if 8

CYCLOMATIC COMPLEXITY

Total No of Nodes (N) : 8


Total No of Edges (E) : 8
Total Number of Independent Paths V(G)  E-N+2
 8-8+2
2

10
2. SOURCE CODE
package example;
import [Link].*;
public class Big2_PathCoverage {
DataInputStream ds=new DataInputStream([Link]);
int a,b;
int nodes,edges,CC=0;
public void findBig2()throws Exception
{
[Link]("Enter a 1st number : ");
++nodes;
++edges;
a=[Link]([Link]());
++nodes;
++edges;
[Link]("Enter a 2nd number : ");
++nodes;
++edges;
b=[Link]([Link]());
++nodes;
++edges;
if(a>b)
{
[Link](a+" is Big Number");
}
else
{
[Link](b+" is Big Number");
}
// Single If-Else Statement: So increment the node by 4 & edge by 4
nodes+=4;
edges+=4;
}

public void findPathBig2()


{
CC=(edges-nodes)+2;
[Link]("===============================");
[Link]("\t\tPath Coverage Report");
[Link]("===============================");
[Link]("\t*** Cyclomatic Complexity ***");
[Link]("Total Edges in the program : "+edges);
[Link]("Total Nodes in the program : "+nodes);
[Link]("Total Independent Paths from Start to End : "+CC);

11
}
public static void main(String[] args)throws Exception {
// Object Creation
Big2_PathCoverage obj=new Big2_PathCoverage();
obj.findBig2();
obj.findPathBig2();
}
}

3. OUTPUT

12
III. EXAMPLE OF PATH COVERGAE TESTING-SELECTION STATEMENTS

(PATH COVERAGE FOR BIGGEST OF THREE NUMBERS)

1. FLOWGRAPH

Enter 1st Number


1

Read Number ‘a’ 2

Enter 2nd Number 3

Read Number ‘b’ 4

Enter 3rd Number 5

6
Read Number ‘c’ 6

7
if (a>b) && (a>c) 6

8 9
‘a’ is big else if b>c
6
6

10 11
‘b’ is big ‘c’ is big 16

end-if 12
6

13
CYCLOMATIC COMPLEXITY

Total No of Nodes (N) : 12


Total No of Edges (E) : 13
Total Number of Independent Paths V(G)  E-N+2
 13-12+2
3
2. SOURCE CODE
package example;
import [Link].*;
public class Big3_PathCoverage {
DataInputStream ds=new DataInputStream([Link]);
int a,b,c;
int nodes,edges,VG=0;
public void findBig3()throws Exception
{
[Link]("Enter a 1st number : ");
++nodes;
++edges;
a=[Link]([Link]());
++nodes;
++edges;
[Link]("Enter a 2nd number : ");
++nodes;
++edges;
b=[Link]([Link]());
++nodes;
++edges;
[Link]("Enter a 3rd number : ");
++nodes;
++edges;
c=[Link]([Link]());
++nodes;
++edges;
if((a>b)&&(a>c))
{
[Link](a+" is Big Number");
}
else if(b>c)
{
[Link](b+" is Big Number");
}
else
{

14
[Link](c+" is Big Number");
}
// Nested Single If-Else Statement: So increment the node by 6& edge by 7

nodes+=6;
edges+=7;
}
public void findPathBig3()
{
VG=(edges-nodes)+2;
[Link]("=================================");
[Link]("\t\tPath Coverage Report");
[Link]("=================================");
[Link]("\t*** Cyclomatic Complexity ***");
[Link]("Total Edges in the program : "+edges);
[Link]("Total Nodes in the program : "+nodes);
[Link]("Total Independent Paths from Start to End : "+VG);
}
public static void main(String[] args)throws Exception {
// Object Creation
Big3_PathCoverage obj=new Big3_PathCoverage();
obj.findBig3();
obj.findPathBig3();
}
}

15
3. OUTPUT

16
IV. EXAMPLE OF PATH COVERGAE TESTING-SELECTION STATEMENTS

(PATH COVERAGE FOR SIMPLE CALC)

1. FLOWGRAPH

Enter 1st Number 1

Read Number ‘a’ 2

Enter 2nd Number 3

Read Number ‘b’ 4

Enter the choice 5

Read Choice ‘ch’ 6


6

case (ch) 7
6

Add Sub Mul Div Default 8 9 10 11 12


(ch) (ch) (ch) (ch) 6 6 00 00 00
6 6 6

end-case 13
00
6

CYCLOMATIC COMPLEXITY

Total No of Nodes (N) : 13


Total No of Edges (E) : 16
Total Number of Independent Paths V(G)  E-N+2
 16-13+2
5

17
2. SOURCE CODE
package example;
import [Link].*;
public class ArthCase_PT {
DataInputStream ds=new DataInputStream([Link]);
int a,b,ch,r;
int nodes,edges,VG=0;
public void findCalc()throws Exception
{
[Link]("Enter the 1st number : ");
++nodes;
++edges;
a=[Link]([Link]());
++nodes;
++edges;
[Link]("Enter the 2nd number : ");
++nodes;
++edges;
b=[Link]([Link]());
++nodes;
++edges;
[Link]("Enter ur choice between 1-4 : ");
++nodes;
++edges;
ch=[Link]([Link]());
++nodes;
++edges;
switch(ch)
{
case 1:
r=a+b;
[Link]("Add : "+r);
break;

case 2:
r=a-b;
[Link]("Sub : "+r);
break;

case 3:
r=a*b;
[Link]("Mul : "+r);
break;
case 4:

18
r=a/b;
[Link]("Div : "+r);
break;
default:
[Link]("Invalid choice!");
break;
}
// Switch Case 4 Labels: So increment the node by 7 & edge by 10
nodes+=7;
edges+=10;
}
public void findPathCalc()
{
VG=(edges-nodes)+2;
[Link]("============================");
[Link]("\t\tPath Coverage Report");
[Link]("============================");
[Link]("\t*** Cyclomatic Complexity ***");
[Link]("Total Edges in the program : "+edges);
[Link]("Total Nodes in the program : "+nodes);
[Link]("Total Independent Paths from Start to End : "+VG);
}
public static void main(String[] args)throws Exception {
// Object Creation
ArthCase_PT obj=new ArthCase_PT();
[Link]();
[Link]();
}
}

19
3. OUTPUT
3.1 ADD RESULT

3.2 MULTIPLICATION RESULT

20
V. EXAMPLE OF PATH COVERGAE TESTING-LOOPING STATEMENTS

(PATH COVERAGE FOR FACTORIAL PROGRAM)

1. FLOWGRAPH

1. Enter a number ‘N’


1

2. Read ‘N’ 2

3. for loop i<n 3

4. True Statements: f*=(i+1) 4

5. Print Result 5

2. SOURCE CODE
package example;
import [Link].*;
public class JFactorial {
DataInputStream ds=new DataInputStream([Link]);
int n,f=1;
int nodes=0,edges=0,CC=0;
public void findFact()throws Exception
{
[Link]("Enter a number : ");

21
// increment the node by 1 & edge by 1
++nodes;
++edges;
n=[Link]([Link]());
// increment the node by 1 & edge by 1
++nodes;
++edges;
for(int i=0;i<n;i++)
{
f=f*(i+1);
}
[Link]("Factorial of "+n+ " is --> "+f);
// for loop with true statement only (no branches): so increment the node by 3 & edge by 3
edges+=3;
nodes+=3;
}
public void findPathCoverage()
{
CC=(edges-nodes)+2;
[Link]("==================================");
[Link]("\t\tPath Coverage Report");
[Link]("=================================");
[Link]("\t*** Cyclomatic Complexity ***");
[Link]("Total Edges in the program : "+edges);
[Link]("Total Nodes in the program : "+nodes);
[Link]("Total Independent Paths from Start to End : "+CC);
}
public static void main(String[] args)throws Exception
{
JFactorial obj=new JFactorial();
[Link]();
[Link]();
}
}

22
3. OUTPUT

23
VI. EXAMPLE OF PATH COVERGAE TESTING-LOOP STATEMENTS

(PATH COVERAGE FOR ‘REVERSE OF A NUMBER’)

1. FLOWGRAPH

Enter the Number 1

Read Number ‘n’ 2

While i<n 3

rem=n%10
rs=(rs*10)+rem 4
n=n/10

end-while 5
6

Print result 6
6

CYCLOMATIC COMPLEXITY

Total No of Nodes (N) : 6


Total No of Edges (E) : 6
Total Number of Independent Paths V(G)  E-N+2
 6-6+2
2

24
2. SOURCE CODE
package example;
import [Link].*;
public class JReverseNumber_PT {
DataInputStream ds=new DataInputStream([Link]);
int n,rem,res,d;
int nodes,edges,VG=0;
public void findREV()throws Exception
{
[Link]("Enter the number : ");
++nodes;
++edges;
d=n=[Link]([Link]());
++nodes;
++edges;
while(n!=0)
{
rem=n%10;
res=(res*10)+rem;
n=n/10;
}
// While Loop with only true statements: so increment nodes by 3 & edges by 3
nodes+=3;
edges+=3;
[Link]("Reverse Number of "+d+" is :"+res);
++nodes;
++edges;
}
public void findPathREV()
{
VG=(edges-nodes)+2;
[Link]("=============================”);
[Link]("\t\tPath Coverage Report");
[Link]("=============================");
[Link]("\t*** Cyclomatic Complexity ***");
[Link]("Total Edges in the program : "+edges);
[Link]("Total Nodes in the program : "+nodes);
[Link]("Total Independent Paths from Start to End : "+VG);
}
public static void main(String[] args)throws Exception {
// Object Creation
JReverseNumber_PT obj=new JReverseNumber_PT();
[Link]();
[Link]();

25
}
}

3. OUTPUT

26
VII. EXAMPLE OF PATH COVERGAE TESTING-LOOP STATEMENTS

(PATH COVERAGE FOR ‘VOWELS COUNTING’)

1. FLOWGRAPH

Read Name ‘str’ 1

for i<[Link] 2

if [Link](i)==’a’
vc++ 3
else
nc++

4 5
vc++ nc++

6
end-if

end-while 7
6

Print result 8
6
CYCLOMATIC COMPLEXITY

Total No of Nodes (N) : 8


Total No of Edges (E) : 9
Total Number of Independent Paths V(G)  E-N+2
 9-8+2
3

27
2. SOURCE CODE
package example;
public class JVowelsCounting extends [Link] {
int nodes=0,edges=0,CC=0;
int vc,nc;
// AUTOMATED CODE
public JVowelsCounting() {
initComponents();
}
// SUBMIT BUTTON CODE
private void b1ActionPerformed([Link] evt) {
String str=[Link]().toLowerCase();
++nodes;
++edges;
for(int i=0;i<[Link]();i++)
{

if(([Link](i)=='a')||([Link](i)=='e')||([Link](i)=='i')||([Link](i)=='o')||([Link](i)=='u'))
{
vc++;
}
else
nc++;
}
// for loop with if-else-statement, so increment node by 6 & edge by 7
nodes+=6;
edges+=7;
[Link]("Total Vowels : "+vc+"\n"+"Total Non Vowels :"+nc);
nodes+=6;
edges+=7;
findPathCov();
nodes=edges=vc=nc=0;
}
public void findPathCov()
{
CC=(edges-nodes)+2;
String result="Total No. of Nodes \t: "+nodes+"\n"+"Total No. of Edges \t: "+edges+"\n"+"Total
No. of Independent Paths \t: "+CC;
[Link](result);
}
// CLEAR BUTTON CODE
private void b2ActionPerformed([Link] evt) {
[Link]("");
[Link]("");

28
[Link]("");
nodes=edges=vc=nc=0;
}
// EXIT BUTTON CODE
private void b7ActionPerformed([Link] evt) {
[Link](0);
}
// AUTOMATED CODE: (NO NEED)
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
try {
for ([Link] info :
[Link]()) {
if ("Nimbus".equals([Link]())) {
[Link]([Link]());
break;
}
}
} catch (ClassNotFoundException ex) {
}
/* Create and display the form */
[Link](new Runnable() {
public void run() {
new JVowelsCounting().setVisible(true);
}
});
}

29
3. OUTPUT

30

You might also like