KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > primitives > TestShortStack


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
20 import java.util.EmptyStackException JavaDoc;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25
26 /**
27  * Tests the ShortStack class.
28  *
29  * @author <a HREF="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
30  * @version $Revision: 480451 $ $Date: 2006-11-28 23:45:08 -0800 (Tue, 28 Nov 2006) $
31  */

32 public class TestShortStack extends TestCase
33 {
34     ShortStack stack = null ;
35     
36     
37     /**
38      * Runs the test.
39      *
40      * @param args nada
41      */

42     public static void main( String JavaDoc[] args )
43     {
44         junit.textui.TestRunner.run( TestShortStack.class ) ;
45     }
46
47     public static TestSuite suite() {
48         return new TestSuite(TestShortStack.class);
49     }
50
51     
52     /* (non-Javadoc)
53      * @see junit.framework.TestCase#setUp()
54      */

55     protected void setUp() throws Exception JavaDoc
56     {
57         super.setUp() ;
58         stack = new ShortStack() ;
59     }
60     
61     
62     /**
63      * Constructor for IntStackTest.
64      * @param arg0
65      */

66     public TestShortStack( String JavaDoc arg0 )
67     {
68         super( arg0 ) ;
69     }
70
71     
72     public void testEmpty()
73     {
74         assertTrue( "Newly created stacks should be empty", stack.empty() ) ;
75         stack.push( ( short ) 12342 ) ;
76         assertFalse( "Stack with item should not be empty", stack.empty() ) ;
77         stack.pop() ;
78         assertTrue( "Stack last int popped should be empty", stack.empty() ) ;
79     }
80
81     
82     public void testPeek()
83     {
84         try
85         {
86             stack.peek() ;
87             fail("Peek should have thrown an EmptyStackException" ) ;
88         }
89         catch( EmptyStackException JavaDoc e )
90         {
91             assertNotNull( "EmptyStackException should not be null", e ) ;
92         }
93         
94         for( int ii = 0; ii < 10; ii++ )
95         {
96             stack.push( ( short ) ii ) ;
97             assertTrue( ii == stack.peek() ) ;
98         }
99     }
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 JavaDoc 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 }
171
Popular Tags