1 19 20 package gnu.trove; 21 22 import junit.framework.TestCase; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.util.Arrays ; 29 import java.util.HashSet ; 30 import java.util.Set ; 31 32 39 40 public class THashSetTest extends TestCase { 41 42 protected Set s; 43 44 public THashSetTest(String name) { 45 super(name); 46 } 47 48 public void setUp() throws Exception { 49 super.setUp(); 50 s = new THashSet(); 51 } 52 53 public void tearDown() throws Exception { 54 super.tearDown(); 55 s = null; 56 } 57 58 public void testIsEmpty() throws Exception { 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 { 68 Object o = "testContains"; 69 s.add(o); 70 assertTrue("contains failed", s.contains(o)); 71 } 72 73 public void testContainsAll() throws Exception { 74 Object [] 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 { 84 assertTrue("add failed", s.add("One")); 85 assertTrue("duplicated add succeded", ! s.add("One")); 86 } 87 88 public void testRemove() throws Exception { 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 { 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 ()); 102 assertEquals("size did not increase after add", i+1, s.size()); 103 } 104 } 105 106 public void testClear() throws Exception { 107 s = new THashSet(); 108 s.addAll(Arrays.asList(new String [] {"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 { 115 s = new THashSet(); 116 s.addAll(Arrays.asList(new String [] {"one","two","three"})); 117 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 118 ObjectOutputStream oos = new ObjectOutputStream (baos); 119 oos.writeObject(s); 120 121 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 122 ObjectInputStream ois = new ObjectInputStream (bais); 123 124 THashSet s2 = (THashSet)ois.readObject(); 125 126 assertEquals(s, s2); 127 } 128 129 public void testNormalLoad() throws Exception { 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 (i)); 134 } 135 assertTrue(set._maxSize > 12); 136 } 137 138 public void testMaxLoad() throws Exception { 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 (i)); 143 } 144 assertTrue(set._maxSize > 12); 145 } 146 147 public void testToArray() { 148 Object [] str = { "hi", "bye", "hello", "goodbye" }; 149 s.addAll(Arrays.asList(str)); 150 Object [] 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 [] str = { "hi", "bye", "hello", "goodbye" }; 158 s.addAll(Arrays.asList(str)); 159 String [] sink = new String [str.length + 2]; 160 sink[sink.length - 1] = "residue"; 161 sink[sink.length - 2] = "will be cleared"; 162 Object [] res = s.toArray(sink); 163 164 Set copy = new HashSet (); 165 copy.addAll(Arrays.asList(res)); 166 167 Set bogey = new HashSet (); 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 { 199 for (int i = 0; i < 10000; i++) { 200 s.add(new Integer (i)); 201 } 202 } 203 204 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 e) { 216 didThrow = true; 217 } 218 assertTrue("expected THashSet to throw an IllegalArgumentException", didThrow); 219 } 220 221 public static class Crap { 225 public boolean equals(Object 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 [] args) throws Exception { 235 junit.textui.TestRunner.run(new THashSetTest("testBadlyWrittenObject")); 236 } 237 } | Popular Tags |