There are two programs.
For program 1
1 public int findLast(int[] x, int y){ 2 //Effects: If x==null throw NullPointerException 3 //else return the index of the last elements 4 //in x that equals y. 5 //If no such elements 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 }
1、Identify Identify the fault.
i > 0, it should be i ≥ 0, the searching should be stopped at 0, not 1.
2、If possible, identify a test case that does not execute the fault. (Reachability) .
x=null, y=2
It will not execute the statement i>0 where the fault locates.
3、If possible, identify a test case that executes the fault, does not result in an error state.
x=[0, 1, 2], y=1
It will execute the statement i>0 and have the same state if i>0 is fixed by i≥0, not result in an error state.
4、If possible, identify a test case that results in an error, but not a failure.
x=[0, 1, 2], y=3
It will have an error state if the fault is fixed, but will have the same results, both -1.
For program 2
1 public static int lastZero(int [] x){ 2 //Effects: ifx==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 } 12 return -1; 13 }
1、Identify Identify the fault.
for(i=0; i<length; i++) should be for(i=length-1; i≥0; i--), it should search from end to start to get the index of the LAST 0.
2、If possible, identify a test case that does not execute the fault. (Reachability) .
x=null
It will not execute the statement where the fault locates.
3、If possible, identify a test case that executes the fault, does not result in an error state.
x=[0]
It will execute the statement where the fault locates and have the same state, if the fault is fixed.
4、If possible, identify a test case that results in an error, but not a failure.
x=[1, 2, 3]
It will have an error state if the fault is fixed, but will have the same results, both -1.