知识点回顾

  • 基本定义
  • 软件故障(software Fault):软件中的静态代码的缺陷
  • 软件错误(software Error):不正确的内部状态,该状态是故障的表现。
  • 软件失败(software Failure):与需求或期望的行为的描述有关德尔、外部的、不正确的行为
  • 找出一个失败的三个条件:
  • 程序中包含错误的位置必须找到(可达性,reachability)
  • 执行该位置后,程序的状态必须是不正确的(影响,infection)
  • 受到影响的状态必须传播出来,引起该程序某个输出是不正确的(传播,propagation)

作业习题

public int findLast (int[] x, int y) {
//Effects: If x==null throw
NullPointerException
// else return the index of the last element
// in x that equals y.
// If no such element exists, return -1
for (int i=x.length-1; i > 0; i--)
{
if (x[i] == y)
{
return i;
}
}
return -1;
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0
  • 这段代码中的错误:

在第6行代码处应该是i >= 0

  • 提供一个不会执行故障的测试用例:

x=[2,3,5],y =5,期望值是2

  • 提供一个执行故障但是不会导致错误状态的测试用例:

不存在

  • 提供一个导致故障而不会失败的测试用例:

x[2,3,5],y=-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
  • 这段代码中的错误:

在第6行应该反向遍历,因为要找最后一个0

  • 提供一个不会执行故障的测试用例:

不存在

  • 提供一个执行故障但是不会导致错误状态的测试用例:

不存在

  • 提供一个导致故障而不会失败的测试用例:

x[0,1,1],,期望值是0