软件测试(二)

第二次作业

对于以下两个程序

1、找出fault

2、如果可能的话,找出一个测试用例不执行fault

3、如果可能的话,找出一个测试用例执行fault,但是不产生error

4、如果可能的话,找出一个测试用例产生error,但是不会导致failure

public intfindLast(int[] x, inty) {
//Effects: If x==null throw NullPointerException
// else return the index of the last element
// in x that equals y.
// If no such element exists, return -1
    for (int i=x.length-1; i> 0; i--)
    {
        if (x[i] == y)
        {
            return i;
         }
    }
    return -1;
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0

  

public static intlastZero(int[] x) {
//Effects: if x==null throw NullPointerException
// else return the index of the LAST 0 in x.
// Return -1 if 0 does not occur in x
    for (int i= 0; i< x.length; i++)
    {
        if (x[i] == 0)
        {
            return i;
        }
    } return -1;
}
// test: x=[0, 1, 0]
// Expected = 2

对于第一段代码

(1)、for循环不能遍历数组中的每一个元素,应该把for循环中的控制条件修改为i >= 0

(2)、x数组为空的情况下,会抛出空指针,不会执行fault

(3)、test: x = [ 3,5,2]; y = 2;

           Expected = 2

    数组中的第一个元素不是需要找到的元素,所以不会产生error

(4)、test: x = [ 2,5,2]; y = 3;

           Expected = -1

    数组中没有需要找到的元素,所以会返回-1,产生了error,但没有产生failure。

对于第二段代码

(1)、不能按照要求寻找到数组中的最后一个0的位置,而是找出的第一个0出现的位置。

(2)、x数组为空的情况下,会抛出空指针,不会执行fault

(3)、test: x = [0,1,2];

           Expected = 0

    数组中只有一个0,无论怎么查找,都会返回这个0的索引,执行了fault,但是不会产生error

(4)、test: x = [ 1,2,0,3];

           Expected = 2

    

原文地址:https://www.cnblogs.com/zyqBlog/p/5253653.html