軟體測試 section1.2. EXERCISES 第三題

來源:互聯網
上載者:User

標籤:

 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 第三題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.