软件测试作业2:对faulty,error和failure的理解和应用

本次课程要求学生理解fauly,error,failure之间的区别和联系,以下是对两段不同程序的问题分析与解答。

题目要求如下:

1、Identify the fault

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

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

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

程序片段1如下:

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

分析:该程序用后序遍历方法查找数组x中最后一个值为y的索引。从最后一个数值开始到0号索引结束,很显然,for循环没有考虑到第一个数值。

解答:1、将for循环中的 i>0 改为 i>=0;

2、当数组为空时,程序跳过for循环,不执行错误代码段;

3、数组x={1,2,3},y=2时,执行了错误程序段,但没有产生错误;

4、数组x={1,2,3},y=1时,导致了error。

程序片段2如下:

 1 public static int lastZero (int[] x) {
 2 //Effects: if x==null throw NullPointerException
 4 // else return the index of the LAST 0 in x.
 5 // Return -1 if 0 does not occur in x
 6 for (int i = 0; i < x.length; i++)
 7 {
 8 if (x[i] == 0)
 9 {
10 return i;
11 }
12 } return -1;
13 }
14 // test: x=[0, 1, 0]
15 // Expected = 2

分析:该段代码查找数组x中最后一个0的索引值,但从第一个数开始查找,结果返回了第一个值为0的索引值。

1、错误为for循环条件,应改为for{int i = x.length-1; i>=0; i--};

2、当数组中只且只有一个0时,不执行错误;

3、数组x={1,0,1},执行错误代码,但不产生error

4、数组x={1,0,0},产生error,但并没有failure.

原文地址:https://www.cnblogs.com/tjuyyh/p/5250817.html