📄 stacktester.java
字号:
/**
* StackTester.java
* @version 1.0.0
* @author Jason Corless
*/
/**
* A program to test our implementation of the Stack classd
*
*/
public class StackTester
{
public static void main ( String[] args )
{
MyStack s1 = new MyStack(10);
/*
* Test pop from empty stack
*/
try
{
s1.pop();
System.out.println ("Failed: pop on empty stack.");
}
catch ( MyStackEmptyException e )
{
System.out.println ("Passed: pop on empty stack.");
}
/*
* Test top from empty stack
*/
try
{
s1.top();
System.out.println ("Failed: top on empty stack.");
}
catch ( MyStackEmptyException e )
{
System.out.println ("Passed: top on empty stack.");
}
/*
* Test stack full and element ordering
*/
try
{
for ( int i = 0; i < 11; i++ )
{
s1.push ( new Integer ( i ) );
}
System.out.println ("Failed: more than max elements added to stack");
}
catch ( MyStackFullException e )
{
if (s1.size() != 10 )
{
System.out.println ("Failed: stack full before max elements");
}
else
{
System.out.println ("Passed: stack full");
}
}
finally
{
try
{
boolean pass = true;
for ( int i = 9; i >= 0; i-- )
{
Integer myInt = (Integer)s1.pop();
if ( myInt.intValue() != i )
{
System.out.println ("Failed: ordering on stack elements");
pass = false;
break;
}
}
if ( pass )
{
System.out.println ("Passed: ordering on stack elements");
}
}
catch (Exception e )
{
System.out.println ("Failed: Exception unexpected testing element ordering");
}
}
MyStack s2 = new MyStack ();
/*
* Test makeEmpty() and test default constructor allocates 1000 elements
*/
try
{
for ( int i = 0; i < 1000; i++ )
{
s2.push ( "Bob " + i );
}
/*
* Test top()
*/
try
{
String s = (String)s2.top();
if ( !s.equals("Bob " + 999 ) )
{
System.out.println ("Failed: top returned wrong value");
}
else
{
System.out.println ("Passed: top returned correct element");
}
}
catch ( Exception e )
{
System.out.println ("Failed: top threw exception");
}
if ( s2.size() != 1000 )
{
System.out.println ("Failed: wrong number of elements");
}
s2.makeEmpty();
if ( s2.size() != 0 )
{
System.out.println ("Failed: wrong number of elements");
}
try
{
s2.pop();
System.out.println ("Failed: pop on empty stack!");
}
catch ( MyStackEmptyException e )
{
System.out.println ("Passed: pop after makeEmpty()");
}
try
{
s2.push ( new Integer(5) );
s2.push ( new Integer(4) );
if ( ((Integer)(s2.pop())).intValue() != 4 )
{
System.out.println ("Failed: push elements after makeEmpty()");
}
else
{
System.out.println ("Passed: elements pushed after makeEmpty()");
}
}
catch ( Exception e )
{
System.out.println ("Failed: push elements after makeEmpty()");
}
}
catch ( Exception e )
{
System.out.println ("Failed: Exception adding elements");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -