软件测试作业2

对于第一道题:

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 (inti=x.length-1; i> 0; i--)
    {
        if (x[i] == y)
        {
        return i;
        }
    }
    return -1;
    }
// test: x=[2, 3, 5]; y = 2
// Expected = 0            

  

Fault:i>0的写法是错误的,应该是i>=0;

不执行这个fault的测试用例:x=[2,3,4];y=2;

执行了这个fault但是没有导致error:x=[2,3,4];y=2;

导致了error但是没有导致failure的:x=[2,4,5];y=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 (inti= 0; i< x.length; i++)
    {
            if (x[i] == 0)
            {
            return i;
            }
    }
  
return -1; } // test: x=[0, 1, 0] // Expected = 2

这个fault是for循环写的顺序不对,应该写成从后往前找:for(int i = x.length-1; i >= 0; i--)

不执行这个fault的测试用例:x=[];

执行了这个fault但是没有导致error:x=[2,3,0];

导致了error但是没有导致failure的:x=[2,4,0];

//老师其实我还是不大理解error和fault怎么区分,例如这个第二道题,fault是比较大的错误了已经,如果要执行fault,我认为马上就会遇到error,迭代就倒序了。

原文地址:https://www.cnblogs.com/yanwenxiong/p/5259396.html