zoukankan      html  css  js  c++  java
  • 软件测试作业(二)

    Below are two faulty programs. Each includes a test case that results in 

    failure. Answer the following questions (in the next slide) about each program.

    questions:

    1.Identify the fault.

    2.If possible, identify a test case that does not execute the
    fault. (Reachability)

    3.If possible, identify a test case that executes the fault, but
    does not result in an error state

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

    Program 1

     1 public int findLast (int[] x, int y) {
     2   // Effects: If x==null throw NullPointerException
     3   // else return the index of the last element 
     4   // in x that equals y. 
     5   // If no such element exists, return -1
     6   for (int i=x.length-1; i > 0; i--) 
     7   { 
     8     if (x[i] == y) 
     9     {
    10       return i; 
    11     }
    12   }
    13   return -1; 
    14 }
    15 // test: x=[2, 3, 5]; y = 2
    16 // Expected = 0

    1. 数组的下标是从0开始的,所以第6行i>0应该改为i>=0.

    2. x=[],此时x为空,程序会抛出空指针异常而结束,fault不可到达。

    3. x=[1,2,3],y=2,此时fault可到达,但是没有导致error状态,最终返回的结果为1是正确的。

    4. x=[1,2,3],y=0,此时导致的error状态,但是最终返回的结果为-1是正确的。

    Program 2

    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

    1. 该程序的需求是返回最后一个0的下标,但是返回的是第一个出现的0的下标,fault为循环语句,循环应该改为

    for(int i = x.length-1 ; i >= 0 ; i++)

    2. x=[],此时程序抛出空指针异常,fault不可到达

    3. x=[0],此时程序到达fault语句,但是没有导致error状态,返回0是正确的

    4. x=[1,2,3],此时程序导致了error状态,但是返回的-1是正确的。

  • 相关阅读:
    python 操作ie 登陆土豆再退出
    python之sqlite3使用详解(转)
    python 获取当前目录下文件(转)
    python中使用time模块计算代码执行效率的精度测试(转)
    pythonhttplib模块使用(转)
    python技巧31[python中使用enum](转)
    python怎样压缩和解压缩ZIP文件(转)
    httplib2python下的http请求终结者(转)
    Python:使用ctypes库调用外部DLL(转)
    整理了下控制ie的代码(转)
  • 原文地址:https://www.cnblogs.com/tiny-home/p/5256169.html
Copyright © 2011-2022 走看看