Software testing foundations homework2,
Homework 2
|
Below are two faulty programs. Each except des a test case that results in failure. Answer the following questions (in the next slide) about each program.
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
56 |
|
Questions |
|
Identify the fault.
If possible, identify a test case that does not execute
Fault. (Reachability)
If possible, identify a test case that executes the fault, butdoes not result in an error state.
If possible identify a test case that results in an error, butnot a failure.
Due Date: 23:59:59 March 15.
Please send your answer to tjuscsst@qq.com and post itto your blog. |
Solution: 1.1) the condition in the for loop should be I> = 0, otherwise return-1 directly to 0. 2) test: x = []; y = 23) test: x = [2, 3, 5]; y = 54) test: x = [2, 3, 5]; y = 92.1) The for loop should be open from the last bit of the array; otherwise, the first 02 is found) test: x = [] 3) test: x = [1, 1, 1] 4) test: x = [0, 1, 1]