ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMapTest.java (file contents):
Revision 1.9 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.38 by jsr166, Tue Sep 24 17:03:14 2013 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.util.Enumeration;
13 < import java.io.*;
11 > import java.util.concurrent.ConcurrentHashMap;
12  
13 < public class ConcurrentHashMapTest extends JSR166TestCase{
13 > public class ConcurrentHashMapTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());  
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18 <        return new TestSuite(ConcurrentHashMapTest.class);
18 >        return new TestSuite(ConcurrentHashMapTest.class);
19      }
20  
21      /**
22 <     * Create a map from Integers 1-5 to Strings "A"-"E".
22 >     * Returns a new map from Integers 1-5 to Strings "A"-"E".
23       */
24 <    private static ConcurrentHashMap map5() {  
25 <        ConcurrentHashMap map = new ConcurrentHashMap(5);
24 >    private static ConcurrentHashMap<Integer, String> map5() {
25 >        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
26          assertTrue(map.isEmpty());
27 <        map.put(one, "A");
28 <        map.put(two, "B");
29 <        map.put(three, "C");
30 <        map.put(four, "D");
31 <        map.put(five, "E");
27 >        map.put(one, "A");
28 >        map.put(two, "B");
29 >        map.put(three, "C");
30 >        map.put(four, "D");
31 >        map.put(five, "E");
32          assertFalse(map.isEmpty());
33          assertEquals(5, map.size());
34 <        return map;
34 >        return map;
35 >    }
36 >
37 >    /** Re-implement Integer.compare for old java versions */
38 >    static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
39 >
40 >    // classes for testing Comparable fallbacks
41 >    static class BI implements Comparable<BI> {
42 >        private final int value;
43 >        BI(int value) { this.value = value; }
44 >        public int compareTo(BI other) {
45 >            return compare(value, other.value);
46 >        }
47 >        public boolean equals(Object x) {
48 >            return (x instanceof BI) && ((BI)x).value == value;
49 >        }
50 >        public int hashCode() { return 42; }
51 >    }
52 >    static class CI extends BI { CI(int value) { super(value); } }
53 >    static class DI extends BI { DI(int value) { super(value); } }
54 >
55 >    static class BS implements Comparable<BS> {
56 >        private final String value;
57 >        BS(String value) { this.value = value; }
58 >        public int compareTo(BS other) {
59 >            return value.compareTo(other.value);
60 >        }
61 >        public boolean equals(Object x) {
62 >            return (x instanceof BS) && value.equals(((BS)x).value);
63 >        }
64 >        public int hashCode() { return 42; }
65 >    }
66 >
67 >    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
68 >        implements Comparable<LexicographicList<E>> {
69 >        LexicographicList(Collection<E> c) { super(c); }
70 >        LexicographicList(E e) { super(Collections.singleton(e)); }
71 >        public int compareTo(LexicographicList<E> other) {
72 >            int common = Math.min(size(), other.size());
73 >            int r = 0;
74 >            for (int i = 0; i < common; i++) {
75 >                if ((r = get(i).compareTo(other.get(i))) != 0)
76 >                    break;
77 >            }
78 >            if (r == 0)
79 >                r = compare(size(), other.size());
80 >            return r;
81 >        }
82 >        private static final long serialVersionUID = 0;
83 >    }
84 >
85 >    static class CollidingObject {
86 >        final String value;
87 >        CollidingObject(final String value) { this.value = value; }
88 >        public int hashCode() { return this.value.hashCode() & 1; }
89 >        public boolean equals(final Object obj) {
90 >            return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
91 >        }
92 >    }
93 >
94 >    static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
95 >        ComparableCollidingObject(final String value) { super(value); }
96 >        public int compareTo(final ComparableCollidingObject o) {
97 >            return value.compareTo(o.value);
98 >        }
99      }
100  
101      /**
102 <     *  clear removes all pairs
102 >     * Inserted elements that are subclasses of the same Comparable
103 >     * class are found.
104 >     */
105 >    public void testComparableFamily() {
106 >        ConcurrentHashMap<BI, Boolean> m =
107 >            new ConcurrentHashMap<BI, Boolean>();
108 >        for (int i = 0; i < 1000; i++) {
109 >            assertTrue(m.put(new CI(i), true) == null);
110 >        }
111 >        for (int i = 0; i < 1000; i++) {
112 >            assertTrue(m.containsKey(new CI(i)));
113 >            assertTrue(m.containsKey(new DI(i)));
114 >        }
115 >    }
116 >
117 >    /**
118 >     * Elements of classes with erased generic type parameters based
119 >     * on Comparable can be inserted and found.
120 >     */
121 >    public void testGenericComparable() {
122 >        ConcurrentHashMap<Object, Boolean> m =
123 >            new ConcurrentHashMap<Object, Boolean>();
124 >        for (int i = 0; i < 1000; i++) {
125 >            BI bi = new BI(i);
126 >            BS bs = new BS(String.valueOf(i));
127 >            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
128 >            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
129 >            assertTrue(m.putIfAbsent(bis, true) == null);
130 >            assertTrue(m.containsKey(bis));
131 >            if (m.putIfAbsent(bss, true) == null)
132 >                assertTrue(m.containsKey(bss));
133 >            assertTrue(m.containsKey(bis));
134 >        }
135 >        for (int i = 0; i < 1000; i++) {
136 >            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
137 >        }
138 >    }
139 >
140 >    /**
141 >     * Elements of non-comparable classes equal to those of classes
142 >     * with erased generic type parameters based on Comparable can be
143 >     * inserted and found.
144 >     */
145 >    public void testGenericComparable2() {
146 >        ConcurrentHashMap<Object, Boolean> m =
147 >            new ConcurrentHashMap<Object, Boolean>();
148 >        for (int i = 0; i < 1000; i++) {
149 >            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
150 >        }
151 >
152 >        for (int i = 0; i < 1000; i++) {
153 >            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
154 >            assertTrue(m.containsKey(bis));
155 >        }
156 >    }
157 >
158 >    /**
159 >     * Mixtures of instances of comparable and non-comparable classes
160 >     * can be inserted and found.
161 >     */
162 >    public void testMixedComparable() {
163 >        int size = 1200;        // makes measured test run time -> 35ms
164 >        ConcurrentHashMap<Object, Object> map =
165 >            new ConcurrentHashMap<Object, Object>();
166 >        Random rng = new Random();
167 >        for (int i = 0; i < size; i++) {
168 >            Object x;
169 >            switch (rng.nextInt(4)) {
170 >            case 0:
171 >                x = new Object();
172 >                break;
173 >            case 1:
174 >                x = new CollidingObject(Integer.toString(i));
175 >                break;
176 >            default:
177 >                x = new ComparableCollidingObject(Integer.toString(i));
178 >            }
179 >            assertNull(map.put(x, x));
180 >        }
181 >        int count = 0;
182 >        for (Object k : map.keySet()) {
183 >            assertEquals(map.get(k), k);
184 >            ++count;
185 >        }
186 >        assertEquals(count, size);
187 >        assertEquals(map.size(), size);
188 >        for (Object k : map.keySet()) {
189 >            assertEquals(map.put(k, k), k);
190 >        }
191 >    }
192 >
193 >    /**
194 >     * clear removes all pairs
195       */
196      public void testClear() {
197          ConcurrentHashMap map = map5();
198 <        map.clear();
199 <        assertEquals(map.size(), 0);
198 >        map.clear();
199 >        assertEquals(0, map.size());
200      }
201  
202      /**
203 <     *  Maps with same contents are equal
203 >     * Maps with same contents are equal
204       */
205      public void testEquals() {
206          ConcurrentHashMap map1 = map5();
207          ConcurrentHashMap map2 = map5();
208          assertEquals(map1, map2);
209          assertEquals(map2, map1);
210 <        map1.clear();
210 >        map1.clear();
211          assertFalse(map1.equals(map2));
212          assertFalse(map2.equals(map1));
213      }
214  
215      /**
216 <     *  contains returns true for contained value
216 >     * hashCode() equals sum of each key.hashCode ^ value.hashCode
217 >     */
218 >    public void testHashCode() {
219 >        ConcurrentHashMap<Integer,String> map = map5();
220 >        int sum = 0;
221 >        for (Map.Entry<Integer,String> e : map.entrySet())
222 >            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
223 >        assertEquals(sum, map.hashCode());
224 >    }
225 >
226 >    /**
227 >     * contains returns true for contained value
228       */
229      public void testContains() {
230          ConcurrentHashMap map = map5();
231 <        assertTrue(map.contains("A"));
231 >        assertTrue(map.contains("A"));
232          assertFalse(map.contains("Z"));
233      }
234 <    
234 >
235      /**
236 <     *  containsKey returns true for contained key
236 >     * containsKey returns true for contained key
237       */
238      public void testContainsKey() {
239          ConcurrentHashMap map = map5();
240 <        assertTrue(map.containsKey(one));
240 >        assertTrue(map.containsKey(one));
241          assertFalse(map.containsKey(zero));
242      }
243  
244      /**
245 <     *  containsValue returns true for held values
245 >     * containsValue returns true for held values
246       */
247      public void testContainsValue() {
248          ConcurrentHashMap map = map5();
249 <        assertTrue(map.contains("A"));
250 <        assertFalse(map.contains("Z"));
249 >        assertTrue(map.containsValue("A"));
250 >        assertFalse(map.containsValue("Z"));
251      }
252  
253      /**
254 <     *   enumeration returns an enumeration containing the correct
255 <     *   elements
254 >     * enumeration returns an enumeration containing the correct
255 >     * elements
256       */
257      public void testEnumeration() {
258          ConcurrentHashMap map = map5();
259 <        Enumeration e = map.elements();
260 <        int count = 0;
261 <        while(e.hasMoreElements()){
262 <            count++;
263 <            e.nextElement();
264 <        }
265 <        assertEquals(5, count);
101 <    }
102 <
103 <    /**
104 <     *   Clone creates an equal map
105 <     */
106 <    public void testClone() {
107 <        ConcurrentHashMap map = map5();
108 <        ConcurrentHashMap m2 = (ConcurrentHashMap)(map.clone());
109 <        assertEquals(map, m2);
259 >        Enumeration e = map.elements();
260 >        int count = 0;
261 >        while (e.hasMoreElements()) {
262 >            count++;
263 >            e.nextElement();
264 >        }
265 >        assertEquals(5, count);
266      }
267  
268      /**
269 <     *  get returns the correct element at the given key,
270 <     *  or null if not present
269 >     * get returns the correct element at the given key,
270 >     * or null if not present
271       */
272      public void testGet() {
273          ConcurrentHashMap map = map5();
274 <        assertEquals("A", (String)map.get(one));
274 >        assertEquals("A", (String)map.get(one));
275          ConcurrentHashMap empty = new ConcurrentHashMap();
276          assertNull(map.get("anything"));
277      }
278  
279      /**
280 <     *  isEmpty is true of empty map and false for non-empty
280 >     * isEmpty is true of empty map and false for non-empty
281       */
282      public void testIsEmpty() {
283          ConcurrentHashMap empty = new ConcurrentHashMap();
284          ConcurrentHashMap map = map5();
285 <        assertTrue(empty.isEmpty());
285 >        assertTrue(empty.isEmpty());
286          assertFalse(map.isEmpty());
287      }
288  
289      /**
290 <     *   keys returns an enumeration containing all the keys from the map
290 >     * keys returns an enumeration containing all the keys from the map
291       */
292      public void testKeys() {
293          ConcurrentHashMap map = map5();
294 <        Enumeration e = map.keys();
295 <        int count = 0;
296 <        while(e.hasMoreElements()){
297 <            count++;
298 <            e.nextElement();
299 <        }
300 <        assertEquals(5, count);
294 >        Enumeration e = map.keys();
295 >        int count = 0;
296 >        while (e.hasMoreElements()) {
297 >            count++;
298 >            e.nextElement();
299 >        }
300 >        assertEquals(5, count);
301      }
302  
303      /**
304 <     *   keySet returns a Set containing all the keys
304 >     * keySet returns a Set containing all the keys
305       */
306      public void testKeySet() {
307          ConcurrentHashMap map = map5();
308 <        Set s = map.keySet();
309 <        assertEquals(5, s.size());
310 <        assertTrue(s.contains(one));
311 <        assertTrue(s.contains(two));
312 <        assertTrue(s.contains(three));
313 <        assertTrue(s.contains(four));
314 <        assertTrue(s.contains(five));
308 >        Set s = map.keySet();
309 >        assertEquals(5, s.size());
310 >        assertTrue(s.contains(one));
311 >        assertTrue(s.contains(two));
312 >        assertTrue(s.contains(three));
313 >        assertTrue(s.contains(four));
314 >        assertTrue(s.contains(five));
315 >    }
316 >
317 >    /**
318 >     * keySet.toArray returns contains all keys
319 >     */
320 >    public void testKeySetToArray() {
321 >        ConcurrentHashMap map = map5();
322 >        Set s = map.keySet();
323 >        Object[] ar = s.toArray();
324 >        assertTrue(s.containsAll(Arrays.asList(ar)));
325 >        assertEquals(5, ar.length);
326 >        ar[0] = m10;
327 >        assertFalse(s.containsAll(Arrays.asList(ar)));
328 >    }
329 >
330 >    /**
331 >     * Values.toArray contains all values
332 >     */
333 >    public void testValuesToArray() {
334 >        ConcurrentHashMap map = map5();
335 >        Collection v = map.values();
336 >        Object[] ar = v.toArray();
337 >        ArrayList s = new ArrayList(Arrays.asList(ar));
338 >        assertEquals(5, ar.length);
339 >        assertTrue(s.contains("A"));
340 >        assertTrue(s.contains("B"));
341 >        assertTrue(s.contains("C"));
342 >        assertTrue(s.contains("D"));
343 >        assertTrue(s.contains("E"));
344 >    }
345 >
346 >    /**
347 >     * entrySet.toArray contains all entries
348 >     */
349 >    public void testEntrySetToArray() {
350 >        ConcurrentHashMap map = map5();
351 >        Set s = map.entrySet();
352 >        Object[] ar = s.toArray();
353 >        assertEquals(5, ar.length);
354 >        for (int i = 0; i < 5; ++i) {
355 >            assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
356 >            assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
357 >        }
358      }
359  
360      /**
# Line 163 | Line 362 | public class ConcurrentHashMapTest exten
362       */
363      public void testValues() {
364          ConcurrentHashMap map = map5();
365 <        Collection s = map.values();
366 <        assertEquals(5, s.size());
367 <        assertTrue(s.contains("A"));
368 <        assertTrue(s.contains("B"));
369 <        assertTrue(s.contains("C"));
370 <        assertTrue(s.contains("D"));
371 <        assertTrue(s.contains("E"));
365 >        Collection s = map.values();
366 >        assertEquals(5, s.size());
367 >        assertTrue(s.contains("A"));
368 >        assertTrue(s.contains("B"));
369 >        assertTrue(s.contains("C"));
370 >        assertTrue(s.contains("D"));
371 >        assertTrue(s.contains("E"));
372      }
373  
374      /**
# Line 177 | Line 376 | public class ConcurrentHashMapTest exten
376       */
377      public void testEntrySet() {
378          ConcurrentHashMap map = map5();
379 <        Set s = map.entrySet();
380 <        assertEquals(5, s.size());
379 >        Set s = map.entrySet();
380 >        assertEquals(5, s.size());
381          Iterator it = s.iterator();
382          while (it.hasNext()) {
383              Map.Entry e = (Map.Entry) it.next();
384 <            assertTrue(
384 >            assertTrue(
385                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
386                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
387                         (e.getKey().equals(three) && e.getValue().equals("C")) ||
# Line 192 | Line 391 | public class ConcurrentHashMapTest exten
391      }
392  
393      /**
394 <     *   putAll  adds all key-value pairs from the given map
394 >     * putAll adds all key-value pairs from the given map
395       */
396      public void testPutAll() {
397          ConcurrentHashMap empty = new ConcurrentHashMap();
398          ConcurrentHashMap map = map5();
399 <        empty.putAll(map);
400 <        assertEquals(5, empty.size());
401 <        assertTrue(empty.containsKey(one));
402 <        assertTrue(empty.containsKey(two));
403 <        assertTrue(empty.containsKey(three));
404 <        assertTrue(empty.containsKey(four));
405 <        assertTrue(empty.containsKey(five));
399 >        empty.putAll(map);
400 >        assertEquals(5, empty.size());
401 >        assertTrue(empty.containsKey(one));
402 >        assertTrue(empty.containsKey(two));
403 >        assertTrue(empty.containsKey(three));
404 >        assertTrue(empty.containsKey(four));
405 >        assertTrue(empty.containsKey(five));
406      }
407  
408      /**
409 <     *   putIfAbsent works when the given key is not present
409 >     * putIfAbsent works when the given key is not present
410       */
411      public void testPutIfAbsent() {
412          ConcurrentHashMap map = map5();
413 <        map.putIfAbsent(six, "Z");
413 >        map.putIfAbsent(six, "Z");
414          assertTrue(map.containsKey(six));
415      }
416  
417      /**
418 <     *   putIfAbsent does not add the pair if the key is already present
418 >     * putIfAbsent does not add the pair if the key is already present
419       */
420      public void testPutIfAbsent2() {
421          ConcurrentHashMap map = map5();
# Line 224 | Line 423 | public class ConcurrentHashMapTest exten
423      }
424  
425      /**
426 <     *   replace fails when the given key is not present
426 >     * replace fails when the given key is not present
427       */
428      public void testReplace() {
429          ConcurrentHashMap map = map5();
430 <        assertNull(map.replace(six, "Z"));
430 >        assertNull(map.replace(six, "Z"));
431          assertFalse(map.containsKey(six));
432      }
433  
434      /**
435 <     *   replace succeeds if the key is already present
435 >     * replace succeeds if the key is already present
436       */
437      public void testReplace2() {
438          ConcurrentHashMap map = map5();
# Line 241 | Line 440 | public class ConcurrentHashMapTest exten
440          assertEquals("Z", map.get(one));
441      }
442  
244
443      /**
444       * replace value fails when the given key not mapped to expected value
445       */
446      public void testReplaceValue() {
447          ConcurrentHashMap map = map5();
448          assertEquals("A", map.get(one));
449 <        assertFalse(map.replace(one, "Z", "Z"));
449 >        assertFalse(map.replace(one, "Z", "Z"));
450          assertEquals("A", map.get(one));
451      }
452  
# Line 258 | Line 456 | public class ConcurrentHashMapTest exten
456      public void testReplaceValue2() {
457          ConcurrentHashMap map = map5();
458          assertEquals("A", map.get(one));
459 <        assertTrue(map.replace(one, "A", "Z"));
459 >        assertTrue(map.replace(one, "A", "Z"));
460          assertEquals("Z", map.get(one));
461      }
462  
265
463      /**
464 <     *   remove removes the correct key-value pair from the map
464 >     * remove removes the correct key-value pair from the map
465       */
466      public void testRemove() {
467          ConcurrentHashMap map = map5();
468 <        map.remove(five);
469 <        assertEquals(4, map.size());
470 <        assertFalse(map.containsKey(five));
468 >        map.remove(five);
469 >        assertEquals(4, map.size());
470 >        assertFalse(map.containsKey(five));
471      }
472  
473      /**
# Line 278 | Line 475 | public class ConcurrentHashMapTest exten
475       */
476      public void testRemove2() {
477          ConcurrentHashMap map = map5();
478 <        map.remove(five, "E");
479 <        assertEquals(4, map.size());
480 <        assertFalse(map.containsKey(five));
481 <        map.remove(four, "A");
482 <        assertEquals(4, map.size());
483 <        assertTrue(map.containsKey(four));
287 <
478 >        map.remove(five, "E");
479 >        assertEquals(4, map.size());
480 >        assertFalse(map.containsKey(five));
481 >        map.remove(four, "A");
482 >        assertEquals(4, map.size());
483 >        assertTrue(map.containsKey(four));
484      }
485  
486      /**
487 <     *   size returns the correct values
487 >     * size returns the correct values
488       */
489      public void testSize() {
490          ConcurrentHashMap map = map5();
491          ConcurrentHashMap empty = new ConcurrentHashMap();
492 <        assertEquals(0, empty.size());
493 <        assertEquals(5, map.size());
492 >        assertEquals(0, empty.size());
493 >        assertEquals(5, map.size());
494      }
495  
496      /**
# Line 304 | Line 500 | public class ConcurrentHashMapTest exten
500          ConcurrentHashMap map = map5();
501          String s = map.toString();
502          for (int i = 1; i <= 5; ++i) {
503 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
503 >            assertTrue(s.contains(String.valueOf(i)));
504          }
505 <    }        
505 >    }
506  
507      // Exception tests
508 <    
508 >
509      /**
510 <     * Cannot create with negative capacity
510 >     * Cannot create with only negative capacity
511       */
512      public void testConstructor1() {
513          try {
514 <            new ConcurrentHashMap(-1,0,1);
514 >            new ConcurrentHashMap(-1);
515              shouldThrow();
516 <        } catch(IllegalArgumentException e){}
516 >        } catch (IllegalArgumentException success) {}
517      }
518  
519      /**
520 <     * Cannot create with negative concurrency level
521 <     */
520 >     * Constructor (initialCapacity, loadFactor) throws
521 >     * IllegalArgumentException if either argument is negative
522 >      */
523      public void testConstructor2() {
524          try {
525 <            new ConcurrentHashMap(1,0,-1);
525 >            new ConcurrentHashMap(-1, .75f);
526              shouldThrow();
527 <        } catch(IllegalArgumentException e){}
527 >        } catch (IllegalArgumentException e) {}
528 >
529 >        try {
530 >            new ConcurrentHashMap(16, -1);
531 >            shouldThrow();
532 >        } catch (IllegalArgumentException e) {}
533      }
534  
535      /**
536 <     * Cannot create with only negative capacity
536 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
537 >     * throws IllegalArgumentException if any argument is negative
538       */
539      public void testConstructor3() {
540          try {
541 <            new ConcurrentHashMap(-1);
541 >            new ConcurrentHashMap(-1, .75f, 1);
542 >            shouldThrow();
543 >        } catch (IllegalArgumentException e) {}
544 >
545 >        try {
546 >            new ConcurrentHashMap(16, -1, 1);
547 >            shouldThrow();
548 >        } catch (IllegalArgumentException e) {}
549 >
550 >        try {
551 >            new ConcurrentHashMap(16, .75f, -1);
552              shouldThrow();
553 <        } catch(IllegalArgumentException e){}
553 >        } catch (IllegalArgumentException e) {}
554      }
555  
556      /**
557 +     * ConcurrentHashMap(map) throws NullPointerException if the given
558 +     * map is null
559 +     */
560 +    public void testConstructor4() {
561 +        try {
562 +            new ConcurrentHashMap(null);
563 +            shouldThrow();
564 +        } catch (NullPointerException e) {}
565 +    }
566 +
567 +    /**
568 +     * ConcurrentHashMap(map) creates a new map with the same mappings
569 +     * as the given map
570 +     */
571 +    public void testConstructor5() {
572 +        ConcurrentHashMap map1 = map5();
573 +        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
574 +        assertTrue(map2.equals(map1));
575 +        map2.put(one, "F");
576 +        assertFalse(map2.equals(map1));
577 +    }
578 +
579 +
580 +    /**
581       * get(null) throws NPE
582       */
583      public void testGet_NullPointerException() {
# Line 348 | Line 585 | public class ConcurrentHashMapTest exten
585              ConcurrentHashMap c = new ConcurrentHashMap(5);
586              c.get(null);
587              shouldThrow();
588 <        } catch(NullPointerException e){}
588 >        } catch (NullPointerException success) {}
589      }
590  
591      /**
# Line 359 | Line 596 | public class ConcurrentHashMapTest exten
596              ConcurrentHashMap c = new ConcurrentHashMap(5);
597              c.containsKey(null);
598              shouldThrow();
599 <        } catch(NullPointerException e){}
599 >        } catch (NullPointerException success) {}
600      }
601  
602      /**
# Line 370 | Line 607 | public class ConcurrentHashMapTest exten
607              ConcurrentHashMap c = new ConcurrentHashMap(5);
608              c.containsValue(null);
609              shouldThrow();
610 <        } catch(NullPointerException e){}
610 >        } catch (NullPointerException success) {}
611      }
612  
613      /**
# Line 381 | Line 618 | public class ConcurrentHashMapTest exten
618              ConcurrentHashMap c = new ConcurrentHashMap(5);
619              c.contains(null);
620              shouldThrow();
621 <        } catch(NullPointerException e){}
621 >        } catch (NullPointerException success) {}
622      }
623  
624      /**
# Line 392 | Line 629 | public class ConcurrentHashMapTest exten
629              ConcurrentHashMap c = new ConcurrentHashMap(5);
630              c.put(null, "whatever");
631              shouldThrow();
632 <        } catch(NullPointerException e){}
632 >        } catch (NullPointerException success) {}
633      }
634  
635      /**
# Line 403 | Line 640 | public class ConcurrentHashMapTest exten
640              ConcurrentHashMap c = new ConcurrentHashMap(5);
641              c.put("whatever", null);
642              shouldThrow();
643 <        } catch(NullPointerException e){}
643 >        } catch (NullPointerException success) {}
644      }
645  
646      /**
# Line 414 | Line 651 | public class ConcurrentHashMapTest exten
651              ConcurrentHashMap c = new ConcurrentHashMap(5);
652              c.putIfAbsent(null, "whatever");
653              shouldThrow();
654 <        } catch(NullPointerException e){}
654 >        } catch (NullPointerException success) {}
655      }
656  
657      /**
# Line 425 | Line 662 | public class ConcurrentHashMapTest exten
662              ConcurrentHashMap c = new ConcurrentHashMap(5);
663              c.replace(null, "whatever");
664              shouldThrow();
665 <        } catch(NullPointerException e){}
665 >        } catch (NullPointerException success) {}
666      }
667  
668      /**
# Line 436 | Line 673 | public class ConcurrentHashMapTest exten
673              ConcurrentHashMap c = new ConcurrentHashMap(5);
674              c.replace(null, one, "whatever");
675              shouldThrow();
676 <        } catch(NullPointerException e){}
676 >        } catch (NullPointerException success) {}
677      }
678  
679      /**
# Line 447 | Line 684 | public class ConcurrentHashMapTest exten
684              ConcurrentHashMap c = new ConcurrentHashMap(5);
685              c.putIfAbsent("whatever", null);
686              shouldThrow();
687 <        } catch(NullPointerException e){}
687 >        } catch (NullPointerException success) {}
688      }
689  
453
690      /**
691       * replace(x, null) throws NPE
692       */
# Line 459 | Line 695 | public class ConcurrentHashMapTest exten
695              ConcurrentHashMap c = new ConcurrentHashMap(5);
696              c.replace("whatever", null);
697              shouldThrow();
698 <        } catch(NullPointerException e){}
698 >        } catch (NullPointerException success) {}
699      }
700  
701      /**
# Line 470 | Line 706 | public class ConcurrentHashMapTest exten
706              ConcurrentHashMap c = new ConcurrentHashMap(5);
707              c.replace("whatever", null, "A");
708              shouldThrow();
709 <        } catch(NullPointerException e){}
709 >        } catch (NullPointerException success) {}
710      }
711  
712      /**
# Line 481 | Line 717 | public class ConcurrentHashMapTest exten
717              ConcurrentHashMap c = new ConcurrentHashMap(5);
718              c.replace("whatever", one, null);
719              shouldThrow();
720 <        } catch(NullPointerException e){}
720 >        } catch (NullPointerException success) {}
721      }
722  
487
723      /**
724       * remove(null) throws NPE
725       */
# Line 494 | Line 729 | public class ConcurrentHashMapTest exten
729              c.put("sadsdf", "asdads");
730              c.remove(null);
731              shouldThrow();
732 <        } catch(NullPointerException e){}
732 >        } catch (NullPointerException success) {}
733      }
734  
735      /**
# Line 506 | Line 741 | public class ConcurrentHashMapTest exten
741              c.put("sadsdf", "asdads");
742              c.remove(null, "whatever");
743              shouldThrow();
744 <        } catch(NullPointerException e){}
744 >        } catch (NullPointerException success) {}
745      }
746  
747      /**
748 <     * A deserialized map equals original
748 >     * remove(x, null) returns false
749       */
750 <    public void testSerialization() {
751 <        ConcurrentHashMap q = map5();
752 <
753 <        try {
519 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
520 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
521 <            out.writeObject(q);
522 <            out.close();
523 <
524 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
525 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
526 <            ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
527 <            assertEquals(q.size(), r.size());
528 <            assertTrue(q.equals(r));
529 <            assertTrue(r.equals(q));
530 <        } catch(Exception e){
531 <            e.printStackTrace();
532 <            unexpectedException();
533 <        }
750 >    public void testRemove3() {
751 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
752 >        c.put("sadsdf", "asdads");
753 >        assertFalse(c.remove("sadsdf", null));
754      }
755  
756 +    /**
757 +     * A deserialized map equals original
758 +     */
759 +    public void testSerialization() throws Exception {
760 +        Map x = map5();
761 +        Map y = serialClone(x);
762 +
763 +        assertNotSame(x, y);
764 +        assertEquals(x.size(), y.size());
765 +        assertEquals(x, y);
766 +        assertEquals(y, x);
767 +    }
768  
769      /**
770       * SetValue of an EntrySet entry sets value in the map.
771       */
772      public void testSetValueWriteThrough() {
773 <        // Adapted from a bug report by Eric Zoerner
773 >        // Adapted from a bug report by Eric Zoerner
774          ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
775          assertTrue(map.isEmpty());
776          for (int i = 0; i < 20; i++)
777              map.put(new Integer(i), new Integer(i));
778          assertFalse(map.isEmpty());
779          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
780 <        
781 <        // assert that entry1 is not 16
782 <        assertTrue("entry is 16, test not valid",
783 <                   !entry1.getKey().equals(new Integer(16)));
784 <        
785 <        // remove 16 (a different key) from map
786 <        // which just happens to cause entry1 to be cloned in map
787 <        map.remove(new Integer(16));
788 <        entry1.setValue("XYZ");
557 <        assertTrue(map.containsValue("XYZ")); // fails
780 >        // Unless it happens to be first (in which case remainder of
781 >        // test is skipped), remove a possibly-colliding key from map
782 >        // which, under some implementations, may cause entry1 to be
783 >        // cloned in map
784 >        if (!entry1.getKey().equals(new Integer(16))) {
785 >            map.remove(new Integer(16));
786 >            entry1.setValue("XYZ");
787 >            assertTrue(map.containsValue("XYZ")); // fails if write-through broken
788 >        }
789      }
790 <    
790 >
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines