KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 /**
25  * Tests the CharStack class.
26  *
27  * @author Apache Directory Project
28  * @since Commons Primitives 1.1
29  * @version $Revision: 480451 $ $Date: 2006-11-28 23:45:08 -0800 (Tue, 28 Nov 2006) $
30  */

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

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

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

65     public TestCharStack( String JavaDoc 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( 'A' ) ;
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 JavaDoc e )
89         {
90             assertNotNull( "EmptyStackException should not be null", e ) ;
91         }
92         
93         for( char 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 JavaDoc e )
109         {
110             assertNotNull( "EmptyStackException should not be null", e ) ;
111         }
112         
113         for( char ii = 0; ii < 10; ii++ )
114         {
115             stack.push( ii ) ;
116             assertTrue( ii == stack.pop() ) ;
117         }
118
119         for( char ii = 0; ii < 10; ii++ )
120         {
121             stack.push( ii ) ;
122         }
123         for( char ii = 10; ii < 0; ii-- )
124         {
125             stack.push( ( char ) ii ) ;
126             assertTrue( ii == stack.pop() ) ;
127         }
128     }
129
130     
131     public void testPush()
132     {
133         stack.push( ( char ) 0 ) ;
134         stack.push( ( char ) 0 ) ;
135         assertFalse( stack.empty() ) ;
136         assertTrue( 0 == stack.pop() ) ;
137         assertTrue( 0 == stack.pop() ) ;
138     }
139
140     
141     public void testSearch()
142     {
143         stack.push( ( char ) 0 ) ;
144         stack.push( ( char ) 1 ) ;
145         assertTrue( 2 == stack.search( ( char ) 0 ) ) ;
146         stack.push( ( char ) 0 ) ;
147         assertTrue( 1 == stack.search( ( char ) 0 ) ) ;
148         stack.push( ( char ) 0 ) ;
149         assertTrue( 3 == stack.search( ( char ) 1 ) ) ;
150         assertTrue( -1 == stack.search( ( char ) 44 ) ) ;
151     }
152
153     public void testArrayConstructor() {
154         char[] array = { 1, 2, 3, 4 };
155         stack = new CharStack(array);
156         assertEquals(array.length,stack.size());
157         for(int i=array.length-1;i>=0;i--) {
158             assertEquals(array[i],stack.pop());
159         }
160     }
161     
162     public void testPeekN() {
163         char[] array = { 1, 2, 3, 4 };
164         stack = new CharStack(array);
165         for(int i=array.length-1;i>=0;i--) {
166             assertEquals(array[i],stack.peek((array.length-1)-i));
167         }
168     }
169 }
170
Popular Tags