软件测试(二)——样例测试

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.

Questions
1、Identify the fault.

2、If possible, identify a test case that does not execute the fault. (Reachability)

3、If possible, identify a test case that executes the fault, but does not result in an error state.

4、If possible identify a test case that results in an error, but not a failure.

程序一:

public int findLast (int[] x, int y) {
 //Effects: If x==null throw NullPointerException
 // else return the index of the last element
 // in x that equals y.
 // If no such element exists, return -1
 for (int i=x.length-1; i > 0; i--) {
 if (x[i] == y) {
 return i;
 } 
}
 return -1;
 } 
// test: x=[2, 3, 5]; y = 2 
// Expected = 0

Fault:for循环中的i>0应该为i>=0

不执行fault:令x==null, y任意,因为x==null throw NullPointerException,不会到达fault 的位置。

Expected: NullPointerException

Actual: NullPointerException

 

执行到fault但没有errors: x=[1,2,3,5],y=2,因为在i=1的时候x[1]==y终止循环,会执行fault的地方但是始终i>0,所以不会产生errors。

Expected:1

Actual: 1

 

有errors但是没有failure:x=[1,2,3,5],y=0,因为y不在x里面,程序跳出循环,程序没有找到,返回-1。

Expected: -1

Actual:-1

程序二:

public static int lastZero (int[] x) {
 //Effects: if x==null throw NullPointerException 
 // else return the index of the LAST 0 in x. 
 // Return -1 if 0 does not occur in x
 for (int i = 0; i < x.length; i++) {
  if (x[i] == 0) {
   return i;
  }
 }
 return -1;
}
// test: x=[0, 1, 0] 
// Expected = 2

Fault: for循环的搜索顺序应该从大到小。应该为 for (int i=x.length-1; i>= 0; i--)

不执行fault:不存在,所有样例都会进入for循环。

执行到fault但没有errors:x=[0],这时数组的长度是1,就没有从大到小或者从小到大的概念了。

Expected:0

Actual: 0

 

有errors但是没有failure:x=[1,0,2,3],由于检索顺序反了,只要数组内元素个数大于1就会产生errors.

Expected:1

Actual: 1

 

posted @ 2017-02-26 14:50  w什么小姐  阅读(203)  评论(0编辑  收藏  举报