Professional Documents
Culture Documents
Chapter 2.3
Graph Coverage for Source Code
http://www.cs.gmu.edu/~offutt/softwaretest/
Overview
if (x < y)
{ 1
return; x<y
} x >= y
return 2
print (x);
return;
print (x)
3 return
x = 0;
x=0 1
while (x < y) dummy node
{
2
y = f (x, y); x<y x >= y implicitly
x = x + 1; x=0 1
initializes loop
} 3 4
y =f (x,y)
x=x+1 2
x<y x >= y
for (x = 0; x < y; x++)
{ y = f (x, y) 3 5
y = f (x, y);
}
4 x=x+1
implicitly
increments loop
Introduction to Software Testing (Ch 2) © Ammann & Offutt 7
CFG : do Loop, break and continue
x = 0; x = 0;
do while (x < y) 1 x=0
{ {
y = f (x, y); y = f (x, y);
x = x + 1; if (y == 0) 2
} while (x < y); {
println (y) break;
3 y = f (x,y)
} else if (y <
y == 0
0)
{ 4 break
x=0 1 y = y*2;
continue; 5
} y<0
y = f (x, y) x = x + 1; y = y*2
2 x = x+1 6 continue
x >= y }
x<y
print (y); 7 x=x+1
3
print (y) 8
Introduction to Software Testing (Ch 2) © Ammann & Offutt 8
CFG : The Case (switch) Structure
If a break is omitted …
read ( c) ;
switch ( c )
{
case ‘N’: 1 read ( c );
y = 25; c == ‘N’
default
break; c == ‘Y’
case ‘Y’: y = 25; y = 50; y = 0;
y = 50; break; 2 break; 3 4 break;
break;
default:
y = 0; 5
break; print (y);
}
print (y);
try
{ 1 s = br.readLine()
IOException
s = br.readLine();
if (s.length() > 96)
throw new Exception 2 3
(“too long”); e.printStackTrace() len <= 96 len > 96
} catch IOException e) {
e.printStackTrace(); 4 5 throw
} catch Exception e) {
e.printStackTrace();
6 e.printStackTrace()
}
return (s);
7 return (s);
sum = 0;
for (int i = 0; i < length; i++)
{
sum += numbers [ i ];
}
med = numbers [ length / 2];
mean = sum / (double) length;
varsum = 0;
for (int i = 0; i < length; i++)
{
varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean));
}
var = varsum / ( length - 1.0 );
sd = Math.sqrt ( var );
sum = 0;
for (int i = 0; i < length; i++) 2 i=0
{
sum += numbers [ i ];
}
med = numbers [ length / 2]; i >=
mean = sum / (double) length; 3 length
varsum = 0; i < length
for (int i = 0; i < length; i++)
{ i++ 4
varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean)); 5
} i=0
var = varsum / ( length - 1.0 );
sd = Math.sqrt ( var );
Edge Coverage
2 TR Test Path
A. [ 1, 2 ] [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
B. [ 2, 3 ]
3
C. [ 3, 4 ]
4
D. [ 3, 5 ]
5 E. [ 4, 3 ]
F. [ 5, 6 ]
G. [ 6, 7 ]
6 H. [ 6, 8 ]
I. [ 7, 6 ]
7 8
Introduction to Software Testing (Ch 2) © Ammann & Offutt 13
Control Flow TRs and Test Paths – EPC
1 Edge-Pair Coverage
TR Test Paths
2 A. [ 1, 2, 3 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
B. [ 2, 3, 4 ] ii. [ 1, 2, 3, 5, 6, 8 ]
C. [ 2, 3, 5 ] iii. [ 1, 2, 3, 4, 3, 4, 3, 5, 6, 7,
3 D. [ 3, 4, 3 ] 6, 7, 6, 8 ]
E. [ 3, 5, 6 ]
4 F. [ 4, 3, 5 ] TP TRs toured sidetrips
5
G. [ 5, 6, 7 ] i A, B, D, E, F, G, I, J C, H
H. [ 5, 6, 8 ] ii A, C, E, H
6 I. [ 6, 7, 6 ] iii A, B, D, E, F, G, I, C, H
J, K, L
J. [ 7, 6, 8 ]
K. [ 4, 3, 4 ]
7 8 L. [ 7, 6, 7 ]
Introduction to Software Testing (Ch 2) © Ammann & Offutt 14
Control Flow TRs and Test Paths – PPC
Prime Path Coverage
1
TR Test Paths
A. [ 3, 4, 3 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
2 B. [ 4, 3, 4 ] ii. [ 1, 2, 3, 4, 3, 4, 3,
C. [ 7, 6, 7 ] 5, 6, 7, 6, 7, 6, 8 ]
D. [ 7, 6, 8 ] iii. [ 1, 2, 3, 4, 3, 5, 6, 8 ]
3 E. [ 6, 7, 6 ] iv. [ 1, 2, 3, 5, 6, 7, 6, 8 ]
F. [ 1, 2, 3, 4 ] v. [ 1, 2, 3, 5, 6, 8 ]
4
5 G. [ 4, 3, 5, 6, 7 ]
TP TRs toured sidetrips
H. [ 4, 3, 5, 6, 8 ]
i A, D, E, F, G H, I, J
I. [ 1, 2, 3, 5, 6, 7 ]
6 J. [ 1, 2, 3, 5, 6, 8 ] ii A, B, C, D, E, F, G, H, I, J
iii A, F, H J
iv D, E, F, I J
7 8 v J
Introduction to Software Testing (Ch 2) © Ammann & Offutt 15
Data Flow Coverage for Source
• def : a location where a value is stored into memory
– x appears on the left side of an assignment (x = 44;)
– x is an actual parameter in a call and the method changes its value
– x is a formal parameter of a method (implicit def when method starts)
– x is an input to a program
• use : a location where variable’s value is accessed
– x appears on the right side of an assignment
– x appears in a conditional test
– x is an actual parameter to a method
– x is an output of the program
– x is an output of a method in a return statement
• If a def and a use appear on the same node, then it is only a DU-
pair if the def occurs after the use and the node is in a loop
sum = 0.0;
for (int i = 0; i < length; i++)
{
sum += numbers [ i ];
}
med = numbers [ length / 2 ];
mean = sum / (double) length;
varsum = 0.o;
for (int i = 0; i < length; i++)
{
varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean));
}
var = varsum / ( length - 1 );
sd = Math.sqrt ( var );
2 i=0
3 i >= length
i < length
med = numbers [ length / 2 ]
4 5 mean = sum / (double) length
varsum = 0
sum += numbers [ i ]
i=0
i++
6 i >= length
i < length
var = varsum / ( length - 1.0 )
7 8 sd = Math.sqrt ( var )
varsum = … print (length, mean, med, var, sd)
i++
Introduction to Software Testing (Ch 2) © Ammann & Offutt 18
CFG for Stats – With Defs & Uses
1 def (1) = { numbers, sum, length }
2 def (2) = { i }
[ 1, 2, 3, 4 ] [ 4, 3, 4 ]
[ 1, 2, 3, 5 ] [ 4, 3, 5 ]
[ 1, 2, 3, 5, 6, 7 ] [ 5, 6, 7 ]
[ 1, 2, 3, 5, 6, 8 ] [ 5, 6, 8 ]
[ 2, 3, 4 ] [ 7, 6, 7 ]
[ 2, 3, 5 ] [ 7, 6, 8 ]