【发布时间】: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 == 1,i = 1也会为单元素数组返回0。