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.49 by jsr166, Sun Jul 17 19:02:07 2016 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.
# Line 16 | Line 17 | import java.util.Map;
17   import java.util.Random;
18   import java.util.Set;
19   import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
20  
21   import junit.framework.Test;
23 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 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 118 | Line 128 | public class ConcurrentHashMapTest exten
128       */
129      public void testComparableFamily() {
130          int size = 500;         // makes measured test run time -> 60ms
131 <        ConcurrentHashMap<BI, Boolean> m =
122 <            new ConcurrentHashMap<BI, Boolean>();
131 >        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
132          for (int i = 0; i < size; i++) {
133 <            assertTrue(m.put(new CI(i), true) == null);
133 >            assertNull(m.put(new CI(i), true));
134          }
135          for (int i = 0; i < size; i++) {
136              assertTrue(m.containsKey(new CI(i)));
# Line 135 | Line 144 | public class ConcurrentHashMapTest exten
144       */
145      public void testGenericComparable() {
146          int size = 120;         // makes measured test run time -> 60ms
147 <        ConcurrentHashMap<Object, Boolean> m =
139 <            new ConcurrentHashMap<Object, Boolean>();
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));
# Line 160 | Line 168 | public class ConcurrentHashMapTest exten
168       */
169      public void testGenericComparable2() {
170          int size = 500;         // makes measured test run time -> 60ms
171 <        ConcurrentHashMap<Object, Boolean> m =
164 <            new ConcurrentHashMap<Object, Boolean>();
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 < size; i++) {
177 <            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
177 >            LexicographicList<BI> bis = new LexicographicList<>(new BI(i));
178              assertTrue(m.containsKey(bis));
179          }
180      }
# Line 178 | Line 185 | public class ConcurrentHashMapTest exten
185       */
186      public void testMixedComparable() {
187          int size = 1200;        // makes measured test run time -> 35ms
188 <        ConcurrentHashMap<Object, Object> map =
182 <            new ConcurrentHashMap<Object, Object>();
188 >        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
189          Random rng = new Random();
190          for (int i = 0; i < size; i++) {
191              Object x;
# Line 333 | 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 771 | 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 822 | Line 843 | public class ConcurrentHashMapTest exten
843          assertEquals(mapSize, map.size());
844      }
845  
825    /**
826     * Tests performance of computeIfAbsent when the element is present.
827     * See JDK-8161372
828     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck
829     */
830    public void testcomputeIfAbsent_performance() {
831        final int mapSize = 20;
832        final int iterations = expensiveTests ? (1 << 23) : mapSize * 2;
833        final int threads = expensiveTests ? 10 : 2;
834        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
835        for (int i = 0; i < mapSize; i++)
836            map.put(i, i);
837        final ExecutorService pool = Executors.newFixedThreadPool(2);
838        try (PoolCleaner cleaner = cleaner(pool)) {
839            Runnable r = new CheckedRunnable() {
840                public void realRun() {
841                    int result = 0;
842                    for (int i = 0; i < iterations; i++)
843                        result += map.computeIfAbsent(i % mapSize, (k) -> k + k);
844                    if (result == -42) throw new Error();
845                }};
846            for (int i = 0; i < threads; i++)
847                pool.execute(r);
848        }
849    }
850
846   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines