標籤:
1 public int findLast(int[] x, int y){ 2 //Effects:If X==null thro NullPointerException 3 //else return the index of the last element 4 //in x that equals y. 5 //If no such element exists, return -1 6 for(int i=x.length-1; i> 0;i--) 7 { 8 if(x[i] ==y) 9 {10 return i;11 }12 }13 return -1;14 }<br> //test: x=[2,3,5];y=2<br> //Expected = 0<br>
a)Identify the fault.
在for迴圈中判斷條件i>0,應該改為i>=0,否則數組第一個元素就不會被遍曆到。
b)If possible, identify a test case that does not excute the fault.
不會執行故障代碼的測試案例:x=[2,3,5] ; y=5
Expected:2; Actually:2 EQUAL!
c)If possible, identify a test case that executes the fault, but not result in an error state
會執行故障代碼的,但不會導致內部錯誤(error)的測試案例:x=[2,3,5]; y=6
Expected:-1; Actually:-1 EQUAL!
d)If possible identify a test case that results in an error, but not a failure.
會導致內部錯誤但不是程式失效(failure)的測試案例:x=Null; y=1
Expected:NullpointerException; Actually:NullpointerExpection
e)For the given test case, identifu the first error state. Be sure to describe the complete state.
對於給定的測試案例,我們可以看到程式不會執行到數組下標為0的地址,所以最後的結果狀態給的值就是-1,和預期的0不一致
f)Fix the fault and verify that the given test now produes the expected output.
只用把for迴圈中的i>0,改為i>=0,那麼預期的結果和實際的結果就都是0.
1 public static int lastZero(int[] x){ 2 //Effects:if x==null throw NullPointerExpection 3 //else return the index of the LAST 0 in x 4 //Return -1 if 0 does not occur in x 5 6 for(int i=0; i<x.length; i++) 7 { 8 if(x[i] ==0) 9 {10 return i;11 }12 }13 return -1;14 }15 //test: x=[0,1,0]16 //Expected: 2
a)Identify the fault.
故障代碼是for迴圈是從前往後迴圈,遇見等於0的值就返回數組下標,是尋找數組中的第一個0的位置。和函數的需求不一致
b)If possible, identify a test case that does not excute the fault.
不會執行故障代碼的測試案例:x=Null;
Expected:NullPointerException; Actually:NullPointerException EQUAL!
c)If possible, identify a test case that executes the fault, but not result in an error state
會執行故障代碼的,但不會導致內部錯誤(error)的測試案例:x=[2,3,5];
Expected:-1; Actually:-1 EQUAL!
d)If possible identify a test case that results in an error, but not a failure.
會導致內部錯誤但不是程式失效(failure)的測試案例:x=Null;
Expected:NullpointerException; Actually:NullpointerExpection
e)For the given test case, identify the first error state. Be sure to describe the complete state.
對於給定的測試案例x=[0,1,0],函數在訪問到第一個元素時發現等於0,所以就直接返回第一個元素的數組下標0,和期望的2不一致。
f)Fix the fault and verify that the given test now produes the expected output.
只用把for迴圈改為for(int i =x.length-1;i>=0;i--)即可
那麼預期的結果和實際的結果就都是2.
1 public int countPositive(int[] x){ 2 //Effects:If x==null throw NullPointerException 3 //else return the number of 4 //positive elements in x 5 6 int count = 0; 7 for(int i = 0; i<x.length; i++) 8 { 9 if(x[i]==0)10 {11 count++;12 }13 }14 return count;15 }16 17 //test:x=[-4,2,0,2]18 //Expected = 2
a)Identify the fault.
這個函數的功能是計算數組中正數的數量,而if條件中x[i]>=0,判斷的是非負數的數量,應該改為x[i]>0
b)If possible, identify a test case that does not excute the fault.
不會執行故障代碼的測試案例:x=Null;
Expected:NullPointerException; Actually:NullPointerException EQUAL!
c)If possible, identify a test case that executes the fault, but not result in an error state
會執行故障代碼的,但不會導致內部錯誤(error)的測試案例:x=[-2,3,5];
Expected:2; Actually:2 EQUAL!
d)If possible identify a test case that results in an error, but not a failure.
會導致內部錯誤但不是程式失效(failure)的測試案例:x=Null;
Expected:NullpointerException; Actually:NullpointerExpection
e)For the given test case, identify the first error state. Be sure to describe the complete state.
對於給定的測試案例x=[-4,2,0,2],當訪問到第三個元素0的時候,滿足條件所以count的值增加1,使得最後實際的值是3而不是預期的2,不一致。
f)Fix the fault and verify that the given test now produes the expected output.
if條件中x[i]>=0,判斷的是非負數的數量,應該改為x[i]>0
那麼預期的結果和實際的結果就都是2.
1 public static int oddOrPos(int[] x){ 2 //Effects: if x==null throw NullPointerException 3 //else return the number of elements in x that 4 //are either off or positive(or both) 5 6 int count= 0; 7 for(int i = 0;i < x.length;i++) 8 { 9 if(x[i] % 2 ==1 || x[i] >0)10 {11 count++;12 }13 }14 return count;15 }16 //test: x=[-3,-2,0,1,4]17 //Expected = 3
a)Identify the fault.
if判斷條件中x[i] % 2 ==1會有故障,函數的功能是判斷正數和奇數的數量,而這個條件沒有考慮到負數的情況所以會漏掉負數中奇數的數量。
b)If possible, identify a test case that does not excute the fault.
不會執行故障代碼的測試案例:x=Null;
Expected:NullPointerException; Actually:NullPointerException EQUAL!
c)If possible, identify a test case that executes the fault, but not result in an error state
會執行故障代碼的,但不會導致內部錯誤(error)的測試案例:x=[2,3,5];
Expected:2; Actually:2 EQUAL!
d)If possible identify a test case that results in an error, but not a failure.
會導致內部錯誤但不是程式失效(failure)的測試案例:x=Null;
Expected:NullpointerException; Actually:NullpointerExpection
e)For the given test case, identify the first error state. Be sure to describe the complete state.
對於第一個數的判定,取餘2不等於1,所以判斷不是奇數使得實際值和預期值不一致
f)Fix the fault and verify that the given test now produes the expected output.
將 x[i] % 2 ==1 改為 x[i] % 2 != 0即可
那麼預期的結果和實際的結果就都是2.
軟體測試 section1.2. EXERCISES 第三題