软件测试(Software Testing)HW2: locate faults and identify test cases

Code1

 1 public int findLast(int[]x, int y){
 2     //Effects: If x==null throw NullPointerException
 3     //else return the index of the last element in x that equals y.
 4     //If no such element exists, return -1
 5     for (int i = x.length - 1; i > 0; i--){
 6         if(x[i] == y){
 7             return i;
 8         }
 9     }
10     return -1;
11 }

1. Identify the fault.

  The condition expression of the for loop, should be i >= 0. it’s wrong because it ignores the condition when i=0.

 

2. If possible, identify a test case that does not execute the fault. (Reachability)

  “Does not execute the fault” means the program ends without running the condition of i=0. In other words, the program return in the if statement.

  Here is the test case satisfy this question. 

1 x = [1, 2, 3, 4];
2 y = 4;
3 Excepted = 3;
4 Actual = 3;

 

3. If possible, identify a test case that executes the fault, but does not result in an error state.

  There are no such a test case satisfy this question. Because if the fault execute the for loop ignored the condition of i=0. It forget to check result of i=0, the error state is already happened.

 

4. If possible identify a test case that results in an error, but not a failure.

  This question means the program is execute the fault but the result of the program is correct. In other word. The program return -1.

  Here is the test case satisfy the question.

1 x = [1, 2, 3, 4];
2 y = 5;
3 Excepted = -1;
4 Actual = -1;

 

 

Code2

 1 public static int lastZero(int[] x){
 2         //Effects: if x==null throws 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             if(x[i] == 0){
 7                 return i;
 8             }
 9         }
10         return -1;
11 }

1. Identify the fault.

  This function need the index of the last 0. The fault is the direction of for loop. The direction should from the x.length-1 to 0, like this

        for (int i = x.length-1; i >= 0; i--)

  The program actually finds the first 0.

 

2. If possible, identify a test case that does not execute the fault. (Reachability)

  If the test case doesn’t include 0. The program doesn’t face the problem of the for loop direction. So it won’t execute the fault.

  Here is the test case satisfy the question.

1 x = [1, 2, 3, 4];
2 Expected = -1;
3 Actual = -1;

 

3. If possible, identify a test case that executes the fault, but does not result in an error state.

  If the program executes the fault, the test case must include only one 0. And the 0 must be the last number of the array.

  when we get 0, we shouldn’t immediately return. We should continue to check if there are still 0 behind this one.

  But if the 0 is the last number, we doesn’t need to check any more. So the program is not in error state. The program executes the fault, but does not result in an error state.

  Here is the test case satisfy the question.

1 X = [1, 2, 3, 0];
2 Expected = 3;
3 Actual = 3;

 

4. If possible identify a test case that results in an error, but not a failure.

  If the test case includes only one 0, and the 0 is not the last number. The test case like this satisfy the question.

  when we get 0, we should continue to check. But the program return immediately. It doesn’t continue to check. It is in an error state. But the result is right, it doesn’t cause a failure.

  Here is the test case satisfy the question.

1 X = [1, 0, 2, 3];
2 Expected = 1;
3 Actual = 1;

 

posted @ 2017-02-27 15:14  喷水小火龙  阅读(296)  评论(0编辑  收藏  举报