Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions about each progrma.
(a) Indentify the fault.
(b) If possible, identify a test case that does not execute the fault.
(c) If possible, identify a test case that executes the fault, but does not result in an error state.
(d) If possible, identify a test case that resutls in an error, but not a failure. Hint: Don't forget about the program counter.
(e) For the given test case, identify the first error state. Be sure to describe the complete state.
(f) Fix the fault and verify that the given test now produces the expected output.
Program 1:
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 sunch 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) fault: in the for loop, it shoule be:
for(int i = x.length-1; i >= 0; i--)
(b) test case: x = [1]; y = 2, it doesn't execute i > 0
(c) test case: x = [1, 2, 3]; y = 3, it executes x > 0 but doesn't go to i = 0
(d) test case: x = [1, 2, 3]; y = -1, it goes to i = 0 but the result is right
(e) first error state: x = [2, 3, 4], y = 2, i = 0, it should compare x[0] to y, but it doesn't
(f) see(a)
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, 2]
// Excepted = 2;
(a) fault: in the for loop, it shoule be:
for(int i = x.length-1; i >= 0; i--)
(b) test: x = []
(c) test: x = [1]
(d) test: x = [0, 1]
(e) first error state: x = [0, 1, 0], i = 0, after that i should decrease 1
(f) see (a)
Program 3:
public int countPositive(int[] x){
//Effects: If x==null throw NullPointerException
// else return the number of
// positive elements in x.
int count = 0;
for(int i = 0; i < x.length; i++)
{
if(x[i] >= 0)
{
count++;
}
}
return count;
}
//test: x= [-4, 2, 0, 2]
// Excepted = 2
(a) fault: it shoule be:
if(x[i] > 0)
(b) test case: x = []
(c) test case: x = [1]
(d) if it goes to an error, it must results in failure
(e) first error state: x = [-4, 2, 0, 2], i = 2, after that it should continue the for loop instead of increasing the count
(f) see (a)
Program 4:
public static int oddOrPos(int[] x){
//Effects: if x==null throw NullPointerException
// else return the number of elements in x that
// are either odd or positive (or both)
int count = 0;
for(int i =0; i < x.length; i++)
{
if(x[i]%2 == 1 || x[i] > 0)
{
count++;
}
}
return count;
}
//test: x = [-3, -2, 0, 1, 4]
// Excepted = 3
(a) fault: it should be:
if(x[i] % 2 == -1 || x[i] > 0)
(b) test: x = []
(c) test: x = [1]
(d) test: the test case that results in an error, but not a failure does not exist
(e) first error state: x = [-3, -2, 0, 1, 4], i = 0, after that it should execute count++, but it doesn't
(f) see(a)