• 软件测试(二)


      Hw2: 指出以下程序的错误并设计测试样例。要求测试样例能够做到:1.不执行fault 2.执行fault,但程序不进入error状态 3.使程序进入error,但不会引起failure

    (Fault: 程序内部的静态错误,及代码中的错误。 Error:因执行到了含fault的代码段而使得程序进入非正常的状态,但error仍是内部的,对外不一定展现。 Failure:对某一输入,没有得到期望的输出结果。failure是对外的)

    程序一:

     1 public intfindLast(int[] x, inty) {
     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                    

    fault: for循环的终止条件应为i>=0

    不会执行fault的样例: x为null, y随意。由于x为null,for循环的变量i的赋值会因为null没有length所以抛出NullPointerException,不会到达fault的位置

    Excepted: NullPointerException

    Actual: NullPointerException

    执行到fault但是并没有error的样例: x=[1,2,3,4,5] , y=2。 由于在i=1的时候就会因x[1]==y而终止循环,所以虽然每次for循环都会执行i>0的判断(执行fault),但是并没又因此而出现error(没有error是因为这个样例里并没有因为i>0这一fault而终止循环)。

    Excepted: 1

    Actual: 1

    处于error但无failure的样例:x=[1,2,3,4,5] y=0。 由于y根本就不在x中,因此虽然程序跳出循环是因为fault(此时处于了error状态),但是结果仍是没找到,返回了-1.

    Excepted: -1

    Actual: -1

    程序二:

     1 public static intlastZero(int[] x) {
     2 //Effects: if x==null throw NullPointerException
     3 // else return the index of the LAST 0 in x.
     4 // Return -1 if 0 does not occur in x
     5     for (int i= 0; i< x.length; i++)
     6     {
     7         if (x[i] == 0)
     8         {
     9             return i;
    10         }
    11     } return -1;
    12 }
    13 // test: x=[0, 1, 0]
    14 // Expected = 2    

    fault: for循环的搜索顺序应该从大到小。即 for (int i=x.length-1; i>= 0; i--)

    不会执行fault的样例: 不存在,所有的样例都会进入到for循环,一旦开始了i=0的赋值,就进入了fault

    执行到fault但是并没有error的样例: x=[0] 。 由于数组的长度为1,所以这时就没有了从大到小或从小到大的概念了。fault不会引起error

    Excepted: 0

    Actual: 0

    处于error但无failure的样例:x=[1,2,0,4,5] 。 由于检索的顺序反了,所以只要是数组内元素个数多于一个,就是处于error。

    Excepted: 2

    Actual: 2

  • 相关阅读:
    Leetcode-Pascal's Triangle
    SRM 619
    请用漂亮欢呼-------Day38
    创建list方法总结
    [ZJOI2019]语言
    jekyll 在博客添加流程图
    jekyll 在博客添加流程图
    HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具
    HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具
    How to use code to exit the application in UWP
  • 原文地址:https://www.cnblogs.com/sywang/p/5252463.html
Copyright © 2020-2023  润新知