package examples.stack2;
import examples.stack2.*;
import resources.*;
/**
* Author: W. M. Farmer
* Revised: February 21, 2007
*
* Description: Tests the public methods of the StackPlus class.
*
* @author W. M. Farmer
*/
public class TestStackPlus {
/**
* Tests the StackPlus class.
*/
public static void main(String args[]) {
StackPlus s = new StackPlus();
StackPlus t = new StackPlus();
StackPlus u = new StackPlus();
StackPlus v = new StackPlus();
printState(s);
testPush(s, new StringElement("0"));
testPush(s, new StringElement("1"));
testPush(s, new StringElement("2"));
testPush(s, new StringElement("3"));
testPush(t, new StringElement("0"));
testPush(t, new StringElement("1"));
testPush(t, new StringElement("2"));
testPush(t, new StringElement("3"));
testPop(s);
testPop(s);
testPush(s, new StringElement("4"));
testSetTop(s, new StringElement("5"));
testPush(u,s);
testPush(u,new StringElement("6"));
testPush(v,s);
testPush(v,t);
testSameStack(s,s);
testSameStack(s,t);
testSameStack(s,u);
testSameStack(u,u);
testSameStack(u,v);
testReset(s);
testReset(u);
testTop(s);
testPop(s);
}
private static void printState(StackPlus s) {
try {
System.out.println("Stack contents: " + s.toString() + ".");
System.out.println("Height = " + String.valueOf(s.height()) + ".");
if (s.height() != 0)
System.out.println("Top = " + s.top().toString() + ".");
else
System.out.println("Top is undefined.");
if (s.isEmpty())
System.out.println("Empty = true.");
else
System.out.println("Empty = false.");
System.out.println();
}
catch (EmptyStackException x) {
// EmptyStackException should not occur.
System.out.println(x.toString());
}
}
private static void testTop(StackPlus s) {
System.out.println("Top of " + s.toString() + ".");
try {
s.top();
}
catch (EmptyStackException x) {
System.out.println(x.toString());
}
printState(s);
}
private static void testPush(StackPlus s, Element e) {
System.out.println("Push " + e.toString() + " onto " +
s.toString() + ".");
s.push(e);
printState(s);
}
private static void testPop(StackPlus s) {
System.out.println("Pop " + s.toString() + ".");
try {
s.pop();
}
catch (EmptyStackException x) {
System.out.println(x.toString());
}
printState(s);
}
private static void testSetTop(StackPlus s, Element e) {
System.out.println("Set top of " + s.toString() +
" to " + e.toString() + ".");
try {
s.setTop(e);
}
catch (EmptyStackException x) {
System.out.println(x.toString());
}
printState(s);
}
private static void testReset(StackPlus s) {
System.out.println("Reset " + s.toString() + ".");
s.reset();
printState(s);
}
private static void testSameStack(StackPlus s, StackPlus t) {
boolean b = s.same(t);
System.out.println("Are the stacks " + s.toString() + " and " +
t.toString() + " the same? " + b);
System.out.println();
}
}