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.19 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.55 by jsr166, Fri Aug 4 03:30:21 2017 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
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 > 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 >        main(suite(), args);
26      }
27      public static Test suite() {
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<Integer, String> map = new ConcurrentHashMap<>(5);
36          assertTrue(map.isEmpty());
37          map.put(one, "A");
38          map.put(two, "B");
# Line 36 | Line 44 | public class ConcurrentHashMapTest exten
44          return map;
45      }
46  
47 +    /** Re-implement Integer.compare for old java versions */
48 +    static int compare(int x, int y) {
49 +        return (x < y) ? -1 : (x > y) ? 1 : 0;
50 +    }
51 +
52 +    // classes for testing Comparable fallbacks
53 +    static class BI implements Comparable<BI> {
54 +        private final int value;
55 +        BI(int value) { this.value = value; }
56 +        public int compareTo(BI other) {
57 +            return compare(value, other.value);
58 +        }
59 +        public boolean equals(Object x) {
60 +            return (x instanceof BI) && ((BI)x).value == value;
61 +        }
62 +        public int hashCode() { return 42; }
63 +    }
64 +    static class CI extends BI { CI(int value) { super(value); } }
65 +    static class DI extends BI { DI(int value) { super(value); } }
66 +
67 +    static class BS implements Comparable<BS> {
68 +        private final String value;
69 +        BS(String value) { this.value = value; }
70 +        public int compareTo(BS other) {
71 +            return value.compareTo(other.value);
72 +        }
73 +        public boolean equals(Object x) {
74 +            return (x instanceof BS) && value.equals(((BS)x).value);
75 +        }
76 +        public int hashCode() { return 42; }
77 +    }
78 +
79 +    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
80 +        implements Comparable<LexicographicList<E>> {
81 +        LexicographicList(Collection<E> c) { super(c); }
82 +        LexicographicList(E e) { super(Collections.singleton(e)); }
83 +        public int compareTo(LexicographicList<E> other) {
84 +            int common = Math.min(size(), other.size());
85 +            int r = 0;
86 +            for (int i = 0; i < common; i++) {
87 +                if ((r = get(i).compareTo(other.get(i))) != 0)
88 +                    break;
89 +            }
90 +            if (r == 0)
91 +                r = compare(size(), other.size());
92 +            return r;
93 +        }
94 +        private static final long serialVersionUID = 0;
95 +    }
96 +
97 +    static class CollidingObject {
98 +        final String value;
99 +        CollidingObject(final String value) { this.value = value; }
100 +        public int hashCode() { return this.value.hashCode() & 1; }
101 +        public boolean equals(final Object obj) {
102 +            return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
103 +        }
104 +    }
105 +
106 +    static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
107 +        ComparableCollidingObject(final String value) { super(value); }
108 +        public int compareTo(final ComparableCollidingObject o) {
109 +            return value.compareTo(o.value);
110 +        }
111 +    }
112 +
113 +    /**
114 +     * Inserted elements that are subclasses of the same Comparable
115 +     * class are found.
116 +     */
117 +    public void testComparableFamily() {
118 +        int size = 500;         // makes measured test run time -> 60ms
119 +        ConcurrentHashMap<BI, Boolean> m =
120 +            new ConcurrentHashMap<BI, Boolean>();
121 +        for (int i = 0; i < size; i++) {
122 +            assertNull(m.put(new CI(i), true));
123 +        }
124 +        for (int i = 0; i < size; i++) {
125 +            assertTrue(m.containsKey(new CI(i)));
126 +            assertTrue(m.containsKey(new DI(i)));
127 +        }
128 +    }
129 +
130 +    /**
131 +     * Elements of classes with erased generic type parameters based
132 +     * on Comparable can be inserted and found.
133 +     */
134 +    public void testGenericComparable() {
135 +        int size = 120;         // makes measured test run time -> 60ms
136 +        ConcurrentHashMap<Object, Boolean> m =
137 +            new ConcurrentHashMap<Object, Boolean>();
138 +        for (int i = 0; i < size; i++) {
139 +            BI bi = new BI(i);
140 +            BS bs = new BS(String.valueOf(i));
141 +            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
142 +            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
143 +            assertNull(m.putIfAbsent(bis, true));
144 +            assertTrue(m.containsKey(bis));
145 +            if (m.putIfAbsent(bss, true) == null)
146 +                assertTrue(m.containsKey(bss));
147 +            assertTrue(m.containsKey(bis));
148 +        }
149 +        for (int i = 0; i < size; i++) {
150 +            assertTrue(m.containsKey(Collections.singletonList(new BI(i))));
151 +        }
152 +    }
153 +
154 +    /**
155 +     * Elements of non-comparable classes equal to those of classes
156 +     * with erased generic type parameters based on Comparable can be
157 +     * inserted and found.
158 +     */
159 +    public void testGenericComparable2() {
160 +        int size = 500;         // makes measured test run time -> 60ms
161 +        ConcurrentHashMap<Object, Boolean> m =
162 +            new ConcurrentHashMap<Object, Boolean>();
163 +        for (int i = 0; i < size; i++) {
164 +            m.put(Collections.singletonList(new BI(i)), true);
165 +        }
166 +
167 +        for (int i = 0; i < size; i++) {
168 +            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
169 +            assertTrue(m.containsKey(bis));
170 +        }
171 +    }
172 +
173 +    /**
174 +     * Mixtures of instances of comparable and non-comparable classes
175 +     * can be inserted and found.
176 +     */
177 +    public void testMixedComparable() {
178 +        int size = 1200;        // makes measured test run time -> 35ms
179 +        ConcurrentHashMap<Object, Object> map =
180 +            new ConcurrentHashMap<Object, Object>();
181 +        Random rng = new Random();
182 +        for (int i = 0; i < size; i++) {
183 +            Object x;
184 +            switch (rng.nextInt(4)) {
185 +            case 0:
186 +                x = new Object();
187 +                break;
188 +            case 1:
189 +                x = new CollidingObject(Integer.toString(i));
190 +                break;
191 +            default:
192 +                x = new ComparableCollidingObject(Integer.toString(i));
193 +            }
194 +            assertNull(map.put(x, x));
195 +        }
196 +        int count = 0;
197 +        for (Object k : map.keySet()) {
198 +            assertEquals(map.get(k), k);
199 +            ++count;
200 +        }
201 +        assertEquals(count, size);
202 +        assertEquals(map.size(), size);
203 +        for (Object k : map.keySet()) {
204 +            assertEquals(map.put(k, k), k);
205 +        }
206 +    }
207 +
208      /**
209 <     *  clear removes all pairs
209 >     * clear removes all pairs
210       */
211      public void testClear() {
212          ConcurrentHashMap map = map5();
213          map.clear();
214 <        assertEquals(map.size(), 0);
214 >        assertEquals(0, map.size());
215      }
216  
217      /**
218 <     *  Maps with same contents are equal
218 >     * Maps with same contents are equal
219       */
220      public void testEquals() {
221          ConcurrentHashMap map1 = map5();
# Line 59 | Line 228 | public class ConcurrentHashMapTest exten
228      }
229  
230      /**
231 <     *  contains returns true for contained value
231 >     * hashCode() equals sum of each key.hashCode ^ value.hashCode
232 >     */
233 >    public void testHashCode() {
234 >        ConcurrentHashMap<Integer,String> map = map5();
235 >        int sum = 0;
236 >        for (Map.Entry<Integer,String> e : map.entrySet())
237 >            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
238 >        assertEquals(sum, map.hashCode());
239 >    }
240 >
241 >    /**
242 >     * contains returns true for contained value
243       */
244      public void testContains() {
245          ConcurrentHashMap map = map5();
# Line 68 | Line 248 | public class ConcurrentHashMapTest exten
248      }
249  
250      /**
251 <     *  containsKey returns true for contained key
251 >     * containsKey returns true for contained key
252       */
253      public void testContainsKey() {
254          ConcurrentHashMap map = map5();
# Line 77 | Line 257 | public class ConcurrentHashMapTest exten
257      }
258  
259      /**
260 <     *  containsValue returns true for held values
260 >     * containsValue returns true for held values
261       */
262      public void testContainsValue() {
263          ConcurrentHashMap map = map5();
# Line 86 | Line 266 | public class ConcurrentHashMapTest exten
266      }
267  
268      /**
269 <     *   enumeration returns an enumeration containing the correct
270 <     *   elements
269 >     * enumeration returns an enumeration containing the correct
270 >     * elements
271       */
272      public void testEnumeration() {
273          ConcurrentHashMap map = map5();
# Line 101 | Line 281 | public class ConcurrentHashMapTest exten
281      }
282  
283      /**
284 <     *  get returns the correct element at the given key,
285 <     *  or null if not present
284 >     * get returns the correct element at the given key,
285 >     * or null if not present
286       */
287      public void testGet() {
288          ConcurrentHashMap map = map5();
289          assertEquals("A", (String)map.get(one));
290          ConcurrentHashMap empty = new ConcurrentHashMap();
291          assertNull(map.get("anything"));
292 +        assertNull(empty.get("anything"));
293      }
294  
295      /**
296 <     *  isEmpty is true of empty map and false for non-empty
296 >     * isEmpty is true of empty map and false for non-empty
297       */
298      public void testIsEmpty() {
299          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 122 | Line 303 | public class ConcurrentHashMapTest exten
303      }
304  
305      /**
306 <     *   keys returns an enumeration containing all the keys from the map
306 >     * keys returns an enumeration containing all the keys from the map
307       */
308      public void testKeys() {
309          ConcurrentHashMap map = map5();
# Line 136 | Line 317 | public class ConcurrentHashMapTest exten
317      }
318  
319      /**
320 <     *   keySet returns a Set containing all the keys
320 >     * keySet returns a Set containing all the keys
321       */
322      public void testKeySet() {
323          ConcurrentHashMap map = map5();
# Line 150 | Line 331 | public class ConcurrentHashMapTest exten
331      }
332  
333      /**
334 <     *  keySet.toArray returns contains all keys
334 >     * Test keySet().removeAll on empty map
335 >     */
336 >    public void testKeySet_empty_removeAll() {
337 >        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
338 >        Set<Integer> set = map.keySet();
339 >        set.removeAll(Collections.emptyList());
340 >        assertTrue(map.isEmpty());
341 >        assertTrue(set.isEmpty());
342 >        // following is test for JDK-8163353
343 >        set.removeAll(Collections.emptySet());
344 >        assertTrue(map.isEmpty());
345 >        assertTrue(set.isEmpty());
346 >    }
347 >
348 >    /**
349 >     * keySet.toArray returns contains all keys
350       */
351      public void testKeySetToArray() {
352          ConcurrentHashMap map = map5();
# Line 163 | Line 359 | public class ConcurrentHashMapTest exten
359      }
360  
361      /**
362 <     *  Values.toArray contains all values
362 >     * Values.toArray contains all values
363       */
364      public void testValuesToArray() {
365          ConcurrentHashMap map = map5();
# Line 179 | Line 375 | public class ConcurrentHashMapTest exten
375      }
376  
377      /**
378 <     *  entrySet.toArray contains all entries
378 >     * entrySet.toArray contains all entries
379       */
380      public void testEntrySetToArray() {
381          ConcurrentHashMap map = map5();
# Line 226 | Line 422 | public class ConcurrentHashMapTest exten
422      }
423  
424      /**
425 <     *   putAll  adds all key-value pairs from the given map
425 >     * putAll adds all key-value pairs from the given map
426       */
427      public void testPutAll() {
428          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 241 | Line 437 | public class ConcurrentHashMapTest exten
437      }
438  
439      /**
440 <     *   putIfAbsent works when the given key is not present
440 >     * putIfAbsent works when the given key is not present
441       */
442      public void testPutIfAbsent() {
443          ConcurrentHashMap map = map5();
# Line 250 | Line 446 | public class ConcurrentHashMapTest exten
446      }
447  
448      /**
449 <     *   putIfAbsent does not add the pair if the key is already present
449 >     * putIfAbsent does not add the pair if the key is already present
450       */
451      public void testPutIfAbsent2() {
452          ConcurrentHashMap map = map5();
# Line 258 | Line 454 | public class ConcurrentHashMapTest exten
454      }
455  
456      /**
457 <     *   replace fails when the given key is not present
457 >     * replace fails when the given key is not present
458       */
459      public void testReplace() {
460          ConcurrentHashMap map = map5();
# Line 267 | Line 463 | public class ConcurrentHashMapTest exten
463      }
464  
465      /**
466 <     *   replace succeeds if the key is already present
466 >     * replace succeeds if the key is already present
467       */
468      public void testReplace2() {
469          ConcurrentHashMap map = map5();
# Line 275 | Line 471 | public class ConcurrentHashMapTest exten
471          assertEquals("Z", map.get(one));
472      }
473  
278
474      /**
475       * replace value fails when the given key not mapped to expected value
476       */
# Line 296 | Line 491 | public class ConcurrentHashMapTest exten
491          assertEquals("Z", map.get(one));
492      }
493  
299
494      /**
495 <     *   remove removes the correct key-value pair from the map
495 >     * remove removes the correct key-value pair from the map
496       */
497      public void testRemove() {
498          ConcurrentHashMap map = map5();
# Line 318 | Line 512 | public class ConcurrentHashMapTest exten
512          map.remove(four, "A");
513          assertEquals(4, map.size());
514          assertTrue(map.containsKey(four));
321
515      }
516  
517      /**
518 <     *   size returns the correct values
518 >     * size returns the correct values
519       */
520      public void testSize() {
521          ConcurrentHashMap map = map5();
# Line 338 | Line 531 | public class ConcurrentHashMapTest exten
531          ConcurrentHashMap map = map5();
532          String s = map.toString();
533          for (int i = 1; i <= 5; ++i) {
534 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
534 >            assertTrue(s.contains(String.valueOf(i)));
535          }
536      }
537  
538      // Exception tests
539  
540      /**
541 <     * Cannot create with negative capacity
541 >     * Cannot create with only negative capacity
542       */
543      public void testConstructor1() {
544          try {
545 <            new ConcurrentHashMap(-1,0,1);
545 >            new ConcurrentHashMap(-1);
546              shouldThrow();
547          } catch (IllegalArgumentException success) {}
548      }
549  
550      /**
551 <     * Cannot create with negative concurrency level
551 >     * Constructor (initialCapacity, loadFactor) throws
552 >     * IllegalArgumentException if either argument is negative
553       */
554      public void testConstructor2() {
555          try {
556 <            new ConcurrentHashMap(1,0,-1);
556 >            new ConcurrentHashMap(-1, .75f);
557 >            shouldThrow();
558 >        } catch (IllegalArgumentException success) {}
559 >
560 >        try {
561 >            new ConcurrentHashMap(16, -1);
562              shouldThrow();
563          } catch (IllegalArgumentException success) {}
564      }
565  
566      /**
567 <     * Cannot create with only negative capacity
567 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
568 >     * throws IllegalArgumentException if any argument is negative
569       */
570      public void testConstructor3() {
571          try {
572 <            new ConcurrentHashMap(-1);
572 >            new ConcurrentHashMap(-1, .75f, 1);
573 >            shouldThrow();
574 >        } catch (IllegalArgumentException success) {}
575 >
576 >        try {
577 >            new ConcurrentHashMap(16, -1, 1);
578              shouldThrow();
579          } catch (IllegalArgumentException success) {}
580 +
581 +        try {
582 +            new ConcurrentHashMap(16, .75f, -1);
583 +            shouldThrow();
584 +        } catch (IllegalArgumentException success) {}
585 +    }
586 +
587 +    /**
588 +     * ConcurrentHashMap(map) throws NullPointerException if the given
589 +     * map is null
590 +     */
591 +    public void testConstructor4() {
592 +        try {
593 +            new ConcurrentHashMap(null);
594 +            shouldThrow();
595 +        } catch (NullPointerException success) {}
596 +    }
597 +
598 +    /**
599 +     * ConcurrentHashMap(map) creates a new map with the same mappings
600 +     * as the given map
601 +     */
602 +    public void testConstructor5() {
603 +        ConcurrentHashMap map1 = map5();
604 +        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
605 +        assertTrue(map2.equals(map1));
606 +        map2.put(one, "F");
607 +        assertFalse(map2.equals(map1));
608      }
609  
610      /**
611       * get(null) throws NPE
612       */
613      public void testGet_NullPointerException() {
614 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
615          try {
382            ConcurrentHashMap c = new ConcurrentHashMap(5);
616              c.get(null);
617              shouldThrow();
618          } catch (NullPointerException success) {}
# Line 389 | Line 622 | public class ConcurrentHashMapTest exten
622       * containsKey(null) throws NPE
623       */
624      public void testContainsKey_NullPointerException() {
625 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
626          try {
393            ConcurrentHashMap c = new ConcurrentHashMap(5);
627              c.containsKey(null);
628              shouldThrow();
629          } catch (NullPointerException success) {}
# Line 400 | Line 633 | public class ConcurrentHashMapTest exten
633       * containsValue(null) throws NPE
634       */
635      public void testContainsValue_NullPointerException() {
636 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
637          try {
404            ConcurrentHashMap c = new ConcurrentHashMap(5);
638              c.containsValue(null);
639              shouldThrow();
640          } catch (NullPointerException success) {}
# Line 411 | Line 644 | public class ConcurrentHashMapTest exten
644       * contains(null) throws NPE
645       */
646      public void testContains_NullPointerException() {
647 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
648          try {
415            ConcurrentHashMap c = new ConcurrentHashMap(5);
649              c.contains(null);
650              shouldThrow();
651          } catch (NullPointerException success) {}
# Line 422 | Line 655 | public class ConcurrentHashMapTest exten
655       * put(null,x) throws NPE
656       */
657      public void testPut1_NullPointerException() {
658 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
659          try {
426            ConcurrentHashMap c = new ConcurrentHashMap(5);
660              c.put(null, "whatever");
661              shouldThrow();
662          } catch (NullPointerException success) {}
# Line 433 | Line 666 | public class ConcurrentHashMapTest exten
666       * put(x, null) throws NPE
667       */
668      public void testPut2_NullPointerException() {
669 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
670          try {
437            ConcurrentHashMap c = new ConcurrentHashMap(5);
671              c.put("whatever", null);
672              shouldThrow();
673          } catch (NullPointerException success) {}
# Line 444 | Line 677 | public class ConcurrentHashMapTest exten
677       * putIfAbsent(null, x) throws NPE
678       */
679      public void testPutIfAbsent1_NullPointerException() {
680 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
681          try {
448            ConcurrentHashMap c = new ConcurrentHashMap(5);
682              c.putIfAbsent(null, "whatever");
683              shouldThrow();
684          } catch (NullPointerException success) {}
# Line 455 | Line 688 | public class ConcurrentHashMapTest exten
688       * replace(null, x) throws NPE
689       */
690      public void testReplace_NullPointerException() {
691 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
692          try {
459            ConcurrentHashMap c = new ConcurrentHashMap(5);
693              c.replace(null, "whatever");
694              shouldThrow();
695          } catch (NullPointerException success) {}
# Line 466 | Line 699 | public class ConcurrentHashMapTest exten
699       * replace(null, x, y) throws NPE
700       */
701      public void testReplaceValue_NullPointerException() {
702 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
703          try {
470            ConcurrentHashMap c = new ConcurrentHashMap(5);
704              c.replace(null, one, "whatever");
705              shouldThrow();
706          } catch (NullPointerException success) {}
# Line 477 | Line 710 | public class ConcurrentHashMapTest exten
710       * putIfAbsent(x, null) throws NPE
711       */
712      public void testPutIfAbsent2_NullPointerException() {
713 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
714          try {
481            ConcurrentHashMap c = new ConcurrentHashMap(5);
715              c.putIfAbsent("whatever", null);
716              shouldThrow();
717          } catch (NullPointerException success) {}
718      }
719  
487
720      /**
721       * replace(x, null) throws NPE
722       */
723      public void testReplace2_NullPointerException() {
724 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
725          try {
493            ConcurrentHashMap c = new ConcurrentHashMap(5);
726              c.replace("whatever", null);
727              shouldThrow();
728          } catch (NullPointerException success) {}
# Line 500 | Line 732 | public class ConcurrentHashMapTest exten
732       * replace(x, null, y) throws NPE
733       */
734      public void testReplaceValue2_NullPointerException() {
735 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
736          try {
504            ConcurrentHashMap c = new ConcurrentHashMap(5);
737              c.replace("whatever", null, "A");
738              shouldThrow();
739          } catch (NullPointerException success) {}
# Line 511 | Line 743 | public class ConcurrentHashMapTest exten
743       * replace(x, y, null) throws NPE
744       */
745      public void testReplaceValue3_NullPointerException() {
746 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
747          try {
515            ConcurrentHashMap c = new ConcurrentHashMap(5);
748              c.replace("whatever", one, null);
749              shouldThrow();
750          } catch (NullPointerException success) {}
751      }
752  
521
753      /**
754       * remove(null) throws NPE
755       */
756      public void testRemove1_NullPointerException() {
757 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
758 +        c.put("sadsdf", "asdads");
759          try {
527            ConcurrentHashMap c = new ConcurrentHashMap(5);
528            c.put("sadsdf", "asdads");
760              c.remove(null);
761              shouldThrow();
762          } catch (NullPointerException success) {}
# Line 535 | Line 766 | public class ConcurrentHashMapTest exten
766       * remove(null, x) throws NPE
767       */
768      public void testRemove2_NullPointerException() {
769 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
770 +        c.put("sadsdf", "asdads");
771          try {
539            ConcurrentHashMap c = new ConcurrentHashMap(5);
540            c.put("sadsdf", "asdads");
772              c.remove(null, "whatever");
773              shouldThrow();
774          } catch (NullPointerException success) {}
# Line 553 | Line 784 | public class ConcurrentHashMapTest exten
784      }
785  
786      /**
787 <     * A deserialized map equals original
787 >     * A deserialized/reserialized map equals original
788       */
789      public void testSerialization() throws Exception {
790 <        ConcurrentHashMap q = map5();
790 >        Map x = map5();
791 >        Map y = serialClone(x);
792  
793 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
794 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
795 <        out.writeObject(q);
796 <        out.close();
565 <
566 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
567 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
568 <        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
569 <        assertEquals(q.size(), r.size());
570 <        assertTrue(q.equals(r));
571 <        assertTrue(r.equals(q));
793 >        assertNotSame(x, y);
794 >        assertEquals(x.size(), y.size());
795 >        assertEquals(x, y);
796 >        assertEquals(y, x);
797      }
798  
574
799      /**
800       * SetValue of an EntrySet entry sets value in the map.
801       */
# Line 583 | Line 807 | public class ConcurrentHashMapTest exten
807              map.put(new Integer(i), new Integer(i));
808          assertFalse(map.isEmpty());
809          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
810 +        // Unless it happens to be first (in which case remainder of
811 +        // test is skipped), remove a possibly-colliding key from map
812 +        // which, under some implementations, may cause entry1 to be
813 +        // cloned in map
814 +        if (!entry1.getKey().equals(new Integer(16))) {
815 +            map.remove(new Integer(16));
816 +            entry1.setValue("XYZ");
817 +            assertTrue(map.containsValue("XYZ")); // fails if write-through broken
818 +        }
819 +    }
820  
821 <        // assert that entry1 is not 16
822 <        assertTrue("entry is 16, test not valid",
823 <                   !entry1.getKey().equals(new Integer(16)));
824 <
825 <        // remove 16 (a different key) from map
826 <        // which just happens to cause entry1 to be cloned in map
827 <        map.remove(new Integer(16));
828 <        entry1.setValue("XYZ");
829 <        assertTrue(map.containsValue("XYZ")); // fails
821 >    /**
822 >     * Tests performance of removeAll when the other collection is much smaller.
823 >     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
824 >     */
825 >    public void testRemoveAll_performance() {
826 >        final int mapSize = expensiveTests ? 1_000_000 : 100;
827 >        final int iterations = expensiveTests ? 500 : 2;
828 >        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
829 >        for (int i = 0; i < mapSize; i++)
830 >            map.put(i, i);
831 >        Set<Integer> keySet = map.keySet();
832 >        Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
833 >        for (int i = 0; i < iterations; i++)
834 >            assertFalse(keySet.removeAll(removeMe));
835 >        assertEquals(mapSize, map.size());
836      }
837  
838   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines