Branch Coverage
10.2.2 Branch Coverage
The criterion of branch coverage provides that we generate test data so that for each branch in the control structure of the candidate program (whether it arises in an if-then statement, an if-then-else statement, or a while statement), the program proceeds through each of the True branch and the False branch at least once. As an illustration of this criterion, we consider the following program:
#include <iostream> #include <cmath> using namespace std; /* constants
float eps = 0.000001; /* state variables */
10.2 CONTROL FLOW COVERAGE 205 float x, y, z;
/* functions */ Bool tri(float x, float y, float z); bool equal(float a, float b); bool equi(float x, float y, float z); bool iso(float x, float y, float z); bool right(float x, float y, float z); 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;} else {if (equi(x,y,z))
{cout << “equilateral” << endl;} else if (iso(x,y,z)) {if (right(x,y,z)) {cout << “isoceles right” << endl;}
else {cout << “isoceles” <<endl;} else {if (right(x,y,z)) {cout << “right” << endl;} else {cout << “scalene” << endl;}}}} bool tri (float x, float y, float z)
{return ((x<=y+z) && (y<=x+z) && (z<=x+y));} bool equal (float a, float b) {return abs(a-b)<eps;} bool equi(float x, float y, float z) {return (equal(x,y) && equal(y,z));} bool iso(float x, float y, float z) {return (equal(x,y) || equal(y,z) || equal(x,z));} bool right(float x, float y, float z)
{return (equal(x*x+y*y,z*z) || equal(x*x+z*z,y*y) ||
equal(y*y+z*z,x*x));}
The following set of paths allows us to traverse every branch at least once:
• p1: cout << “enter the triangle sides on one line” << endl; cin >> x >> y >> z;
((!tri(x,y,z))? True) {cout << “not a triangle” << endl;} • p2: cout << “enter the triangle sides on one line” << endl;
cin >> x >> y >> z; ((!tri(x,y,z))? False);((equi(x,y,z))? True) {cout << “equilateral” << endl;}
206 STRUCTURAL CRITERIA
• p3: cout << “enter the triangle sides on one line” << endl; cin >> x >> y >> z;
((!tri(x,y,z))? False); ((equi(x,y,z))? False); ((iso(x,y,z))? True); ((right(x,y,z))? True);
{cout << “isoceles right” << endl;} • p4: cout << “enter the triangle sides on one line” << endl;
cin >> x >> y >> z; ((!tri(x,y,z))? False); ((equi(x,y,z))? False); ((iso(x,y,z))? True); ((right(x,y,z))? False);
{cout << “isoceles” << endl;} • p5: cout << “enter the triangle sides on one line” << endl;
cin >> x >> y >> z; ((!tri(x,y,z))? False); ((equi(x,y,z))? False);
((iso(x,y,z))? False); ((right(x,y,z))? True); {cout << “right” << endl;}
• p6: cout << “enter the triangle sides on one line” << endl; cin >> x >> y >> z;
((!tri(x,y,z))? False); ((equi(x,y,z))? False); ((iso(x,y,z))? False); ((right(x,y,z))? False);
{cout << “scalene” << endl;}
A brief inspection of the paths presented above enables us to check that each condition appears at least once with the outcome True and once with the outcome False. In the following table, we list the paths, along with their path conditions and a test vector that satisfies the path condition.
Path Path condition Test vector p1
((!tri(x,y,z))? True)
p2 ((!tri(x,y,z))? False); ((equi(x,y,z))? True)
p3 ((!tri(x,y,z))? False); ((equi(x,y,z))? False);
((iso(x,y,z))? True); ((right(x,y,z))? True); p4
((!tri(x,y,z))? False); ((equi(x,y,z))? False);
((iso(x,y,z))? True); ((right(x,y,z))? False); p5
((!tri(x,y,z))? False); ((equi(x,y,z))? False);
((iso(x,y,z))? False); ((right(x,y,z))? True); p6
((!tri(x,y,z))? False); ((equi(x,y,z))? False);
((iso(x,y,z))? False); ((right(x,y,z))? False);
10.2 CONTROL FLOW COVERAGE 207