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