// ShortSequenceTest1.java
// Tests the insert and append methods of class ShortSequence.
// Tests insert for in-range indices only
public class ShortSequenceTest1 {
public static void main(String[] args)
{
System.out.println();
System.out.println("We will now try to instantiate an "
+ "ShortSequence object with capacity -1. ");
System.out.println("It SHOULD complain.");
try {
ShortSequence sequence1 = new ShortSequence(-1);
System.out.println("Creation of sequence1 was successful."
+ " It should not have been.");
} catch ( IllegalArgumentException iae ) {
System.out.println(iae.toString());
System.out.println("ShortSequence sequence1 was not created."
+ " Good!");
} // catch
System.out.println();
System.out.println("Next, we will now try to instantiate an "
+ "ShortSequence object with capacity 0. ");
System.out.println("This should work OK, without complaint.");
ShortSequence sequence2 = new ShortSequence(0);
System.out.println("ShortSequence sequence2 was created successfully,");
System.out.println("initially empty with capacity 0.");
list(sequence2, "sequence2", 0, 0);
System.out.println("We will now append 4 elements, "
+ "then test our access to them.");
System.out.println();
testAppend(sequence2, "sequence2", (short) 100, 10, 1);
testAppend(sequence2, "sequence2", (short) 110, 10, 2);
testAppend(sequence2, "sequence2", (short) 120, 10, 3);
testAppend(sequence2, "sequence2", (short) 130, 10, 4);
System.out.println("Next, we will test the getNumberAt method.");
ConsoleInput.pause();
testGetNumberAt(sequence2, "sequence2", 0, false);
testGetNumberAt(sequence2, "sequence2", 3, false);
testGetNumberAt(sequence2, "sequence2", -1, true);
testGetNumberAt(sequence2, "sequence2", 4, true);
list(sequence2, "sequence2", 10, 4);
} // 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 current contents of " +
name + ":");
System.out.println(sequence);
System.out.println("Finished displaying current contents of " +
name + ".");
ConsoleInput.pause();
} // method list
public static void testAppend(ShortSequence sequence,
String name,
short dataToBeappended,
int newCapacityShouldBe,
int newNumberOfElementsShouldBe)
{
System.out.println(name + ".append(" + dataToBeappended +
")");
sequence.append(dataToBeappended);
list(sequence, name, newCapacityShouldBe, newNumberOfElementsShouldBe);
} // method testAppend
public static void testGetNumberAt(ShortSequence sequence,
String name,
int position,
boolean shouldBeOutOfRange)
{
System.out.println(name + ".getNumberAt(" + position +")");
if ( shouldBeOutOfRange )
System.out.println("SHOULD be out of range.");
else
System.out.println("Should NOT be out of range.");
try {
int n = sequence.getNumberAt(position);
System.out.println("In " + name
+ ", the value at location "
+ position + " is " + n + ".");
} catch ( IndexOutOfBoundsException ioore ) {
System.out.println(ioore.toString());
} // catch
System.out.println();
} // method testGetNumberAt
} // class ShortSequenceTest1