View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.primitives;
18  
19  import java.util.EmptyStackException;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  /**
25   * Tests the BooleanStack class.
26   *
27   * @author Apache Directory Project
28   * @since Commons Primitives 1.1
29   * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
30   */
31  public class TestBooleanStack extends TestCase
32  {
33      BooleanStack stack = null ;
34      
35      
36      /**
37       * Runs the test. 
38       * 
39       * @param args nada
40       */
41      public static void main( String[] args )
42      {
43          junit.textui.TestRunner.run( TestBooleanStack.class ) ;
44      }
45  
46      public static TestSuite suite() {
47          return new TestSuite(TestBooleanStack.class);
48      }
49  
50      
51      /* (non-Javadoc)
52       * @see junit.framework.TestCase#setUp()
53       */
54      protected void setUp() throws Exception
55      {
56          super.setUp() ;
57          stack = new BooleanStack() ;
58      }
59      
60      
61      /**
62       * Constructor for IntStackTest.
63       * @param arg0
64       */
65      public TestBooleanStack( String arg0 )
66      {
67          super( arg0 ) ;
68      }
69  
70      
71      public void testEmpty()
72      {
73          assertTrue( "Newly created stacks should be empty", stack.empty() ) ;
74          stack.push( true ) ;
75          assertFalse( "Stack with item should not be empty", stack.empty() ) ;
76          stack.pop() ;
77          assertTrue( "Stack last int popped should be empty", stack.empty() ) ;
78      }
79  
80      
81      public void testPeek()
82      {
83          try
84          {
85              stack.peek() ;
86              fail("Peek should have thrown an EmptyStackException" ) ;
87          }
88          catch( EmptyStackException e )
89          {
90              assertNotNull( "EmptyStackException should not be null", e ) ;
91          }
92          
93          for( int ii = 0; ii < 10; ii++ )
94          {
95              if ( ii % 2 == 0 )
96              {
97                  stack.push( false ) ;
98                  assertFalse( stack.peek() ) ;
99              }
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 }