Lecture 11 Part 2
Lecture 11 Part 2
• A program unit accepts inputs, performs computations, assigns new values to variables, and returns
results.
• One can visualize of “flow” of data values from one statement to another.
• A data value produced in one statement is expected to be used later.
• Example
• Obtain a file pointer ……. use it later.
• If the later use is never verified, we do not know if the earlier assignment is acceptable.
• Two motivations of data flow testing
• The memory location for a variable is accessed in a “desirable” way.
• Verify the correctness of data values “defined” (i.e. generated) – observe that all the
“uses” of the value produce the desired results.
• Idea: A programmer can perform a number of tests on data values.
• These tests are collectively known as data flow testing.
2
• Data flow testing can be performed at two
conceptual levels.
• Static data flow testing
• Dynamic data flow testing
• Static data flow testing
• Identify potential defects, commonly
known as data flow anomaly.
The General • Analyze source code.
Idea • Do not execute code.
• Dynamic data flow testing
• Involves actual program execution.
• Bears similarity with control flow
testing.
• Identify paths to execute them.
• Paths are identified based on data
flow testing criteria.
3
• Anomaly: It is an abnormal way of doing
something.
• Example 1: The second definition of x
overrides the first.
• x = f1(y);
Data Flow • x = f2(z);
4
• Type 1: Defined and then defined again (Example 1 above)
• Interpretations of Example 1
• The first statement is redundant.
• 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).
• Note: It is for the programmer to make the desired
interpretation.
Data Flow • Type 2: Undefined but referenced
Anomaly • Example: x = x – y – w; /* w has not been defined by the
programmer. */
• Two interpretations
• The programmer made a mistake in using w.
• The programmer wants to use the compiler
assigned value of w.
• Type 3: Defined but not referenced
• Example: Consider x = f(x, y). If x is not used
subsequently, we have a Type 3 anomaly.
5
• A programmer
6
manipulates/uses variables
in several ways.
• Initialization, assignment,
Overview of Dynamic Data Flow
used in a condition. */
• A data flow graph is a
8
s
i
n
Data Flow Testing
• Advantages
• To find a variable that is used but never defined,
• To find a variable that is defined but never used,
• To find a variable that is defined multiple times before it is use,
• Deallocating a variable before it is used.
• Disadvantages
• Time consuming and costly process
• Requires knowledge of programming languages
Practice
Solution
Practice Question
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int n, reversed_number = 0, remainder,r;
5. cout << "Enter an integer: ";
6. cin >> n;
7. while(n != 0) {
8. remainder = n % 10;
9. reversed_number = reversed_number * 10 + remainder;
10. n /= 10;
11. }
12. cout << "Reversed Number = " << reversed_number;
13. return 0;
14.}