CONTROL FLOW GRAPH
4.3 CONTROL FLOW GRAPH
A CFG is a graphical representation of a program unit. Three symbols are used to construct a CFG, as shown in Figure 4.2. A rectangle represents a sequential
4.3 CONTROL FLOW GRAPH
Sequential computation
Decision point
Merge point
Figure 4.2 Symbols in a CFG.
computation. A maximal sequential computation can be represented either by a single rectangle or by many rectangles, each corresponding to one statement in the source code.
We label each computation and decision box with a unique integer. The two branches of a decision box are labeled with T and F to represent the true and false evaluations, respectively, of the condition within the box. We will not label a merge node, because one can easily identify the paths in a CFG even without explicitly considering the merge nodes. Moreover, not mentioning the merge nodes in a path will make a path description shorter.
We consider the openfiles() function shown in Figure 4.3 to illustrate the process of drawing a CFG. The function has three statements: an assignment state- ment int i = 0;, a conditional statement if(), and a return(i) statement. The reader may note that irrespective of the evaluation of the if(), the function performs the same action, namely, null. In Figure 4.4, we show a high-level representation of
FILE *fptr1, *fptr2, *fptr3; /* These are global variables. */ int openfiles(){
This function tries to open files "file1", "file2", and "file3" for read access, and returns the number of files successfully opened. The file pointers of the opened files are put in the global variables.
*/ int i = 0; if(
((( fptr1 = fopen("file1", "r")) != NULL) && (i++)
((( fptr2 = fopen("file2", "r")) != NULL) && (i++)
&& (0)) || ((( fptr3 = fopen("file3", "r")) != NULL) && (i++)) ); return(i);
Figure 4.3 Function to open three files.
92 CHAPTER 4 CONTROL FLOW TESTING
1 Entry point
i=0 2
if() T
3 Exit point
Figure 4.4 High-level CFG representation of openfiles(). return(i) The three nodes are numbered 1, 2, and 3.
the control flow in openfiles() with three nodes numbered 1, 2, and 3. The flow graph shows just two paths in openfiles().
A closer examination of the condition part of the if() statement reveals that there are not only Boolean and relational operators in the condition part, but also assignment statements. Some of their examples are given below:
Assignment statements: fptr1 = fopen(“file1”, “r”) and i ++ Relational operator: fptr1! = NULL Boolean operators: && and ||
Execution of the assignment statements in the condition part of the if statement depends upon the component conditions. For example, consider the following com- ponent condition in the if part:
((( fptr1 = fopen("file1", "r")) != NULL) && (i++) && (0)) The above condition is executed as follows:
• Execute the assignment statement fptr1 = fopen(“file1”, “r”). • Execute the relational operation fptr1! = NULL. • If the above relational operator evaluates to false, skip the evaluation of
the subsequent condition components (i++) && (0). • If the relational operator evaluates to true, then first (i) is evaluated to true
or false. Irrespective of the outcome of this evaluation, the next statement executed is (i++).
• If (i) has evaluated to true, then the condition (0) is evaluated. Otherwise, evaluation of (0) is skipped.
In Figure 4.5, we show a detailed CFG for the openfiles() function. The figure illustrates a fact that a CFG can take up a complex structure even for a small program unit.
We give a Java method, called ReturnAverage(), in Figure 4.6. The method accepts four parameters, namely value, AS, MIN, and MAX, where value is an integer array and AS is the maximum size of the array. The array can hold fewer number of elements than AS; such a scenarion is semantically represented by having the value −999 denoting the end of the array. For example, AS = 15,
4.4 PATHS IN A CONTROL FLOW GRAPH
1 i=0
2 8 fptr1 = fopen("file1", "r")
fptr2 = fopen("file2", "r") 3 9
fptr1 != NULL fptr2 != NULL T
14 fptr3 = fopen("file3", "r")
15 fptr3 != NULL
21 return(i)
Figure 4.5 Detailed CFG representation of openfiles(). The numbers 1– 21 are the nodes.
whereas the 10th element of the array is −999, which means that there are 10 elements—0–9–in the array. MIN and MAX are two integer values that are used
to perform certain computations within the method. The method sums up the values of all those elements of the array which fall within the closed range [MIN, MAX], counts their number, and returns their average value. The CFG of the method is shown in Figure 4.7.