软件测试作业2 — 样例测试
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.
Answer:
Program1:
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 return i; } 10 } 11 return -1; 12 } 13 // test: x=[2, 3, 5]; y = 2 14 // Expected = 0
1.Fault:
Line 6:i > 0
it should be for(int i=x.length-1; i >= 0; i--)
2. A test case that does not execute the fault:
x=[ ]; y =2
Expected: NullPointerException
Actual: NullPointerException
3. A test case that executes the fault, but does not result in an error state:
x=[2,3,5]; y=3
Expected: 1
Actual: 1
4. A test case that results in an error, but not a failure:
x=[2,3,5]; y=4
Expected:-1
Actual: -1
Program2:
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 return i; } 9 } return -1; } 10 // test: x=[0, 1, 0] 11 // Expected = 2
1.Fault:
Line 5:for (int i = 0; i < x.length; i++)
it should be if(int i = x.length-1; i>=0; i--)
2. A test case that does not execute the fault:
x=[ ];
Expected: NullPointerException
Actual: NullPointerException
3. A test case that executes the fault, but does not result in an error state:
x=[1,1,1];
Expected: -1
Actual: -1
4. A test case that results in an error, but not a failure:
x=[1,0,1];
Expected:2
Actual: 2
浙公网安备 33010602011771号