001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections.primitives ;
018
019
020import java.util.EmptyStackException;
021
022import junit.framework.TestCase;
023import junit.framework.TestSuite;
024
025
026/**
027 * Tests the ShortStack class.
028 *
029 * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
030 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
031 */
032public class TestShortStack extends TestCase
033{
034    ShortStack stack = null ;
035    
036    
037    /**
038     * Runs the test. 
039     * 
040     * @param args nada
041     */
042    public static void main( String[] args )
043    {
044        junit.textui.TestRunner.run( TestShortStack.class ) ;
045    }
046
047    public static TestSuite suite() {
048        return new TestSuite(TestShortStack.class);
049    }
050
051    
052    /* (non-Javadoc)
053     * @see junit.framework.TestCase#setUp()
054     */
055    protected void setUp() throws Exception
056    {
057        super.setUp() ;
058        stack = new ShortStack() ;
059    }
060    
061    
062    /**
063     * Constructor for IntStackTest.
064     * @param arg0
065     */
066    public TestShortStack( String arg0 )
067    {
068        super( arg0 ) ;
069    }
070
071    
072    public void testEmpty()
073    {
074        assertTrue( "Newly created stacks should be empty", stack.empty() ) ;
075        stack.push( ( short ) 12342 ) ;
076        assertFalse( "Stack with item should not be empty", stack.empty() ) ;
077        stack.pop() ;
078        assertTrue( "Stack last int popped should be empty", stack.empty() ) ;
079    }
080
081    
082    public void testPeek()
083    {
084        try
085        {
086            stack.peek() ;
087            fail("Peek should have thrown an EmptyStackException" ) ;
088        }
089        catch( EmptyStackException e )
090        {
091            assertNotNull( "EmptyStackException should not be null", e ) ;
092        }
093        
094        for( int ii = 0; ii < 10; ii++ )
095        {    
096            stack.push( ( short ) ii ) ;
097            assertTrue( ii == stack.peek() ) ;
098        }
099    }
100
101    
102    public void testPop()
103    {
104        try
105        {
106            stack.pop() ;
107            fail("Pop should have thrown an EmptyStackException" ) ;
108        }
109        catch( EmptyStackException e )
110        {
111            assertNotNull( "EmptyStackException should not be null", e ) ;
112        }
113        
114        for( short ii = 0; ii < 10; ii++ )
115        {    
116            stack.push( ii ) ;
117            assertTrue( ii == stack.pop() ) ;
118        }
119
120        for( short ii = 0; ii < 10; ii++ )
121        {    
122            stack.push( ii ) ;
123        }
124        for( short ii = 10; ii < 0; ii-- )
125        {    
126            stack.push( ii ) ;
127            assertTrue( ii == stack.pop() ) ;
128        }
129    }
130
131    
132    public void testPush()
133    {
134        stack.push( ( short ) 0 ) ;
135        stack.push( ( short ) 0 ) ;
136        assertFalse( stack.empty() ) ;
137        assertTrue( 0 == stack.pop() ) ;
138        assertTrue( 0 == stack.pop() ) ;
139    }
140
141    
142    public void testSearch()
143    {
144        stack.push( ( short ) 0 ) ;
145        stack.push( ( short ) 1 ) ;
146        assertTrue( 2 == stack.search( ( short ) 0 ) ) ;
147        stack.push( ( short ) 0 ) ;
148        assertTrue( 1 == stack.search( ( short ) 0 ) ) ;
149        stack.push( ( short ) 0 ) ;
150        assertTrue( 3 == stack.search( ( short ) 1 ) ) ;
151        assertTrue( -1 == stack.search( ( short ) 44 ) ) ;
152    }
153
154    public void testArrayConstructor() {
155        short[] array = { 1, 2, 3, 4 };
156        stack  = new ShortStack(array);
157        assertEquals(array.length,stack.size());
158        for(int i=array.length-1;i>=0;i--) {
159            assertEquals(array[i],stack.pop());
160        }
161    }
162    
163    public void testPeekN() {
164        short[] array = { 1, 2, 3, 4 };
165        stack  = new ShortStack(array);
166        for(int i=array.length-1;i>=0;i--) {
167            assertEquals(array[i],stack.peek((array.length-1)-i));
168        }
169    }
170}