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.36 by dl, Sun Jul 21 22:24:18 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<Integer, String> map5() {
47 <        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
47 >        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
48          assertTrue(map.isEmpty());
49          map.put(one, "A");
50          map.put(two, "B");
# Line 35 | Line 57 | public class ConcurrentHashMapTest exten
57      }
58  
59      /** Re-implement Integer.compare for old java versions */
60 <    static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
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> {
# Line 103 | Line 127 | public class ConcurrentHashMapTest exten
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 119 | 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 143 | 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      }
# Line 160 | Line 184 | public class ConcurrentHashMapTest exten
184       * can be inserted and found.
185       */
186      public void testMixedComparable() {
187 <        int size = 10000;
188 <        ConcurrentHashMap<Object, Object> map =
189 <            new ConcurrentHashMap<Object, Object>();
166 <        Random rng = new Random(1370014958369218000L);
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)) {
# Line 212 | Line 235 | public class ConcurrentHashMapTest exten
235          assertFalse(map2.equals(map1));
236      }
237  
215
238      /**
239 <      * hashCode() equals sum of each key.hashCode ^ value.hashCode
240 <      */
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;
# Line 275 | 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 316 | 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 520 | Line 558 | public class ConcurrentHashMapTest exten
558      /**
559       * Constructor (initialCapacity, loadFactor) throws
560       * IllegalArgumentException if either argument is negative
561 <      */
561 >     */
562      public void testConstructor2() {
563          try {
564              new ConcurrentHashMap(-1, .75f);
565              shouldThrow();
566 <        } catch (IllegalArgumentException e) {}        
567 <        
566 >        } catch (IllegalArgumentException success) {}
567 >
568          try {
569              new ConcurrentHashMap(16, -1);
570              shouldThrow();
571 <        } catch (IllegalArgumentException e) {}        
571 >        } catch (IllegalArgumentException success) {}
572      }
573 <    
574 <     /**
575 <      * Constructor (initialCapacity, loadFactor, concurrencyLevel)
576 <      * throws IllegalArgumentException if any argument is negative
577 <      */
573 >
574 >    /**
575 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
576 >     * throws IllegalArgumentException if any argument is negative
577 >     */
578      public void testConstructor3() {
579 <         try {
580 <             new ConcurrentHashMap(-1, .75f, 1);
581 <             shouldThrow();
582 <         } catch (IllegalArgumentException e) {}        
583 <
584 <         try {
585 <             new ConcurrentHashMap(16, -1, 1);
586 <             shouldThrow();
587 <         } catch (IllegalArgumentException e) {}
588 <        
589 <         try {
590 <             new ConcurrentHashMap(16, .75f, -1);
591 <             shouldThrow();
592 <         } catch (IllegalArgumentException e) {}
593 <     }
579 >        try {
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
# Line 562 | Line 600 | public class ConcurrentHashMapTest exten
600          try {
601              new ConcurrentHashMap(null);
602              shouldThrow();
603 <        } catch (NullPointerException e) {}
603 >        } catch (NullPointerException success) {}
604      }
605  
606      /**
# Line 577 | Line 615 | public class ConcurrentHashMapTest exten
615          assertFalse(map2.equals(map1));
616      }
617  
580
618      /**
619       * get(null) throws NPE
620       */
621      public void testGet_NullPointerException() {
622 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
623          try {
586            ConcurrentHashMap c = new ConcurrentHashMap(5);
624              c.get(null);
625              shouldThrow();
626          } catch (NullPointerException success) {}
# Line 593 | 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 {
597            ConcurrentHashMap c = new ConcurrentHashMap(5);
635              c.containsKey(null);
636              shouldThrow();
637          } catch (NullPointerException success) {}
# Line 604 | 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 {
608            ConcurrentHashMap c = new ConcurrentHashMap(5);
646              c.containsValue(null);
647              shouldThrow();
648          } catch (NullPointerException success) {}
# Line 615 | 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 {
619            ConcurrentHashMap c = new ConcurrentHashMap(5);
657              c.contains(null);
658              shouldThrow();
659          } catch (NullPointerException success) {}
# Line 626 | 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 {
630            ConcurrentHashMap c = new ConcurrentHashMap(5);
668              c.put(null, "whatever");
669              shouldThrow();
670          } catch (NullPointerException success) {}
# Line 637 | 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 {
641            ConcurrentHashMap c = new ConcurrentHashMap(5);
679              c.put("whatever", null);
680              shouldThrow();
681          } catch (NullPointerException success) {}
# Line 648 | 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 {
652            ConcurrentHashMap c = new ConcurrentHashMap(5);
690              c.putIfAbsent(null, "whatever");
691              shouldThrow();
692          } catch (NullPointerException success) {}
# Line 659 | 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 {
663            ConcurrentHashMap c = new ConcurrentHashMap(5);
701              c.replace(null, "whatever");
702              shouldThrow();
703          } catch (NullPointerException success) {}
# Line 670 | 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 {
674            ConcurrentHashMap c = new ConcurrentHashMap(5);
712              c.replace(null, one, "whatever");
713              shouldThrow();
714          } catch (NullPointerException success) {}
# Line 681 | 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 {
685            ConcurrentHashMap c = new ConcurrentHashMap(5);
723              c.putIfAbsent("whatever", null);
724              shouldThrow();
725          } catch (NullPointerException success) {}
# Line 692 | 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 {
696            ConcurrentHashMap c = new ConcurrentHashMap(5);
734              c.replace("whatever", null);
735              shouldThrow();
736          } catch (NullPointerException success) {}
# Line 703 | 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 {
707            ConcurrentHashMap c = new ConcurrentHashMap(5);
745              c.replace("whatever", null, "A");
746              shouldThrow();
747          } catch (NullPointerException success) {}
# Line 714 | 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 {
718            ConcurrentHashMap c = new ConcurrentHashMap(5);
756              c.replace("whatever", one, null);
757              shouldThrow();
758          } catch (NullPointerException success) {}
# Line 725 | 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 {
729            ConcurrentHashMap c = new ConcurrentHashMap(5);
730            c.put("sadsdf", "asdads");
768              c.remove(null);
769              shouldThrow();
770          } catch (NullPointerException success) {}
# Line 737 | 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 {
741            ConcurrentHashMap c = new ConcurrentHashMap(5);
742            c.put("sadsdf", "asdads");
780              c.remove(null, "whatever");
781              shouldThrow();
782          } catch (NullPointerException success) {}
# Line 755 | 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 789 | 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