Execution Paths
10.1.1 Execution Paths
Now that we are interested in structural test data generation, we need to look at programs rather than specifications. We assume that our programs are written in a traditional C-like programming language, which includes the following statements:
• Variable declarations, of the form <data type><variable name>. • Labeled statements, of the form <label>: <statement>. • Assignment statements, of the form <variable name> = <expression>. • Sequence statements, of the form <statement1> ; <statement2>. • Conditional statements, of the form if <condition><statement>. • Alternation statements, of the form if <condition><statement1> else
• Iteration statements, of the form while <condition><statement>. • Statement blocks, of the form {<statement>}. • Function calls, of the form <function name> ( <parameter list> ).
Because we use structured constructs exclusively, any program is executed in sequence from the first statement to the last statement (we have no exits or return statements in the middle of a program). Hence all executions of our programs start from the first statement and proceed through the code according to the outcome of the various conditions that appear therein. Hence the following definitions.
An elementary statement is any one of the following • An assignment statement
• A function call •
A condition test of the form
○ ( <condition> ? true ) ○ ( <condition> ? false )
A path through a program is a sequence of elementary statements separated by semicolons such that whenever two elementary statements <es1> and <es2> follow each other in the path, then one of the following conditions holds:
• <es1> and <es2> follow each other in the program, or • <es2> is derived from the condition of a conditional statement or an alternative
statement or an iterative statement which follows <es1> in the program, or
10.1 PATHS AND PATH CONDITIONS 195
• <es1> is the last elementary statement of a conditional statement or an alternative statement that precedes <es2> in the program, or • <es1> is derived from the condition of an iterative statement that precedes <es2> in the program. • <es1> has the form (c? true), where c is the condition of a conditional statement or an alternative statement, and <es2> is the first statement of the then-branch, or • <es1> has the form (c? false), where c is the condition of an alternative statement and (es2> is the first statement of the else branch, or • <es1> has the form (c? true), where c is the condition of an iterative statement and <es2> is the first statement of the loop body.
As an application, we consider the following program and write some of its paths. Note that the definition of paths does not stipulate that they start from the beginning of the programs nor that they end with its last statement, as we want to have the liberty to define partial paths through the code. Nevertheless the sample paths we show below start at the first statement of the program and end at its last statement.
Program g:
{int x; int y; read(x); read(y); // assuming x>0, y>0 while (x!=y) {if (x>y) {x=x-y;} else {y=y-x;}}; write(x);}
Sample paths through this program include the following:
• p0: int x; int y; read(x); read(y); ((x!=y)? false); write(x);
• p1: int x; int y; read(x); read(y); ((x!=y)? true); ((x>y)? true); x=x-y;
((x!=y)? false); write(x); • p2: int x; int y; read(x); read(y);
((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? false); write(x);
• p3: int x; int y; read(x); read(y); ((x!=y)? true); ((x>y)? true); x=x-y; ((x!=y)? true); ((x>y)? true); x=x-y;
((x!=y)? false); write(x);
196 STRUCTURAL CRITERIA
• p4: int x; int y; read(x); read(y);
((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? false); write(x);
• p5: int x; int y; read(x); read(y); ((x!=y)? true); ((x>y)? true); x=x-y;
((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? false); write(x);
• p6: int x; int y; read(x); read(y);
((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? true); ((x>y)? true); x=x-y; ((x!=y)? false); write(x);
• p7: int x; int y; read(x); read(y); ((x!=y)? true); ((x>y)? true); x=x-y;
((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? true); ((x>y)? true); x=x-y; ((x!=y)? false); write(x);
• p8: int x; int y; read(x); read(y);
((x!=y)? true); ((x>y)? false); y=y-x; ((x!=y)? true); ((x>y)? true); x=x-y; ((x!=y)? true); ((x>y)? false); y=y-x;
((x!=y)? false); write(x);
and so on. Each path represents a trace of execution through the original program.