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 junit.framework.TestCase;
040: import java.util.*;
041:
042: import edu.rice.cs.drjava.DrJavaTestCase;
043:
044: /**
045: * A JUnit test case class.
046: * Every method starting with the word "test" will be called when running
047: * the test with JUnit.
048: */
049: public class OrderedBidirectionalHashMapTest extends DrJavaTestCase {
050:
051: public void testSearch() {
052: Double dbl1 = new Double(.1);
053: Double dbl2 = new Double(.2);
054: Double dbl3 = new Double(.3);
055:
056: Double[] dbls = new Double[] { dbl1, dbl2, dbl3 };
057:
058: Integer int1 = new Integer(1);
059: Integer int2 = new Integer(2);
060: Integer int3 = new Integer(3);
061:
062: Integer[] ints = new Integer[] { int1, int2, int3 };
063:
064: BidirectionalHashMap<Integer, Double> iTod = new OrderedBidirectionalHashMap<Integer, Double>();
065:
066: assertTrue("Empty BHM is empty", iTod.isEmpty());
067: assertTrue("Empty BHM has no values", iTod.values().isEmpty());
068:
069: assertEquals("Initial size of 0", iTod.size(), 0);
070:
071: assertFalse("Should not find non-existent key", iTod
072: .containsKey(int1));
073: assertFalse("Should not find non-existent key", iTod
074: .containsKey(int2));
075: assertFalse("Should not find non-existent key", iTod
076: .containsKey(int3));
077:
078: assertFalse("Should not find non-existent value", iTod
079: .containsValue(dbl1));
080: assertFalse("Should not find non-existent value", iTod
081: .containsValue(dbl2));
082: assertFalse("Should not find non-existent value", iTod
083: .containsValue(dbl3));
084:
085: iTod.put(int1, dbl1);
086:
087: assertFalse("NonEmpty BHM is not empty", iTod.isEmpty());
088: assertFalse("NonEmpty BHM has some values", iTod.values()
089: .isEmpty());
090:
091: assertTrue("Should find key", iTod.containsKey(int1));
092: assertFalse("Should not find non-existent key", iTod
093: .containsKey(int2));
094: assertFalse("Should not find non-existent key", iTod
095: .containsKey(int3));
096:
097: assertTrue("Should find value", iTod.containsValue(dbl1));
098: assertFalse("Should not find non-existent value", iTod
099: .containsValue(dbl2));
100: assertFalse("Should not find non-existent value", iTod
101: .containsValue(dbl3));
102:
103: iTod.put(int2, dbl2);
104: iTod.put(int3, dbl3);
105:
106: Collection<Double> valsCol = iTod.values();
107:
108: Object[] vals = iTod.valuesArray();
109: Object[] colVals = valsCol.toArray();
110:
111: // These collections are enumerated in order of insertion
112:
113: // System.out.println("dbls = " + Arrays.asList(dbls).toString());
114: // System.out.println("vals = " + Arrays.asList(vals).toString());
115: assertTrue("values() test", Arrays.equals(vals, colVals));
116: assertTrue("values test", Arrays.equals(dbls, vals));
117:
118: Iterator<Double> it = iTod.valuesIterator();
119: try {
120: it.remove();
121: fail("Removing non-existent element should generate IllegalStateException");
122: } catch (IllegalStateException e) {
123: }
124:
125: Double val = it.next();
126: Integer key = iTod.getKey(val);
127: iTod.removeKey(val);
128: assertEquals("Size should be 2", 2, iTod.size());
129: assertTrue("Iterator should be non empty", it.hasNext());
130:
131: assertFalse("Should not find non-existent key", iTod
132: .containsKey(key));
133: assertFalse("Should not find non-existent key", iTod
134: .containsValue(val));
135:
136: it = iTod.valuesIterator();
137: val = it.next();
138: key = iTod.getKey(val);
139: it.remove();
140: assertEquals("Size should be 1", 1, iTod.size());
141: assertTrue("Iterator should be non empty", it.hasNext());
142:
143: assertFalse("Should not find non-existent key", iTod
144: .containsKey(key));
145: assertFalse("Should not find non-existent value", iTod
146: .containsValue(val));
147:
148: iTod.clear();
149: }
150:
151: public void testRemove() {
152: Double dbl1 = new Double(.1);
153: Double dbl2 = new Double(.2);
154: Double dbl3 = new Double(.3);
155:
156: Integer int1 = new Integer(1);
157: Integer int2 = new Integer(2);
158: Integer int3 = new Integer(3);
159: BidirectionalHashMap<Double, Integer> dToi = new OrderedBidirectionalHashMap<Double, Integer>();
160:
161: assertEquals("Initial size of 0", dToi.size(), 0);
162: dToi.clear();
163: assertEquals("Initial size of 0", dToi.size(), 0);
164:
165: dToi.put(dbl1, int1);
166: assertEquals("Size should be 1", dToi.size(), 1);
167: dToi.put(dbl2, int2);
168: assertEquals("Size should be 2", dToi.size(), 2);
169: dToi.put(dbl3, int3);
170: assertEquals("Size should be 3", dToi.size(), 3);
171:
172: dToi.removeKey(int1);
173: assertEquals("Size should be 2", dToi.size(), 2);
174:
175: // Test of removeKey
176: assertEquals("Removing key should return associated value",
177: null, dToi.removeKey(int1));
178: assertEquals("Size should be 2", dToi.size(), 2);
179: dToi.put(dbl1, int1);
180: assertEquals("Size should be 3", dToi.size(), 3);
181: try {
182: dToi.put(dbl3, int3);
183: fail("Adding existing element should generate IllegalArgumentException");
184: } catch (IllegalArgumentException e) {
185: }
186: assertEquals("Size should be 3", dToi.size(), 3);
187:
188: // Test of removeValue
189: dToi.removeValue(dbl3);
190: assertEquals("Size should be 2", dToi.size(), 2);
191: assertEquals("Removing value should retrun associated key",
192: int2, dToi.removeValue(dbl2));
193:
194: assertEquals("Size should be 1", dToi.size(), 1);
195: dToi.put(dbl3, int3);
196: assertEquals("Size should be 2", dToi.size(), 2);
197: try {
198: dToi.put(dbl3, int3);
199: fail("Adding existing element should generate IllegalArgumentException");
200: } catch (IllegalArgumentException e) {
201: }
202: assertEquals("Size should be 2", dToi.size(), 2);
203:
204: dToi.clear();
205: assertEquals("Cleared size of 0", dToi.size(), 0);
206:
207: assertFalse("Iterator to cleared list should be empty", dToi
208: .valuesIterator().hasNext());
209: }
210:
211: public void testPut() {
212:
213: String one = "1";
214: String two = "2";
215: String three = "3";
216:
217: Integer int1 = new Integer(1);
218: Integer int2 = new Integer(2);
219: Integer int3 = new Integer(3);
220:
221: OrderedBidirectionalHashMap<String, Integer> myhash = new OrderedBidirectionalHashMap<String, Integer>();
222:
223: assertEquals("Expected null", null, myhash.getValue(one));
224: assertEquals("Expected null", null, myhash.getValue(two));
225: assertEquals("Expected null", null, myhash.getValue(three));
226:
227: assertEquals("Expected null", null, myhash.getKey(int1));
228: assertEquals("Expected null", null, myhash.getKey(int2));
229: assertEquals("Expected null", null, myhash.getKey(int3));
230:
231: myhash.put(one, int1);
232: myhash.put(two, int2);
233: myhash.put(three, int3);
234:
235: assertTrue("Given one, should get 1",
236: myhash.getValue(one) == int1);
237: assertTrue("Given two, should get 2",
238: myhash.getValue(two) == int2);
239: assertTrue("Given three, should get 3",
240: myhash.getValue(three) == int3);
241:
242: assertTrue("Given 1, should get one",
243: myhash.getKey(int1) == one);
244: assertTrue("Given 2, should get two",
245: myhash.getKey(int2) == two);
246: assertTrue("Given 3, should get three",
247: myhash.getKey(int3) == three);
248:
249: Iterator<Integer> it = myhash.valuesIterator();
250: try {
251: it.remove();
252: fail("Removing non-existent element should generate IllegalStateException");
253: } catch (IllegalStateException e) {
254: }
255:
256: Integer value = it.next();
257: String key = myhash.getKey(value);
258: assertEquals("key and value should match", value.toString(),
259: key);
260: it.remove();
261: assertEquals("After removing key, it should not appear in map",
262: null, myhash.getValue(key));
263: assertEquals(
264: "After removing value, it should not appear in map",
265: null, myhash.getKey(value));
266:
267: value = it.next();
268: 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:
278: value = it.next();
279: key = myhash.getKey(value);
280: assertEquals("key and value should match", value.toString(),
281: key);
282: it.remove();
283: assertEquals("After removing key, it should not appear in map",
284: null, myhash.getValue(key));
285: assertEquals(
286: "After removing value, it should not appear in map",
287: null, myhash.getKey(value));
288:
289: /* myhash should be empty now */
290: it = myhash.valuesIterator();
291: assertFalse("Map should be empty", it.hasNext());
292: }
293: }
|