Software Testing HW2 fault error failure
Question 1:
public int findLast(int[] x, int y){ //Effects: If x==null throw NullPointerException //else return the index of the last elements //in x that equals y. //If no such elements exists, return -1 for(int i=x.length-1; i>0; i--) { if(x[i] == y) { return i; } } return -1; }
1.Identify the fault.
The condition of loop is fault. It makes the loop works from the last element in x to the second element. For example, if x=[2,3,5], it will test x[2],x[1], without x[0].
2.If possible, identify a test case that does not execute the fault.
There exists the fault, but the code will not run that code. It means it will end before the fault.
For example, Test: x=[2,3,5]; y=5 Expected=2
3.If possible, identify a test case that executes the fault, does not result in an error state.
It will execute the fault, however that doesn't make an error.
For example, Test: x=[2,3,5]; y=4 Expected=-1
4. If possible identify a test case that results in an error, but not a failure.
Test: x=[2,3,5]; y=2 Expected=0
Question 2:
public static int lastZero(int [] x){ //Effects: ifx==null throw NullPointerException //else return the index of the LAST 0 in x. //Return -1 if 0 does not occur in x. for(int i=0; i<x.length; i++) { if(x[i] == 0) { return i; } } return -1; }
1.Identify the fault.
The loop begins from the first element to the last one. If it find x[i]==0, it will return i. However i may be not the last 0 in x.
2.If possible, identify a test case that does not execute the fault.
For example, Test: x=[2,3,5]; y=5 Expected=2
3.If possible, identify a test case that executes the fault, does not result in an error state.
Test: x=[2,1,1]; Expected=-1
4. If possible identify a test case that results in an error, but not a failure.
Test: x=[0,1,0]; Expected=2
If there is any questions, please contect with me.
Zhaodanning@tju.edu.cn