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 FloatStack 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 TestFloatStack extends TestCase
32  {
33      FloatStack 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( TestFloatStack.class ) ;
44      }
45  
46      public static TestSuite suite() {
47          return new TestSuite(TestFloatStack.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 FloatStack() ;
58      }
59      
60      
61      /**
62       * Constructor for IntStackTest.
63       * @param arg0
64       */
65      public TestFloatStack( 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( 1.4f ) ;
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( float ii = 0; ii < 10; ii++ )
94          {    
95              stack.push( ii ) ;
96              assertTrue( ii == stack.peek() ) ;
97          }
98      }
99  
100     
101     public void testPop()
102     {
103         try
104         {
105             stack.pop() ;
106             fail("Pop should have thrown an EmptyStackException" ) ;
107         }
108         catch( EmptyStackException e )
109         {
110             assertNotNull( "EmptyStackException should not be null", e ) ;
111         }
112         
113         for( float ii = 0; ii < 10; ii++ )
114         {    
115             stack.push( ii ) ;
116             assertTrue( ii == stack.pop() ) ;
117         }
118 
119         for( float ii = 0; ii < 10; ii++ )
120         {    
121             stack.push( ii ) ;
122         }
123         for( float ii = 10; ii < 0; ii-- )
124         {    
125             stack.push( ii ) ;
126             assertTrue( ii == stack.pop() ) ;
127         }
128     }
129 
130     
131     public void testPush()
132     {
133         stack.push( ( float ) 1.4f ) ;
134         stack.push( ( float ) 2.67f ) ;
135         assertFalse( stack.empty() ) ;
136         assertTrue( 2.67f == stack.pop() ) ;
137         assertTrue( 1.4f == stack.pop() ) ;
138     }
139 
140     
141     public void testSearch()
142     {
143         stack.push( ( float ) 0 ) ;
144         stack.push( ( float ) 1 ) ;
145         assertTrue( 2 == stack.search( ( float ) 0 ) ) ;
146         stack.push( ( float ) 0 ) ;
147         assertTrue( 1 == stack.search( ( float ) 0 ) ) ;
148         stack.push( ( float ) 0 ) ;
149         assertTrue( 3 == stack.search( ( float ) 1 ) ) ;
150         assertTrue( -1 == stack.search( ( float ) 44 ) ) ;
151     }
152     
153     public void testArrayConstructor() {
154         float[] array = { 1.0f, 2.0f, 3.0f, 4.0f };
155         stack  = new FloatStack(array);
156         assertEquals(array.length,stack.size());
157         for(int i=array.length-1;i>=0;i--) {
158             assertEquals(array[i],stack.pop(),0.0f);
159         }
160     }
161     
162     public void testPeekN() {
163         float[] array = { 1.0f, 2.0f, 3.0f, 4.0f };
164         stack  = new FloatStack(array);
165         for(int i=array.length-1;i>=0;i--) {
166             assertEquals(array[i],stack.peek((array.length-1)-i),0.0f);
167         }
168     }
169     
170 }