软件测试(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;

 

转载于:https://www.cnblogs.com/lmns/p/6474291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值