ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:54 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11 import java.util.Enumeration;
12
13 public class ConcurrentHashMapTest extends TestCase{
14 public static void main(String[] args) {
15 junit.textui.TestRunner.run (suite());
16 }
17 public static Test suite() {
18 return new TestSuite(ConcurrentHashMapTest.class);
19 }
20
21 static final Integer one = new Integer(1);
22 static final Integer two = new Integer(2);
23 static final Integer three = new Integer(3);
24 static final Integer four = new Integer(4);
25 static final Integer five = new Integer(5);
26
27 private static ConcurrentHashMap map5() {
28 ConcurrentHashMap map = new ConcurrentHashMap(5);
29 assertTrue(map.isEmpty());
30 map.put(one, "A");
31 map.put(two, "B");
32 map.put(three, "C");
33 map.put(four, "D");
34 map.put(five, "E");
35 assertFalse(map.isEmpty());
36 assertEquals(5, map.size());
37 return map;
38 }
39
40 /**
41 * Test to verify clear correctly removes all key-element pairs from the map
42 */
43 public void testClear(){
44 ConcurrentHashMap map = map5();
45 map.clear();
46 assertEquals(map.size(), 0);
47 }
48
49 /**
50 * Test to verify contains gives the appropriate value
51 */
52 public void testContains(){
53 ConcurrentHashMap map = map5();
54 assertTrue(map.contains("A"));
55 assertFalse(map.contains("Z"));
56 }
57
58 /**
59 * Test to verify containsKey gives the appropriate value
60 */
61 public void testContainsKey(){
62 ConcurrentHashMap map = map5();
63 assertTrue(map.containsKey(one));
64 assertFalse(map.containsKey(new Integer(100)));
65 }
66
67 /**
68 * Identical to normal contains
69 */
70 public void testContainsValue(){
71 ConcurrentHashMap map = map5();
72 assertTrue(map.contains("A"));
73 assertFalse(map.contains("Z"));
74 }
75
76 /**
77 * tes to verify enumeration returns an enumeration containing the correct elements
78 */
79 public void testEnumeration(){
80 ConcurrentHashMap map = map5();
81 Enumeration e = map.elements();
82 int count = 0;
83 while(e.hasMoreElements()){
84 count++;
85 e.nextElement();
86 }
87 assertEquals(5, count);
88 }
89
90 /**
91 * Test to verify get returns the correct element at the given index
92 */
93 public void testGet(){
94 ConcurrentHashMap map = map5();
95 assertEquals("A", (String)map.get(one));
96 }
97
98 /**
99 * Test to verify get on a nonexistant key returns null
100 */
101 public void testGet2(){
102 ConcurrentHashMap empty = new ConcurrentHashMap();
103 assertNull(empty.get("anything"));
104 }
105
106 /**
107 * Simple test to verify isEmpty returns the correct value
108 */
109 public void testIsEmpty(){
110 ConcurrentHashMap empty = new ConcurrentHashMap();
111 ConcurrentHashMap map = map5();
112 assertTrue(empty.isEmpty());
113 assertFalse(map.isEmpty());
114 }
115
116 /**
117 * Test to verify keys returns an enumeration containing all the keys from the map
118 */
119 public void testKeys(){
120 ConcurrentHashMap map = map5();
121 Enumeration e = map.keys();
122 int count = 0;
123 while(e.hasMoreElements()){
124 count++;
125 e.nextElement();
126 }
127 assertEquals(5, count);
128 }
129
130 /**
131 * Test to verify keySet returns a Set containing all the keys
132 */
133 public void testKeySet(){
134 ConcurrentHashMap map = map5();
135 Set s = map.keySet();
136 assertEquals(5, s.size());
137 assertTrue(s.contains(one));
138 assertTrue(s.contains(two));
139 assertTrue(s.contains(three));
140 assertTrue(s.contains(four));
141 assertTrue(s.contains(five));
142 }
143
144 public void testValues(){
145 ConcurrentHashMap map = map5();
146 Collection s = map.values();
147 assertEquals(5, s.size());
148 assertTrue(s.contains("A"));
149 assertTrue(s.contains("B"));
150 assertTrue(s.contains("C"));
151 assertTrue(s.contains("D"));
152 assertTrue(s.contains("E"));
153 }
154
155 public void testEntrySet(){
156 ConcurrentHashMap map = map5();
157 Set s = map.entrySet();
158 assertEquals(5, s.size());
159 Iterator it = s.iterator();
160 while (it.hasNext()) {
161 Map.Entry e = (Map.Entry) it.next();
162 assertTrue(
163 (e.getKey().equals(one) && e.getValue().equals("A")) ||
164 (e.getKey().equals(two) && e.getValue().equals("B")) ||
165 (e.getKey().equals(three) && e.getValue().equals("C")) ||
166 (e.getKey().equals(four) && e.getValue().equals("D")) ||
167 (e.getKey().equals(five) && e.getValue().equals("E")));
168 }
169 }
170
171 /**
172 * Test to verify putAll correctly adds all key-value pairs from the given map
173 */
174 public void testPutAll(){
175 ConcurrentHashMap empty = new ConcurrentHashMap();
176 ConcurrentHashMap map = map5();
177 empty.putAll(map);
178 assertEquals(5, empty.size());
179 assertTrue(empty.containsKey(one));
180 assertTrue(empty.containsKey(two));
181 assertTrue(empty.containsKey(three));
182 assertTrue(empty.containsKey(four));
183 assertTrue(empty.containsKey(five));
184 }
185
186 /**
187 * Test to verify putIfAbsent works when the given key is not present
188 */
189 public void testPutIfAbsent(){
190 ConcurrentHashMap map = map5();
191 map.putIfAbsent(new Integer(6), "Z");
192 assertTrue(map.containsKey(new Integer(6)));
193 }
194
195 /**
196 * Test to verify putIfAbsent does not add the pair if the key is already present
197 */
198 public void testPutIfAbsent2(){
199 ConcurrentHashMap map = map5();
200 assertEquals("A", map.putIfAbsent(one, "Z"));
201 }
202
203 /**
204 * Test to verify remove removes the correct key-value pair from the map
205 */
206 public void testRemove(){
207 ConcurrentHashMap map = map5();
208 map.remove(five);
209 assertEquals(4, map.size());
210 assertFalse(map.containsKey(five));
211 }
212
213 public void testRemove2(){
214 ConcurrentHashMap map = map5();
215 map.remove(five, "E");
216 assertEquals(4, map.size());
217 assertFalse(map.containsKey(five));
218 map.remove(four, "A");
219 assertEquals(4, map.size());
220 assertTrue(map.containsKey(four));
221
222 }
223
224 /**
225 * Simple test to verify size returns the correct values
226 */
227 public void testSize(){
228 ConcurrentHashMap map = map5();
229 ConcurrentHashMap empty = new ConcurrentHashMap();
230 assertEquals(0, empty.size());
231 assertEquals(5, map.size());
232 }
233
234 public void testToString(){
235 ConcurrentHashMap map = map5();
236 String s = map.toString();
237 for (int i = 1; i <= 5; ++i) {
238 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
239 }
240 }
241
242 // Exception tests
243
244 public void testConstructor1(){
245 try{
246 new ConcurrentHashMap(-1,0,1);
247 fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
248 }catch(IllegalArgumentException e){}
249 }
250
251 public void testConstructor2(){
252 try{
253 new ConcurrentHashMap(1,0,-1);
254 fail("ConcurrentHashMap(int, float, int) should throw Illegal Argument Exception");
255 }catch(IllegalArgumentException e){}
256 }
257
258 public void testConstructor3(){
259 try{
260 new ConcurrentHashMap(-1);
261 fail("ConcurrentHashMap(int) should throw Illegal Argument Exception");
262 }catch(IllegalArgumentException e){}
263 }
264
265 public void testGet_NullPointerException(){
266 try{
267 ConcurrentHashMap c = new ConcurrentHashMap(5);
268 c.get(null);
269 fail("ConcurrentHashMap - Object get(Object) should throw Null Pointer exception");
270 }catch(NullPointerException e){}
271 }
272
273 public void testContainsKey_NullPointerException(){
274 try{
275 ConcurrentHashMap c = new ConcurrentHashMap(5);
276 c.containsKey(null);
277 fail("ConcurrenthashMap - boolean containsKey(Object) should throw Null Pointer exception");
278 }catch(NullPointerException e){}
279 }
280
281 public void testContainsValue_NullPointerException(){
282 try{
283 ConcurrentHashMap c = new ConcurrentHashMap(5);
284 c.containsValue(null);
285 fail("ConcurrentHashMap - boolean containsValue(Object) should throw Null Pointer exception");
286 }catch(NullPointerException e){}
287 }
288
289 public void testContains_NullPointerException(){
290 try{
291 ConcurrentHashMap c = new ConcurrentHashMap(5);
292 c.contains(null);
293 fail("ConcurrentHashMap - boolean contains(Object) should throw Null Pointer exception");
294 }catch(NullPointerException e){}
295 }
296
297 public void testPut1_NullPointerException(){
298 try{
299 ConcurrentHashMap c = new ConcurrentHashMap(5);
300 c.put(null, "whatever");
301 fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
302 }catch(NullPointerException e){}
303 }
304
305 public void testPut2_NullPointerException(){
306 try{
307 ConcurrentHashMap c = new ConcurrentHashMap(5);
308 c.put("whatever", null);
309 fail("ConcurrentHashMap - Object put(Object, Object) should throw Null Pointer exception");
310 }catch(NullPointerException e){}
311 }
312
313 public void testPutIfAbsent1_NullPointerException(){
314 try{
315 ConcurrentHashMap c = new ConcurrentHashMap(5);
316 c.putIfAbsent(null, "whatever");
317 fail("ConcurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
318 }catch(NullPointerException e){}
319 }
320
321 public void testPutIfAbsent2_NullPointerException(){
322 try{
323 ConcurrentHashMap c = new ConcurrentHashMap(5);
324 c.putIfAbsent("whatever", null);
325 fail("COncurrentHashMap - Object putIfAbsent(Object, Object) should throw Null Pointer exception");
326 }catch(NullPointerException e){}
327 }
328
329
330 public void testRemove1_NullPointerException(){
331 try{
332 ConcurrentHashMap c = new ConcurrentHashMap(5);
333 c.put("sadsdf", "asdads");
334 c.remove(null);
335 fail("ConcurrentHashMap - Object remove(Object) should throw Null pointer exceptione");
336 }catch(NullPointerException e){}
337 }
338
339 public void testRemove2_NullPointerException(){
340 try{
341 ConcurrentHashMap c = new ConcurrentHashMap(5);
342 c.put("sadsdf", "asdads");
343 c.remove(null, "whatever");
344 fail("ConcurrentHashMap - Object remove(Object, Object) should throw Null pointer exceptione");
345 }catch(NullPointerException e){}
346 }
347
348 }