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 OrderedHashSetTest extends DrJavaTestCase {
050:
051: public void testSearch() {
052:
053: Integer int1 = new Integer(1);
054: Integer int2 = new Integer(2);
055: Integer int3 = new Integer(3);
056:
057: Integer[] ints = new Integer[] { int1, int2, int3 };
058:
059: OrderedHashSet<Integer> iTod = new OrderedHashSet<Integer>();
060:
061: assertTrue("Empty BHM is empty", iTod.isEmpty());
062: assertTrue("Empty BHM has no values", iTod.elements().isEmpty());
063:
064: assertEquals("Initial size of 0", iTod.size(), 0);
065: try {
066: iTod.get(0);
067: fail("Queue should be empty forcing an Exception to be thrown");
068: } catch (IndexOutOfBoundsException e) { /* silently succeed */
069: }
070:
071: assertFalse("Should not find non-existent key", iTod
072: .contains(int1));
073: assertFalse("Should not find non-existent key", iTod
074: .contains(int2));
075: assertFalse("Should not find non-existent key", iTod
076: .contains(int3));
077:
078: iTod.add(int1);
079:
080: assertFalse("NonEmpty BHM is not empty", iTod.isEmpty());
081: assertFalse("NonEmpty BHM has some values", iTod.elements()
082: .isEmpty());
083:
084: assertTrue("Should find key", iTod.contains(int1));
085: assertEquals("Should find key using index", int1, iTod.get(0));
086: assertFalse("Should not find non-existent key", iTod
087: .contains(int2));
088: assertFalse("Should not find non-existent key", iTod
089: .contains(int3));
090:
091: iTod.add(int2);
092: iTod.add(int3);
093:
094: assertEquals("get(1) test", int2, iTod.get(1)); // we should rename int1, int2, int3 as int0, int1, int2
095: assertEquals("get(2) test", int3, iTod.get(2));
096:
097: Collection<Integer> valsCol = iTod.elements();
098:
099: Object[] vals = iTod.toArray();
100: Object[] colVals = valsCol.toArray();
101:
102: // These collections are enumerated in order of insertion
103:
104: assertTrue("elements() test", Arrays.equals(vals, colVals));
105:
106: Iterator<Integer> it = iTod.iterator();
107: try {
108: it.remove();
109: fail("Removing non-existent element should generate IllegalStateException");
110: } catch (IllegalStateException e) {
111: }
112:
113: Integer key = it.next();
114: iTod.remove(key);
115: assertEquals("Size should be 2", 2, iTod.size());
116: assertTrue("Iterator should be non empty", it.hasNext());
117:
118: assertFalse("Should not find non-existent key", iTod
119: .contains(key));
120:
121: it = iTod.iterator();
122: key = it.next();
123: it.remove();
124: assertEquals("Size should be 1", 1, iTod.size());
125: assertTrue("Iterator should be non empty", it.hasNext());
126:
127: assertFalse("Should not find non-existent key", iTod
128: .contains(key));
129:
130: iTod.remove(0);
131: assertTrue("iTod should be empty", iTod.isEmpty());
132:
133: }
134:
135: public void testRemove() {
136:
137: Integer int1 = new Integer(1);
138: Integer int2 = new Integer(2);
139: Integer int3 = new Integer(3);
140:
141: OrderedHashSet<Integer> dToi = new OrderedHashSet<Integer>();
142:
143: assertEquals("Initial size of 0", dToi.size(), 0);
144: dToi.clear();
145: assertEquals("Initial size of 0", dToi.size(), 0);
146:
147: dToi.add(int1);
148: assertEquals("Size should be 1", dToi.size(), 1);
149: dToi.add(int2);
150: assertEquals("Size should be 2", dToi.size(), 2);
151: dToi.add(int3);
152: assertEquals("Size should be 3", dToi.size(), 3);
153:
154: dToi.remove(int1);
155: assertEquals("Size should be 2", dToi.size(), 2);
156:
157: // Test of removeKey
158: assertFalse("Removed key should be found", dToi.remove(int1));
159: assertEquals("Size should be 2", dToi.size(), 2);
160: dToi.add(int1);
161: assertEquals("Size should be 3", dToi.size(), 3);
162: assertFalse("Adding existing element should return false", dToi
163: .add(int1));
164: assertEquals("Size should be 3", dToi.size(), 3);
165:
166: dToi.remove(int3);
167: assertEquals("Size should be 2", dToi.size(), 2);
168: dToi.remove(int2);
169: assertFalse("Removed key should not be found", dToi
170: .contains(int2));
171:
172: assertEquals("Size should be 1", dToi.size(), 1);
173: dToi.add(int3);
174: assertEquals("Size should be 2", dToi.size(), 2);
175: assertFalse("Adding existing element should return false", dToi
176: .add(int3));
177: assertEquals("Size should be 2", dToi.size(), 2);
178:
179: Integer i = dToi.remove(1);
180: assertEquals("Deleted element should be int3", i, int3);
181: assertEquals("Deleted element should be int1", dToi.remove(0),
182: int1);
183: assertEquals("Resulting size of 0", dToi.size(), 0);
184:
185: assertFalse("Iterator to cleared list should be empty", dToi
186: .iterator().hasNext());
187: }
188:
189: public void testPut() {
190:
191: String one = "1";
192: String two = "2";
193: String three = "3";
194:
195: OrderedHashSet<String> myhash = new OrderedHashSet<String>();
196:
197: assertFalse("Expected false", myhash.contains(one));
198: assertFalse("Expected false", myhash.contains(two));
199: assertFalse("Expected false", myhash.contains(three));
200:
201: myhash.add(one);
202: myhash.add(two);
203: myhash.add(three);
204:
205: assertTrue("one should be in the set", myhash.contains(one));
206: assertTrue("two should be in the set", myhash.contains(two));
207: assertTrue("three should be in the set", myhash.contains(three));
208:
209: Iterator<String> it = myhash.iterator();
210: try {
211: it.remove();
212: fail("Removing non-existent element should generate IllegalStateException");
213: } catch (IllegalStateException e) {
214: }
215:
216: String key = it.next();
217: it.remove();
218: assertFalse("After removing key, it should not appear in set",
219: myhash.contains(key));
220:
221: key = it.next();
222: it.remove();
223: assertFalse("After removing key, it should not appear in set",
224: myhash.contains(key));
225:
226: key = it.next();
227: it.remove();
228: assertFalse("After removing key, it should not appear in set",
229: myhash.contains(key));
230:
231: /* myhash should be empty now */
232: it = myhash.iterator();
233: assertFalse("Set should be empty", it.hasNext());
234: }
235: }
|