• 软件测试作业2


    课后1.2第三题

    1:

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

      (a) for循环没有遍历数组第一个元素

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

      (b) 注意空指针错误,那么如果x数组为空即可

      x == null ; y == 1;
      期望的输出:NullPointerException;
      实际的输出:NullPointerException;

      (c) 执行故障,但是不会导致错误状态的测试用例,需要保证最后一次出现y的数值绝对不可能是在数组第一个位置,给出一组可能的测试用例:

     

      x == [ 1, 2, 3] ; y == 2;
      期望的输出:1
      实际的输出:1

      (d) 如果一组数据进行了上述的算法之后,会导致错误而非失败,那么y对应的值就一定不能出现在x数组中,给出一组可能的测试用例

      x == [1, 2, 3] y == 4
      期望的输出: -1
      实际的输出: -1

    第二段程序:

    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

      (a) for循环应该从数组的最后开始往前遍历,这样寻找最后一个0的位置比较方便。

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

      (b) 所有的测试用例都会执行故障。

      (c) 如果数组长度为0时,for循环根本不会执行,也就不会出现错误;当数组长度为1时,循环只会执行一次,此时无论顺序遍历还是倒序遍历,最后的结果都是一样的,所以也同样不会出现错误

        x == [65536]
        期望的输出 : -1 
        实际的输出 : -1

      (d) 因为题中的代码是顺序遍历而非倒序,所以只要进入循环超过一次,那么就会有错误出现,想要不导致失败,那么测试用例中就需要有0个或者1个0,这样最后输出的结果就和遍历的顺序无关了

        x == [1, 2, 3]
        期望的输出: -1
        实际的输出: -1
  • 相关阅读:
    SpringBoot Data Jpa基本使用
    spring cloud oauth2(五) 白名单设置
    spring cloud oauth2(四) 资源服务搭建
    spring cloud oauth2(三) 自定义授权类型 手机号+短信验证码
    spring cloud oauth2(二) 自定义授权类型 图片验证码
    spring cloud oauth2(一) 授权服务搭建
    设计模式 选自《闻缺陷则喜》此书可免费下载
    设计模式六大原则 节选自《闻缺陷则喜》(此书可免费下载)
    架构模式 节选自《闻缺陷则喜》(此书可免费下载)
    架构内容 节选自《闻缺陷则喜》(此书可免费下载)
  • 原文地址:https://www.cnblogs.com/wwxtju/p/5276744.html
Copyright © 2020-2023  润新知