软件测试 Homework2

Below are two faulty programs. Each includes a test case that results in failure. Answer the following questions (in the next slide) about each program.

代码1:

 1 public int findLast (int[] x, int y) {
 2   // Effects: If x==null throw 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 }
15 // test: x=[2, 3, 5]; y = 2
16 // Expected = 0

 

1. for循环中的条件判断应为:(int i=x.length-1; i > =0;i--);

2. test: x=[]; 抛出空指针异常,没有执行下面的程序,则没有执行fault

3.test: x=[3, 2, 5]; y = 2 Expected= 1

执行了含有fault的程序,但是并没有产生错误,即执行了fault,没有执行error

4. test: x=[3, 2, 5]; y = 1 Expected = -1

没有遍历x=1,直接返回了-1,因此执行了error,没有执行failure。

 

代码2:

 1 public static int lastZero (int[] x) {
 2   //Effects: if x==null throw NullPointerException
 3   // else return the index of the LAST 0 in x.
 4   // Return -1 if 0 does not occur in x
 5   for (int i = 0; i < x.length; i++)
 6   {
 7     if (x[i] == 0)
 8     {
 9       return i;
10     } 
11   } return -1;
12 }
13 // test: x=[0, 1, 0]
14 // Expected = 2

1、 for循环中的条件判断应为:(int i=x.length-1; i > =0; i--);

2、 test: x=[];(抛出空指针异常,没有执行下面的程序,则没有执行fault)

3、test: x=[1, 2, 0], Expected = 2

执行了含有fault的程序,但是并没有产生错误,即执行了fault,没有执行error

4、 test: x=[3, 2, 5]; Expected = -1

遍历到i=3时,返回了-1,不符合设计目的,因此执行了error,没有执行failure。

 

posted @ 2018-03-10 20:35  a97404  阅读(108)  评论(0编辑  收藏  举报