public int FindLast (int[] x, int y) { //effects:if x==null thro 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;< c9/>} } return-1;}
test:x=[2,3,5];y=2
Expected = 0
A) Identify the fault.
In the For loop, the condition i>0 should be changed to i>=0, otherwise the first element of the array will not be traversed, so that the first element will affect the expected result.
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=[2,3,5]; Y=3
Expected:1; Actually:1 equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[2,3,5]; Y=4
Expected:-1; Actually:-1 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null; Y=1
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, Identifu the first error state. Be sure to describe the complete state.
For a given test case, we can see that the program does not execute to the address labeled 0 in the array, so the final result state gives a value of-1, and the expected 0 inconsistency
f) Fix The fault and verify that the given test now produes the expected output.
To change the i>0 in the For loop to i>=0, the expected result and the actual result are all 0.
public static int Lastzero (int[] x) { //effects:if x==null throw nullpointerexpection //else return the index of th E 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.
The fault code is that the for loop is looped back from the previous loop, and a value equal to 0 returns the array subscript, which is the position of the first 0 in the array. Inconsistent with the requirements of the function
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=null;
Expected:nullpointerexception; Actually:nullpointerexception equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[2,3,5];
Expected:-1; Actually:-1 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null;
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, identify the first error state. Be sure to describe the complete state.
For a given test case x=[0,1,0], the function finds equal to 0 when accessing the first element, so it returns the array subscript 0 of the first element directly, and the expected 2 inconsistency.
f) Fix The fault and verify that the given test now produes the expected output.
Just change the For loop to for (int i =x.length-1;i>=0;i--)
So the expected results and the actual results are all 2.
public int countpositive (int[] x) { //effects:if x==null throw nullpointerexception //else return the number of< C2/>//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] //expected = 2
A) Identify the fault.
The function is to calculate the number of positive numbers in an array, and if the x[i]>=0 in the IF condition, the number of non-negative numbers to be judged should be changed to x[i]>0
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=null;
Expected:nullpointerexception; Actually:nullpointerexception equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[-2,3,5];
Expected:2; Actually:2 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null;
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, identify the first error state. Be sure to describe the complete state.
For a given test case x=[-4,2,0,2], when the third element 0 is accessed, the value of Count is incremented by 1, so that the last actual value is 3 instead of the expected 2, which is inconsistent.
f) Fix The fault and verify that the given test now produes the expected output.
If the x[i]>=0, judging the number of non-negative numbers, should be changed to x[i]>0
So the expected results and the actual results are all 2.
public static int Oddorpos (int[] x) { //effects:if x==null throw nullpointerexception //else return the Number of elements in X that //are either off 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] //expected = 3
A) Identify the fault.
If the condition of X[i]% 2 ==1 will fail, function is to determine the number of positive and odd numbers, and this condition does not take into account negative conditions, so will miss the odd number of negative numbers.
b) If possible, identify a test case this does not excute the fault.
Test cases where the failure code is not executed: x=null;
Expected:nullpointerexception; Actually:nullpointerexception equal!
c) If possible, identify a test case this executes the fault, but does not result in an error state
A test case that executes a fault code but does not result in an internal error (Error): x=[2,3,5];
Expected:2; Actually:2 equal!
D) If possible identify a test case this results in an error, and not a failure.
Test cases that cause internal errors but are not program invalidation (failure): X=null;
Expected:nullpointerexception; Actually:nullpointerexpection
e) for the given test case, identify the first error state. Be sure to describe the complete state.
For the determination of the first number, the remainder 2 is not equal to 1, so the judgment is not odd so that the actual value and the expected value are inconsistent
f) Fix The fault and verify that the given test now produes the expected output.
Change X[i]% 2 ==1 to x[i]% 2! = 0
So the expected results and the actual results are all 2.
Software Test section1.2. Exercises third question