Testing Pre- and Post-Conditions

In many cases, your methods will not work with certain inputs. A frequent example: How will your method behave if it is passed a null pointer? Have a look at the javadoc of the SearchArray constructor. It clearly mentions the precondition as 'The input array to the constructor cannot be null'. You will find the following code in the constructor ensuring that this precondition is always met.

//Precondition checking code
if(array==null){
	throw new NullPointerException("Constructor precondition " +
		"not met: Input array cannot be null");
}


A unit test case for this precondition is provided in the main method:
//test case to check constructor precondition 
/*
System.out.println("Passing null array. Expecting error statement.");
int[] testArray2=null;
s=new SearchArray(testArray2,0,100);
*/
Uncomment the above section and run the code. A null array is passed in the constructor and an error message is expected which you will find in the output console. Once you have confirmed through this test case that you pre condition checking code works, you should comment back the above section so that your program can reach the subsequent test cases below the above test case. Note an important aspect of all test cases uptill now: They all have tried to break the code by explicitly trying to violate the set conditions (class invariants and pre conditions). Such test cases ensure that in cases of erroneous input, the code exits gracefully with a meaningful message printed to the user.

Objective 2 for this lab

Read the javadoc for the search() method of SearchArray. It mentions the precondition as 'the least possible value of x can be minVal and the max possible value can be maxVal' where x is the value being searched for.
Your second objective for this lab is to write code which ensures that the above precondition is met. Enter your code in the following section inside the search() method.
/*
 * Enter Pre-condition checking code here
 */

Next you should write a unit test case checking if the above code works. Enter the testing code in the following section of the main method.
/*
 * Precondition checking test case for search()
 */