Data Flow Testing and Program Slicing
Data Flow Testing and Program Slicing
program slicing
Data flow testing
• Data flow testing helps catch different kinds of anomalies in the code. These
anomalies include:
1. input(x)
2. if(x>5)
3. z = x + 10
4. else
5. z=x-5
6. print("Value of Z: ", z)
• In the above piece of code, if the value of x entered by the user is greater than 5,
then the order of execution of statements would be:
• 1, 2, 3, 6
• If the value entered by the user in line 1 is less than or equal to 5, the order of
execution of statements would be:
• 1, 4, 5, 6
• Hence, the control flow graph of the above piece of code will be:
• Using the above control flow graph and code, we can
deduce the table below. This table mentions the node at
which the variable was declared and the nodes at which
it was used:
Variable Name
Defined At Used At
x 1 2
z 3, 5 6
• We can use the above table to ensure that no anomaly
occurs in the code by ensuring multiple tests. E.g., each
variable is declared before it is used.
Making associations
• (line number where the variable is declared, line number where the variable is
used, name of the variable)
• For example, (1, 3, x) would mean that the variable ‘x’ is defined on line 1 and used
on line 3.
• Now consider the following pseudo-code:
1. input(x)
2. if(x>5)
3. z = x + 10
4. else
5. z=x-5
6. print("Value of Z: ", z)
• For the above snippet of pseudo-code, we will make the following
associations:
• predicate use: the use of a variable is called p-use. Its value is used to
decide the flow of the program, e.g., line 2.
• computational use: the use of a variable is called c-use when its value is
used compute another variable or the output, e.g., line 3.
• After the associations are made, these associations can be divided into these groups:
• The statements and variables which are found to be extra are removed from the code.
Program slicing
• Slicing or program slicing is a technique used in software testing
which takes a slice or a group of program statements in the program
for testing particular test conditions or cases that may affect a value
at a particular point of interest. It can also be used for the purpose of
debugging in order to find the bugs more easily and quickly.
• Slicing techniques were originally defined by Mark
Weiser and they were only static in nature at that time.
Afterward, Bogdan Korel and Janusz Laski introduced
dynamic slicing, which can work for a particular
execution of the program.
It is of 2 types: static slicing and dynamic slicing, these
are explained as following below:
• 1. Static slicing:
• A static slice of a program contains all statements that may affect the
value of a variable at any point for any arbitrary execution of the
program.
• Static slices are generally larger.
• It considers every possible execution of the program.
• 2. Dynamic slicing:
• A dynamic slice of a program contains all the statements that actually
affect the value of a variable at any point for a particular execution of
the program.
• Dynamic slices are generally smaller.
• Considers only a particular execution of the program.
Code example
• int z = 10;
• int n;
• cin >> n;
• int sum = 0;
• if (n > 10)
• sum = sum + n;
• else
• sum = sum - n;
• cout << "Hey";
• Static slice for the variable sum:
int n;
• cin >> n;
• int sum = 0;
• if (n > 10)
• sum = sum + n;
• else
• sum = sum - n;
• Dynamic slice for the variable sum when n = 22;
int n;
• cin >> n;
• int sum = 0;
• if (n > 10)
• sum = sum + n;
• As it can be observed in the above example the static slice takes all
the possible execution (in this case it is 2) of the program which may
affect the value of the variable sum. Whereas in the case of dynamic
slicing, it considers only a particular execution (when n = 22) of the
program which actually affects the value of the variable sum.
• Hence, the dynamic slice is always smaller than a static slice.