Fault:
可能導致程序失敗的因素,可理解成具體的代碼。
Error:
在程序運行過程中與設計時的預先設想不一致的情況,如變量值錯誤,錯誤的運行路徑等。
Failure:
當一程序不能完成所要求的功能時,即失敗。
函數findLast:
- Identify the fault.
在邊界判斷時出錯 在i=0 時退出循環,沒有檢驗第一個int值。
for循環中的條件判斷應為:(int i=x.length-1; i > =0; i--)。
2. If possible, identify a test case that does not execute the fault. (Reachability)
test: x=[]。
直接拋出空指針異常而不執行以后的代碼,所以沒有執行fault。
- If possible, identify a test case that executes the fault, but does not result in an error state.
test: x=[1, 2, 3]; y = 2
Expected= 1
執行了含有fault的代碼在產生error返回了正確結果。
4. If possible identify a test case that results in an error, but not a failure.
test: x=[2, 4, 5]; y = 1
Expected = -1
沒有遍歷到x[0],直接返回了-1,因此執行了error但是沒有產生 failure。
函數lastZero:
1、 Identify the fault.
應該從后往前遍歷,for循環中的條件判斷應為:(int i=x.length-1; i > =0; i--);
2、 If possible, identify a test case that does not execute the fault. (Reachability)
test: x=[]。
3、 If possible, identify a test case that executes the fault, but does not result in an error state.
test: x=[3, 2, 1];
Expected = -1
遍歷了沒有發現0 沒有進入到error的情況。
4、 If possible identify a test case that results in an error, but not a failure.
test: x=[1, 2, 0]
Expected = 2
發生了error沒有發生failure,
返回了第一個0的位置 但只有一個0,結果正確,代碼邏輯錯誤。