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
019import java.util.EmptyStackException;
020
021import junit.framework.TestCase;
022import junit.framework.TestSuite;
023
024/**
025 * Tests the BooleanStack class.
026 *
027 * @author Apache Directory Project
028 * @since Commons Primitives 1.1
029 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
030 */
031public class TestBooleanStack extends TestCase
032{
033    BooleanStack stack = null ;
034    
035    
036    /**
037     * Runs the test. 
038     * 
039     * @param args nada
040     */
041    public static void main( String[] args )
042    {
043        junit.textui.TestRunner.run( TestBooleanStack.class ) ;
044    }
045
046    public static TestSuite suite() {
047        return new TestSuite(TestBooleanStack.class);
048    }
049
050    
051    /* (non-Javadoc)
052     * @see junit.framework.TestCase#setUp()
053     */
054    protected void setUp() throws Exception
055    {
056        super.setUp() ;
057        stack = new BooleanStack() ;
058    }
059    
060    
061    /**
062     * Constructor for IntStackTest.
063     * @param arg0
064     */
065    public TestBooleanStack( String arg0 )
066    {
067        super( arg0 ) ;
068    }
069
070    
071    public void testEmpty()
072    {
073        assertTrue( "Newly created stacks should be empty", stack.empty() ) ;
074        stack.push( true ) ;
075        assertFalse( "Stack with item should not be empty", stack.empty() ) ;
076        stack.pop() ;
077        assertTrue( "Stack last int popped should be empty", stack.empty() ) ;
078    }
079
080    
081    public void testPeek()
082    {
083        try
084        {
085            stack.peek() ;
086            fail("Peek should have thrown an EmptyStackException" ) ;
087        }
088        catch( EmptyStackException e )
089        {
090            assertNotNull( "EmptyStackException should not be null", e ) ;
091        }
092        
093        for( int ii = 0; ii < 10; ii++ )
094        {
095            if ( ii % 2 == 0 )
096            {
097                stack.push( false ) ;
098                assertFalse( stack.peek() ) ;
099            }
100            else
101            {
102                stack.push( true ) ;
103                assertTrue( stack.peek() ) ;
104            }
105        }
106    }
107
108    
109    public void testPop()
110    {
111        try
112        {
113            stack.pop() ;
114            fail("Pop should have thrown an EmptyStackException" ) ;
115        }
116        catch( EmptyStackException e )
117        {
118            assertNotNull( "EmptyStackException should not be null", e ) ;
119        }
120        
121        for( int ii = 0; ii < 10; ii++ )
122        {    
123            if ( ii % 2 == 0 )
124            {
125                stack.push( false ) ;
126                assertFalse( stack.pop() ) ;
127            }
128            else
129            {
130                stack.push( true ) ;
131                assertTrue( stack.pop() ) ;
132            }
133        }
134
135        for( int ii = 0; ii < 10; ii++ )
136        {    
137            if ( ii % 2 == 0 )
138            {
139                stack.push( false ) ;
140            }
141            else
142            {
143                stack.push( true ) ;
144            }
145        }
146
147        for( short ii = 10; ii < 0; ii-- )
148        {    
149            if ( ii % 2 == 0 )
150            {
151                stack.push( false ) ;
152                assertFalse( stack.pop() ) ;
153            }
154            else
155            {
156                stack.push( true ) ;
157                assertTrue( stack.pop() ) ;
158            }
159        }
160    }
161
162    
163    public void testPush()
164    {
165        stack.push( false ) ;
166        stack.push( false ) ;
167        stack.push( true ) ;
168        assertFalse( stack.empty() ) ;
169        assertTrue( stack.pop() ) ;
170        assertFalse( stack.pop() ) ;
171        assertFalse( stack.pop() ) ;
172    }
173
174    
175    public void testSearch()
176    {
177        stack.push( false ) ;
178        assertTrue( -1 == stack.search( true ) ) ;
179        stack.push( true ) ;
180        assertTrue( 2 == stack.search( false ) ) ;
181        stack.push( false ) ;
182        assertTrue( 1 == stack.search( false ) ) ;
183        stack.push( false ) ;
184        assertTrue( 3 == stack.search( true ) ) ;
185    }
186    
187    public void testArrayConstructor() {
188        boolean[] array = { true, false, true, true };
189        stack  = new BooleanStack(array);
190        assertEquals(array.length,stack.size());
191        for(int i=array.length-1;i>=0;i--) {
192            assertEquals(array[i],stack.pop());
193        }
194    }
195    
196    public void testPeekN() {
197        boolean[] array = { true, false, true, true };
198        stack  = new BooleanStack(array);
199        for(int i=array.length-1;i>=0;i--) {
200            assertEquals(array[i],stack.peek((array.length-1)-i));
201        }
202    }
203    
204}