7CCSMASE
Tutorial 7 (Week 8) – Solutions Consider the following program (Euclid’s algorithm):
public int gcd(int x, int y) { int tmp;
//A //B //C //D //E
//F //G //H
// I
}
while(y!=0){ tmp = x % y; x = y;
y = tmp;
} if(y!=0){
print(“Error in computation.”); exit(1);
}
return x;
(a) The control flow graph has two decision nodes: a While loop at node B, with the false branch going to F (and the true branch going via the loop statements), and If at node F.
(b) Statement coverage is coverage of statements in the program by the test cases. In this example, statements G and H are unreachable. It is not a problem, but defensive programming (explicitly defining the error handling for unexpected values).
(c) The True branch in F is unreachable – this is the block of defensive programming.
(d) One test case with y not 0 is sufficient to obtain maximal statement coverage possible for this program. For example, (6,2).