// SortingDemoExceptionsTest1.java
//
// Demonstrates selectionSort and isSorted methods
// defined in class SortingDemoExceptions, which
// uses exceptions to detect inappropriate parameter
// values and an assertion to detect incorrect
// postconditions for selectionSort.
//
// See comments in main method for testing options.
//
// To compile this program in Java 1.4, type:
// javac -source 1.4 SortingDemoExceptionsTest1.java
// (Compile it normally in Java 1.5 or later.)
//
// To run this program with assertions enabled, type:
// java -ea SortingDemoExceptionsTest1
// (Otherwise, assertions are disabled.)
/**
* Test program for selectionSort and isSorted
* methods defined in class SortingDemoExceptions.
* Main method contains commented-out options to
* facilitate testing of parameter values
* including inappropriate and boundary cases.
*
* @author Dorothy L. Nixon
*/
public class SortingDemoExceptionsTest1
{
/**
* Main method.
*
* @param args command-line arguments. None needed;
*/
public static void main(String[] args)
{
short[] numbers = new short[] {5, -1, 9, 4, 8, 2, -3, 10, 3};
int lengthFilled = 6;
// To test various alternative values of numbers,
// both appropriate and inappropriate, uncomment one
// of the following lines at a time:
//
// numbers = new short[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
// numbers = new short[] {9, 8, 7, 6, 5, 4, 3, 2, 1};
// numbers = new short[] {2, 1};
// numbers = new short[] {1}; // trivial array (okay)
// numbers = new short[] {}; // empty array (okay)
// numbers = null; // no array (not okay)
// To test various alternative values of lengthFilled,
// both appropriate and inappropriate, uncomment one
// of the following lines at a time:
//
// lengthFilled = 0; // appropriate
// lengthFilled = numbers.length; // appropriate
// lengthFilled = -1; // inappropriate
// lengthFilled = numbers.length + 1; // inappropriate
// Sort numbers in ascending order. Test selectionSort:
SortingDemoExceptions.selectionSort(numbers, lengthFilled);
// Below is a test of the isSorted method, which
// determines whether a subarray is sorted. To test
// isSorted for unsorted arrays, comment out the above
// call to selectionSort.
boolean sorted = SortingDemoExceptions.isSorted(numbers,
lengthFilled);
System.out.println(
"According to the isSorted method, the array "
+ (sorted?"IS":"is NOT") + " sorted.");
// Display the numbers:
System.out.print("End test. Array contains: ");
for ( int i = 0; i < lengthFilled; i++ )
System.out.print(" " + numbers[i]);
System.out.println();
} // method main
} // class SortingDemoExceptionsTest1