1 22 23 24 package com.mchange.v2.util.junit; 25 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import com.mchange.v2.util.DoubleWeakHashMap; 30 31 import junit.framework.TestCase; 32 33 public class DoubleWeakHashMapJUnitTestCase extends TestCase 34 { 35 public void testGetNeverAdded() 36 { 37 Map m = new DoubleWeakHashMap(); 38 assertNull( m.get("foo") ); 39 } 40 41 public void testHardAdds() 42 { 43 Integer a = new Integer (1); 44 Integer b = new Integer (2); 45 Integer c = new Integer (3); 46 47 String poop = new String ("poop"); 48 String scoop = new String ("scoop"); 49 String doop = new String ("dcoop"); 50 51 Map m = new DoubleWeakHashMap(); 52 m.put(a, poop); 53 m.put(b, scoop); 54 m.put(c, doop); 55 assertEquals("Size should be three, viewed via Map directly.", m.size(), 3); 56 assertEquals("Size should be three, viewed via keySet .", m.keySet().size(), 3); 57 assertEquals("Size should be three, viewed via values Collection.", m.values().size(), 3); 58 59 int count = 0; 60 for (Iterator ii = m.keySet().iterator(); ii.hasNext();) 61 { 62 count += ((Integer ) ii.next()).intValue(); 63 } 64 assertEquals("Count should be six, viewed via values Collection.", count, 6); 65 66 Integer d = new Integer (4); 67 m.put(d, poop); 68 m.values().remove(poop); 69 assertEquals("After removing a doubled value, size should be 2", m.size(), 2); 70 } 71 72 public void testWeakness() 73 { 74 Integer a = new Integer (1); 75 Integer b = new Integer (2); 76 Integer c = new Integer (3); 77 78 String poop = new String ("poop"); 79 80 Map m = new DoubleWeakHashMap(); 81 m.put(a, poop); 82 m.put(b, new Object ()); 83 m.put(c, new Object ()); 84 85 assertEquals("1) Weak values should not yet have been removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 3); 87 88 System.gc(); 91 92 byte[] bArray = new byte[1024 * 1024]; 94 95 assertEquals("2) Weak values should have been automatically removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 1); 96 97 m.put( new Object (), b); 98 99 assertEquals("3) Weak key should not yet have been removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 2); 101 102 System.gc(); 103 bArray = new byte[1024 * 1024]; 105 106 assertEquals("4) Weak key should have been automatically removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 1); 107 } 108 } 109 | Popular Tags |