Graphs Source Code
Graphs Source Code
Meenakshi D’Souza
Steps to be followed:
1 Modelling control flow in code as graphs.
if (x<y)
{
y:=0; x<y x>=y
x:=x+1;
} y:=0
x:=x+1 x:=y+1
else
{
x:=y+1;
} z:=x+1
z:=x+1;
CFG: If statement
if (x<y)
{ x<y
y:=0;
x:=x+1; y:=0
x>=y
} x:=x+1
z:=x+1;
z:=x+1
CFG: If statement with return
if (x<y)
{
return x<y
}
print(x); x>=y
return
return; print(x);
return;
read(c); read(c);
switch(c)
’N’ default
{
’Y’
case ’N’:z=25; z=25;
case ’Y’:{x=50;
break;}
default:{x=0; x=50; x=0;
break;
break;} break;
}
print(x);
print(x);
1 x=0;
x=0;
while(x<y)
{
y=f(x,y); 2
x=x+1; x<y x>=y
}
3 4
y=f(x,y);
x=x+1;
1 x=0
for(x=0;x<y;x++)
{
y=f(x,y); 2
} x<y x>=y
3 5
y=f(x,y)
4 x=x+1
x=0;
x=0;
do x<y
{
y=f(x,y); y=f(x,y);
x=x+1; x=x+1;
}while(x<y);
print(y);
x>=y
CFG: While loop with break and continue
x=0; x=0;
1
while(x<y)
{
y=f(x,y);
if(y==0) 2
{ y=f(x,y);
break; 3
}else if(y<0) y==0
{
y=y*2;
4 break;
continue;
}
x=x+1;
} 5
print(y); y<0
y=y*2;
6 continue;
7 x=x+1;
print(y); 8
CFG: Exceptions (try-catch)
try s=br.readLine();
{ IOException
s=br.readLine();
if(s.length()>96)
throw new Exception length<=96
("too long"); length>96
if(s.length()==0) e.printStackTrace();
throw new Exception throw
length!=0
("too short");
}(catch IOException e){ length==0
e.printStackTrace();
}(catch Exception e){
throw
e.getMessage();
}
return(s);
e.getMessage();
return(s);
Example program: Statistics
2 i=0
3
i>=length
i<length
i++ 4 5
i=0
6
i<length
i>=length
7 8
i++
Edge coverage for Statistics program
TR:
3
{[1,2],[2,3],[3,4],[4,3],[3,5],[5,6],[6,7],[7,6],
[7,8]}.
4 5
Test path: [1,2,3,4,3,5,6,7,6,8]
7 8
Edge pair coverage for Statistics program
1 TR:
A. [1,2,3]
2 B. [2,3,4], C. [2,3,5]
D. [3,4,3], E. [3,5,6]
3 F. [4,3,5], G. [4,3,4]
H. [5,6,7], I. [5,6,8]
4 5 J. [6,7,6]
K. [7,6,8], L. [7,6,7].
1
TR:
A. [3,4,3]
2 B. [4,3,4]
Test paths:
C. [7,6,7]
i. [1,2,3,4,3,5,6,7,6,8]
3 D. [7,6,8]
ii. [1,2,3,4,3,4,3,5,6,7,6,7,6,8]
E. [6,7,6]
iii. [1,2,3,4,3,5,6,8]
F. [1,2,3,4]
4 5 iv. [1,2,3,5,6,7,6,8]
G. [4,3,5,6,7]
v. [1,2,3,5,6,8]
H. [4,3,5,6,8]
6 I. [1,2,3,5,6,7]
J. [1,2,3,5,6,8]
7 8
Credits
Part of the material used in these slides are derived from the
presentations of the book Introduction to Software Testing, by
Paul Ammann and Jeff Offutt.