KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > sosnoski > util > stack > StringStackTest


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package tests.sosnoski.util.stack;
24
25 import com.sosnoski.util.stack.StringStack;
26
27 import junit.framework.*;
28
29 public class StringStackTest extends TestCase
30 {
31     private static final int TEST_ITEMS = 10;
32     
33     private StringStack m_stack;
34     
35     public StringStackTest(String JavaDoc name) {
36         super(name);
37     }
38     
39     public void setUp() {
40         m_stack = new StringStack();
41     }
42     
43     public void tearDown() {
44         m_stack = null;
45     }
46     
47     private String JavaDoc gen(int index) {
48         return Integer.toString(index);
49     }
50     
51     private boolean ifMatch(String JavaDoc a, int b) {
52         return gen(b).equals(a);
53     }
54     
55     private void fillSequential(int count) {
56         for (int i = 0; i < count; i++) {
57             m_stack.push(gen(i));
58         }
59     }
60     
61     public void testAdd() {
62         fillSequential(TEST_ITEMS);
63         assert(m_stack.size() == TEST_ITEMS);
64         fillSequential(TEST_ITEMS);
65         assert(m_stack.size() == TEST_ITEMS*2);
66     }
67     
68     public void testClear() {
69         fillSequential(TEST_ITEMS);
70         m_stack.clear();
71         assert(m_stack.size() == 0);
72         assert(m_stack.isEmpty());
73     }
74     
75     public void testPop() {
76         fillSequential(TEST_ITEMS);
77         assert(m_stack.size() == TEST_ITEMS);
78         for (int i = 0; i < TEST_ITEMS; i++) {
79             assert(ifMatch(m_stack.pop(), TEST_ITEMS-1-i));
80         }
81         assert(m_stack.size() == 0);
82         fillSequential(TEST_ITEMS);
83         assert(ifMatch(m_stack.pop(TEST_ITEMS/2), TEST_ITEMS/2));
84         for (int i = 0; i < TEST_ITEMS/2; i++) {
85             assert(ifMatch(m_stack.pop(), TEST_ITEMS/2-1-i));
86         }
87     }
88     
89     public void testPeek() {
90         fillSequential(TEST_ITEMS);
91         assert(ifMatch(m_stack.peek(), TEST_ITEMS-1));
92         for (int i = 0; i < TEST_ITEMS; i++) {
93             assert(ifMatch(m_stack.pop(), TEST_ITEMS-1-i));
94         }
95     }
96     
97     public void testToArray() {
98         fillSequential(TEST_ITEMS);
99         String JavaDoc[] array = m_stack.toArray();
100         assert(array.length == TEST_ITEMS);
101         for (int i = 0; i < TEST_ITEMS; i++) {
102             assert(ifMatch(array[i], i));
103         }
104     }
105     
106     public void testClone() {
107         fillSequential(TEST_ITEMS);
108         StringStack clone = (StringStack)m_stack.clone();
109         assert(clone.size() == TEST_ITEMS);
110         for (int i = 0; i < TEST_ITEMS; i++) {
111             assert(ifMatch(m_stack.pop(), TEST_ITEMS-1-i));
112         }
113     }
114     
115     public static Test suite() {
116         return new TestSuite(StringStackTest.class);
117     }
118     
119     public static void main(String JavaDoc[] args) {
120         String JavaDoc[] names = { StringStackTest.class.getName() };
121         junit.textui.TestRunner.main(names);
122     }
123 }
124
Popular Tags