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.32 by jsr166, Wed Jun 12 18:09:00 2013 UTC vs.
Revision 1.57 by jsr166, Sun Jan 7 22:59:18 2018 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
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   * Other contributors include Andrew Wright, Jeffrey Hayes,
7   * Pat Fisher, Mike Judd.
8   */
9  
10 < import junit.framework.*;
11 < import java.util.*;
10 > import java.util.ArrayList;
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.Collections;
14 > import java.util.Enumeration;
15 > import java.util.Iterator;
16 > import java.util.Map;
17 > import java.util.Random;
18 > import java.util.Set;
19   import java.util.concurrent.ConcurrentHashMap;
20  
21 + import junit.framework.Test;
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);
28 >        class Implementation implements MapImplementation {
29 >            public Class<?> klazz() { return ConcurrentHashMap.class; }
30 >            public Map emptyMap() { return new ConcurrentHashMap(); }
31 >            public Object makeKey(int i) { return i; }
32 >            public Object makeValue(int i) { return i; }
33 >            public boolean isConcurrent() { return true; }
34 >            public boolean permitsNullKeys() { return false; }
35 >            public boolean permitsNullValues() { return false; }
36 >            public boolean supportsSetValue() { return true; }
37 >        }
38 >        return newTestSuite(
39 >            ConcurrentHashMapTest.class,
40 >            MapTest.testSuite(new Implementation()));
41      }
42  
43      /**
44       * Returns a new map from Integers 1-5 to Strings "A"-"E".
45       */
46 <    private static ConcurrentHashMap map5() {
47 <        ConcurrentHashMap map = new ConcurrentHashMap(5);
46 >    private static ConcurrentHashMap<Integer, String> map5() {
47 >        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
48          assertTrue(map.isEmpty());
49          map.put(one, "A");
50          map.put(two, "B");
# Line 34 | Line 56 | public class ConcurrentHashMapTest exten
56          return map;
57      }
58  
59 +    /** Re-implement Integer.compare for old java versions */
60 +    static int compare(int x, int y) {
61 +        return (x < y) ? -1 : (x > y) ? 1 : 0;
62 +    }
63 +
64      // classes for testing Comparable fallbacks
65      static class BI implements Comparable<BI> {
66          private final int value;
67          BI(int value) { this.value = value; }
68          public int compareTo(BI other) {
69 <            return Integer.compare(value, other.value);
69 >            return compare(value, other.value);
70          }
71          public boolean equals(Object x) {
72              return (x instanceof BI) && ((BI)x).value == value;
# Line 73 | Line 100 | public class ConcurrentHashMapTest exten
100                      break;
101              }
102              if (r == 0)
103 <                r = Integer.compare(size(), other.size());
103 >                r = compare(size(), other.size());
104              return r;
105          }
106          private static final long serialVersionUID = 0;
107      }
108  
109 +    static class CollidingObject {
110 +        final String value;
111 +        CollidingObject(final String value) { this.value = value; }
112 +        public int hashCode() { return this.value.hashCode() & 1; }
113 +        public boolean equals(final Object obj) {
114 +            return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
115 +        }
116 +    }
117 +
118 +    static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
119 +        ComparableCollidingObject(final String value) { super(value); }
120 +        public int compareTo(final ComparableCollidingObject o) {
121 +            return value.compareTo(o.value);
122 +        }
123 +    }
124 +
125      /**
126       * Inserted elements that are subclasses of the same Comparable
127       * class are found.
128       */
129      public void testComparableFamily() {
130 <        ConcurrentHashMap<BI, Boolean> m =
131 <            new ConcurrentHashMap<BI, Boolean>();
132 <        for (int i = 0; i < 1000; i++) {
133 <            assertTrue(m.put(new CI(i), true) == null);
130 >        int size = 500;         // makes measured test run time -> 60ms
131 >        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
132 >        for (int i = 0; i < size; i++) {
133 >            assertNull(m.put(new CI(i), true));
134          }
135 <        for (int i = 0; i < 1000; i++) {
135 >        for (int i = 0; i < size; i++) {
136              assertTrue(m.containsKey(new CI(i)));
137              assertTrue(m.containsKey(new DI(i)));
138          }
# Line 100 | Line 143 | public class ConcurrentHashMapTest exten
143       * on Comparable can be inserted and found.
144       */
145      public void testGenericComparable() {
146 <        ConcurrentHashMap<Object, Boolean> m =
147 <            new ConcurrentHashMap<Object, Boolean>();
148 <        for (int i = 0; i < 1000; i++) {
146 >        int size = 120;         // makes measured test run time -> 60ms
147 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
148 >        for (int i = 0; i < size; i++) {
149              BI bi = new BI(i);
150              BS bs = new BS(String.valueOf(i));
151 <            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
152 <            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
153 <            assertTrue(m.putIfAbsent(bis, true) == null);
151 >            LexicographicList<BI> bis = new LexicographicList<>(bi);
152 >            LexicographicList<BS> bss = new LexicographicList<>(bs);
153 >            assertNull(m.putIfAbsent(bis, true));
154              assertTrue(m.containsKey(bis));
155              if (m.putIfAbsent(bss, true) == null)
156                  assertTrue(m.containsKey(bss));
157              assertTrue(m.containsKey(bis));
158          }
159 <        for (int i = 0; i < 1000; i++) {
160 <            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
159 >        for (int i = 0; i < size; i++) {
160 >            assertTrue(m.containsKey(Collections.singletonList(new BI(i))));
161          }
162      }
163  
# Line 124 | Line 167 | public class ConcurrentHashMapTest exten
167       * inserted and found.
168       */
169      public void testGenericComparable2() {
170 <        ConcurrentHashMap<Object, Boolean> m =
171 <            new ConcurrentHashMap<Object, Boolean>();
172 <        for (int i = 0; i < 1000; i++) {
173 <            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
170 >        int size = 500;         // makes measured test run time -> 60ms
171 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
172 >        for (int i = 0; i < size; i++) {
173 >            m.put(Collections.singletonList(new BI(i)), true);
174          }
175  
176 <        for (int i = 0; i < 1000; i++) {
177 <            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
176 >        for (int i = 0; i < size; i++) {
177 >            LexicographicList<BI> bis = new LexicographicList<>(new BI(i));
178              assertTrue(m.containsKey(bis));
179          }
180      }
181  
182      /**
183 +     * Mixtures of instances of comparable and non-comparable classes
184 +     * can be inserted and found.
185 +     */
186 +    public void testMixedComparable() {
187 +        int size = 1200;        // makes measured test run time -> 35ms
188 +        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
189 +        Random rng = new Random();
190 +        for (int i = 0; i < size; i++) {
191 +            Object x;
192 +            switch (rng.nextInt(4)) {
193 +            case 0:
194 +                x = new Object();
195 +                break;
196 +            case 1:
197 +                x = new CollidingObject(Integer.toString(i));
198 +                break;
199 +            default:
200 +                x = new ComparableCollidingObject(Integer.toString(i));
201 +            }
202 +            assertNull(map.put(x, x));
203 +        }
204 +        int count = 0;
205 +        for (Object k : map.keySet()) {
206 +            assertEquals(map.get(k), k);
207 +            ++count;
208 +        }
209 +        assertEquals(count, size);
210 +        assertEquals(map.size(), size);
211 +        for (Object k : map.keySet()) {
212 +            assertEquals(map.put(k, k), k);
213 +        }
214 +    }
215 +
216 +    /**
217       * clear removes all pairs
218       */
219      public void testClear() {
# Line 159 | Line 236 | public class ConcurrentHashMapTest exten
236      }
237  
238      /**
239 +     * hashCode() equals sum of each key.hashCode ^ value.hashCode
240 +     */
241 +    public void testHashCode() {
242 +        ConcurrentHashMap<Integer,String> map = map5();
243 +        int sum = 0;
244 +        for (Map.Entry<Integer,String> e : map.entrySet())
245 +            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
246 +        assertEquals(sum, map.hashCode());
247 +    }
248 +
249 +    /**
250       * contains returns true for contained value
251       */
252      public void testContains() {
# Line 209 | Line 297 | public class ConcurrentHashMapTest exten
297          assertEquals("A", (String)map.get(one));
298          ConcurrentHashMap empty = new ConcurrentHashMap();
299          assertNull(map.get("anything"));
300 +        assertNull(empty.get("anything"));
301      }
302  
303      /**
# Line 250 | Line 339 | public class ConcurrentHashMapTest exten
339      }
340  
341      /**
342 +     * Test keySet().removeAll on empty map
343 +     */
344 +    public void testKeySet_empty_removeAll() {
345 +        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
346 +        Set<Integer> set = map.keySet();
347 +        set.removeAll(Collections.emptyList());
348 +        assertTrue(map.isEmpty());
349 +        assertTrue(set.isEmpty());
350 +        // following is test for JDK-8163353
351 +        set.removeAll(Collections.emptySet());
352 +        assertTrue(map.isEmpty());
353 +        assertTrue(set.isEmpty());
354 +    }
355 +
356 +    /**
357       * keySet.toArray returns contains all keys
358       */
359      public void testKeySetToArray() {
# Line 442 | Line 546 | public class ConcurrentHashMapTest exten
546      // Exception tests
547  
548      /**
549 <     * Cannot create with negative capacity
549 >     * Cannot create with only negative capacity
550       */
551      public void testConstructor1() {
552          try {
553 <            new ConcurrentHashMap(-1,0,1);
553 >            new ConcurrentHashMap(-1);
554              shouldThrow();
555          } catch (IllegalArgumentException success) {}
556      }
557  
558      /**
559 <     * Cannot create with negative concurrency level
559 >     * Constructor (initialCapacity, loadFactor) throws
560 >     * IllegalArgumentException if either argument is negative
561       */
562      public void testConstructor2() {
563          try {
564 <            new ConcurrentHashMap(1,0,-1);
564 >            new ConcurrentHashMap(-1, .75f);
565 >            shouldThrow();
566 >        } catch (IllegalArgumentException success) {}
567 >
568 >        try {
569 >            new ConcurrentHashMap(16, -1);
570              shouldThrow();
571          } catch (IllegalArgumentException success) {}
572      }
573  
574      /**
575 <     * Cannot create with only negative capacity
575 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
576 >     * throws IllegalArgumentException if any argument is negative
577       */
578      public void testConstructor3() {
579          try {
580 <            new ConcurrentHashMap(-1);
580 >            new ConcurrentHashMap(-1, .75f, 1);
581 >            shouldThrow();
582 >        } catch (IllegalArgumentException success) {}
583 >
584 >        try {
585 >            new ConcurrentHashMap(16, -1, 1);
586 >            shouldThrow();
587 >        } catch (IllegalArgumentException success) {}
588 >
589 >        try {
590 >            new ConcurrentHashMap(16, .75f, -1);
591              shouldThrow();
592          } catch (IllegalArgumentException success) {}
593      }
594  
595      /**
596 +     * ConcurrentHashMap(map) throws NullPointerException if the given
597 +     * map is null
598 +     */
599 +    public void testConstructor4() {
600 +        try {
601 +            new ConcurrentHashMap(null);
602 +            shouldThrow();
603 +        } catch (NullPointerException success) {}
604 +    }
605 +
606 +    /**
607 +     * ConcurrentHashMap(map) creates a new map with the same mappings
608 +     * as the given map
609 +     */
610 +    public void testConstructor5() {
611 +        ConcurrentHashMap map1 = map5();
612 +        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
613 +        assertTrue(map2.equals(map1));
614 +        map2.put(one, "F");
615 +        assertFalse(map2.equals(map1));
616 +    }
617 +
618 +    /**
619       * get(null) throws NPE
620       */
621      public void testGet_NullPointerException() {
622 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
623          try {
479            ConcurrentHashMap c = new ConcurrentHashMap(5);
624              c.get(null);
625              shouldThrow();
626          } catch (NullPointerException success) {}
# Line 486 | Line 630 | public class ConcurrentHashMapTest exten
630       * containsKey(null) throws NPE
631       */
632      public void testContainsKey_NullPointerException() {
633 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
634          try {
490            ConcurrentHashMap c = new ConcurrentHashMap(5);
635              c.containsKey(null);
636              shouldThrow();
637          } catch (NullPointerException success) {}
# Line 497 | Line 641 | public class ConcurrentHashMapTest exten
641       * containsValue(null) throws NPE
642       */
643      public void testContainsValue_NullPointerException() {
644 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
645          try {
501            ConcurrentHashMap c = new ConcurrentHashMap(5);
646              c.containsValue(null);
647              shouldThrow();
648          } catch (NullPointerException success) {}
# Line 508 | Line 652 | public class ConcurrentHashMapTest exten
652       * contains(null) throws NPE
653       */
654      public void testContains_NullPointerException() {
655 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
656          try {
512            ConcurrentHashMap c = new ConcurrentHashMap(5);
657              c.contains(null);
658              shouldThrow();
659          } catch (NullPointerException success) {}
# Line 519 | Line 663 | public class ConcurrentHashMapTest exten
663       * put(null,x) throws NPE
664       */
665      public void testPut1_NullPointerException() {
666 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
667          try {
523            ConcurrentHashMap c = new ConcurrentHashMap(5);
668              c.put(null, "whatever");
669              shouldThrow();
670          } catch (NullPointerException success) {}
# Line 530 | Line 674 | public class ConcurrentHashMapTest exten
674       * put(x, null) throws NPE
675       */
676      public void testPut2_NullPointerException() {
677 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
678          try {
534            ConcurrentHashMap c = new ConcurrentHashMap(5);
679              c.put("whatever", null);
680              shouldThrow();
681          } catch (NullPointerException success) {}
# Line 541 | Line 685 | public class ConcurrentHashMapTest exten
685       * putIfAbsent(null, x) throws NPE
686       */
687      public void testPutIfAbsent1_NullPointerException() {
688 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
689          try {
545            ConcurrentHashMap c = new ConcurrentHashMap(5);
690              c.putIfAbsent(null, "whatever");
691              shouldThrow();
692          } catch (NullPointerException success) {}
# Line 552 | Line 696 | public class ConcurrentHashMapTest exten
696       * replace(null, x) throws NPE
697       */
698      public void testReplace_NullPointerException() {
699 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
700          try {
556            ConcurrentHashMap c = new ConcurrentHashMap(5);
701              c.replace(null, "whatever");
702              shouldThrow();
703          } catch (NullPointerException success) {}
# Line 563 | Line 707 | public class ConcurrentHashMapTest exten
707       * replace(null, x, y) throws NPE
708       */
709      public void testReplaceValue_NullPointerException() {
710 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
711          try {
567            ConcurrentHashMap c = new ConcurrentHashMap(5);
712              c.replace(null, one, "whatever");
713              shouldThrow();
714          } catch (NullPointerException success) {}
# Line 574 | Line 718 | public class ConcurrentHashMapTest exten
718       * putIfAbsent(x, null) throws NPE
719       */
720      public void testPutIfAbsent2_NullPointerException() {
721 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
722          try {
578            ConcurrentHashMap c = new ConcurrentHashMap(5);
723              c.putIfAbsent("whatever", null);
724              shouldThrow();
725          } catch (NullPointerException success) {}
# Line 585 | Line 729 | public class ConcurrentHashMapTest exten
729       * replace(x, null) throws NPE
730       */
731      public void testReplace2_NullPointerException() {
732 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
733          try {
589            ConcurrentHashMap c = new ConcurrentHashMap(5);
734              c.replace("whatever", null);
735              shouldThrow();
736          } catch (NullPointerException success) {}
# Line 596 | Line 740 | public class ConcurrentHashMapTest exten
740       * replace(x, null, y) throws NPE
741       */
742      public void testReplaceValue2_NullPointerException() {
743 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
744          try {
600            ConcurrentHashMap c = new ConcurrentHashMap(5);
745              c.replace("whatever", null, "A");
746              shouldThrow();
747          } catch (NullPointerException success) {}
# Line 607 | Line 751 | public class ConcurrentHashMapTest exten
751       * replace(x, y, null) throws NPE
752       */
753      public void testReplaceValue3_NullPointerException() {
754 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
755          try {
611            ConcurrentHashMap c = new ConcurrentHashMap(5);
756              c.replace("whatever", one, null);
757              shouldThrow();
758          } catch (NullPointerException success) {}
# Line 618 | Line 762 | public class ConcurrentHashMapTest exten
762       * remove(null) throws NPE
763       */
764      public void testRemove1_NullPointerException() {
765 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
766 +        c.put("sadsdf", "asdads");
767          try {
622            ConcurrentHashMap c = new ConcurrentHashMap(5);
623            c.put("sadsdf", "asdads");
768              c.remove(null);
769              shouldThrow();
770          } catch (NullPointerException success) {}
# Line 630 | Line 774 | public class ConcurrentHashMapTest exten
774       * remove(null, x) throws NPE
775       */
776      public void testRemove2_NullPointerException() {
777 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
778 +        c.put("sadsdf", "asdads");
779          try {
634            ConcurrentHashMap c = new ConcurrentHashMap(5);
635            c.put("sadsdf", "asdads");
780              c.remove(null, "whatever");
781              shouldThrow();
782          } catch (NullPointerException success) {}
# Line 648 | Line 792 | public class ConcurrentHashMapTest exten
792      }
793  
794      /**
795 <     * A deserialized map equals original
795 >     * A deserialized/reserialized map equals original
796       */
797      public void testSerialization() throws Exception {
798          Map x = map5();
# Line 682 | Line 826 | public class ConcurrentHashMapTest exten
826          }
827      }
828  
829 +    /**
830 +     * Tests performance of removeAll when the other collection is much smaller.
831 +     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
832 +     */
833 +    public void testRemoveAll_performance() {
834 +        final int mapSize = expensiveTests ? 1_000_000 : 100;
835 +        final int iterations = expensiveTests ? 500 : 2;
836 +        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
837 +        for (int i = 0; i < mapSize; i++)
838 +            map.put(i, i);
839 +        Set<Integer> keySet = map.keySet();
840 +        Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
841 +        for (int i = 0; i < iterations; i++)
842 +            assertFalse(keySet.removeAll(removeMe));
843 +        assertEquals(mapSize, map.size());
844 +    }
845 +
846   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines