Chapter - Four Structural (White Box) Testing Part II
Chapter - Four Structural (White Box) Testing Part II
2
Basic Idea
4
General Idea
Data flow testing can be performed at two conceptual levels.
– Static data flow testing
6
Data Flow Anomaly
Type 1: Defined and then defined again (Example 1 previous slide)
• The first statement has a fault -- the intended one might be: w = f1(y).
• The second statement has a fault – the intended one might be: v = f2(z).
– A third kind of data flow anomaly is to define a variable and then to undefined it
without using it in any subsequent computation.
– Example: Consider x = f(x, y). in which a new value is assigned to the variable x.
– If the value of x is not used in any subsequent computation, then we should be
suspicious of the computation represented by x = f (x, y). Hence, this form of
anomaly is called “defined but not referenced.
8
Data Flow Anomaly…
The concept of a state-transition diagram is used to model a program
variable to identify data flow anomaly.
Components of the state-transition diagrams
The states
U: Undefined state: meaning that just a memory location has been
allocated to the variable but no value has yet been assigned.
D: Defined but not referenced
R: Defined and referenced: The variable remains in the R state as
long as the programmer keeps referencing the value of the variable.
If the programmer assigns a new value to the variable, the variable
moves back to the D state.
A: Abnormal, a programming anomaly has occurred.
The actions
d: define the variable, a process of assigning a value to the variable
r: reference (or, read) read the value of the variable
u: undefine the variable, the programmer can take an action to the
9
variable
Data Flow Anomaly
– One should not feel confident that a variable has been assigned the correct value, if
no test case causes the execution of a path from the point of assignment to a point
where the value is used.
Note
• Assignment of correct value means whether or not a value has been correctly
generated.
• Use of a variable means
–If new values of the same variable or other variables are generated.
–If the variable is used in a conditional statement to alter the flow of control.
The above motivation indicates that certain kinds of paths are executed in data flow
12 testing.
Overview of Dynamic Data Flow Testing…
13
Data Flow Graph
– A data flow graph is drawn with the objective of identifying data definitions and
their uses.
– Each occurrence of a data variable (The life cycle of data in programming code) is
classified as follows:
– Definition: it includes defining, creation and initialization of data variables and the
allocation of the memory to its data object.
– Example :
– Usage: This occurs when the value is fetched from the memory location of the variable. There are
two forms of uses of a variable.
• Computation use (c-use): This directly affects the computation being performed. In a c-
use, a potentially new value of another variable or of the same variable is produced.
– Example: x = 2*y; /* y has been used to compute a value of x. */
• Predicate use (p-use): This refers to the use of a variable in a predicate controlling the
flow of execution.
– Example: if (y > 100) { … } /* y has been used in a condition. */
15
Data Flow Graph
• A data flow graph is a directed graph constructed as follows.
– A sequence of definitions and c-uses is associated with each node of
the graph.
– A set of p-uses is associated with each edge of the graph.
– The entry node has a definition of each edge parameter and each
nonlocal variable used in the program.
– The exit node has an un-definition of each local variable.
16
Data Flow Graph
Example code: R e t u r n Av e r a g e ( ) fro m thi s c ha p te r pa r t I
public static double ReturnAverage (int value[], int AS, int MIN, int MAX)
{
/* Function: ReturnAverage Computes the average of all those numbers in the input array in the
positive range [MIN, MAX]. The maximum size of the array is AS. But, the array size could be
smaller than AS in which case the end of input is represented by -999. */
int i, ti, tv, sum;
double av;
i = 0; ti = 0; tv = 0; sum = 0;
while (ti < AS && value[i] ! = -999)
{
ti++;
if (value[i] > = MIN && value[i] < = MAX)
{
tv++;
sum = sum + value[i];
}
i++;
}
if (tv > 0)
av = (double) sum/tv;
else
av = (double) -999;
return (av);
}
17 Figure 3: A function to compute the average of selected
integers in an array.
Data Flow Graph
– Example: (2 – 3 – 4 – 6 – 3
– 4 – 6 – 3 – 4 – 5) is a def-
clear path w.r.t. tv in Fig. 4.
19 – Example: (2 – 3 – 4 – 5) and (2 – 3 – 4 – 6) are def-clear paths w.r.t. variable tv
from node 2 to 5 and from node 2 to 6, respectively, in Fig. 4.
Data Flow Terms
• Global definition: A node i has a global definition of variable x if node i has a
definition of x and there is a def-clear path w.r.t. x from node i to some
✓node containing a global c-use, or
✓edge containing a p-use of variable x.
TA B L E 1 D e f ( ) a n d c-u s e ( ) S e t s of
N o d e s in Figure 4 TABLE 2 Predicates and p-use() Set of Edges in
Figure 4
20
Data Flow Terms
Simple path: A simple path is a path in which all nodes, except possibly the
first and the last, are distinct.
– Example: Paths (2 – 3 – 4 – 5) and (3 – 4 – 6 – 3) are simple paths.
Loop-free paths: A loop-free path is a path in which all nodes are distinct.
Complete path: A complete path is a path from the entry node to the exit
node.
21
Data Flow Terms
• Definition- u s e p a t h (Du-path): A path (n1 – n2 – … – nj – nk) is a du-
path w.r.t. variable x if node n1 has a global definition of x and either
22
Data Flow Testing Criteria
• Seven data flow testing criteria
– All-defs
– All-c-uses
– All-p-uses
– All-p-uses/some-c-uses
– All-c-uses/some-p-uses
– All-uses
– All-du-paths
23
Data Flow Testing Criteria
1. All-defs
– For each variable x and each node i, such that x has a global
definition in node i, select a complete path which includes a def-
clear path from node i to
• node j having a global c-use of x, or
• edge (j, k) having a p-use of x.
node i, select complete paths which include def-clear paths from node i to
all nodes j such that there is a global c-use of x in j.
– Example (partial): Consider variable ti, which has a global definition in 2
node i, select complete paths which include def-clear paths from node i to all
edges (j, k) such that there is a p-use of x on (j, k).
– Example (partial): Consider variable tv, which has a global definition in 2
and p-uses on edges (7, 8) and (7, 9). From node 2, there are def-clear paths to
(7, 8) and (7, 9), namely (2 –3 – 7 – 8) and (2 – 3 – 7 – 9). The two complete
paths are: (1 – 2 – 3 – 7 – 8 – 10) and (1 – 2 –3 – 7 – 9 – 10).
25
Data Flow Testing Criteria
4. All-p-uses/some-c-uses
– Some-c-uses: For each variable x and each node i, such that x has a global
definition in node i, select complete paths which include def-clear paths from
node i to some nodes j such that there is a global c-use of x in j.
– Example (partial): Consider variable i, which has a global definition in 2.
There is no p-use of i. Corresponding to the global definition of i in 2, there is a
global c-use of i in 6. The def-clear path from node 2 to 6 is (2 – 3 – 4 – 5 – 6).
A complete path that includes the above def-clear path is (1 –2 – 3 – 4 – 5 – 6 –
3-7 – 9 – 10).
26
Data Flow Testing Criteria
5. All-c-uses/some-p-uses
– This criterion is identical to the all-c-uses criterion except when a variable x has no c-use. If
x has no global c-use, then this criterion reduces to the some-p-uses criterion.
– Some-p-uses: For each variable x and each node i, such that x has a global definition in
node i, select complete paths which include def-clear paths from node i to some edges (j, k)
such that there is a p-use of x on (j, k).
27
Data Flow Testing Criteria
6. All-uses
This criterion is the conjunction to the all-p-uses criterion and the all-c-
uses criterion.
7. All-du-paths
• For each variable x and for each node i, such that x has a global definition
in node i, select complete paths which include all du-paths from node i
– To all nodes j such that there is a global c-use of x in j, and
– To all edges (j, k) such that there is a p-use of x on (j, k).
28
Comparison of Data Flow Testing Criteria
29
Comparison of Data Flow Testing Criteria…
Figure 5: The relationship among DF (data flow) testing criteria [6] (©[1988] IEEE).
30
Feasible Paths and Test Selection Criteria
31
Comparison of Testing Techniques
Figure 6: The relationship among the FDF (feasible data flow) testing criteria [6] (©[1988]
IEEE).
32
Comparison of Testing Techniques…
33
Summary
D a t a flow is a readily identifiable concept in a program unit.
Data flow testing
– Static
– Dynamic
Static data flow analysis
– Data flow anomaly can occur due to programming errors.
– Three types of data flow anomalies
• (Type 1: dd), (Type 2: ur), (Type 3, du)
• Analyze the code to identify and remove data flow anomalies.
Dynamic data flow analysis
– Obtain a data flow graph from a program unit.
– Select paths based on DF criteria: all-defs, all-c-uses, all-p-uses, all-
uses, all-c-uses/some- p-uses, all-p-uses/some-c-uses, all-du-paths.
– The includes relationship is useful in comparing selection criteria.
34