Below are two faulty programs. Each includes a test case that results in failure. Answer the following questions (in the next slide) about each program.
- Identify the fault.
- If possible, identify a test case that does not execute the fault. (Reachability)
- If possible, identify a test case that executes the fault, but does not result in an error state.
- If possible identify a test case that results in an error, but not a failure.
Program 1:
1 public intfindLast(int[] x, int y) { 2 //Effects: If x==null throw NullPointerException 3 // else return the index of the last element 4 // in x that equals y. 5 // If no such element exists, return -1 6 for (int i=x.length-1; i> 0; i--) 7 { 8 if (x[i] == y) 9 { 10 return i; 11 } 12 } 13 return -1; 14 } 15 // test: x=[2, 3, 5]; y = 2 16 // Expected = 0
1.Fault: row 6, it should be i >= 0;
2.if x is null and whatever y is, it wouldn't be a fault;
3.Example
x = [1,3,5], y = 5
Expected: 2
Actual: 2
It is a fault, but not an error;
4.Exmaple x = [2,4,6], y = 5
Expected: -1
Actual: -1
It is an error, but not a failure;
Program 2:
1 public static intlastZero(int[] x) { 2 //Effects: if x==null throw NullPointerException 3 // else return the index of the LAST 0 in x. 4 // Return -1 if 0 does not occur in x 5 for (int i= 0; i< x.length; i++) 6 { 7 if (x[i] == 0) 8 { 9 return i; 10 } 11 } return -1; 12 } 13 // test: x=[0, 1, 0] 14 // Expected = 2
1.Fault: The program finds the first 0. Row 5 should be for(int i = x.length-1; i >= 0; i--);
2.Not exist, once it enters the for loop, it is a fault;
3.When 0 is the only element of x,
x = [0]
Expected: 0
Actual: 0
It is a fault, but not an error.
4.When the number of 0 is no more than 1. For example:
x = [2, 3, 4]
Expected: -1
Actual: -1
it is an error, but not a failure.