DA-IICT
IT 415: Software Testing and Quality Analysis
White-Box Test Case Design
Data Flow Analysis
Saurabh Tiwari
DA-IICT
White-Box Test Case Design Techniques
Control Flow Testing Data Flow Testing
Statement coverage All p-use
Decision coverage All c-use
Condition coverage All d-use
Decision-Condition All uses
coverage
Multiple condition
coverage
Basis Path Testing
Loop testing
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
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 …
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
CFG : The if-Return Statement
if (x < y)
{ 1
return; x<y
} x >= y
return 2
print (x);
return;
print (x)
3 return
No edge from node 2 to 3.
The return nodes must be distinct.
Data Flow Analysis
• What is it?
– A form of static analysis based on the definition and usage of variables
• How it is performed?
– Analysis of data use
• The usage of data on paths through the program code is checked
• Use to detect data flow anomalies
– Unintended or unexpected sequence of operations on a variable
• What is an anomaly?
– An inconsistency that can lead to failure, but does not necessarily so
– May be flagged as a risk
8
Data Flow Analysis
• Examples of data flow anomalies
– Reading variables without previous initialization
– Not using the values of a variable at all
• The usage of every single variable is inspected
• Three types of usage or states of variables
– Defined (d) : the variable is assigned a value
– Reference (r) : the value of the variable is read and/or used
– Undefined (u) : the variable has no defined value
9
Data Flow Analysis
• Three types of data flow anomalies
– ur-anomaly : an undefined value (u) of a variable is read on a program
path (r)
– du-anomaly : the variable is assigned a value (d) that becomes
invalid/undefined (u) without having been used in the meantime
– dd-anomaly : the variable receives a value for the second time (d) and the
first value had not been used (d)
10
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
Example Data Flow – Stats
public static void computeStats (int [ ] numbers)
{
int length = numbers.length;
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.0;
for (int i = 0; i < length; i++)
{
varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean));
}
var = varsum / ( length - 1 );
sd = Math.sqrt ( var );
System.out.println ("length: " + length);
System.out.println ("mean: " + mean);
System.out.println ("median: " + med);
System.out.println ("variance: " + var);
System.out.println ("standard deviation: " + sd);
}
Control Flow Graph for Stats
( numbers )
1 sum = 0
length = numbers.length
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++
CFG for Stats – With Defs & Uses
1 def (1) = { numbers, sum, length }
2 def (2) = { i }
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 }
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 }
DU Pairs for Stats
variable DU Pairs defs come before uses, 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
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 ]
Questions?
Next Lecture….
Levels of Testing, Junit Testing Framework