KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > BidirectionalHashMapTest


1 package edu.rice.cs.util;
2 import edu.rice.cs.drjava.DrJavaTestCase;
3
4 import java.util.Arrays JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 /**
9  * A JUnit test case class.
10  * Every method starting with the word "test" will be called when running
11  * the test with JUnit.
12  */

13 public class BidirectionalHashMapTest extends DrJavaTestCase {
14   
15   public void testSearch() {
16     Double JavaDoc dbl1 = new Double JavaDoc(.1);
17     Double JavaDoc dbl2 = new Double JavaDoc(.2);
18     Double JavaDoc dbl3 = new Double JavaDoc(.3);
19     
20     Double JavaDoc[] dbls = new Double JavaDoc[]{dbl1, dbl2, dbl3};
21
22     Integer JavaDoc int1 = new Integer JavaDoc(1);
23     Integer JavaDoc int2 = new Integer JavaDoc(2);
24     Integer JavaDoc int3 = new Integer JavaDoc(3);
25     
26     Integer JavaDoc[] ints = new Integer JavaDoc[]{int1, int2, int3};
27      
28     BidirectionalHashMap<Integer JavaDoc, Double JavaDoc> iTod = new BidirectionalHashMap<Integer JavaDoc, Double JavaDoc>();
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 JavaDoc<Double JavaDoc> valsCol = iTod.values();
60     
61     Object JavaDoc[] vals = iTod.valuesArray();
62     Object JavaDoc[] colVals = valsCol.toArray();
63     
64     // These collections are enumerated in any order
65

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 JavaDoc<Double JavaDoc> it = iTod.valuesIterator();
74     try {
75       it.remove();
76       fail("Removing non-existent element should generate IllegalStateException");
77     } catch(IllegalStateException JavaDoc e) { }
78
79     Double JavaDoc val = it.next();
80     Integer JavaDoc 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 JavaDoc dbl1 = new Double JavaDoc(.1);
104     Double JavaDoc dbl2 = new Double JavaDoc(.2);
105     Double JavaDoc dbl3 = new Double JavaDoc(.3);
106
107     Integer JavaDoc int1 = new Integer JavaDoc(1);
108     Integer JavaDoc int2 = new Integer JavaDoc(2);
109     Integer JavaDoc int3 = new Integer JavaDoc(3);
110     BidirectionalHashMap<Double JavaDoc, Integer JavaDoc> dToi = new BidirectionalHashMap<Double JavaDoc, Integer JavaDoc>();
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     // Test of removeKey
127
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 JavaDoc e) { }
136     assertEquals("Size should be 3", dToi.size(), 3);
137
138     // Test of removeValue
139
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 JavaDoc 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   /**
159    * A test method.
160    * (Replace "X" with a name describing the test. You may write as
161    * many "testSomething" methods in this class as you wish, and each
162    * one will be called when running JUnit over this class.)
163    */

164   public void testPut() {
165     
166     String JavaDoc one = "1";
167     String JavaDoc two = "2";
168     String JavaDoc three = "3";
169     
170     Integer JavaDoc int1 = new Integer JavaDoc(1);
171     Integer JavaDoc int2 = new Integer JavaDoc(2);
172     Integer JavaDoc int3 = new Integer JavaDoc(3);
173     
174     BidirectionalHashMap<String JavaDoc, Integer JavaDoc> myhash = new BidirectionalHashMap<String JavaDoc, Integer JavaDoc>();
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 JavaDoc<Integer JavaDoc> it = myhash.valuesIterator();
199     try {
200       it.remove();
201       fail("Removing non-existent element should generate IllegalStateException");
202     } catch(IllegalStateException JavaDoc e) { }
203     
204     Integer JavaDoc value = it.next();
205     String JavaDoc 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     /* myhash should be empty now */
228     it = myhash.valuesIterator();
229     assertFalse("Map should be empty", it.hasNext());
230   }
231   
232 }
Popular Tags