    1   package baseCode.dataStructure;
    2
    3   import junit.framework.TestCase;
    4   import baseCode.dataStructure.Stack;
    5
    6   /***
    7    * <p>
    8    * </p>
    9    * <p>
    10   * </p>
    11   * <p>
    12   * Copyright (c) 2004
    13   * </p>
    14   * <p>
    15   * Institution: Columbia University
    16   * </p>
    17   *
    18   * @author Paul Pavlidis
    19   * @version $Id: TestStack.java,v 1.1 2005/03/17 13:58:41 pavlidis Exp $
    20   */
    21
    22  public class TestStack extends TestCase {
    23     private Stack stack = null;
    24     private Stack stackShort = null;
    25
    26     protected void setUp() throws Exception {
    27        super.setUp();
    28
    29        stack = new Stack();
    30        stack.push( new Integer( 1 ) );
    31        stack.push( new Integer( 2 ) );
    32        stack.push( new Integer( 3 ) );
    33        stack.push( new Integer( 4 ) );
    34        stack.push( new Integer( 5 ) );
    35
    36        stackShort = new Stack( 3 );
    37        stackShort.push( new Integer( 1 ) );
    38     }
    39
    40     protected void tearDown() throws Exception {
    41        stack = null;
    42        super.tearDown();
    43     }
    44
    45     public void testPop() {
    46        Integer expectedReturn = new Integer( 3 );
    47        stack.pop();
    48        stack.pop();
    49        Integer actualReturn = ( Integer ) stack.pop();
    50        assertEquals( "return value", expectedReturn, actualReturn );
    51     }
    52
    53     public void testPush() {
    54        Integer expectedReturn = new Integer( 100 );
    55        stack.push( new Integer( 101 ) );
    56        stack.push( new Integer( 102 ) );
    57        stack.push( new Integer( 103 ) );
    58        stack.push( expectedReturn );
    59        Integer actualReturn = ( Integer ) stack.pop();
    60        assertEquals( "return value", expectedReturn, actualReturn );
    61     }
    62
    63     public void testTop() {
    64        Integer expectedReturn = new Integer( 5 );
    65        Integer actualReturn = ( Integer ) stack.top();
    66        assertEquals( "return value", expectedReturn, actualReturn );
    67     }
    68
    69     public void testEmptyPop() {
    70        stackShort.pop();
    71        Integer actualReturn = ( Integer ) stackShort.pop();
    72        Integer expectedReturn = null;
    73        assertEquals( "return value", expectedReturn, actualReturn );
    74     }
    75
    76     public void testFullPush() {
    77        try {
    78           stackShort.push( new Integer( 495 ) );
    79           stackShort.push( new Integer( 495 ) );
    80           stackShort.push( new Integer( 495 ) );
    81           stackShort.push( new Integer( 495 ) );
    82           stackShort.push( new Integer( 495 ) );
    83           fail( "Should raise an IndexOutOfBoundsException" );
    84        } catch ( IndexOutOfBoundsException success ) {
    85        }
    86
    87     }
    88
    89  }