Questions:
1)Identify the fault. If possible, identify a test
case that does not execute the fault. (Reachability)
2)If possible, identify a test case that executes the fault, but does not result in an error state.
3)If possible identify a test case that results in an error, but not a failure.
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
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
1、(1)fault:由i>0知,i不可能为0,所以不可能得到期望值0,应该为 i >= 0
(2) 不执行故障测试用例 :x=[]; y=2 期望值=-1
(3) 执行故障不导致错误用例 : x=[3,2,5]; y=2 期望值=1
(4)导致错误但不是失败结果的用例: x=[1,3,5]; y=4 期望值=-1
2、 (1)fault: 从前到后遍历,数组第一个即为0,所以到第一个时已经返回了0,不可能得到期望2
(2) 不执行故障测试用例 :x=[] 期望值=-1
(3) 执行故障不导致错误用例 : x=[0] 期望值=0,实际值=0
(4)导致错误但不是失败结果的用例: x=[1,3,0] 期望值=2,实际值=2