Condition Coverage
10.2.3 Condition Coverage
A variation of the previous criterion can be applied for programs that have compound conditions and it provides for generating test data to let each term of any condition (rather than the condition as a whole) take both truth values, true and false. So that if the condition has the form
a b,
it is not enough to let the condition be true then false, but we want to ensure that each individual term takes both truth values through the test. To generate test data accord- ing to this criterion, we proceed as follows:
For each term, say a, of each condition of the program, say C, choose a path in the program that starts at the beginning of the program and ends at C; let P be the function of this path. Then generate:
• A state in dom(P /a ) and •
A state in dom(P /−a ).
As an illustration, we consider the following triangle program, which we modify specifically to create compound conditions.
#include <iostream> #include <cmath> using namespace std; /* constants
float eps = 0.000001; /* state variables */ float x, y, z; /* functions */ bool tri(float x, float y, float z);
// triangle bool equal(float a, float b);
// equal, within eps bool equi(float x, float y, float z);
// equilateral bool iso(float x, float y, float z);
// isoceles bool right(float x, float y, float z); // right triangle int main () {cout << “enter the triangle sides on one line” << endl;
cin >> x >> y >> z; if (!tri(x,y,z)) {cout << “not a triangle” << endl;} if (tri(x,y,z) && equi(x,y,z)) {cout << “equilateral”
<< endl;} if (tri(x,y,z) && (!equi(x,y,z)) && iso(x,y,z) && (!right(x,y,z))) {cout << “isoceles” << endl;}
if (tri(x,y,z) && (!equi(x,y,z)) && (!iso(x,y,z)) && right(x,y,z))
208 STRUCTURAL CRITERIA
{cout << “right” << endl;} if (tri(x,y,z) && (!equi(x,y,z)) && iso(x,y,z) && right(x,y,z)) {cout << “isoceles right” << endl;} if (tri(x,y,z) && (!equi(x,y,z)) && (!iso(x,y,z)) && (!right(x,y,z))) {cout << “scalene” << endl;}}
We can simplify this program by referring to the following identities: • An equilateral triangle is isosceles; hence a non-isosceles triangle is not equilateral.
A triplet (x, y, z) that satisfies right(x, y, z) necessarily satisfies tri(x, y, z). •
A triplet (x, y, z) that satisfies equi(x, y, z) necessarily satisfiestri(x, y, z). We find:
int main () {cout << “enter the triangle sides on one line” << endl; cin >> x >> y >> z; if (!tri(x,y,z)) {cout << “not a triangle” << endl;} if (equi(x,y,z)) {cout << “equilateral” << endl;} if (tri(x,y,z) && (!equi(x,y,z)) && iso(x,y,z) &&
(!right(x,y,z))) {cout << “isoceles” << endl;}
if ((!iso(x,y,z)) && right(x,y,z)) {cout << “right” << endl;} if ((!equi(x,y,z)) && iso(x,y,z) && right(x,y,z))
{cout << “isoceles right” << endl;} if (tri(x,y,z) && (!iso(x,y,z)) && (!right(x,y,z))) {cout << “scalene” << endl;}}
In this particular example, all the if-statements refer to the same values of variables x, y, and z. Indeed, though they are written sequentially, the if-statements are merely printing messages to the output stream and are not altering the program variables that are invoked in the conditions. A cursory inspection of the structure of the conditions reveals that the criterion of condition coverage is satisfied if we generated test data to ensure that each elementary function call returns both truth values, in turn. The following table shows sample test data that satisfies this criterion.
False tri(x,y,z)
equi(x,y,z)
iso(x,y,z)
right(x,y,z)
Note that the data generated to make iso(x, y, z) true does not define a triangle, nor does it have to—it only has to make iso(x, y, z) true; such data will cause the program
10.2 CONTROL FLOW COVERAGE 209