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.6 by dl, Fri Nov 28 12:38:08 2003 UTC vs.
Revision 1.41 by jsr166, Thu Jan 15 18:34:19 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines