0% found this document useful (0 votes)
1 views25 pages

13 SourceCode

Chapter 7.3 discusses graph coverage for source code in software testing, focusing on control flow graphs (CFGs) that represent program execution paths. It covers various coverage criteria such as node and edge coverage, as well as data flow coverage, explaining how to model control structures and handle loops, conditionals, and exceptions. The chapter includes examples and illustrations to demonstrate the application of these concepts in testing software effectively.

Uploaded by

bhpsecondbrain
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)
1 views25 pages

13 SourceCode

Chapter 7.3 discusses graph coverage for source code in software testing, focusing on control flow graphs (CFGs) that represent program execution paths. It covers various coverage criteria such as node and edge coverage, as well as data flow coverage, explaining how to model control structures and handle loops, conditionals, and exceptions. The chapter includes examples and illustrations to demonstrate the application of these concepts in testing software effectively.

Uploaded by

bhpsecondbrain
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

Introduction to Software

Testing
Chapter 7.3
Graph Coverage for Source
Code

Paul Ammann & Jeff Offutt

[Link]

Update March 2016


Overview

• A 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

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 2


Control Flow Graphs
• A CFG models all executions of a method by describing
control structures
• Nodes : Statements or sequences of statements (basic
blocks)
• Edges : Transfers of control
• Basic Block : A sequence of statements such that if the first
statement is executed, all statements will be (no branches)
• CFGs are sometimes annotated with extra information
– branch predicates
– defs
– uses
• Rules for translating statements into graphs …
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 3
CFG : The if Statement

if (x < y)
{
y = 0; 1
x = x + 1; x<y x >= y
} y=0
x=x+1 2 3 x=y
else
{
x = y; 4
}
if (x < y) 1
{ x<y
y = 0; y=0 x >= y
x=x+1 2
x = x + 1;
}
3

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 4


CFG : The if-Return Statement

if (x < y)
{ 1
return; x<y
} x >= y
print (x);
return 2
return;
print (x)
3
return

No edge from node 2 to 3.


The return nodes must be distinct.

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 5


Loops

• Loops require “extra” nodes to be added

• Nodes that do not represent statements or basic blocks

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 6


CFG : while and for Loops

x=0 1
x = 0;
while (x < y) dummy node
{
2
y = f (x, y); x<y x >= y implicitly
1
x = x + 1; initializes loop x = 0
} 3 4
return (x); 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);
}
return (x); 4 x=x+
1

implicitly
increments loop
Introduction to Software Testing, Edition 2 (Ch 7) © 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); {
return (y); break; 3 y =f(x,y)
} else if (y < 0) y == 0
{
y = y*2; 4 break
x=0 1 continue;
} 5
y<0
x = x + 1;
y = f (x, y) y = y*2
2 x = x+1
} 6 continue
x >= y return (y);
x<y
7 x=x+1
3
return (y) 8
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 8
CFG : The case (switch) Structure

read ( c) ;
switch ( c )
{
case ‘N’: 1 read ( c );
z = 25; c == ‘N’
case ‘Y’: c == ‘Y’ default
x = 50;
break; 2 3 4
z = 25; x = 0;
default: x = 50; break;
x = 0; break;
break; 5
} print (x);
print (x);

Cases without breaks fall


through to the next case
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 9
CFG : Exceptions (try-catch)
try
{
1 s = [Link]()
s = [Link]();
if ([Link]() > 96) IOException
throw new Exception
2 3
(“too long”); length <= 96
if ([Link]() == 0) [Link]()
length > 96
throw new Exception
throw 4 5
(“too short”); length != 0
} (catch IOException e) { length == 0
[Link]();
7
} (catch Exception e) { throw
[Link]();
}
6
return (s); [Link]()

8
return (s)
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 10
Example Control Flow – Stats
public static void computeStats (int [ ] numbers)
{
int length = [Link];
double med, var, sd, mean, sum, varsum;

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 = [Link] ( var );

[Link] ("length: " + length);


[Link] ("mean: " + mean);
[Link] ("median: " + med);
[Link] ("variance: " + var);
[Link] ("standard deviation: " + sd);
}
©
© Ammann
Ammann &
& Offutt
Offutt 11
Introduction to Software Testing, Edition 2 (Ch 7)
Control Flow Graph for Stats
public static void computeStats (int [ ] numbers)
{
int length = [Link]; 1
double med, var, sd, mean, sum, varsum;

sum = 0;
for (int i = 0; i < length; i++) 2 i=0
{
sum += numbers [ i ];
}
med = numbers [ length / 2]; i >= length
mean = sum / (double) length; 3
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 = [Link] ( var );
6
[Link] ("length: " + length);
[Link] ("mean: " + mean); i < length
[Link] ("median: " + med); i >= length
[Link] ("variance: " + var);
[Link] ("standard deviation: " + sd); 7 8
} i++
©
© Ammann
Ammann &
& Offutt
Offutt 12
Introduction to Software Testing, Edition 2 (Ch 7)
Control Flow TRs and Test Paths—EC
1

Edge Coverage
2 TR Test Path
A. [ 1, 2 ] [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
3 B. [ 2, 3 ]
C. [ 3, 4 ]
4 D. [ 3, 5 ]
5 E. [ 4, 3 ]
F. [ 5, 6 ]
6
G. [ 6, 7 ]
H. [ 6, 8 ]
I. [ 7, 6 ]
7 8
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 13
Control Flow TRs and Test Paths—EPC
Edge-Pair Coverage
1
TR Test Paths
A. [ 1, 2, 3 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
2 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,
D. [ 3, 4, 3 ] 6, 7, 6, 8 ]
3
E. [ 3, 5, 6 ]
TP TRs toured sidetrips
F. [ 4, 3, 5 ]
4 i A, B, D, E, F, G, I, J C, H
5 G. [ 5, 6, 7 ]
H. [ 5, 6, 8 ] ii A, C, E, H

I. [ 6, 7, 6 ] iii A, B, D, E, F, G, I, J, K, C, H
L
6 J. [ 7, 6, 8 ]
K. [ 4, 3, 4 ] TP iii makes TP i
L. [ 7, 6, 7 ] redundant. A minimal
7 8 set of TPs is cheaper.
Introduction to Software Testing, Edition 2 (Ch 7) © 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 G. [ 4, 3, 5, 6, 7 ]
5 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
TP ii makes iv D, E, F, I J
7 8 TP i redundant. v J
Introduction to Software Testing, Edition 2 (Ch 7) © 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
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 16
Example Data Flow – Stats
public static void computeStats (int [ ] numbers)
{
int length = [Link];
double med, var, sd, mean, sum, varsum;

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 = [Link] ( var );

[Link] ("length: " + length);


[Link] ("mean: " + mean);
[Link] ("median: " + med);
[Link] ("variance: " + var);
[Link] ("standard deviation: " + sd);
}
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 17
Control Flow Graph for Stats
( numbers )
1 sum = 0
length = [Link]

i=0 Annotate with the


2
statements …

3 i >= length

i < length
med = numbers [ length / 2
]
4 5 mean = sum / (double)
length
sum += numbers [ i
] varsum = 0
i++ i=0
6 i >= length
i < length
var = varsum / ( length - 1.0 )
7 8 sd = [Link] ( var )
varsum = … print (length, mean, med, var,
i++ sd)
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 18
CFG for Stats – With Defs & Uses
def (1) = { numbers, sum, length }
1 use (1) = { numbers}

Turn the
2 def (2) = { i } annotations into
def and use sets …

3 use (3, 5) = { i, length }


use (3, 4) = { i, length }
def (5) = { med, mean, varsum, i }
4 5 use (5) = { numbers, length, sum }
def (4) = { sum, i }
use (4) = { sum, numbers, i }
6 use (6, 8) = { i, length }
use (6, 7) = { i, length }
def (8) = { var, sd }
def (7) = { varsum, i } 7 8 use (8) = { varsum, length, mean,
use (7) = { varsum, numbers, i, mean } med, var, sd }
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 19
Defs and Uses Tables for Stats
Node Def Use Edge Use
1 { numbers, sum, { numbers } (1, 2)
length } (2, 3)
2 {i}
(3, 4) { i, length }
3
(4, 3)
4 { sum, i } { numbers, i, sum }
(3, 5) { i, length }
5 { med, mean, { numbers, length, sum }
varsum, i } (5, 6)
6 (6, 7) { i, length }
7 { varsum, i } { varsum, numbers, i, (7, 6)
mean } (6, 8) { i, length }
8 { var, sd } { varsum, length, var, mean,
med, var, sd }

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 20


DU Pairs for Stats
defs come before uses,
variable DU Pairs do not count as DU pairs
numbers (1, 4) (1, 5) (1, 7)
length (1, 5) (1, 8) (1, (3,4)) (1, (3,5)) (1, (6,7)) (1, (6,8))
med (5, 8)
var (8, 8) defs after use in loop,
sd (8, 8) these are valid DU pairs
mean (5, 7) (5, 8)
No def-clear path …
sum (1, 4) (1, 5) (4, 4) (4, 5)
different scope for i
varsum (5, 7) (5, 8) (7, 7) (7, 8)
i (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))
(7, 7) (7, (6,7)) (7, (6,8)) No path through graph
from nodes 5 and 7 to 4 or 3
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 21
DU Paths for Stats
variable DU Pairs DU Paths variable DU Pairs DU Paths
numbers (1, 4) [ 1, 2, 3, 4 ] mean (5, 7) [ 5, 6, 7 ]
(1, 5) [ 1, 2, 3, 5 ] (5, 8) [ 5, 6, 8 ]
(1, 7) [ 1, 2, 3, 5, 6, 7 ] varsum (5, 7) [ 5, 6, 7 ]
length (1, 5) [ 1, 2, 3, 5 ] (5, 8) [ 5, 6, 8 ]
(1, 8) [ 1, 2, 3, 5, 6, 8 ] (7, 7) [ 7, 6, 7 ]
(1, (3,4)) [ 1, 2, 3, 4 ] (7, 8) [ 7, 6, 8 ]
(1, (3,5)) [ 1, 2, 3, 5 ] i (2, 4) [ 2, 3, 4 ]
(1, (6,7)) [ 1, 2, 3, 5, 6, 7 ] (2, (3,4)) [ 2, 3, 4 ]
(1, (6,8)) [ 1, 2, 3, 5, 6, 8 ] (2, (3,5)) [ 2, 3, 5 ]
(4, 4) [ 4, 3, 4 ]
med (5, 8) [ 5, 6, 8 ] (4, (3,4)) [ 4, 3, 4 ]
var (8, 8) No path needed (4, (3,5)) [ 4, 3, 5 ]
sd (8, 8) No path needed (5, 7) [ 5, 6, 7 ]
(5, (6,7)) [ 5, 6, 7 ]
sum (1, 4) [ 1, 2, 3, 4 ] (5, (6,8)) [ 5, 6, 8 ]
(1, 5) [ 1, 2, 3, 5 ] (7, 7) [ 7, 6, 7 ]
(4, 4) [ 4, 3, 4 ] (7, (6,7)) [ 7, 6, 7 ]
(4, 5) [ 4, 3, 5 ] (7, (6,8)) [ 7, 6, 8 ]
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 22
DU Paths for Stats—No Duplicates
There are 38 DU paths for Stats, but only 12 unique
[ 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 ]

4 expect a loop not to be “entered”

6 require at least one iteration of a loop

2 require at least two iterations of a loop

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 23


Test Cases and Test Paths
Test Case : numbers = (44) ; length = 1
Test Path : [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
Additional DU Paths covered (no sidetrips)
[ 1, 2, 3, 4 ] [ 2, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 7, 6, 8 ]
The five stars that require at least one iteration of a loop

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
Introduction to Software Testing, Edition 2 (Ch 7) © 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

• A few subtle decisions must be made to translate control


structures into the graph

• Some tools will assign each statement to a unique node


– These slides and the book uses basic blocks
– Coverage is the same, although the bookkeeping will differ

Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 25

You might also like