Introduction To Software Testing Graph Coverage For Source Code
Introduction To Software Testing Graph Coverage For Source Code
Overview
The most common application of graph criteria is to
program source Graph : Usually the control flow graph (CFG) Node coverage : Execute every statement Edge coverage : Execute every branch Loops : Looping structures such as for loops, while loops, etc. Data flow coverage : Augment the CFG
defs are statements that assign values to variables uses are statements that use variables
Edges : Transfers of control CFGs are sometimes annotated with extra information branch predicates defs uses Rules for translating statements into graphs
1
x<y y=0 x=x+1 x >= y
2 4
x=y
if (x < y) { y = 0; x = x + 1; }
1
x<y
y=0 x=x+1
x >= y
3
Introduction to Software Testing (Ch 2) Ammann & Offutt 4
1
x<y return
x >= y
Loops
Loops require extra nodes to be added Nodes that do not represent statements or basic blocks
1 dummy node
2
x<y x >= y
3
y =f (x,y) x=x+1
x=0
2
x<y x >= y
y = f (x, y)
x=x+1
x=0
2 3
y = f (x,y) y == 0
4 5
y<0
break
x=0
1
y = f (x, y) x = x+1 x<y
2
x >= y
y = y*2 continue
3 8
Ammann & Offutt
x=x+1
1
c == N
read ( c ); default
c == Y y = 25; break;
y = 50; break;
y = 0; break;
5
print (y);
10
("length: " + length); ("mean: " + mean); ("median: " + med); ("variance: " + var); ("standard deviation: " + sd);
Ammann & Offutt Ammann & Offutt 11
varsum = 0; i < length for (int i = 0; i < length; i++) i++ 4 { varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean)); } var = varsum / ( length - 1.0 ); sd = Math.sqrt ( var );
i=0
("length: " + length); ("mean: " + mean); ("median: " + med); ("variance: " + var); ("standard deviation: " + sd);
Ammann & Offutt Ammann & Offutt
Edge Coverage
2
3 4 5
TR Test Path A. [ 1, 2 ] [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] B. [ 2, 3 ] C. [ 3, 4 ] D. [ 3, 5 ] E. [ 4, 3 ] F. [ 5, 6 ] G. [ 6, 7 ] H. [ 6, 8 ] I. [ 7, 6 ]
8
Ammann & Offutt 13
7
Introduction to Software Testing (Ch 2)
Edge-Pair Coverage
TR A. [ 1, 2, 3 ] B. [ 2, 3, 4 ] C. [ 2, 3, 5 ] D. [ 3, 4, 3 ] E. [ 3, 5, 6 ] F. [ 4, 3, 5 ] G. [ 5, 6, 7 ] H. [ 5, 6, 8 ] I. [ 6, 7, 6 ] J. [ 7, 6, 8 ] K. [ 4, 3, 4 ] L. [ 7, 6, 7 ] Test Paths i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] ii. [ 1, 2, 3, 5, 6, 8 ] iii. [ 1, 2, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 8 ]
TP i ii iii TRs toured A, B, D, E, F, G, I, J sidetrips C, H C, H
3 4 5
A, C, E, H
A, B, D, E, F, G, I, J, K, L
7
Introduction to Software Testing (Ch 2)
14
3 4 5
TR A. [ 3, 4, 3 ] B. [ 4, 3, 4 ] C. [ 7, 6, 7 ] D. [ 7, 6, 8 ] E. [ 6, 7, 6 ] F. [ 1, 2, 3, 4 ] G. [ 4, 3, 5, 6, 7 ] H. [ 4, 3, 5, 6, 8 ] I. [ 1, 2, 3, 5, 6, 7 ] J. [ 1, 2, 3, 5, 6, 8 ]
TRs toured
A, D, E, F, G A, B, C, D, E, F, G, A, F, H
sidetrips H, I, J H, I, J J J
iii
iv
D, E, I
J
7
Introduction to Software Testing (Ch 2)
v
Ammann & Offutt
15
pair if the def occurs after the use and the node is in a loop
Introduction to Software Testing (Ch 2) Ammann & Offutt 16
("length: " + length); ("mean: " + mean); ("median: " + med); ("variance: " + var); ("standard deviation: " + sd);
i=0
3
i < length
i >= length med = numbers [ length / 2 ] mean = sum / (double) length varsum = 0 i=0 i >= length var = varsum / ( length - 1.0 ) sd = Math.sqrt ( var ) print (length, mean, med, var, sd)
Ammann & Offutt 18
4
sum += numbers [ i ] i++
6
i < length varsum = i++
Introduction to Software Testing (Ch 2)
def (2) = { i }
3
use (3, 4) = { i, length }
4
def (4) = { sum, i } use (4) = { sum, numbers, i }
def (5) = { med, mean, varsum, i } use (5) = { numbers, length, sum }
6
use (6, 7) = { i, length }
use (6, 8) = { i, length } def (8) = { var, sd } use (8) = { varsum, length, mean, med, var, sd }
Ammann & Offutt 19
Def
{ numbers, sum, length } {i}
Use
{ numbers }
Edge
(1, 2) (2, 3)
(3, 4) (4, 3) (3, 5) (5, 6) (6, 7) (7, 6) (6, 8)
Use
{ var, sd }
20
(5, 8) (8, 8) defs after use in loop, these are valid DU pairs (8, 8) (5, 7) (5, 8) No def-clear path (1, 4) (1, 5) (4, 4) (4, 5) different scope for i (5, 7) (5, 8) (7, 7) (7, 8) (2, 4) (2, (3,4)) (2, (3,5)) (2, 7) (2, (6,7)) (2, (6,8)) (4, 4) (4, (3,4)) (4, (3,5)) (4, 7) (4, (6,7)) (4, (6,8)) (5, 7) (5, (6,7)) (5, (6,8)) No path through graph from (7, 7) (7, (6,7)) (7, (6,8)) nodes 5 and 7 to 4 or 3
Ammann & Offutt 21
DU Pairs (5, 7) (5, 8) (5, 7) (5, 8) (7, 7) (7, 8) (2, 4) (2, (3,4)) (2, (3,5)) (4, 4) (4, (3,4)) (4, (3,5)) (5, 7) (5, (6,7)) (5, (6,8)) (7, 7) (7, (6,7)) (7, (6,8))
DU Paths [ 5, 6, 7 ] [ 5, 6, 8 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] [ 2, 3, 4 ] [ 2, 3, 4 ] [ 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 7 ] [ 7, 6, 8 ]
22
length
23
Test Case : numbers = (2, 10, 15) ; length = 3 Test Path : [ 1, 2, 3, 4, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 7, 6, 8 ] DU Paths covered (no sidetrips)
[ 4, 3, 4 ] [ 7, 6, 7 ] The two stars that require at least two iterations of a loop
Other DU paths require arrays with length 0 to skip loops But the method fails with index out of bounds exception
med = numbers [length / 2]; A fault was found
Ammann & Offutt 24
Summary
Applying the graph test criteria to control flow graphs is
relatively straightforward
Most of the developmental research work was done with CFGs
25