软件测试-作业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) i>0 should be i>=0
(2) x=[ ]; Expected: NullPointerException; Actual: NullPointerException
(3) x=[1,2,3]; y=2; Expected: 1 Actual: 1
(4) x=[1,2,3]; 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)for(int i = 0; i < x.length; i++) should be for(int i = x.length-1; i>=0; i--)
(2)x=[ ]; Expected: NullPointerException; Actual: NullPointerException
(3)x=[1,2,3]; Expected: -1; Actual: -1
(4)x=[0]; Expected: 0; Actual: 0