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.48 by jsr166, Sun Jul 17 03:40:07 2016 UTC vs.
Revision 1.62 by jsr166, Sun Sep 29 20:40:48 2019 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.
# Line 18 | Line 19 | import java.util.Set;
19   import java.util.concurrent.ConcurrentHashMap;
20  
21   import junit.framework.Test;
21 import junit.framework.TestSuite;
22  
23   public class ConcurrentHashMapTest extends JSR166TestCase {
24      public static void main(String[] args) {
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 boolean isConcurrent() { return true; }
32 >            public boolean permitsNullKeys() { return false; }
33 >            public boolean permitsNullValues() { return false; }
34 >            public boolean supportsSetValue() { return true; }
35 >        }
36 >        return newTestSuite(
37 >            ConcurrentHashMapTest.class,
38 >            MapTest.testSuite(new Implementation()));
39      }
40  
41      /**
42       * Returns a new map from Integers 1-5 to Strings "A"-"E".
43       */
44      private static ConcurrentHashMap<Integer, String> map5() {
45 <        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
45 >        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
46          assertTrue(map.isEmpty());
47          map.put(one, "A");
48          map.put(two, "B");
# Line 44 | Line 54 | public class ConcurrentHashMapTest exten
54          return map;
55      }
56  
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
57      // classes for testing Comparable fallbacks
58      static class BI implements Comparable<BI> {
59          private final int value;
60          BI(int value) { this.value = value; }
61          public int compareTo(BI other) {
62 <            return compare(value, other.value);
62 >            return Integer.compare(value, other.value);
63          }
64          public boolean equals(Object x) {
65              return (x instanceof BI) && ((BI)x).value == value;
# Line 88 | Line 93 | public class ConcurrentHashMapTest exten
93                      break;
94              }
95              if (r == 0)
96 <                r = compare(size(), other.size());
96 >                r = Integer.compare(size(), other.size());
97              return r;
98          }
99          private static final long serialVersionUID = 0;
# Line 116 | Line 121 | public class ConcurrentHashMapTest exten
121       */
122      public void testComparableFamily() {
123          int size = 500;         // makes measured test run time -> 60ms
124 <        ConcurrentHashMap<BI, Boolean> m =
120 <            new ConcurrentHashMap<BI, Boolean>();
124 >        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
125          for (int i = 0; i < size; i++) {
126 <            assertTrue(m.put(new CI(i), true) == null);
126 >            assertNull(m.put(new CI(i), true));
127          }
128          for (int i = 0; i < size; i++) {
129              assertTrue(m.containsKey(new CI(i)));
# Line 133 | Line 137 | public class ConcurrentHashMapTest exten
137       */
138      public void testGenericComparable() {
139          int size = 120;         // makes measured test run time -> 60ms
140 <        ConcurrentHashMap<Object, Boolean> m =
137 <            new ConcurrentHashMap<Object, Boolean>();
140 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
141          for (int i = 0; i < size; i++) {
142              BI bi = new BI(i);
143              BS bs = new BS(String.valueOf(i));
144 <            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
145 <            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
146 <            assertTrue(m.putIfAbsent(bis, true) == null);
144 >            LexicographicList<BI> bis = new LexicographicList<>(bi);
145 >            LexicographicList<BS> bss = new LexicographicList<>(bs);
146 >            assertNull(m.putIfAbsent(bis, true));
147              assertTrue(m.containsKey(bis));
148              if (m.putIfAbsent(bss, true) == null)
149                  assertTrue(m.containsKey(bss));
# Line 158 | Line 161 | public class ConcurrentHashMapTest exten
161       */
162      public void testGenericComparable2() {
163          int size = 500;         // makes measured test run time -> 60ms
164 <        ConcurrentHashMap<Object, Boolean> m =
162 <            new ConcurrentHashMap<Object, Boolean>();
164 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
165          for (int i = 0; i < size; i++) {
166              m.put(Collections.singletonList(new BI(i)), true);
167          }
168  
169          for (int i = 0; i < size; i++) {
170 <            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
170 >            LexicographicList<BI> bis = new LexicographicList<>(new BI(i));
171              assertTrue(m.containsKey(bis));
172          }
173      }
# Line 176 | Line 178 | public class ConcurrentHashMapTest exten
178       */
179      public void testMixedComparable() {
180          int size = 1200;        // makes measured test run time -> 35ms
181 <        ConcurrentHashMap<Object, Object> map =
180 <            new ConcurrentHashMap<Object, Object>();
181 >        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
182          Random rng = new Random();
183          for (int i = 0; i < size; i++) {
184              Object x;
# Line 331 | Line 332 | public class ConcurrentHashMapTest exten
332      }
333  
334      /**
335 +     * Test keySet().removeAll on empty map
336 +     */
337 +    public void testKeySet_empty_removeAll() {
338 +        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
339 +        Set<Integer> set = map.keySet();
340 +        set.removeAll(Collections.emptyList());
341 +        assertTrue(map.isEmpty());
342 +        assertTrue(set.isEmpty());
343 +        // following is test for JDK-8163353
344 +        set.removeAll(Collections.emptySet());
345 +        assertTrue(map.isEmpty());
346 +        assertTrue(set.isEmpty());
347 +    }
348 +
349 +    /**
350       * keySet.toArray returns contains all keys
351       */
352      public void testKeySetToArray() {
# Line 769 | Line 785 | public class ConcurrentHashMapTest exten
785      }
786  
787      /**
788 <     * A deserialized map equals original
788 >     * A deserialized/reserialized map equals original
789       */
790      public void testSerialization() throws Exception {
791          Map x = map5();
# Line 808 | Line 824 | public class ConcurrentHashMapTest exten
824       * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
825       */
826      public void testRemoveAll_performance() {
827 <        int mapSize = expensiveTests ? 1_000_000 : 100;
828 <        int iterations = expensiveTests ? 500 : 2;
829 <        ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
827 >        final int mapSize = expensiveTests ? 1_000_000 : 100;
828 >        final int iterations = expensiveTests ? 500 : 2;
829 >        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
830          for (int i = 0; i < mapSize; i++)
831              map.put(i, i);
832          Set<Integer> keySet = map.keySet();
# Line 819 | Line 835 | public class ConcurrentHashMapTest exten
835              assertFalse(keySet.removeAll(removeMe));
836          assertEquals(mapSize, map.size());
837      }
838 +
839 +    public void testReentrantComputeIfAbsent() {
840 +        ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16);
841 +        try {
842 +            for (int i = 0; i < 100; i++) { // force a resize
843 +                map.computeIfAbsent(i, key -> findValue(map, key));
844 +            }
845 +            fail("recursive computeIfAbsent should throw IllegalStateException");
846 +        } catch (IllegalStateException success) {}
847 +    }
848 +
849 +    private Integer findValue(ConcurrentHashMap<Integer, Integer> map,
850 +                              Integer key) {
851 +        return (key % 5 == 0) ?  key :
852 +            map.computeIfAbsent(key + 1, k -> findValue(map, k));
853 +    }
854 +
855   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines