Job Title
Below is four faulty programs. Each includes a test case, results in failure.
Answer the following questions (in the next slide).
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
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
Problem
Identify The fault.
If possible, identify a test case this does not execute the
Fault. (reachability)
If possible, identify a test case this executes the fault, but
does not the result in a error state.
If possible identify a test case this results in an error, but
Not a failure.
Job Answer:
1. The For loop of the first program should I be greater than or equal to 0,
The second procedure should be changed to for (int i=x.length-1; i >= 0; i--);
2. The first program x = [], y = 2,
The second program will execute the fault regardless of how it is entered.
3. The first program x = [2,3,4],y = 4,
The second program test:x = [3];
4 First program test:x = [2],
The second program test:x = [1,0,3].
Tjuscsst Second Assignment