软件测试(二)

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.

转载于:https://www.cnblogs.com/ph0en1x/p/6441004.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值