// ShortSequenceTest3.java
// Tests the insert method of class ShortSequence
// for in-range and out-of-range indices.
public class ShortSequenceTest3 {
public static void main(String[] args)
{
if ( args.length == 0 ) {
System.out.println("Enter an integer to test insert "
+ "method of class ShortSequence.");
System.out.println("The range is 0 to 3, inclusive. "
+ "Test out-of-range values too.");
System.exit(0);
} // if
ShortSequence sequence2 = new ShortSequence(2);
System.out.println("sequence2 is initially empty with capacity 2.");
System.out.println(sequence2);
System.out.println();
System.out.println("We will now append 4 elements.");
sequence2.append((short) 100);
sequence2.append((short) 110);
sequence2.append((short) 120);
sequence2.append((short) 130);
list(sequence2, "sequence2", 12, 4);
System.out.println();
int index = Integer.parseInt(args[0]);
System.out.println("We will now insert another element "
+ "at your specified location " + index + ".");
boolean outOfRange = ( index < 0 || index >= 4 );
testInsert(sequence2, "sequence2", (short) 250,
index, outOfRange, 12, 5);
} // method main
public static void list(ShortSequence sequence,
String name,
int capacityShouldBe,
int numberOfElementsShouldBe)
{
System.out.println(name + ": capacity " + sequence.getCapacity() +
" should be " + capacityShouldBe +
", number of elements " +
sequence.getLengthFilled() +
" should be " + numberOfElementsShouldBe);
System.out.println("Following are the contents of " +
name + ":");
System.out.println(sequence);
System.out.println("Finished displaying contents of " +
name + ".");
} // method list
public static void testInsert(ShortSequence sequence,
String name,
short dataToBeInserted,
int position,
boolean shouldBeOutOfRange,
int newCapacityShouldBe,
int newNumberOfElementsShouldBe)
{
System.out.println(name + ".insert(" + position +
", " + dataToBeInserted + ")");
if ( shouldBeOutOfRange )
System.out.println("SHOULD be out of range.");
else
System.out.println("Should NOT be out of range.");
sequence.insert(position, dataToBeInserted);
list(sequence, name, newCapacityShouldBe, newNumberOfElementsShouldBe);
} // method testInsert
} // class ShortSequenceTest3