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.34 by dl, Sat Jul 20 16:50:12 2013 UTC vs.
Revision 1.40 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
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.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());
# Line 21 | Line 31 | public class ConcurrentHashMapTest exten
31      /**
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 map = new ConcurrentHashMap<Integer, String>(5);
36          assertTrue(map.isEmpty());
37          map.put(one, "A");
38          map.put(two, "B");
# Line 103 | Line 113 | public class ConcurrentHashMapTest exten
113       * class are found.
114       */
115      public void testComparableFamily() {
116 +        int size = 500;         // makes measured test run time -> 60ms
117          ConcurrentHashMap<BI, Boolean> m =
118              new ConcurrentHashMap<BI, Boolean>();
119 <        for (int i = 0; i < 1000; i++) {
119 >        for (int i = 0; i < size; i++) {
120              assertTrue(m.put(new CI(i), true) == null);
121          }
122 <        for (int i = 0; i < 1000; i++) {
122 >        for (int i = 0; i < size; i++) {
123              assertTrue(m.containsKey(new CI(i)));
124              assertTrue(m.containsKey(new DI(i)));
125          }
# Line 119 | Line 130 | public class ConcurrentHashMapTest exten
130       * on Comparable can be inserted and found.
131       */
132      public void testGenericComparable() {
133 +        int size = 120;         // makes measured test run time -> 60ms
134          ConcurrentHashMap<Object, Boolean> m =
135              new ConcurrentHashMap<Object, Boolean>();
136 <        for (int i = 0; i < 1000; i++) {
136 >        for (int i = 0; i < size; i++) {
137              BI bi = new BI(i);
138              BS bs = new BS(String.valueOf(i));
139              LexicographicList<BI> bis = new LexicographicList<BI>(bi);
# Line 132 | Line 144 | public class ConcurrentHashMapTest exten
144                  assertTrue(m.containsKey(bss));
145              assertTrue(m.containsKey(bis));
146          }
147 <        for (int i = 0; i < 1000; i++) {
148 <            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
147 >        for (int i = 0; i < size; i++) {
148 >            assertTrue(m.containsKey(Collections.singletonList(new BI(i))));
149          }
150      }
151  
# Line 143 | Line 155 | public class ConcurrentHashMapTest exten
155       * inserted and found.
156       */
157      public void testGenericComparable2() {
158 +        int size = 500;         // makes measured test run time -> 60ms
159          ConcurrentHashMap<Object, Boolean> m =
160              new ConcurrentHashMap<Object, Boolean>();
161 <        for (int i = 0; i < 1000; i++) {
162 <            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
161 >        for (int i = 0; i < size; i++) {
162 >            m.put(Collections.singletonList(new BI(i)), true);
163          }
164  
165 <        for (int i = 0; i < 1000; i++) {
165 >        for (int i = 0; i < size; i++) {
166              LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
167              assertTrue(m.containsKey(bis));
168          }
# Line 160 | Line 173 | public class ConcurrentHashMapTest exten
173       * can be inserted and found.
174       */
175      public void testMixedComparable() {
176 <        int size = 10000;
177 <        ConcurrentHashMap<Object, Object> map =
176 >        int size = 1200;        // makes measured test run time -> 35ms
177 >        ConcurrentHashMap<Object, Object> map =
178              new ConcurrentHashMap<Object, Object>();
179 <        Random rng = new Random(1370014958369218000L);
180 <        for(int i = 0; i < size; i++) {
179 >        Random rng = new Random();
180 >        for (int i = 0; i < size; i++) {
181              Object x;
182 <            switch(rng.nextInt(4)) {
182 >            switch (rng.nextInt(4)) {
183              case 0:
184                  x = new Object();
185                  break;
# Line 213 | Line 226 | public class ConcurrentHashMapTest exten
226      }
227  
228      /**
229 +     * hashCode() equals sum of each key.hashCode ^ value.hashCode
230 +     */
231 +    public void testHashCode() {
232 +        ConcurrentHashMap<Integer,String> map = map5();
233 +        int sum = 0;
234 +        for (Map.Entry<Integer,String> e : map.entrySet())
235 +            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
236 +        assertEquals(sum, map.hashCode());
237 +    }
238 +
239 +    /**
240       * contains returns true for contained value
241       */
242      public void testContains() {
# Line 496 | Line 520 | public class ConcurrentHashMapTest exten
520      // Exception tests
521  
522      /**
523 <     * Cannot create with negative capacity
523 >     * Cannot create with only negative capacity
524       */
525      public void testConstructor1() {
526          try {
527 <            new ConcurrentHashMap(-1,0,1);
527 >            new ConcurrentHashMap(-1);
528              shouldThrow();
529          } catch (IllegalArgumentException success) {}
530      }
531  
532      /**
533 <     * Cannot create with negative concurrency level
534 <     */
533 >     * Constructor (initialCapacity, loadFactor) throws
534 >     * IllegalArgumentException if either argument is negative
535 >      */
536      public void testConstructor2() {
537          try {
538 <            new ConcurrentHashMap(1,0,-1);
538 >            new ConcurrentHashMap(-1, .75f);
539              shouldThrow();
540 <        } catch (IllegalArgumentException success) {}
540 >        } catch (IllegalArgumentException e) {}
541 >
542 >        try {
543 >            new ConcurrentHashMap(16, -1);
544 >            shouldThrow();
545 >        } catch (IllegalArgumentException e) {}
546      }
547  
548      /**
549 <     * Cannot create with only negative capacity
549 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
550 >     * throws IllegalArgumentException if any argument is negative
551       */
552      public void testConstructor3() {
553          try {
554 <            new ConcurrentHashMap(-1);
554 >            new ConcurrentHashMap(-1, .75f, 1);
555              shouldThrow();
556 <        } catch (IllegalArgumentException success) {}
556 >        } catch (IllegalArgumentException e) {}
557 >
558 >        try {
559 >            new ConcurrentHashMap(16, -1, 1);
560 >            shouldThrow();
561 >        } catch (IllegalArgumentException e) {}
562 >
563 >        try {
564 >            new ConcurrentHashMap(16, .75f, -1);
565 >            shouldThrow();
566 >        } catch (IllegalArgumentException e) {}
567      }
568  
569      /**
570 +     * ConcurrentHashMap(map) throws NullPointerException if the given
571 +     * map is null
572 +     */
573 +    public void testConstructor4() {
574 +        try {
575 +            new ConcurrentHashMap(null);
576 +            shouldThrow();
577 +        } catch (NullPointerException e) {}
578 +    }
579 +
580 +    /**
581 +     * ConcurrentHashMap(map) creates a new map with the same mappings
582 +     * as the given map
583 +     */
584 +    public void testConstructor5() {
585 +        ConcurrentHashMap map1 = map5();
586 +        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
587 +        assertTrue(map2.equals(map1));
588 +        map2.put(one, "F");
589 +        assertFalse(map2.equals(map1));
590 +    }
591 +
592 +
593 +    /**
594       * get(null) throws NPE
595       */
596      public void testGet_NullPointerException() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines