001: ///////////////////////////////////////////////////////////////////////////////
002: // Copyright (c) 2001-2006, Eric D. Friedman All Rights Reserved.
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: ///////////////////////////////////////////////////////////////////////////////
018:
019: package gnu.trove;
020:
021: import junit.framework.TestCase;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.util.*;
028:
029: /**
030: *
031: * Created: Sat Nov 3 10:31:38 2001
032: *
033: * @author Eric D. Friedman
034: * @version $Id: THashMapTest.java,v 1.10 2007/03/30 20:53:22 robeden Exp $
035: */
036:
037: public class THashMapTest extends TestCase {
038:
039: protected THashMap map;
040: protected int count;
041:
042: public THashMapTest(String name) {
043: super (name);
044: }
045:
046: public void setUp() throws Exception {
047: super .setUp();
048: map = new THashMap();
049: count = 0;
050: }
051:
052: public void tearDown() throws Exception {
053: super .tearDown();
054: map = null;
055: count = 0;
056: }
057:
058: public void testPut() throws Exception {
059: assertEquals("put succeeded", null, map.put("One", "two"));
060: assertEquals("size did not reflect put", 1, map.size());
061: assertEquals("put/get failed", "two", map.get("One"));
062: assertEquals("second put failed", "two", map.put("One", "foo"));
063: }
064:
065: public void testClear() throws Exception {
066: assertEquals("initial size was not zero", 0, map.size());
067: assertEquals("put succeeded", null, map.put("One", "two"));
068: assertEquals("size did not reflect put", 1, map.size());
069: map.clear();
070: assertEquals("cleared size was not zero", 0, map.size());
071: }
072:
073: public void testContainsKey() throws Exception {
074: Object key = "hi";
075: assertTrue("should not contain key initially", !map
076: .containsKey(key));
077: assertEquals("put succeeded", null, map
078: .put(key, new Integer(1)));
079: assertTrue("key not found after put", map.containsKey(key));
080: }
081:
082: public void testGet() throws Exception {
083: Object key = "hi", val = "one", val2 = "two";
084:
085: map.put(key, val);
086: assertEquals("get did not return expected value", val, map
087: .get(key));
088: map.put(key, val2);
089: assertEquals("get did not return expected value on second put",
090: val2, map.get(key));
091: }
092:
093: public void testValues() throws Exception {
094: Object k1 = "1", k2 = "2", k3 = "3", k4 = "4", k5 = "5";
095: Object v1 = "x", v2 = "y", v3 = "z";
096:
097: map.put(k1, v1);
098: map.put(k2, v1);
099: map.put(k3, v2);
100: map.put(k4, v3);
101: map.put(k5, v2);
102: Collection vals = map.values();
103: assertEquals("size was not 5", 5, vals.size());
104: vals.remove("z");
105: assertEquals("size was not 4", 4, vals.size());
106: vals.remove("y");
107: assertEquals("size was not 3", 3, vals.size());
108: vals.remove("y");
109: assertEquals("size was not 2", 2, vals.size());
110: assertEquals("map did not diminish to 2 entries", 2, map.size());
111: }
112:
113: public void testKeySet() throws Exception {
114: Object key1 = "hi", key2 = "bye", key3 = "whatever";
115: Object val = "x";
116:
117: map.put(key1, val);
118: map.put(key2, val);
119: map.put(key3, val);
120:
121: Set keys = map.keySet();
122: assertTrue("keyset did not match expected set", keys
123: .containsAll(Arrays.asList(new Object[] { key1, key2,
124: key3 })));
125: assertEquals(3, map.size());
126:
127: int count = 0;
128: Iterator it = keys.iterator();
129: while (it.hasNext()) {
130: count++;
131: it.next();
132: }
133: assertEquals(map.size(), count);
134:
135: for (Iterator i = keys.iterator(); i.hasNext();) {
136: Object o = i.next();
137: if (o.equals(key2)) {
138: i.remove();
139: }
140: }
141: assertTrue("keyset did not match expected set",
142: keys.containsAll(Arrays.asList(new Object[] { key1,
143: key3 })));
144: }
145:
146: public void testForEachEntry() throws Exception {
147: Object[] keys = { "one", "two", "three", "four" };
148: Object[] vals = { new Integer(1), new Integer(2),
149: new Integer(3), new Integer(4) };
150: for (int i = 0; i < keys.length; i++) {
151: map.put(keys[i], vals[i]);
152: }
153:
154: TObjectObjectProcedure proc = new TObjectObjectProcedure() {
155: public boolean execute(Object key, Object value) {
156: count += ((Integer) value).intValue();
157: return true;
158: }
159: };
160: map.forEachEntry(proc);
161: assertEquals(10, count);
162: }
163:
164: public void testForEachEntryInterrupt() throws Exception {
165: Object[] keys = { "one", "two", "three", "four" };
166: Object[] vals = { new Integer(1), new Integer(2),
167: new Integer(3), new Integer(4) };
168: for (int i = 0; i < keys.length; i++) {
169: map.put(keys[i], vals[i]);
170: }
171:
172: TObjectObjectProcedure proc = new TObjectObjectProcedure() {
173: public boolean execute(Object key, Object value) {
174: count += ((Integer) value).intValue();
175: return count >= 6 ? false : true;
176: }
177: };
178: map.forEachEntry(proc);
179: assertTrue(count < 10);
180: }
181:
182: public void testTransformValues() throws Exception {
183: Object[] keys = { "one", "two", "three", "four" };
184: Object[] vals = { new Integer(1), new Integer(2),
185: new Integer(3), new Integer(4) };
186: for (int i = 0; i < keys.length; i++) {
187: map.put(keys[i], vals[i]);
188: }
189:
190: TObjectFunction func = new TObjectFunction() {
191: public Object execute(Object value) {
192: int num = ((Integer) value).intValue();
193: return new Integer(num << 1);
194: }
195: };
196: map.transformValues(func);
197: assertEquals(new Integer(2), map.get("one"));
198: assertEquals(new Integer(4), map.get("two"));
199: assertEquals(new Integer(6), map.get("three"));
200: assertEquals(new Integer(8), map.get("four"));
201: }
202:
203: public void testKeyIterator() throws Exception {
204: Object[] keys = { "one", "two", "three", "four" };
205: Object[] vals = { new Integer(1), new Integer(2),
206: new Integer(3), new Integer(4) };
207: for (int i = 0; i < keys.length; i++) {
208: map.put(keys[i], vals[i]);
209: }
210:
211: int count = 0;
212: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
213: i.next();
214: count++;
215: }
216: assertEquals(4, count);
217: }
218:
219: public void testCompact() throws Exception {
220: Integer[] data = new Integer[1000];
221:
222: for (int i = 0; i < 1000; i++) {
223: data[i] = new Integer(i);
224: map.put(data[i], data[i]);
225: }
226: assertTrue(map._maxSize > 1000);
227: for (int i = 0; i < 1000; i += 2) {
228: // try {
229: map.remove(data[i]);
230: // }
231: // catch( RuntimeException ex ) {
232: // System.err.println("Error on i: " + i);
233: // System.out.println("Hash codes:");
234: // for( int j = 0 ; j < data.length; j++ ) {
235: // if ( ( j % 8 ) == 0 ) {
236: // System.out.println(",");
237: // }
238: // else System.out.print(",");
239: // System.out.print(map._hashingStrategy.computeHashCode(data[j]));
240: // }
241: //
242: //
243: // System.out.println("Remove:");
244: // for( int j = 0 ; j <= i; j+=2 ) {
245: // if ( ( j % 8 ) == 0 ) {
246: // System.out.println(",");
247: // }
248: // else System.out.print(",");
249: // System.out.print(map._hashingStrategy.computeHashCode(data[j]));
250: // }
251: // throw ex;
252: // }
253: }
254: assertEquals(500, map.size());
255: map.compact();
256: assertEquals(500, map.size());
257: assertTrue(map._maxSize < 1000);
258: }
259:
260: public void testContainsNullValue() throws Exception {
261: THashMap mm = new THashMap();
262: mm.put("a", null);
263: assertTrue(mm.containsValue(null));
264: }
265:
266: public void testEntrySetContainsEntryWithNullValue()
267: throws Exception {
268: THashMap mm = new THashMap();
269: mm.put("0", null);
270: Map.Entry ee = (Map.Entry) mm.entrySet().iterator().next();
271: assertTrue(mm.entrySet().contains(ee));
272: }
273:
274: public void testValueSetRemoveNullValue() throws Exception {
275: THashMap mm = new THashMap();
276: mm.put("0", null);
277: assertTrue(mm.values().remove(null));
278: }
279:
280: public void testSizeAfterEntrySetRemove() throws Exception {
281: THashMap mm = new THashMap();
282: mm.put("0", null);
283: Map.Entry ee = (Map.Entry) mm.entrySet().iterator().next();
284: assertTrue(ee.getKey().equals("0"));
285: assertNull(ee.getValue());
286: assertTrue("remove on entrySet() returned false", mm.entrySet()
287: .remove(ee));
288: assertEquals(0, mm.size());
289: }
290:
291: public void testEntrySetRemoveSameKeyDifferentValues()
292: throws Exception {
293: THashMap mm1 = new THashMap();
294: mm1.put("0", "abc");
295: THashMap mm2 = new THashMap();
296: mm2.put("0", "123");
297: Map.Entry ee = (Map.Entry) mm1.entrySet().iterator().next();
298: assertEquals(1, mm2.size());
299: assertTrue(!mm2.entrySet().contains(ee));
300: assertTrue(!mm2.entrySet().remove(ee));
301: }
302:
303: public void testSizeAfterMultipleReplacingPuts() throws Exception {
304: THashMap mm = new THashMap();
305: mm.put("key", "a");
306: assertEquals(1, mm.size());
307: mm.put("key", "b");
308: assertEquals(1, mm.size());
309: }
310:
311: public void testSerializable() throws Exception {
312: THashMap mm = new THashMap();
313: mm.put("a", "b");
314: mm.put("b", "c");
315:
316: ByteArrayOutputStream baos = new ByteArrayOutputStream();
317: ObjectOutputStream oos = new ObjectOutputStream(baos);
318: oos.writeObject(mm);
319:
320: ByteArrayInputStream bais = new ByteArrayInputStream(baos
321: .toByteArray());
322: ObjectInputStream ois = new ObjectInputStream(bais);
323:
324: THashMap mm2 = (THashMap) ois.readObject();
325:
326: assertEquals(mm, mm2);
327: }
328:
329: public void testSerializablePrimitives() throws Exception {
330: TIntIntHashMap mm = new TIntIntHashMap();
331: mm.put(1, -1);
332: mm.put(0, 0);
333: mm.put(127, 68888);
334:
335: ByteArrayOutputStream baos = new ByteArrayOutputStream();
336: ObjectOutputStream oos = new ObjectOutputStream(baos);
337: oos.writeObject(mm);
338:
339: ByteArrayInputStream bais = new ByteArrayInputStream(baos
340: .toByteArray());
341: ObjectInputStream ois = new ObjectInputStream(bais);
342:
343: TIntIntHashMap mm2 = (TIntIntHashMap) ois.readObject();
344:
345: assertEquals(mm, mm2);
346: }
347:
348: public void testRetainEntries() throws Exception {
349: THashMap mm = new THashMap();
350: mm.put("a", "b");
351: mm.put("c", "b");
352: mm.put("d", "b");
353:
354: mm.retainEntries(new TObjectObjectProcedure() {
355: public boolean execute(Object key, Object val) {
356: return key.equals("c");
357: }
358: });
359:
360: assertEquals(1, mm.size());
361: assertTrue(mm.containsKey("c"));
362: assertEquals("b", mm.get("c"));
363: }
364:
365: public void testPutAll() throws Exception {
366: THashMap t = new THashMap();
367: HashMap m = new HashMap();
368: m.put("one", "two");
369: m.put("two", "four");
370: m.put("three", "six");
371: t.putAll(m);
372: }
373:
374: public void testClone() throws Exception {
375: TObjectIntHashMap<String> original = new TObjectIntHashMap<String>();
376: original.put("one", 1);
377: original.put("two", 2);
378: original.put("three", 3);
379:
380: TObjectIntHashMap<String> clone = original.clone();
381:
382: assertEquals(original, clone);
383: assertEquals(3, clone.size());
384: assertEquals(1, clone.get("one"));
385: assertEquals(2, clone.get("two"));
386: assertEquals(3, clone.get("three"));
387:
388: original.clear();
389:
390: assertEquals(3, clone.size());
391: assertEquals(1, clone.get("one"));
392: assertEquals(2, clone.get("two"));
393: assertEquals(3, clone.get("three"));
394: }
395:
396: public void testHashCode() throws Exception {
397: THashMap m1 = new THashMap();
398: THashMap m2 = new THashMap();
399: m1.put(new String("foo"), new String("bar"));
400: m2.put(new String("foo"), new String("bar"));
401: assertEquals(m1.hashCode(), m2.hashCode());
402: assertEquals(m1, m2);
403: m2.put(new String("cruft"), new String("bar"));
404: assertTrue(m1.hashCode() != m2.hashCode());
405: assertTrue(!m1.equals(m2));
406: }
407:
408: public void testBadlyWrittenKey() {
409: boolean didThrow = false;
410: try {
411: for (int i = 0; i < 1000; i++) { // enough to trigger a rehash
412: map.put(new THashSetTest.Crap(), new Integer(i));
413: }
414: } catch (IllegalArgumentException e) {
415: didThrow = true;
416: }
417: assertTrue(
418: "expected THashMap to throw an IllegalArgumentException",
419: didThrow);
420: }
421:
422: public void testKeySetEqualsEquivalentSet() {
423: Set set = new java.util.HashSet();
424: set.add("foo");
425: set.add("doh");
426: set.add("hal");
427:
428: THashMap tv1 = new THashMap();
429: tv1.put("doh", "blah");
430: tv1.put("foo", "blah");
431: tv1.put("hal", "notblah");
432: assertTrue(tv1.keySet().equals(set));
433: }
434:
435: public void testNullValue() {
436: THashMap t = new THashMap();
437: t.put("foo", null);
438: t.put("bar", null);
439: t.put("baz", null);
440:
441: assertEquals(Arrays.asList(new Object[] { null, null, null }),
442: new ArrayList(t.values()));
443: }
444:
445: public void testNullValueSize() {
446: THashMap map = new THashMap();
447:
448: map.put("narf", null);
449: map.put("narf", null);
450: assertEquals(1, map.size());
451: }
452:
453: public void testNullKey() {
454: THashMap t = new THashMap();
455: t.put(null, null);
456: assertEquals(null, t.keySet().iterator().next());
457: }
458:
459: public void testRetainEntrySet() {
460: Map orig = new THashMap();
461: orig.put("one", "frodo");
462: orig.put("two", "bilbo");
463: orig.put("three", "samwise");
464:
465: Map subset = new HashMap();
466: subset.put("two", "bilbo");
467:
468: assertTrue(orig.entrySet().retainAll(subset.entrySet()));
469:
470: assertEquals(subset, orig);
471: }
472:
473: public void testMapEntrySetHashCode() {
474: Map m1 = new THashMap();
475: m1.put("one", "foo");
476: Map m2 = new THashMap();
477: m2.put("one", "foo");
478:
479: Object o1 = m1.entrySet().iterator().next();
480: Object o2 = m1.entrySet().iterator().next();
481: assertTrue(o1 != o2);
482: assertTrue(o1.equals(o2));
483: assertEquals(o1.hashCode(), o2.hashCode());
484: }
485:
486: public void testEqualsAndHashCode() {
487: THashMap<String, String> map1 = new THashMap<String, String>();
488: map1.put("Key1", null);
489:
490: THashMap<String, String> map2 = new THashMap<String, String>();
491: map2.put("Key2", "Value2");
492:
493: assertFalse("map1.equals( map2 )", map1.equals(map2));
494: assertFalse("map2.equals( map1 )", map2.equals(map1));
495:
496: THashMap<String, String> clone_map1 = map1.clone();
497: THashMap<String, String> clone_map2 = map2.clone();
498:
499: assertEquals(map1, clone_map1);
500: assertEquals(map1.hashCode(), clone_map1.hashCode());
501: assertEquals(map2, clone_map2);
502: assertEquals(map2.hashCode(), clone_map2.hashCode());
503: }
504:
505: /**
506: * Test case for bug #1428614.
507: * http://sourceforge.net/tracker/index.php?func=detail&aid=1428614&group_id=39235&atid=424682
508: */
509: public void testRemoveValue() {
510: THashMap<String, String> map = new THashMap<String, String>();
511: map.put("one", "a");
512: map.put("two", "a");
513: map.put("three", "b");
514:
515: assertEquals(3, map.size());
516:
517: map.values().remove("a");
518:
519: assertEquals(2, map.size());
520: }
521:
522: /**
523: * This test case arose out of a problem that was ultimately caused by the
524: * implementation if THashMap.removeAt(int) being incorrect (the super implementation
525: * was being called before the local implementation which caused problems when
526: * auto-compaction occurred). So, it's a little wiggy, but since the case was useful
527: * once, I figure I'll leave it in. - RDE
528: */
529: public void testProblematicRemove() {
530: int[] to_add = new int[] { 9707851, 1432929, 7941420, 8698105,
531: 9178562, 14368620, 2165498, 5759024, 4160722, 1835074,
532: 5570057, 15866937, 1774305, 7645103, 11340758,
533: 14962053, 10326974, 5153366, 8644308, 10981652,
534: 2484113, 8790215, 13765491, 15579334, 16644662,
535: 3538325, 10183070, 2491991, 6963721, 1406388, 14845099,
536: 7614579, 1630785, 11417789, 988981, 12372702, 11197485,
537: 6115155, 185310, 10733210, 4444363, 4256211, 12877269,
538: 2178642, 8551718, 15095256, 922983, 10434400, 15511986,
539: 8792944, 9301853, 6147907, 13778707, 2822511, 8760514,
540: 1112301, 4624291, 8406178, 1708017, 834479, 16113199,
541: 13501060, 477791, 10737091, 2568325, 14839924, 4523777,
542: 13566852, 15722007, 15406704, 932352, 127549, 13010531,
543: 10540591, 5100961, 288048, 9396085, 12844726, 8887992,
544: 12932686, 10827776, 16751176, 15337379, 10192984,
545: 1341540, 15470105, 9555179, 2394843, 1595902, 12345969,
546: 14884594, 313940, 8348091, 15293845, 16706146,
547: 13500767, 12331344, 3959435, 7796580, 7077998, 9458485,
548: 4648605, 14396291, 14246507, 13404097, 14988605,
549: 3600161, 9280290, 12842832, 10606027, 14355582,
550: 1136752, 12921165, 1749219, 5457859, 9197696, 421256,
551: 73503, 10633537, 6952660, 58264, 6164651, 9993341,
552: 1386282, 12470635, 12982240, 4816459, 159250, 8949093,
553: 16447937, 2290043, 1828537, 13148199, 9084516,
554: 10802630, 13549340, 6609445, 2995162, 8040282, 9333002,
555: 9580969, 16406600, 12194236, 14835440, 13041360,
556: 8604473, 12565810, 1988729, 4355359, 1535580, 5149176,
557: 5324752, 3438993, 1660570, 8691895, 5474195, 15334260,
558: 8105437, 13879037, 11351030, 3039475, 14625682,
559: 10849417, 11379670, 14640675, 11176427, 4513965,
560: 16428465, 10578701, 8072595, 15538803, 6526027,
561: 10283251, 8507206, 5188636, 14223982, 3920972,
562: 15659969, 12922874, 13689914, 3658005, 8379437,
563: 5247277, 9938991, 10659712, 10678905, 14485926,
564: 10786998, 2488171, 9881794, 5651833, 14538812,
565: 10444169, 11922157, 5403811, 6785775, 13794224,
566: 11958753, 16494451, 12313218, 1297763, 1126021, 345418,
567: 528188, 2114627, 6400133, 8307281, 490653, 8793363,
568: 16336633, 10653219, 2214875, 13551003, 1001246, 397033,
569: 12381376, 5991449, 1443593, 2622811, 7847125, 944151,
570: 13889218, 14686341, 6939782, 1712728, 12902341,
571: 4138467, 13656813, 973555, 4767079, 9100632, 13222220,
572: 11641873, 9082629, 12639233, 11258141, 2146301,
573: 1867442, 12719413, 16679909, 8734589, 1606713, 9501535,
574: 6761395, 6693929, 13647363, 9914313, 15335895, 2021653,
575: 4068909, 2228794, 12603329, 11807792, 12665538, 395494,
576: 3299447, 5119086, 2917420, 10055436, 4831656, 3927888,
577: 14447609, 4329094, 13077467, 11461522, 14594477,
578: 6481991, 8367571, 7156844, 9223013, 6482120, 10542896,
579: 10286402, 11125093, 14144872, 16495825, 1844776,
580: 860068, 9980169, 14877755, 2804551, 8894974, 12849678,
581: 8215338, 15490728, 3843485, 5184218, 7071904, 7703600,
582: 4633458, 11481528, 15295069, 3736219, 14297843,
583: 3787634, 6015173, 14290065, 7290758, 11764335, 3688927,
584: 7991532, 12075598, 606202, 4674522, 13772937, 6515326,
585: 14974617, 3385263, 4587760, 15178512, 7689244,
586: 15015527, 3087738, 3683764, 5107535, 10120404, 6225460,
587: 8588999, 4151219, 9885848, 6691152, 518908, 13918089,
588: 13393004, 13093729, 16338349, 5945377, 15632500,
589: 4230314, 13832167, 12139768, 5361165, 11457892,
590: 3916190, 2387780, 325816, 6621694, 7540927, 5271271,
591: 10565439, 3281837, 11138623, 6663214, 737100, 6864802,
592: 16592415, 14615312, 4342441, 2525512, 16706191,
593: 14258395, 11878990, 1320531, 14696398, 8201398,
594: 16077183, 12155328, 15225360, 6533378, 16390602,
595: 11750387, 4144864, 3744598, 4136761, 1775074, 3787875,
596: 10061327, 3165792, 6921717, 84292, 7420530, 11805441,
597: 6704350, 4234280, 13377633, 6417611, 81563, 11879309,
598: 6692731, 10285066, 5452490, 2848306, 6094584, 6772150,
599: 2899899, 805004, 7273360, 4566720, 13878010, 10871921,
600: 3724097, 11896809, 15586671, 5744620, 13731591,
601: 16250661, 8560290, 8169917, 7059338, 14615241, 3149669,
602: 4383295, 1292178, 7919990, 846550, 896930, 8769114,
603: 11437924, 3854132, 16345157, 2929809, 186157, 8183120,
604: 10860321, 10092509, 7157818, 8817522, 2944051, 4664124,
605: 6791689, 12517547, 12905829, 12435281, 5992485,
606: 2074096, 13062653, 14148480, 10024647, 7455154,
607: 6534752, 5933059, 9930860, 8221561, 2639915, 10098755,
608: 11468155, 8638604, 15770467, 7790866, 11694410,
609: 2868248, 5710862, 15709, 12374346, 5274287, 10913198,
610: 9607083, 2330533, 11262155, 2500209, 10878393,
611: 11834918, 15572289, 15669880, 11713730, 8818293,
612: 15907500, 12427744, 13540318, 5978200, 13640927,
613: 2411696, 16408949, 1331989, 5941655, 3414928, 16619879,
614: 6441500, 15706705, 9881210, 12620326, 12259629,
615: 6605901, 10543825, 9125515, 12001189, 8309409, 2696396,
616: 3070853, 5120614, 11830622, 10490623, 4149060, 7141756,
617: 7297762, 12039919, 4930206, 16095035, 10203610,
618: 12162006, 10028034, 14040149, 1250372, 9943013,
619: 11150309, 1752285, 6641241, 532227, 2891993, 2146459,
620: 4523080, 1843838, 1876388, 12071882, 5253689, 266407,
621: 14770216, 7346541, 9785383, 12662763, 4087061, 5312086,
622: 8667965, 5935475, 214509, 14935237, 12603345, 12069351,
623: 13056011, 3177187, 13886924, 9688141, 5714168, 5238287,
624: 9839924, 6586077, 12908278, 3257673, 7665358, 16208962,
625: 12373914, 14796394, 11098653, 5975276, 14839805,
626: 2522300, 13055068, 4113411, 11984808, 1418893, 6924381,
627: 11314606, 11633137, 13235995, 8277143, 14057346,
628: 5064134, 2097065, 13223254, 12242045, 13059400,
629: 9799866, 4430809, 11327935, 769464, 13938921, 11178479,
630: 5438437, 1550013, 12839250, 727385, 11354068, 3772220,
631: 15394227, 9336140, 11988768, 860366, 14994301,
632: 15440057, 7845075, 46465, 9200550, 14833083, 6980643,
633: 604527, 10080643, 9045195, 4244693, 3844932, 12717539,
634: 1960282, 12786813, 8615113, 6833198, 5522541, 5791657,
635: 15755268, 3994917, 158446, 12203633, 5002730, 10253904,
636: 1809471, 11479213, 9928035, 11072909, 9512807,
637: 11660261, 16127120, 12596061, 7086362, 15820414,
638: 8387982, 14653636, 10912742, 1941253, 11740079,
639: 15457795, 3976572, 10595620, 7217556, 6197451, 7618490,
640: 258825, 4780842, 5534349, 2921202, 6513404, 16229803,
641: 10332843, 3138363, 15681804, 10802604, 13113540,
642: 13757900, 5443555, 3681765, 5063855, 14185653,
643: 14039650, 9644178, 5024611, 8918836, 11231866,
644: 13523137, 2425209, 8636320, 10944802, 3891863,
645: 12961644, 10984042, 9100512, 11218774, 11581954,
646: 8646320, 11234763, 11887145, 4171898, 5109569,
647: 10742219, 4859349, 16381539, 10419813, 5223261,
648: 8955000, 15061357, 1607571, 7136846, 8670269, 11099382,
649: 1451676, 4261185, 12586581, 15531576, 2504976, 7105767,
650: 6413040, 7144290, 16334106, 1741877, 16270583, 7852246,
651: 3119103, 10743199, 4558377, 7878745, 12289371, 3163084,
652: 11735282, 1935864, 5055074, 820851, 5185654, 14442671,
653: 5212885, 2344949, 1892708, 1153520, 9541794, 12306031,
654: 14732248, 6743381, 5920025, 8969603, 8847687, 6622806,
655: 9462942, 12451035, 2336870, 327996, 9713350, 9963027,
656: 11991923, 3562944, 4520396, 7065629, 2905597, 12675100,
657: 10094378, 5011336, 3908274, 3572588, 15618069,
658: 13341879, 9470980, 13327842, 8432408, 6344466,
659: 12241360, 1543979, 12081229, 11363884, 983612, 6025413,
660: 1848571, 14318469, 14906334, 13381442, 3327004,
661: 15286722, 14443922, 9462145, 15828870, 16292084,
662: 128023, 4199307, 12797648, 6169450, 6767468, 8100201,
663: 9756312, 10612295, 2273164, 3350998, 15889011, 3661060,
664: 9395406, 1435793, 5752767, 16441805, 16677778, 6475274,
665: 12909030, 15902971, 3415473, 9004053, 645884, 515610,
666: 8824322, 16574526, 15956463, 13265827, 6333169,
667: 6924814, 1812983, 3392856, 14761092, 4985619, 7893816,
668: 13931135, 14548854, 11444226, 9118923, 1875105,
669: 7285192, 2101733, 7801836, 11517693, 2349183, 5939167,
670: 11937456, 10886500, 13866155, 12947589, 9640186,
671: 5047153, 1901666, 715551, 13790692, 2933460, 11212664,
672: 9563015, 16642428, 16334427, 7140601, 4655671,
673: 15711153, 750565, 15067249, 16737043, 12684416,
674: 15673315, 2341376, 8935324, 3134061, 10483409, 337177,
675: 13018877, 16599994, 7782302, 1977121, 10593779,
676: 9842381, 14330646, 1456639, 3774065, 12844377, 3016177,
677: 8933870, 12263851, 10455534, 1612109, 16302350,
678: 4895080, 12932155, 1905228, 10253063, 4458040,
679: 16024500, 15902756, 16584305, 12528008, 4171461,
680: 14536742, 9219403, 12927168, 1979395, 15257546,
681: 10619265, 1967594, 1467515, 2028519, 2032612, 3707709,
682: 4887462, 2337860, 183801, 2152077, 15066473, 3694942,
683: 8424967, 15508266, 13386596, 6059869, 10531128,
684: 13828874, 7119662, 5064756, 12552069, 5922533, 802911,
685: 5645620, 10781530, 11246590, 9323418, 16275234,
686: 2144845, 10962831, 4925357, 1704524, 9227431, 13641289,
687: 8489002, 1225340, 8659144, 8671408, 13461400, 4992933,
688: 13370774, 8568931, 2412794, 1312411, 12429994, 1025208,
689: 478829, 11399895, 2242158, 2332498, 10717459, 8151843,
690: 5288043, 7235700, 9162569, 14017735, 10412273,
691: 12712138, 11844638, 11163643, 7756823, 9956164,
692: 14078510, 8442139, 2116890, 10881649, 16223710,
693: 8592664, 15408035, 6522496, 1261635, 14685232, 5071601,
694: 10144049, 967751, 7873356, 5595700, 10647000, 15126220,
695: 1237821, 321796, 6173902, 14476409, 1830511, 12766190,
696: 14322080, 8483740, 13438254, 1854079, 6215655,
697: 11575352, 15129118, 16393883, 16560142, 9079559,
698: 11379168, 6198702, 11864074, 2282933, 16547051,
699: 7156233, 15740343, 4809601, 2344447, 10219155, 4977972,
700: 13592880, 184650, 16420038, 3165940, 9418081, 13446140,
701: 179241, 9394692, 6213074, 1752099, 3516715, 16081239,
702: 13222615, 1499877, 9066661, 12702088, 10706447,
703: 7629231, 13016955, 1069166, 1089471, 6809842, 15634321,
704: 1288782, 1183469, 9576844, 14191973, 2814257, 4260748,
705: 5239952, 4277681, 4629271, 8220928, 8766876, 7388663,
706: 13090704, 15838538, 11015909, 7814987, 14448125,
707: 13000849, 15596437, 2104764, 8398024, 15653431,
708: 3695833, 6613072, 13626967, 2665818, 9249819, 4040305,
709: 8029033, 4822667, 3844052, 14708928, 690088 };
710:
711: int[] to_remove = new int[] { 9707851, 7941420, 9178562,
712: 2165498, 4160722, 5570057, 1774305, 11340758, 10326974,
713: 8644308 };
714:
715: for (int i = 0; i < to_add.length; i++) {
716: Integer obj = new Integer(to_add[i]);
717: map.put(obj, obj);
718: }
719:
720: for (int i = 0; i < to_remove.length; i++) {
721: Integer obj = new Integer(to_remove[i]);
722: map.remove(obj);
723: }
724: }
725:
726: public void testIterable() {
727: Map<String, Integer> m = new THashMap<String, Integer>();
728: m.put("One", Integer.valueOf(1));
729: m.put("Two", Integer.valueOf(2));
730:
731: for (String s : m.keySet()) {
732: assertTrue(s.equals("One") || s.equals("Two"));
733: }
734:
735: for (Integer i : m.values()) {
736: assertTrue(i.intValue() == 1 || i.intValue() == 2);
737: }
738: }
739:
740: public void testGetOverRemovedObjectBug() {
741: Map<BadHashInteger, String> m = new THashMap<BadHashInteger, String>(
742: new TObjectHashingStrategy<BadHashInteger>() {
743: public int computeHashCode(BadHashInteger object) {
744: return object.hashCode();
745: }
746:
747: public boolean equals(BadHashInteger o1,
748: BadHashInteger o2) {
749: return o1.value == o2.value;
750: }
751: });
752:
753: m.put(new BadHashInteger(1), "one");
754: m.put(new BadHashInteger(2), "two");
755:
756: m.remove(new BadHashInteger(1));
757:
758: assertEquals(1, m.size());
759:
760: // Blow up here?
761: assertEquals("two", m.get(new BadHashInteger(2)));
762: }
763:
764: public void testReaddBug() {
765: Map<Integer, String> m = new THashMap<Integer, String>(
766: new TObjectHashingStrategy<Integer>() {
767: public int computeHashCode(Integer object) {
768: return object.intValue();
769: }
770:
771: public boolean equals(Integer o1, Integer o2) {
772: return o1.intValue() == o2.intValue();
773: }
774: });
775:
776: m.put(new Integer(1), "one");
777: assertEquals(1, m.size());
778:
779: m.remove(new Integer(1));
780: assertEquals(0, m.size());
781:
782: m.put(new Integer(1), "one");
783: assertEquals(1, m.size());
784: }
785:
786: private static class BadHashInteger {
787: private final int value;
788:
789: BadHashInteger(int value) {
790: this .value = value;
791: }
792:
793: public boolean equals(Object o) {
794: if (this == o)
795: return true;
796: if (o == null || getClass() != o.getClass())
797: return false;
798:
799: BadHashInteger that = (BadHashInteger) o;
800:
801: if (value != that.value)
802: return false;
803:
804: return true;
805: }
806:
807: public int hashCode() {
808: return 0;
809: }
810: }
811: } // THashMapTests
|