软件测试作业(二)
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
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. 数组的下标是从0开始的,所以第6行i>0应该改为i>=0.
2. x=[],此时x为空,程序会抛出空指针异常而结束,fault不可到达。
3. x=[1,2,3],y=2,此时fault可到达,但是没有导致error状态,最终返回的结果为1是正确的。
4. x=[1,2,3],y=0,此时导致的error状态,但是最终返回的结果为-1是正确的。
Program 2
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
1. 该程序的需求是返回最后一个0的下标,但是返回的是第一个出现的0的下标,fault为循环语句,循环应该改为
for(int i = x.length-1 ; i >= 0 ; i++)
2. x=[],此时程序抛出空指针异常,fault不可到达
3. x=[0],此时程序到达fault语句,但是没有导致error状态,返回0是正确的
4. x=[1,2,3],此时程序导致了error状态,但是返回的-1是正确的。