【问题标题】:fault or error in this method此方法中的错误或错误
【发布时间】:2012-04-19 14:31:15
【问题描述】:

我正在复习软件测试考试。其中一个问题给出了这种方法,并要求识别故障并生成一个不执行故障的测试用例(如果存在)。

代码如下:

public static int oddOrPos(int[] x) {
  //Effects: if x==null throw NullPointerException
  // else return the number of elements in x that
  // are either odd or positive (or both)
  int count = 0;
  for (int i = 1; i < x.length; i++)
  {
    if (x[i]%2 == 0 || x[i] > 0)
    { 
      count++;
    }
  }
  return count;
}

我发现了两个问题。一个是 i 在 for 循环中被初始化为 1,因此 x[0] 没有得到测试。 x[i] % 2 == 0 也应该是 x[i] != 0

这些问题是故障还是错误?我问这个是因为这个问题看起来只有一个错误。

另外,我假设因为总是会执行for循环,所以没有测试用例不会执行错误。

【问题讨论】:

  • 至于x[i] % 2 == 0,请参阅其他答案。但是,您是对的,即使您将条件更改为 x[i] % 2 == 1i = 1 也会为单元素数组返回 0

标签: java testing


【解决方案1】:

实际上x[i] % 2 == 0 应该是x[i] % 2 != 0(如果我们想检测奇数 值和正数。现有代码将检测偶数 值)。

测试用例只是{ -2 } - 这个元素是偶数和负数,所以不应该被计算在内,即使它是错误的,该方法也会返回0{ 1 } 也会给0,这是错误的。

【讨论】:

  • 不,它与剩下的操作员不太一样,请参阅我的回答。
  • 正确,我忘记了底片。修复。
  • 但在您的测试用例 {2} 中,它是偶数,因此不应该被计算在内,但它是肯定的,应该增加计数,所以该方法会返回 1,不是吗?
  • 我的错,我在写答案的核心时忘记了积极因素。希望它现在完全修复。
  • 我是否正确地假设因为 for 循环总是执行,所以不可能有一个不执行错误的测试用例?
【解决方案2】:

如果您想检测奇数负值,您必须寻找-1,而不是像现在这样寻找0

对于奇数值,它将是1。所以基本上你想要任何东西,除了 0

% 运算符是一个余数运算符,不是真正的模运算符,如果第一个给定数字是负数,它返回一个负数:

class Test1 {
    public static void main(String[] args) {
        int a = 5 % 3;  // 2
        int b = 5 / 3;  // 1
        System.out.println("5%3 produces " + a +
                " (note that 5/3 produces " + b + ")");

        int c = 5 % (-3);  // 2
        int d = 5 / (-3);  // -1
        System.out.println("5%(-3) produces " + c +
                " (note that 5/(-3) produces " + d + ")");

        int e = (-5) % 3;  // -2
        int f = (-5) / 3;  // -1
        System.out.println("(-5)%3 produces " + e +
                " (note that (-5)/3 produces " + f + ")");

        int g = (-5) % (-3);  // -2
        int h = (-5) / (-3);  // 1
        System.out.println("(-5)%(-3) produces " + g +
                " (note that (-5)/(-3) produces " + h + ")");
    }
}

另一个“小”错误是条件完成的方式。与检查 oddpositive 不同,寻找 positiveodd 会稍微快一些。这只是因为检查一个数字是否为正比获取其余数更容易。


资源:

【讨论】:

    【解决方案3】:

    这里的主要问题是你的 for 循环从 1 开始,它应该从 0 开始。你总是会错过数组的第一个元素。 x[i]%2 == 0 对偶数返回 true,而不是奇数。所以把它改成 x[i]%2 != 0。

    public class test{
    
    public static void main(String[] args){
    int[] x = {3, 5, -1, -14}
    
    if( 3 == oddOrPos(x)){
        System.out.println("Working");
    else
        System.out.println("Test Fail");
    
    }
    public static int oddOrPos(int[] x) {
      //Effects: if x==null throw NullPointerException
      // else return the number of elements in x that
     // are either odd or positive (or both)
     int count = 0;
     for (int i = 0; i < x.length; i++)
     {
       if (x[i]%2 != 0 || x[i] > 0)
    { 
      count++;
    }
     }
          return count;
    }
    }
    

    【讨论】:

      【解决方案4】:

      据我了解,您的假设是正确的。应该测试数组的第一个位置,因此您指出了i[0]

      但是,对于奇数,x[i]%2 == 0 应该改为 x[i]%2 == 1

      【讨论】:

        猜你喜欢
        • 2018-08-07
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多