Software Testing 2 —— Fault、error and failure小练习

Questions:

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.

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.

Program One:

 1 public int findLast (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             return i; 
10         }
11     }
12     return -1;
13 }
14 // test: x=[2, 3, 5]; y = 2 
15 // Expected = 0

Answers:

1、错误代码:

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

原因:应共遍历x.length次,且数组下标从零开始。

改正为:

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

2、A test case: x = [] ——> If x == null throw NullPointerException,不会执行fault。

3、A test case: x = [2, 3, 5]; y = 3 ——> The result is 1,执行了fault,且结果正确。

4、A test case: x = [2, 3, 5]; y = 4 ——> The result is -1,是error,不是failure。


Program Two:

 1 public static int lastZero (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             return i; 
 9         }
10     } 
11     return -1; 
12 }
13 // test: x=[0, 1, 0] 
14 // Expected = 2

Answers:

1、错误代码:

for (int i = 0; i < x.length; i++)

原因:返回的应该是最后一个零的位置,上述代码返回的是第一个零的位置,且数组下标从零开始。

改正为:

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

2、A test case: x = [] ——> If x == null throw NullPointerException,不会执行fault。

3、A test case: x = [1, 0, 1] ——> The result is 1,执行了fault,且结果正确。

4、A test case: x = [2, 3, 5] ——> The result is -1,是error,不是failure。

原文地址:https://www.cnblogs.com/fogmisty/p/8540745.html