KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
8  * A JUnit test case class.
9  * Every method starting with the word "test" will be called when running
10  * the test with JUnit.
11  */

12 public class OrderedHashSetTest extends DrJavaTestCase {
13   
14   public void testSearch() {
15
16     Integer JavaDoc int1 = new Integer JavaDoc(1);
17     Integer JavaDoc int2 = new Integer JavaDoc(2);
18     Integer JavaDoc int3 = new Integer JavaDoc(3);
19     
20     Integer JavaDoc[] ints = new Integer JavaDoc[]{int1, int2, int3};
21      
22     OrderedHashSet<Integer JavaDoc> iTod = new OrderedHashSet<Integer JavaDoc>();
23     
24     assertTrue("Empty BHM is empty", iTod.isEmpty());
25     assertTrue("Empty BHM has no values", iTod.elements().isEmpty());
26
27     assertEquals("Initial size of 0", iTod.size(), 0);
28     try {
29       iTod.get(0);
30       fail("Queue should be empty forcing an Exception to be thrown");
31     }
32     catch(IndexOutOfBoundsException JavaDoc e) { /* silently succeed */ }
33
34     assertFalse("Should not find non-existent key", iTod.contains(int1));
35     assertFalse("Should not find non-existent key", iTod.contains(int2));
36     assertFalse("Should not find non-existent key", iTod.contains(int3));
37
38     iTod.add(int1);
39     
40     assertFalse("NonEmpty BHM is not empty", iTod.isEmpty());
41     assertFalse("NonEmpty BHM has some values", iTod.elements().isEmpty());
42     
43     assertTrue("Should find key", iTod.contains(int1));
44     assertEquals("Should find key using index", int1, iTod.get(0));
45     assertFalse("Should not find non-existent key", iTod.contains(int2));
46     assertFalse("Should not find non-existent key", iTod.contains(int3));
47
48     iTod.add(int2);
49     iTod.add(int3);
50     
51     assertEquals("get(1) test", int2, iTod.get(1)); // we should rename int1, int2, int3 as int0, int1, int2
52
assertEquals("get(2) test", int3, iTod.get(2));
53     
54     Collection<Integer JavaDoc> valsCol = iTod.elements();
55     
56     Object JavaDoc[] vals = iTod.toArray();
57     Object JavaDoc[] colVals = valsCol.toArray();
58     
59     // These collections are enumerated in order of insertion
60

61     assertTrue("elements() test", Arrays.equals(vals, colVals));
62            
63     Iterator<Integer JavaDoc> it = iTod.iterator();
64     try {
65       it.remove();
66       fail("Removing non-existent element should generate IllegalStateException");
67     } catch(IllegalStateException JavaDoc e) { }
68
69     Integer JavaDoc key = it.next();
70     iTod.remove(key);
71     assertEquals("Size should be 2", 2, iTod.size());
72     assertTrue("Iterator should be non empty", it.hasNext());
73
74     assertFalse("Should not find non-existent key", iTod.contains(key));
75
76     it = iTod.iterator();
77     key = it.next();
78     it.remove();
79     assertEquals("Size should be 1", 1, iTod.size());
80     assertTrue("Iterator should be non empty", it.hasNext());
81
82     assertFalse("Should not find non-existent key", iTod.contains(key));
83
84     iTod.remove(0);
85     assertTrue("iTod should be empty", iTod.isEmpty());
86     
87   }
88   
89   public void testRemove() {
90  
91     Integer JavaDoc int1 = new Integer JavaDoc(1);
92     Integer JavaDoc int2 = new Integer JavaDoc(2);
93     Integer JavaDoc int3 = new Integer JavaDoc(3);
94     
95     OrderedHashSet<Integer JavaDoc> dToi = new OrderedHashSet<Integer JavaDoc>();
96
97     assertEquals("Initial size of 0", dToi.size(), 0);
98     dToi.clear();
99     assertEquals("Initial size of 0", dToi.size(), 0);
100
101     dToi.add(int1);
102     assertEquals("Size should be 1", dToi.size(), 1);
103     dToi.add(int2);
104     assertEquals("Size should be 2", dToi.size(), 2);
105     dToi.add(int3);
106     assertEquals("Size should be 3", dToi.size(), 3);
107
108     dToi.remove(int1);
109     assertEquals("Size should be 2", dToi.size(), 2);
110
111     // Test of removeKey
112
assertFalse("Removed key should be found", dToi.remove(int1));
113     assertEquals("Size should be 2", dToi.size(), 2);
114     dToi.add(int1);
115     assertEquals("Size should be 3", dToi.size(), 3);
116     assertFalse("Adding existing element should return false", dToi.add(int1));
117     assertEquals("Size should be 3", dToi.size(), 3);
118
119     dToi.remove(int3);
120     assertEquals("Size should be 2", dToi.size(), 2);
121     dToi.remove(int2);
122     assertFalse("Removed key should not be found", dToi.contains(int2));
123
124     assertEquals("Size should be 1", dToi.size(), 1);
125     dToi.add(int3);
126     assertEquals("Size should be 2", dToi.size(), 2);
127     assertFalse("Adding existing element should return false", dToi.add(int3));
128     assertEquals("Size should be 2", dToi.size(), 2);
129
130     Integer JavaDoc i = dToi.remove(1);
131     assertEquals("Deleted element should be int3", i, int3);
132     assertEquals("Deleted element should be int1", dToi.remove(0), int1);
133     assertEquals("Resulting size of 0", dToi.size(), 0);
134
135     assertFalse("Iterator to cleared list should be empty", dToi.iterator().hasNext());
136   }
137   
138   public void testPut() {
139     
140     String JavaDoc one = "1";
141     String JavaDoc two = "2";
142     String JavaDoc three = "3";
143     
144     OrderedHashSet<String JavaDoc> myhash = new OrderedHashSet<String JavaDoc>();
145     
146     assertFalse("Expected false", myhash.contains(one));
147     assertFalse("Expected false", myhash.contains(two));
148     assertFalse("Expected false", myhash.contains(three));
149           
150     myhash.add(one);
151     myhash.add(two);
152     myhash.add(three);
153     
154     assertTrue("one should be in the set", myhash.contains(one));
155     assertTrue("two should be in the set", myhash.contains(two));
156     assertTrue("three should be in the set", myhash.contains(three));
157     
158     Iterator<String JavaDoc> it = myhash.iterator();
159     try {
160       it.remove();
161       fail("Removing non-existent element should generate IllegalStateException");
162     } catch(IllegalStateException JavaDoc e) { }
163     
164     String JavaDoc key = it.next();
165     it.remove();
166     assertFalse("After removing key, it should not appear in set", myhash.contains(key));
167     
168     key = it.next();
169     it.remove();
170     assertFalse("After removing key, it should not appear in set", myhash.contains(key));
171     
172     key = it.next();
173     it.remove();
174     assertFalse("After removing key, it should not appear in set", myhash.contains(key));
175     
176     /* myhash should be empty now */
177     it = myhash.iterator();
178     assertFalse("Set should be empty", it.hasNext());
179   }
180 }
181
Popular Tags