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.33 by jsr166, Sun Jul 14 21:41:06 2013 UTC vs.
Revision 1.38 by jsr166, Tue Sep 24 17:03:14 2013 UTC

# Line 21 | Line 21 | public class ConcurrentHashMapTest exten
21      /**
22       * Returns a new map from Integers 1-5 to Strings "A"-"E".
23       */
24 <    private static ConcurrentHashMap map5() {
25 <        ConcurrentHashMap map = new ConcurrentHashMap(5);
24 >    private static ConcurrentHashMap<Integer, String> map5() {
25 >        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
26          assertTrue(map.isEmpty());
27          map.put(one, "A");
28          map.put(two, "B");
# Line 82 | Line 82 | public class ConcurrentHashMapTest exten
82          private static final long serialVersionUID = 0;
83      }
84  
85 +    static class CollidingObject {
86 +        final String value;
87 +        CollidingObject(final String value) { this.value = value; }
88 +        public int hashCode() { return this.value.hashCode() & 1; }
89 +        public boolean equals(final Object obj) {
90 +            return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
91 +        }
92 +    }
93 +
94 +    static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
95 +        ComparableCollidingObject(final String value) { super(value); }
96 +        public int compareTo(final ComparableCollidingObject o) {
97 +            return value.compareTo(o.value);
98 +        }
99 +    }
100 +
101      /**
102       * Inserted elements that are subclasses of the same Comparable
103       * class are found.
# Line 140 | Line 156 | public class ConcurrentHashMapTest exten
156      }
157  
158      /**
159 +     * Mixtures of instances of comparable and non-comparable classes
160 +     * can be inserted and found.
161 +     */
162 +    public void testMixedComparable() {
163 +        int size = 1200;        // makes measured test run time -> 35ms
164 +        ConcurrentHashMap<Object, Object> map =
165 +            new ConcurrentHashMap<Object, Object>();
166 +        Random rng = new Random();
167 +        for (int i = 0; i < size; i++) {
168 +            Object x;
169 +            switch (rng.nextInt(4)) {
170 +            case 0:
171 +                x = new Object();
172 +                break;
173 +            case 1:
174 +                x = new CollidingObject(Integer.toString(i));
175 +                break;
176 +            default:
177 +                x = new ComparableCollidingObject(Integer.toString(i));
178 +            }
179 +            assertNull(map.put(x, x));
180 +        }
181 +        int count = 0;
182 +        for (Object k : map.keySet()) {
183 +            assertEquals(map.get(k), k);
184 +            ++count;
185 +        }
186 +        assertEquals(count, size);
187 +        assertEquals(map.size(), size);
188 +        for (Object k : map.keySet()) {
189 +            assertEquals(map.put(k, k), k);
190 +        }
191 +    }
192 +
193 +    /**
194       * clear removes all pairs
195       */
196      public void testClear() {
# Line 162 | Line 213 | public class ConcurrentHashMapTest exten
213      }
214  
215      /**
216 +     * hashCode() equals sum of each key.hashCode ^ value.hashCode
217 +     */
218 +    public void testHashCode() {
219 +        ConcurrentHashMap<Integer,String> map = map5();
220 +        int sum = 0;
221 +        for (Map.Entry<Integer,String> e : map.entrySet())
222 +            sum += e.getKey().hashCode() ^ e.getValue().hashCode();
223 +        assertEquals(sum, map.hashCode());
224 +    }
225 +
226 +    /**
227       * contains returns true for contained value
228       */
229      public void testContains() {
# Line 445 | Line 507 | public class ConcurrentHashMapTest exten
507      // Exception tests
508  
509      /**
510 <     * Cannot create with negative capacity
510 >     * Cannot create with only negative capacity
511       */
512      public void testConstructor1() {
513          try {
514 <            new ConcurrentHashMap(-1,0,1);
514 >            new ConcurrentHashMap(-1);
515              shouldThrow();
516          } catch (IllegalArgumentException success) {}
517      }
518  
519      /**
520 <     * Cannot create with negative concurrency level
521 <     */
520 >     * Constructor (initialCapacity, loadFactor) throws
521 >     * IllegalArgumentException if either argument is negative
522 >      */
523      public void testConstructor2() {
524          try {
525 <            new ConcurrentHashMap(1,0,-1);
525 >            new ConcurrentHashMap(-1, .75f);
526              shouldThrow();
527 <        } catch (IllegalArgumentException success) {}
527 >        } catch (IllegalArgumentException e) {}
528 >
529 >        try {
530 >            new ConcurrentHashMap(16, -1);
531 >            shouldThrow();
532 >        } catch (IllegalArgumentException e) {}
533      }
534  
535      /**
536 <     * Cannot create with only negative capacity
536 >     * Constructor (initialCapacity, loadFactor, concurrencyLevel)
537 >     * throws IllegalArgumentException if any argument is negative
538       */
539      public void testConstructor3() {
540          try {
541 <            new ConcurrentHashMap(-1);
541 >            new ConcurrentHashMap(-1, .75f, 1);
542              shouldThrow();
543 <        } catch (IllegalArgumentException success) {}
543 >        } catch (IllegalArgumentException e) {}
544 >
545 >        try {
546 >            new ConcurrentHashMap(16, -1, 1);
547 >            shouldThrow();
548 >        } catch (IllegalArgumentException e) {}
549 >
550 >        try {
551 >            new ConcurrentHashMap(16, .75f, -1);
552 >            shouldThrow();
553 >        } catch (IllegalArgumentException e) {}
554      }
555  
556      /**
557 +     * ConcurrentHashMap(map) throws NullPointerException if the given
558 +     * map is null
559 +     */
560 +    public void testConstructor4() {
561 +        try {
562 +            new ConcurrentHashMap(null);
563 +            shouldThrow();
564 +        } catch (NullPointerException e) {}
565 +    }
566 +
567 +    /**
568 +     * ConcurrentHashMap(map) creates a new map with the same mappings
569 +     * as the given map
570 +     */
571 +    public void testConstructor5() {
572 +        ConcurrentHashMap map1 = map5();
573 +        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
574 +        assertTrue(map2.equals(map1));
575 +        map2.put(one, "F");
576 +        assertFalse(map2.equals(map1));
577 +    }
578 +
579 +
580 +    /**
581       * get(null) throws NPE
582       */
583      public void testGet_NullPointerException() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines