软件测试作业(二)FAULT,FAILURE,ERROR辨析与测试用例设计

Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions (in the next slide) about each program.

program1:

public int findLast (int[] x, int y) { 

        //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

program2:

public static int lastZero (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

Questions

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.

Answers

Fault 静态错误 ,Failure 外部错误 ,Error 内部错误

程序一:

1.Fault:for循环的控制条件i > 0这里应该是i>=0,否则将不会访问x[0];

2.一个不执行fault的样例

x = {} y = 3;

这样情况会直接访问无效内存导致程序挂掉

3.执行了fault但是没有返回错误的值

x={1,2,3} y = 2

期望是1,答案也是1

4.导致错误但不会失败的

x={1,2,3},y =0

期望答案是-1,实际答案是-1;

程序二:

1.fault代码是找数组中最后一个0的位置,但是却实现的是查询第一个0的位置

2.不执行fault,因为从循环处就有fault(方向反了)所以不存在不执行fault的用例

3.执行fault但不Error的情况是数组只有一个元素,无论正反遍历都不会有区别(都是只遍历下标为0不存在正反)

比如x={4};

4.执行fault导致error但没有failure的,当x中只有一个或没有0的时候

x = {1,0,2,3}

答案为1期望答案也为1

你可能感兴趣的:(软件测试)