码迷,mamicode.com
首页 > 其他好文 > 详细

软件测试 section1.2. EXERCISES 第三题

时间:2016-03-08 23:57:23      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:

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

  a)Identify the fault.

    在for循环中判断条件i>0,应该改为i>=0,否则数组第一个元素就不会被遍历到,使得第一个元素会影响到预期的结果。

  b)If possible, identify a test case that does not excute the fault.

    不会执行故障代码的测试用例:x=[2,3,5] ; y=3

                Expected:1; Actually:1  EQUAL!

  c)If possible, identify a test case that executes the fault, but not result in an error state

    会执行故障代码的,但不会导致内部错误(error)的测试用例:x=[2,3,5]; y=4

                Expected:-1; Actually:-1 EQUAL!

  d)If possible identify a test case that results in an error, but not a failure.

    会导致内部错误但不是程序失效(failure)的测试用例:x=Null; y=1

                Expected:NullpointerException; Actually:NullpointerExpection

  e)For the given test case, identifu the first error state. Be sure to describe the complete state.

    对于给定的测试用例,我们可以看到程序不会执行到数组下标为0的地址,所以最后的结果状态给的值就是-1,和预期的0不一致

  f)Fix the fault and verify that the given test now produes the expected output.

    只用把for循环中的i>0,改为i>=0,那么预期的结果和实际的结果就都是0.

 

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

  a)Identify the fault.

    故障代码是for循环是从前往后循环,遇见等于0的值就返回数组下标,是寻找数组中的第一个0的位置。和函数的需求不一致

  b)If possible, identify a test case that does not excute the fault.

    不会执行故障代码的测试用例:x=Null; 

                Expected:NullPointerException; Actually:NullPointerException  EQUAL!

  c)If possible, identify a test case that executes the fault, but not result in an error state

    会执行故障代码的,但不会导致内部错误(error)的测试用例:x=[2,3,5]; 

                Expected:-1; Actually:-1 EQUAL!

  d)If possible identify a test case that results in an error, but not a failure.

    会导致内部错误但不是程序失效(failure)的测试用例:x=Null;

                Expected:NullpointerException; Actually:NullpointerExpection

  e)For the given test case, identify the first error state. Be sure to describe the complete state.

    对于给定的测试用例x=[0,1,0],函数在访问到第一个元素时发现等于0,所以就直接返回第一个元素的数组下标0,和期望的2不一致。

  f)Fix the fault and verify that the given test now produes the expected output.

    只用把for循环改为for(int i =x.length-1;i>=0;i--)即可

    那么预期的结果和实际的结果就都是2.

 

public int countPositive(int[] x){
    //Effects:If x==null throw NullPointerException
    //else return the number of
    //positive elements in x
    
    int count  = 0;
    for(int i = 0; i<x.length; i++)
    {
        if(x[i]==0)
        {
            count++;
        }
    }
    return count;
}
 
    //test:x=[-4,2,0,2]
    //Expected = 2

  

  a)Identify the fault.

    这个函数的功能是计算数组中正数的数量,而if条件中x[i]>=0,判断的是非负数的数量,应该改为x[i]>0

  b)If possible, identify a test case that does not excute the fault.

    不会执行故障代码的测试用例:x=Null; 

                Expected:NullPointerException; Actually:NullPointerException  EQUAL!

  c)If possible, identify a test case that executes the fault, but not result in an error state

    会执行故障代码的,但不会导致内部错误(error)的测试用例:x=[-2,3,5]; 

                Expected:2; Actually:2 EQUAL!

  d)If possible identify a test case that results in an error, but not a failure.

    会导致内部错误但不是程序失效(failure)的测试用例:x=Null;

                Expected:NullpointerException; Actually:NullpointerExpection

  e)For the given test case, identify the first error state. Be sure to describe the complete state.

    对于给定的测试用例x=[-4,2,0,2],当访问到第三个元素0的时候,满足条件所以count的值增加1,使得最后实际的值是3而不是预期的2,不一致。

  f)Fix the fault and verify that the given test now produes the expected output.

    if条件中x[i]>=0,判断的是非负数的数量,应该改为x[i]>0

    那么预期的结果和实际的结果就都是2.

 

public static int  oddOrPos(int[] x){
    //Effects: if x==null throw NullPointerException
    //else return the number of elements in x that
    //are either off or positive(or both)

    int count= 0;
    for(int i = 0;i < x.length;i++)
    {
        if(x[i] % 2 ==1 || x[i] >0)
        {
            count++;
        }
    }
    return count;
}
    //test: x=[-3,-2,0,1,4]
    //Expected =  3

  

  a)Identify the fault.

    if判断条件中x[i] % 2 ==1会有故障,函数的功能是判断正数和奇数的数量,而这个条件没有考虑到负数的情况所以会漏掉负数中奇数的数量。

  b)If possible, identify a test case that does not excute the fault.

    不会执行故障代码的测试用例:x=Null; 

                Expected:NullPointerException; Actually:NullPointerException  EQUAL!

  c)If possible, identify a test case that executes the fault, but not result in an error state

    会执行故障代码的,但不会导致内部错误(error)的测试用例:x=[2,3,5]; 

                Expected:2; Actually:2 EQUAL!

  d)If possible identify a test case that results in an error, but not a failure.

    会导致内部错误但不是程序失效(failure)的测试用例:x=Null;

                Expected:NullpointerException; Actually:NullpointerExpection

  e)For the given test case, identify the first error state. Be sure to describe the complete state.

    对于第一个数的判定,取余2不等于1,所以判断不是奇数使得实际值和预期值不一致

  f)Fix the fault and verify that the given test now produes the expected output.

    将 x[i] % 2 ==1 改为 x[i] % 2 != 0即可

    那么预期的结果和实际的结果就都是2.

 

软件测试 section1.2. EXERCISES 第三题

标签:

原文地址:http://www.cnblogs.com/clownice/p/5256164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!