KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > sosnoski > util > hashset > StringHashSetTest


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.hashset;
24
25 import java.util.Iterator JavaDoc;
26
27 import com.sosnoski.util.hashset.StringHashSet;
28
29 import junit.framework.*;
30
31 public class StringHashSetTest extends TestCase
32 {
33     private static final int TEST_ITEMS = 80;
34
35     protected StringHashSet m_hashset;
36
37     private String JavaDoc[] m_stringTable;
38
39     public StringHashSetTest(String JavaDoc name) {
40         super(name);
41         m_stringTable = new String JavaDoc[TEST_ITEMS];
42         for (int i = 0; i < TEST_ITEMS; i++) {
43             m_stringTable[i] = Integer.toString(i);
44         }
45     }
46
47     public void tearDown() {
48         m_hashset.clear();
49     }
50
51     private String JavaDoc gen(int index) {
52         if (index >= 0 && index < TEST_ITEMS) {
53             return m_stringTable[index];
54         } else {
55             return Integer.toString(index);
56         }
57     }
58
59     private void fillSequential(int count) {
60         for (int i = 0; i < count; i++) {
61             assert(m_hashset.add(gen(i)));
62         }
63     }
64
65     public void testAdd() {
66         fillSequential(TEST_ITEMS);
67         assert(m_hashset.size() == TEST_ITEMS);
68         for (int i = 0; i < TEST_ITEMS; i++) {
69             assert(m_hashset.contains(gen(i)));
70         }
71         assert(!m_hashset.add(gen(1)));
72         assert(m_hashset.add(gen(-1)));
73     }
74
75     public void testClear() {
76         fillSequential(TEST_ITEMS);
77         m_hashset.clear();
78         assert(m_hashset.size() == 0);
79     }
80
81     public void testContains() {
82         fillSequential(TEST_ITEMS);
83         assert(m_hashset.size() == TEST_ITEMS);
84         for (int i = 0; i < TEST_ITEMS; i++) {
85             assert(m_hashset.contains(gen(i)));
86         }
87         for (int i = TEST_ITEMS; i < 200; i++) {
88             assert(!m_hashset.contains(gen(i)));
89         }
90     }
91
92     public void testRemove() {
93         fillSequential(TEST_ITEMS);
94         assert(m_hashset.remove(gen(TEST_ITEMS/2)));
95         assert(m_hashset.size() == TEST_ITEMS-1);
96         assert(!m_hashset.remove(gen(TEST_ITEMS/2)));
97         for (int i = 0; i < TEST_ITEMS/2; i++) {
98             assert(m_hashset.contains(gen(i)));
99         }
100         assert(!m_hashset.contains(gen(TEST_ITEMS/2)));
101         for (int i = TEST_ITEMS/2+1; i < TEST_ITEMS; i++) {
102             assert(m_hashset.contains(gen(i)));
103         }
104         assert(!m_hashset.remove(gen(-1)));
105         assert(m_hashset.size() == TEST_ITEMS-1);
106     }
107
108     public void testIterator() {
109         fillSequential(TEST_ITEMS);
110         Iterator JavaDoc iter = m_hashset.iterator();
111         int count = 0;
112         while (iter.hasNext()) {
113             count++;
114             assert(m_hashset.contains((String JavaDoc)iter.next()));
115         }
116         assert(count == TEST_ITEMS);
117     }
118
119     public void testClone() {
120         fillSequential(TEST_ITEMS);
121         StringHashSet clone = (StringHashSet)m_hashset.clone();
122         assert(clone.size() == TEST_ITEMS);
123         for (int i = 0; i < TEST_ITEMS; i++) {
124             assert(clone.contains(gen(i)));
125         }
126     }
127
128     public static Test suite() {
129         TestSuite suite = new TestSuite();
130         suite.addTest(new TestSuite(TestStandard.class));
131         suite.addTest(new TestSuite(TestIdentCompare.class));
132         suite.addTest(new TestSuite(TestIdentHash.class));
133         return suite;
134     }
135
136     public static void main(String JavaDoc[] args) {
137         String JavaDoc[] names = { StringHashSetTest.class.getName() };
138         junit.textui.TestRunner.main(names);
139     }
140
141     public static class TestStandard extends StringHashSetTest
142     {
143         public TestStandard(String JavaDoc name) {
144             super(name);
145         }
146
147         public void setUp() {
148             m_hashset = new StringHashSet(StringHashSet.STANDARD_HASH);
149         }
150     }
151
152     public static class TestIdentCompare extends StringHashSetTest
153     {
154         public TestIdentCompare(String JavaDoc name) {
155             super(name);
156         }
157
158         public void setUp() {
159             m_hashset = new StringHashSet(StringHashSet.IDENTITY_COMP);
160         }
161     }
162
163     public static class TestIdentHash extends StringHashSetTest
164     {
165         public TestIdentHash(String JavaDoc name) {
166             super(name);
167         }
168
169         public void setUp() {
170             m_hashset = new StringHashSet(StringHashSet.IDENTITY_HASH);
171         }
172     }
173 }
174
Popular Tags