KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > THashSetTest


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001-2006, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18

19
20 package gnu.trove;
21
22 import junit.framework.TestCase;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  *
34  * Created: Sat Nov 3 10:33:15 2001
35  *
36  * @author Eric D. Friedman
37  * @version $Id: THashSetTest.java,v 1.2 2006/11/14 23:56:35 robeden Exp $
38  */

39
40 public class THashSetTest extends TestCase {
41
42     protected Set JavaDoc s;
43     
44     public THashSetTest(String JavaDoc name) {
45         super(name);
46     }
47
48     public void setUp() throws Exception JavaDoc {
49         super.setUp();
50         s = new THashSet();
51     }
52
53     public void tearDown() throws Exception JavaDoc {
54         super.tearDown();
55         s = null;
56     }
57
58     public void testIsEmpty() throws Exception JavaDoc {
59         assertTrue("new set wasn't empty", s.isEmpty());
60
61         s.add("One");
62         assertTrue("set with element reports empty", ! s.isEmpty());
63         s.clear();
64         assertTrue("cleared set reports not-empty", s.isEmpty());
65     }
66
67     public void testContains() throws Exception JavaDoc {
68         Object JavaDoc o = "testContains";
69         s.add(o);
70         assertTrue("contains failed", s.contains(o));
71     }
72
73     public void testContainsAll() throws Exception JavaDoc {
74         Object JavaDoc[] o = { "Hello World", "Goodbye World", "Hello Goodbye" };
75         s.addAll(Arrays.asList(o));
76         for (int i = 0; i < o.length; i++) {
77             assertTrue(o[i].toString(),s.contains(o[i]));
78         }
79         assertTrue("containsAll failed: " + s,
80                    s.containsAll(Arrays.asList(o)));
81     }
82
83     public void testAdd() throws Exception JavaDoc {
84         assertTrue("add failed", s.add("One"));
85         assertTrue("duplicated add succeded", ! s.add("One"));
86     }
87
88     public void testRemove() throws Exception JavaDoc {
89         s.add("One");
90         s.add("Two");
91         assertTrue("One was not added", s.contains("One"));
92         assertTrue("One was not removed", s.remove("One"));
93         assertTrue("One was not removed", ! s.contains("One"));
94     }
95
96     public void testSize() throws Exception JavaDoc {
97         s = new THashSet();
98         assertEquals("initial size was not 0",0, s.size());
99
100         for (int i = 0; i < 99; i++) {
101             s.add(new Object JavaDoc());
102             assertEquals("size did not increase after add", i+1, s.size());
103         }
104     }
105
106     public void testClear() throws Exception JavaDoc {
107         s = new THashSet();
108         s.addAll(Arrays.asList(new String JavaDoc[] {"one","two","three"}));
109         assertEquals("size was not 3",3, s.size());
110         s.clear();
111         assertEquals("initial size was not 0",0, s.size());
112     }
113
114     public void testSerialize() throws Exception JavaDoc {
115         s = new THashSet();
116         s.addAll(Arrays.asList(new String JavaDoc[] {"one","two","three"}));
117         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
118         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
119         oos.writeObject(s);
120
121         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
122         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
123
124         THashSet s2 = (THashSet)ois.readObject();
125
126         assertEquals(s, s2);
127     }
128
129     public void testNormalLoad() throws Exception JavaDoc {
130         THashSet set = new THashSet(11, 0.5f);
131         assertEquals(set._maxSize, 11);
132         for (int i = 0; i < 12; i++) {
133             set.add(new Integer JavaDoc(i));
134         }
135         assertTrue(set._maxSize > 12);
136     }
137
138     public void testMaxLoad() throws Exception JavaDoc {
139         THashSet set = new THashSet(11, 1.0f);
140         assertEquals(10, set._maxSize);
141         for (int i = 0; i < 12; i++) {
142             set.add(new Integer JavaDoc(i));
143         }
144         assertTrue(set._maxSize > 12);
145     }
146
147     public void testToArray() {
148         Object JavaDoc[] str = { "hi", "bye", "hello", "goodbye" };
149         s.addAll(Arrays.asList(str));
150         Object JavaDoc[] res = s.toArray();
151         Arrays.sort(str);
152         Arrays.sort(res);
153         assertTrue(Arrays.equals(str, res));
154     }
155
156     public void testToArrayWithParams() {
157         Object JavaDoc[] str = { "hi", "bye", "hello", "goodbye" };
158         s.addAll(Arrays.asList(str));
159         String JavaDoc[] sink = new String JavaDoc[str.length + 2];
160         sink[sink.length - 1] = "residue";
161         sink[sink.length - 2] = "will be cleared";
162         Object JavaDoc[] res = s.toArray(sink);
163
164         Set JavaDoc copy = new HashSet JavaDoc();
165         copy.addAll(Arrays.asList(res));
166         
167         Set JavaDoc bogey = new HashSet JavaDoc();
168         bogey.addAll(Arrays.asList(str));
169         bogey.add("residue");
170         bogey.add(null);
171         assertEquals(bogey, copy);
172     }
173
174     public void testReusesRemovedSlotsOnCollision() {
175         THashSet set = new THashSet(11, 0.5f);
176
177         class Foo {
178             public int hashCode() {
179                 return 4;
180             }
181         }
182
183         Foo f1 = new Foo();
184         Foo f2 = new Foo();
185         Foo f3 = new Foo();
186         set.add(f1);
187         
188         int idx = set.insertionIndex(f2);
189         set.add(f2);
190         assertEquals(f2, set._set[idx]);
191         set.remove(f2);
192         assertEquals(set.REMOVED, set._set[idx]);
193         assertEquals(idx, set.insertionIndex(f3));
194         set.add(f3);
195         assertEquals(f3, set._set[idx]);
196     }
197
198     public void testRehashing() throws Exception JavaDoc {
199         for (int i = 0; i < 10000; i++) {
200             s.add(new Integer JavaDoc(i));
201         }
202     }
203
204     /**
205      * this tests that we throw when people violate the
206      * general contract for hashcode on java.lang.Object
207      */

208     public void testSomeBadlyWrittenObject() {
209       boolean didThrow = false;
210       int i = 0;
211       try {
212         for (; i < 101; i++) {
213             s.add(new Crap());
214         }
215       } catch (IllegalArgumentException JavaDoc e) {
216         didThrow = true;
217       }
218       assertTrue("expected THashSet to throw an IllegalArgumentException", didThrow);
219     }
220
221       // in this junk class, all instances hash to the same
222
// address, but some objects claim to be equal where
223
// others do not.
224
public static class Crap {
225         public boolean equals(Object JavaDoc other) {
226             return other instanceof Crap;
227         }
228
229         public int hashCode() {
230           return System.identityHashCode( this );
231         }
232     }
233
234     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
235       junit.textui.TestRunner.run(new THashSetTest("testBadlyWrittenObject"));
236     }
237 } // THashSetTests
238
Popular Tags