错误程序分析与测试用例设计

    public int findLast(int[] x, int y) {
        // Effects: If x==null throwNullPointerException
        // 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;
    }

此程序错在循环结构i>0,这样不会比较到数组的第一个元素。

要使测试用例不执行fault,使x=null,这样就不会进入循环。

要使测试用例执行fault但不导致错误状态, x=[2,3,5] y=3    Expected 1

要使测试用例导致错误但不是failure,   x=[2,3,5] y=0    Expected -1

 

    public static int lastZero(int[] x) {
        // Effects: if x==null throwNullPointerException
        // 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;
    }

此程序错误在循环是从小到大的,这样会找到第一个0的下标。

要使测试用例不执行fault,不可能,无论如何都会进入循环,也就是fault。

要使测试用例执行fault但不导致错误状态, x=[0]    Expected 0

要使测试用例导致错误但不是failure,   x=[1,0]     Expected 1

posted @ 2017-02-27 21:14  油炸铅笔  阅读(130)  评论(0编辑  收藏  举报