码迷,mamicode.com
首页 > 其他好文 > 详细

软件测试 section1.2. EXERCISES 第三题

时间:2016-03-10 23:41:40      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/yijiutaosheng/p/5263714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!