7CCSMASE
Tutorial 5 Solution
1. We cannot be certain that faults revealed by A are also revealed by B. In fact, as the question is stated, it is even possible that A also satisfies the stronger criterion C2, or an even stronger criterion C3. Even if we knew that A did not satisfy C2, it would not follow that all the faults revealed by A would also be revealed by B. A might be an exceptionally good test suite for reasons unrelated to the adequacy criterion, or we might just have been lucky. For example: see the program below.
1 /** Find the two roots of ax^2 + bx + c,
2 * that is, the values of x for which the result is 0. 3 */
4 class Roots {
5 double root one, root two;
6 int num roots;
7 public roots(double a, double b, double c) {
8 double q;
9 double r;
10 // Apply the textbook quadratic formula:
11 // Roots = -b +- sqrt(b^2 – 4ac) / 2a
12 q = b*b – 4*a*c;
13 if (q > 0&&a!=0) {
14 // If b^2 > 4ac, there are two distinct roots
15 num roots = 2;
16 r=(double) Math.sqrt(q) ;
17 root one = ((0-b) + r)/(2*a);
18 root two = ((0-b) – r)/(2*a);
19 } else if(q==0) { // (BUG HERE)
20 // The equation has exactly one root
21 num roots = 1;
22 root one = (0-b)/(2*a);
23 root two = root one;
24 } else {
25 // The equation has no roots if b^2 < 4ac
26 num roots = 0;
27 root one = -1;
28 root two = -1;
29 }
30 }
31 public int num roots() { return num roots; }
32 public double first root() { return root one; }
Assume that you have a test suite with only one test case, in which q=0 and a=0. This test case will reveal the bug in the program, right? But it certainly does not have 100% branch coverage (in fact, even its state coverage is poor). We can say that it, for example, satisfies the coverage criteria C1 of covering the statements 21, 22, and 23.
Now, let¡¯s assume you have another test suite, with several test cases. In one of them, q>0 and a>0, in another q=0 and a>0, and in the last one, q<0. This test suite clearly has 100% branch coverage (and state coverage), so its coverage criteria subsume the coverage criterion C1. But - it doesn¡¯t reveal the bug!
2. (a) The program crashes in line 22 because of the division by 0. The analysis of dominators
shows that it is control-dominated by line 19, which is the last decision point before 22. This usually indicates that the cause of the error is in line 19. Indeed, in this case, line 19 was the line where we check whether q=0, and do not check whether a=0. The analysis of pre and post dominators is done on the CFG, where the immediate pre-dominator of line 22 is line 21 and the immediate post-dominator of line 22 is line 23.
(b) The fault is revealed if q = 0^a = 0; this will be true only if both a and b are zero. The probability of either being zero is 1 in 232. The probability of both being zero is the product of the individual probabilities, 1 in 232 ¡Á232, or 1 in 264. Note that this is a somewhat simplified analysis, as in reality q is actually computed from a, b, and c, so it is not independent of a.
(c) If line 13 is modified to eliminate the check of a==0, then the bug will be revealed if a=0 and q>=0. The probability of (a=0) is 1/232 (one value out of 232 ). The probability of (q>=0) given that a=0 is 1 (it is easy to see since in this case q=b2, which is always non- negative). Hence the probability of encountering the bug during random testing would be 1/232 in this case.