001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040:
041: import java.util.Arrays;
042: import java.util.Collection;
043: import java.util.Iterator;
044:
045: /**
046: * A JUnit test case class.
047: * Every method starting with the word "test" will be called when running
048: * the test with JUnit.
049: */
050: public class BidirectionalHashMapTest extends DrJavaTestCase {
051:
052: public void testSearch() {
053: Double dbl1 = new Double(.1);
054: Double dbl2 = new Double(.2);
055: Double dbl3 = new Double(.3);
056:
057: Double[] dbls = new Double[] { dbl1, dbl2, dbl3 };
058:
059: Integer int1 = new Integer(1);
060: Integer int2 = new Integer(2);
061: Integer int3 = new Integer(3);
062:
063: Integer[] ints = new Integer[] { int1, int2, int3 };
064:
065: BidirectionalHashMap<Integer, Double> iTod = new BidirectionalHashMap<Integer, Double>();
066:
067: assertTrue("Empty BHM is empty", iTod.isEmpty());
068: assertTrue("Empty BHM has no values", iTod.values().isEmpty());
069:
070: assertEquals("Initial size of 0", iTod.size(), 0);
071:
072: assertFalse("Should not find non-existent key", iTod
073: .containsKey(int1));
074: assertFalse("Should not find non-existent key", iTod
075: .containsKey(int2));
076: assertFalse("Should not find non-existent key", iTod
077: .containsKey(int3));
078:
079: assertFalse("Should not find non-existent value", iTod
080: .containsValue(dbl1));
081: assertFalse("Should not find non-existent value", iTod
082: .containsValue(dbl2));
083: assertFalse("Should not find non-existent value", iTod
084: .containsValue(dbl3));
085:
086: iTod.put(int1, dbl1);
087:
088: assertFalse("NonEmpty BHM is not empty", iTod.isEmpty());
089: assertFalse("NonEmpty BHM has some values", iTod.values()
090: .isEmpty());
091:
092: assertTrue("Should find key", iTod.containsKey(int1));
093: assertFalse("Should not find non-existent key", iTod
094: .containsKey(int2));
095: assertFalse("Should not find non-existent key", iTod
096: .containsKey(int3));
097:
098: assertTrue("Should find value", iTod.containsValue(dbl1));
099: assertFalse("Should not find non-existent value", iTod
100: .containsValue(dbl2));
101: assertFalse("Should not find non-existent value", iTod
102: .containsValue(dbl3));
103:
104: iTod.put(int2, dbl2);
105: iTod.put(int3, dbl3);
106:
107: Collection<Double> valsCol = iTod.values();
108:
109: Object[] vals = iTod.valuesArray();
110: Object[] colVals = valsCol.toArray();
111:
112: // These collections are enumerated in any order
113:
114: Arrays.sort(vals);
115: Arrays.sort(colVals);
116:
117: assertTrue("values() test", Arrays.equals(vals, colVals));
118:
119: assertTrue("values test", Arrays.equals(dbls, vals));
120:
121: Iterator<Double> it = iTod.valuesIterator();
122: try {
123: it.remove();
124: fail("Removing non-existent element should generate IllegalStateException");
125: } catch (IllegalStateException e) {
126: }
127:
128: Double val = it.next();
129: Integer key = iTod.getKey(val);
130: iTod.removeKey(val);
131: assertEquals("Size should be 2", 2, iTod.size());
132: assertTrue("Iterator should be non empty", it.hasNext());
133:
134: assertFalse("Should not find non-existant key", iTod
135: .containsKey(key));
136: assertFalse("Should not find non-existant key", iTod
137: .containsValue(val));
138:
139: it = iTod.valuesIterator();
140: val = it.next();
141: key = iTod.getKey(val);
142: it.remove();
143: assertEquals("Size should be 1", 1, iTod.size());
144: assertTrue("Iterator should be non empty", it.hasNext());
145:
146: assertFalse("Should not find non-existant key", iTod
147: .containsKey(key));
148: assertFalse("Should not find non-existant value", iTod
149: .containsValue(val));
150:
151: iTod.clear();
152: }
153:
154: public void testRemove() {
155: Double dbl1 = new Double(.1);
156: Double dbl2 = new Double(.2);
157: Double dbl3 = new Double(.3);
158:
159: Integer int1 = new Integer(1);
160: Integer int2 = new Integer(2);
161: Integer int3 = new Integer(3);
162: BidirectionalHashMap<Double, Integer> dToi = new BidirectionalHashMap<Double, Integer>();
163:
164: assertEquals("Initial size of 0", dToi.size(), 0);
165: dToi.clear();
166: assertEquals("Initial size of 0", dToi.size(), 0);
167:
168: dToi.put(dbl1, int1);
169: assertEquals("Size should be 1", dToi.size(), 1);
170: dToi.put(dbl2, int2);
171: assertEquals("Size should be 2", dToi.size(), 2);
172: dToi.put(dbl3, int3);
173: assertEquals("Size should be 3", dToi.size(), 3);
174:
175: dToi.removeKey(int1);
176: assertEquals("Size should be 2", dToi.size(), 2);
177:
178: // Test of removeKey
179: assertEquals("Removing key should return associated value",
180: null, dToi.removeKey(int1));
181: assertEquals("Size should be 2", dToi.size(), 2);
182: dToi.put(dbl1, int1);
183: assertEquals("Size should be 3", dToi.size(), 3);
184: try {
185: dToi.put(dbl3, int3);
186: fail("Adding existing element should generate IllegalArgumentException");
187: } catch (IllegalArgumentException e) {
188: }
189: assertEquals("Size should be 3", dToi.size(), 3);
190:
191: // Test of removeValue
192: dToi.removeValue(dbl3);
193: assertEquals("Size should be 2", dToi.size(), 2);
194: assertEquals("Removing value should retrun associated key",
195: int2, dToi.removeValue(dbl2));
196:
197: assertEquals("Size should be 1", dToi.size(), 1);
198: dToi.put(dbl3, int3);
199: assertEquals("Size should be 2", dToi.size(), 2);
200: try {
201: dToi.put(dbl3, int3);
202: fail("Adding existing element should generate IllegalArgumentException");
203: } catch (IllegalArgumentException e) {
204: }
205: assertEquals("Size should be 2", dToi.size(), 2);
206:
207: dToi.clear();
208: assertEquals("Cleared size of 0", dToi.size(), 0);
209:
210: assertFalse("Iterator to cleared list should be empty", dToi
211: .valuesIterator().hasNext());
212: }
213:
214: /**
215: * A test method.
216: * (Replace "X" with a name describing the test. You may write as
217: * many "testSomething" methods in this class as you wish, and each
218: * one will be called when running JUnit over this class.)
219: */
220: public void testPut() {
221:
222: String one = "1";
223: String two = "2";
224: String three = "3";
225:
226: Integer int1 = new Integer(1);
227: Integer int2 = new Integer(2);
228: Integer int3 = new Integer(3);
229:
230: BidirectionalHashMap<String, Integer> myhash = new BidirectionalHashMap<String, Integer>();
231:
232: assertEquals("Expected size of 0", 0, myhash.size());
233:
234: assertEquals("Expected null", null, myhash.getValue(one));
235: assertEquals("Expected null", null, myhash.getValue(two));
236: assertEquals("Expected null", null, myhash.getValue(three));
237:
238: assertEquals("Expected null", null, myhash.getKey(int1));
239: assertEquals("Expected null", null, myhash.getKey(int2));
240: assertEquals("Expected null", null, myhash.getKey(int3));
241:
242: myhash.put(one, int1);
243: myhash.put(two, int2);
244: myhash.put(three, int3);
245:
246: assertTrue("Given one, should get 1",
247: myhash.getValue(one) == int1);
248: assertTrue("Given two, should get 2",
249: myhash.getValue(two) == int2);
250: assertTrue("Given three, should get 3",
251: myhash.getValue(three) == int3);
252:
253: assertTrue("Given 1, should get one",
254: myhash.getKey(int1) == one);
255: assertTrue("Given 2, should get two",
256: myhash.getKey(int2) == two);
257: assertTrue("Given 3, should get three",
258: myhash.getKey(int3) == three);
259:
260: Iterator<Integer> it = myhash.valuesIterator();
261: try {
262: it.remove();
263: fail("Removing non-existent element should generate IllegalStateException");
264: } catch (IllegalStateException e) {
265: }
266:
267: Integer value = it.next();
268: String key = myhash.getKey(value);
269: assertEquals("key and value should match", value.toString(),
270: key);
271: it.remove();
272: assertEquals("After removing key, it should not appear in map",
273: null, myhash.getValue(key));
274: assertEquals(
275: "After removing value, it should not appear in map",
276: null, myhash.getKey(value));
277: assertTrue("Map should contain elements", it.hasNext());
278:
279: value = it.next();
280: key = myhash.getKey(value);
281: assertEquals("key and value should match", value.toString(),
282: key);
283: it.remove();
284: assertEquals("After removing key, it should not appear in map",
285: null, myhash.getValue(key));
286: assertEquals(
287: "After removing value, it should not appear in map",
288: null, myhash.getKey(value));
289: assertTrue("Map should contain elements", it.hasNext());
290:
291: value = it.next();
292: key = myhash.getKey(value);
293: assertEquals("key and value should match", value.toString(),
294: key);
295: it.remove();
296: assertEquals("After removing key, it should not appear in map",
297: null, myhash.getValue(key));
298: assertEquals(
299: "After removing value, it should not appear in map",
300: null, myhash.getKey(value));
301:
302: /* myhash should be empty now */
303: it = myhash.valuesIterator();
304: assertFalse("Map should be empty", it.hasNext());
305: }
306:
307: }
|