(1)findLast
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
(a) Identify the fault:
for (int i=x.length-1; i > 0; i--) ,"i>0" should be "i>=0",otherwise, the 0 index can not be included
(b)If possible, identify a test case that does not execute the fault. (Reachability)
test: x=null;y=1;
expected=null;
fact = null;
(c)If possible, identify a test case that executes the fault, but does not result in an error state.
test:x=[3,2,5];y=2
expected=1;
fact = 1;
(d)If possible identify a test case that results in an error, but not a failure.
test:x=[3,2,5] ; y=6
expected = -1;
fact = -1;
(2)lastZero
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
(a) Identify the fault:
(b)If possible, identify a test case that does not execute the fault. (Reachability)
no possible
(c)If possible, identify a test case that executes the fault, but does not result in an error state.
test:x=[1]
expected=-1;
fact = -1;
(d)If possible identify a test case that results in an error, but not a failure.
test:x=[1,0,1]
expected = 1;
fact = 1;
tips:
Software Fault : A static defect in the software (软件中的设计错误,就是代码本身就写错了)
Software Failure : External, incorrect behavior with respect to the requirements or other description of the expected behavior (失效,与预期不一样,表现出来的)
Software Error : An incorrect internal state that is the manifestation of some fault (在运行过程中的)
fault -> error -> failure,软件中的缺陷在运行时会产生error,当error累计到一定程度时会导致失效,这些都是bug