1 package edu.rice.cs.util; 2 import edu.rice.cs.drjava.DrJavaTestCase; 3 4 import java.util.Arrays ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 8 13 public class BidirectionalHashMapTest extends DrJavaTestCase { 14 15 public void testSearch() { 16 Double dbl1 = new Double (.1); 17 Double dbl2 = new Double (.2); 18 Double dbl3 = new Double (.3); 19 20 Double [] dbls = new Double []{dbl1, dbl2, dbl3}; 21 22 Integer int1 = new Integer (1); 23 Integer int2 = new Integer (2); 24 Integer int3 = new Integer (3); 25 26 Integer [] ints = new Integer []{int1, int2, int3}; 27 28 BidirectionalHashMap<Integer , Double > iTod = new BidirectionalHashMap<Integer , Double >(); 29 30 assertTrue("Empty BHM is empty", iTod.isEmpty()); 31 assertTrue("Empty BHM has no values", iTod.values().isEmpty()); 32 33 assertEquals("Initial size of 0", iTod.size(), 0); 34 35 assertFalse("Should not find non-existent key", iTod.containsKey(int1)); 36 assertFalse("Should not find non-existent key", iTod.containsKey(int2)); 37 assertFalse("Should not find non-existent key", iTod.containsKey(int3)); 38 39 assertFalse("Should not find non-existent value", iTod.containsValue(dbl1)); 40 assertFalse("Should not find non-existent value", iTod.containsValue(dbl2)); 41 assertFalse("Should not find non-existent value", iTod.containsValue(dbl3)); 42 43 iTod.put(int1, dbl1); 44 45 assertFalse("NonEmpty BHM is not empty", iTod.isEmpty()); 46 assertFalse("NonEmpty BHM has some values", iTod.values().isEmpty()); 47 48 assertTrue("Should find key", iTod.containsKey(int1)); 49 assertFalse("Should not find non-existent key", iTod.containsKey(int2)); 50 assertFalse("Should not find non-existent key", iTod.containsKey(int3)); 51 52 assertTrue("Should find value", iTod.containsValue(dbl1)); 53 assertFalse("Should not find non-existent value", iTod.containsValue(dbl2)); 54 assertFalse("Should not find non-existent value", iTod.containsValue(dbl3)); 55 56 iTod.put(int2, dbl2); 57 iTod.put(int3, dbl3); 58 59 Collection <Double > valsCol = iTod.values(); 60 61 Object [] vals = iTod.valuesArray(); 62 Object [] colVals = valsCol.toArray(); 63 64 66 Arrays.sort(vals); 67 Arrays.sort(colVals); 68 69 assertTrue("values() test", Arrays.equals(vals, colVals)); 70 71 assertTrue("values test", Arrays.equals(dbls, vals)); 72 73 Iterator <Double > it = iTod.valuesIterator(); 74 try { 75 it.remove(); 76 fail("Removing non-existent element should generate IllegalStateException"); 77 } catch(IllegalStateException e) { } 78 79 Double val = it.next(); 80 Integer key = iTod.getKey(val); 81 iTod.removeKey(val); 82 assertEquals("Size should be 2", 2, iTod.size()); 83 assertTrue("Iterator should be non empty", it.hasNext()); 84 85 assertFalse("Should not find non-existant key", iTod.containsKey(key)); 86 assertFalse("Should not find non-existant key", iTod.containsValue(val)); 87 88 it = iTod.valuesIterator(); 89 val = it.next(); 90 key = iTod.getKey(val); 91 it.remove(); 92 assertEquals("Size should be 1", 1, iTod.size()); 93 assertTrue("Iterator should be non empty", it.hasNext()); 94 95 assertFalse("Should not find non-existant key", iTod.containsKey(key)); 96 assertFalse("Should not find non-existant value", iTod.containsValue(val)); 97 98 99 iTod.clear(); 100 } 101 102 public void testRemove() { 103 Double dbl1 = new Double (.1); 104 Double dbl2 = new Double (.2); 105 Double dbl3 = new Double (.3); 106 107 Integer int1 = new Integer (1); 108 Integer int2 = new Integer (2); 109 Integer int3 = new Integer (3); 110 BidirectionalHashMap<Double , Integer > dToi = new BidirectionalHashMap<Double , Integer >(); 111 112 assertEquals("Initial size of 0", dToi.size(), 0); 113 dToi.clear(); 114 assertEquals("Initial size of 0", dToi.size(), 0); 115 116 dToi.put(dbl1, int1); 117 assertEquals("Size should be 1", dToi.size(), 1); 118 dToi.put(dbl2, int2); 119 assertEquals("Size should be 2", dToi.size(), 2); 120 dToi.put(dbl3, int3); 121 assertEquals("Size should be 3", dToi.size(), 3); 122 123 dToi.removeKey(int1); 124 assertEquals("Size should be 2", dToi.size(), 2); 125 126 assertEquals("Removing key should return associated value", null, dToi.removeKey(int1)); 128 assertEquals("Size should be 2", dToi.size(), 2); 129 dToi.put(dbl1, int1); 130 assertEquals("Size should be 3", dToi.size(), 3); 131 try { 132 dToi.put(dbl3, int3); 133 fail("Adding existing element should generate IllegalArgumentException"); 134 } 135 catch (IllegalArgumentException e) { } 136 assertEquals("Size should be 3", dToi.size(), 3); 137 138 dToi.removeValue(dbl3); 140 assertEquals("Size should be 2", dToi.size(), 2); 141 assertEquals("Removing value should retrun associated key", int2, dToi.removeValue(dbl2)); 142 143 assertEquals("Size should be 1", dToi.size(), 1); 144 dToi.put(dbl3, int3); 145 assertEquals("Size should be 2", dToi.size(), 2); 146 try { 147 dToi.put(dbl3, int3); 148 fail("Adding existing element should generate IllegalArgumentException"); 149 } 150 catch (IllegalArgumentException e) { } 151 assertEquals("Size should be 2", dToi.size(), 2); 152 153 dToi.clear(); 154 assertEquals("Cleared size of 0", dToi.size(), 0); 155 156 assertFalse("Iterator to cleared list should be empty", dToi.valuesIterator().hasNext()); 157 } 158 164 public void testPut() { 165 166 String one = "1"; 167 String two = "2"; 168 String three = "3"; 169 170 Integer int1 = new Integer (1); 171 Integer int2 = new Integer (2); 172 Integer int3 = new Integer (3); 173 174 BidirectionalHashMap<String , Integer > myhash = new BidirectionalHashMap<String , Integer >(); 175 176 assertEquals("Expected size of 0", 0, myhash.size()); 177 178 assertEquals("Expected null", null, myhash.getValue(one)); 179 assertEquals("Expected null", null, myhash.getValue(two)); 180 assertEquals("Expected null", null, myhash.getValue(three)); 181 182 assertEquals("Expected null", null, myhash.getKey(int1)); 183 assertEquals("Expected null", null, myhash.getKey(int2)); 184 assertEquals("Expected null", null, myhash.getKey(int3)); 185 186 myhash.put(one, int1); 187 myhash.put(two, int2); 188 myhash.put(three, int3); 189 190 assertTrue("Given one, should get 1", myhash.getValue(one) == int1); 191 assertTrue("Given two, should get 2", myhash.getValue(two) == int2); 192 assertTrue("Given three, should get 3", myhash.getValue(three) == int3); 193 194 assertTrue("Given 1, should get one", myhash.getKey(int1) == one); 195 assertTrue("Given 2, should get two", myhash.getKey(int2) == two); 196 assertTrue("Given 3, should get three", myhash.getKey(int3) == three); 197 198 Iterator <Integer > it = myhash.valuesIterator(); 199 try { 200 it.remove(); 201 fail("Removing non-existent element should generate IllegalStateException"); 202 } catch(IllegalStateException e) { } 203 204 Integer value = it.next(); 205 String key = myhash.getKey(value); 206 assertEquals("key and value should match", value.toString(), key); 207 it.remove(); 208 assertEquals("After removing key, it should not appear in map", null, myhash.getValue(key)); 209 assertEquals("After removing value, it should not appear in map", null, myhash.getKey(value)); 210 assertTrue("Map should contain elements", it.hasNext()); 211 212 value = it.next(); 213 key = myhash.getKey(value); 214 assertEquals("key and value should match", value.toString(), key); 215 it.remove(); 216 assertEquals("After removing key, it should not appear in map", null, myhash.getValue(key)); 217 assertEquals("After removing value, it should not appear in map", null, myhash.getKey(value)); 218 assertTrue("Map should contain elements", it.hasNext()); 219 220 value = it.next(); 221 key = myhash.getKey(value); 222 assertEquals("key and value should match", value.toString(), key); 223 it.remove(); 224 assertEquals("After removing key, it should not appear in map", null, myhash.getValue(key)); 225 assertEquals("After removing value, it should not appear in map", null, myhash.getKey(value)); 226 227 228 it = myhash.valuesIterator(); 229 assertFalse("Map should be empty", it.hasNext()); 230 } 231 232 } | Popular Tags |