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
錯誤:數組
for (int i=x.length-1; i > 0; i--)
應改成
for (int i=x.length-1; i >= 0; i--) //原代碼中不會遍歷到數組中第0個數據
不會執行故障的用例: x = [2,3,5];y = 5;Excepted = 2;
執行故障可是不會致使錯誤狀態的測試用例: ??
致使故障而不會失敗的測試用例: x = [2,3,5];y = 0; Excepted = -1;
1 public static int lastZero (int[] x) { 2 //Effects: if x==null throw 3 NullPointerException 4 // else return the index of the LAST 0 in x. 5 // Return -1 if 0 does not occur in x 6 for (int i = 0; i < x.length; i++) 7 { 8 if (x[i] == 0) 9 { 10 return i; 11 } 12 } return -1; 13 } 14 // test: x=[0, 1, 0] 15 // Expected = 2
錯誤:測試
for (int i = 0; i < x.length; i++)
應改成
for (int i = x.length - 1; i >= 0 ; i--) //原代碼搜索出數組中第一個0而非最後一個
不會執行故障的測試用例: ??
執行故障可是不會致使錯誤狀態的測試用例: ??
致使故障而不會失敗的測試用例: x = [1,0,1]; Expected = 1;
知識點:
Reachability:項目中錯誤的位置是能夠找到的infection:項目中該階段必須是不正確的Propagation:項目中有有影響的部分必須致使一些輸出或結果的不正確