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.18 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.30 by jsr166, Thu Apr 11 20:03:44 2013 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
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.util.Enumeration;
13 < import java.io.*;
11 > import java.util.concurrent.ConcurrentHashMap;
12  
13   public class ConcurrentHashMapTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(ConcurrentHashMapTest.class);
19      }
20  
21      /**
22 <     * Create a map from Integers 1-5 to Strings "A"-"E".
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);
# Line 36 | Line 34 | public class ConcurrentHashMapTest exten
34          return map;
35      }
36  
37 +    // classes for testing Comparable fallbacks
38 +    static class BI implements Comparable<BI> {
39 +        private final int value;
40 +        BI(int value) { this.value = value; }
41 +        public int compareTo(BI other) {
42 +            return Integer.compare(value, other.value);
43 +        }
44 +        public boolean equals(Object x) {
45 +            return (x instanceof BI) && ((BI)x).value == value;
46 +        }
47 +        public int hashCode() { return 42; }
48 +    }
49 +    static class CI extends BI { CI(int value) { super(value); } }
50 +    static class DI extends BI { DI(int value) { super(value); } }
51 +
52 +    static class BS implements Comparable<BS> {
53 +        private final String value;
54 +        BS(String value) { this.value = value; }
55 +        public int compareTo(BS other) {
56 +            return value.compareTo(other.value);
57 +        }
58 +        public boolean equals(Object x) {
59 +            return (x instanceof BS) && value.equals(((BS)x).value);
60 +        }
61 +        public int hashCode() { return 42; }
62 +    }
63 +
64 +    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
65 +        implements Comparable<LexicographicList<E>> {
66 +        LexicographicList(Collection<E> c) { super(c); }
67 +        LexicographicList(E e) { super(Collections.singleton(e)); }
68 +        public int compareTo(LexicographicList<E> other) {
69 +            int common = Math.min(size(), other.size());
70 +            int r = 0;
71 +            for (int i = 0; i < common; i++) {
72 +                if ((r = get(i).compareTo(other.get(i))) != 0)
73 +                    break;
74 +            }
75 +            if (r == 0)
76 +                r = Integer.compare(size(), other.size());
77 +            return r;
78 +        }
79 +        private static final long serialVersionUID = 0;
80 +    }
81 +
82      /**
83 <     *  clear removes all pairs
83 >     * Inserted elements that are subclasses of the same Comparable
84 >     * class are found.
85 >     */
86 >    public void testComparableFamily() {
87 >        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
88 >        for (int i = 0; i < 1000; i++) {
89 >            assertTrue(m.put(new CI(i), true) == null);
90 >        }
91 >        for (int i = 0; i < 1000; i++) {
92 >            assertTrue(m.containsKey(new CI(i)));
93 >            assertTrue(m.containsKey(new DI(i)));
94 >        }
95 >    }
96 >
97 >    /**
98 >     * Elements of classes with erased generic type parameters based
99 >     * on Comparable can be inserted and found.
100 >     */
101 >    public void testGenericComparable() {
102 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
103 >        for (int i = 0; i < 1000; i++) {
104 >            BI bi = new BI(i);
105 >            BS bs = new BS(String.valueOf(i));
106 >            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
107 >            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
108 >            assertTrue(m.putIfAbsent(bis, true) == null);
109 >            assertTrue(m.containsKey(bis));
110 >            if (m.putIfAbsent(bss, true) == null)
111 >                assertTrue(m.containsKey(bss));
112 >            assertTrue(m.containsKey(bis));
113 >        }
114 >        for (int i = 0; i < 1000; i++) {
115 >            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
116 >        }
117 >    }
118 >
119 >    /**
120 >     * Elements of non-comparable classes equal to those of classes
121 >     * with erased generic type parameters based on Comparable can be
122 >     * inserted and found.
123 >     */
124 >    public void testGenericComparable2() {
125 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
126 >        for (int i = 0; i < 1000; i++) {
127 >            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
128 >        }
129 >
130 >        for (int i = 0; i < 1000; i++) {
131 >            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
132 >            assertTrue(m.containsKey(bis));
133 >        }
134 >    }
135 >
136 >    /**
137 >     * clear removes all pairs
138       */
139      public void testClear() {
140          ConcurrentHashMap map = map5();
141          map.clear();
142 <        assertEquals(map.size(), 0);
142 >        assertEquals(0, map.size());
143      }
144  
145      /**
146 <     *  Maps with same contents are equal
146 >     * Maps with same contents are equal
147       */
148      public void testEquals() {
149          ConcurrentHashMap map1 = map5();
# Line 59 | Line 156 | public class ConcurrentHashMapTest exten
156      }
157  
158      /**
159 <     *  contains returns true for contained value
159 >     * contains returns true for contained value
160       */
161      public void testContains() {
162          ConcurrentHashMap map = map5();
# Line 68 | Line 165 | public class ConcurrentHashMapTest exten
165      }
166  
167      /**
168 <     *  containsKey returns true for contained key
168 >     * containsKey returns true for contained key
169       */
170      public void testContainsKey() {
171          ConcurrentHashMap map = map5();
# Line 77 | Line 174 | public class ConcurrentHashMapTest exten
174      }
175  
176      /**
177 <     *  containsValue returns true for held values
177 >     * containsValue returns true for held values
178       */
179      public void testContainsValue() {
180          ConcurrentHashMap map = map5();
# Line 86 | Line 183 | public class ConcurrentHashMapTest exten
183      }
184  
185      /**
186 <     *   enumeration returns an enumeration containing the correct
187 <     *   elements
186 >     * enumeration returns an enumeration containing the correct
187 >     * elements
188       */
189      public void testEnumeration() {
190          ConcurrentHashMap map = map5();
# Line 101 | Line 198 | public class ConcurrentHashMapTest exten
198      }
199  
200      /**
201 <     *  get returns the correct element at the given key,
202 <     *  or null if not present
201 >     * get returns the correct element at the given key,
202 >     * or null if not present
203       */
204      public void testGet() {
205          ConcurrentHashMap map = map5();
# Line 112 | Line 209 | public class ConcurrentHashMapTest exten
209      }
210  
211      /**
212 <     *  isEmpty is true of empty map and false for non-empty
212 >     * isEmpty is true of empty map and false for non-empty
213       */
214      public void testIsEmpty() {
215          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 122 | Line 219 | public class ConcurrentHashMapTest exten
219      }
220  
221      /**
222 <     *   keys returns an enumeration containing all the keys from the map
222 >     * keys returns an enumeration containing all the keys from the map
223       */
224      public void testKeys() {
225          ConcurrentHashMap map = map5();
# Line 136 | Line 233 | public class ConcurrentHashMapTest exten
233      }
234  
235      /**
236 <     *   keySet returns a Set containing all the keys
236 >     * keySet returns a Set containing all the keys
237       */
238      public void testKeySet() {
239          ConcurrentHashMap map = map5();
# Line 150 | Line 247 | public class ConcurrentHashMapTest exten
247      }
248  
249      /**
250 <     *  keySet.toArray returns contains all keys
250 >     * keySet.toArray returns contains all keys
251       */
252      public void testKeySetToArray() {
253          ConcurrentHashMap map = map5();
# Line 163 | Line 260 | public class ConcurrentHashMapTest exten
260      }
261  
262      /**
263 <     *  Values.toArray contains all values
263 >     * Values.toArray contains all values
264       */
265      public void testValuesToArray() {
266          ConcurrentHashMap map = map5();
# Line 179 | Line 276 | public class ConcurrentHashMapTest exten
276      }
277  
278      /**
279 <     *  entrySet.toArray contains all entries
279 >     * entrySet.toArray contains all entries
280       */
281      public void testEntrySetToArray() {
282          ConcurrentHashMap map = map5();
# Line 226 | Line 323 | public class ConcurrentHashMapTest exten
323      }
324  
325      /**
326 <     *   putAll  adds all key-value pairs from the given map
326 >     * putAll adds all key-value pairs from the given map
327       */
328      public void testPutAll() {
329          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 241 | Line 338 | public class ConcurrentHashMapTest exten
338      }
339  
340      /**
341 <     *   putIfAbsent works when the given key is not present
341 >     * putIfAbsent works when the given key is not present
342       */
343      public void testPutIfAbsent() {
344          ConcurrentHashMap map = map5();
# Line 250 | Line 347 | public class ConcurrentHashMapTest exten
347      }
348  
349      /**
350 <     *   putIfAbsent does not add the pair if the key is already present
350 >     * putIfAbsent does not add the pair if the key is already present
351       */
352      public void testPutIfAbsent2() {
353          ConcurrentHashMap map = map5();
# Line 258 | Line 355 | public class ConcurrentHashMapTest exten
355      }
356  
357      /**
358 <     *   replace fails when the given key is not present
358 >     * replace fails when the given key is not present
359       */
360      public void testReplace() {
361          ConcurrentHashMap map = map5();
# Line 267 | Line 364 | public class ConcurrentHashMapTest exten
364      }
365  
366      /**
367 <     *   replace succeeds if the key is already present
367 >     * replace succeeds if the key is already present
368       */
369      public void testReplace2() {
370          ConcurrentHashMap map = map5();
# Line 275 | Line 372 | public class ConcurrentHashMapTest exten
372          assertEquals("Z", map.get(one));
373      }
374  
278
375      /**
376       * replace value fails when the given key not mapped to expected value
377       */
# Line 296 | Line 392 | public class ConcurrentHashMapTest exten
392          assertEquals("Z", map.get(one));
393      }
394  
299
395      /**
396 <     *   remove removes the correct key-value pair from the map
396 >     * remove removes the correct key-value pair from the map
397       */
398      public void testRemove() {
399          ConcurrentHashMap map = map5();
# Line 318 | Line 413 | public class ConcurrentHashMapTest exten
413          map.remove(four, "A");
414          assertEquals(4, map.size());
415          assertTrue(map.containsKey(four));
321
416      }
417  
418      /**
419 <     *   size returns the correct values
419 >     * size returns the correct values
420       */
421      public void testSize() {
422          ConcurrentHashMap map = map5();
# Line 338 | Line 432 | public class ConcurrentHashMapTest exten
432          ConcurrentHashMap map = map5();
433          String s = map.toString();
434          for (int i = 1; i <= 5; ++i) {
435 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
435 >            assertTrue(s.contains(String.valueOf(i)));
436          }
437      }
438  
# Line 351 | Line 445 | public class ConcurrentHashMapTest exten
445          try {
446              new ConcurrentHashMap(-1,0,1);
447              shouldThrow();
448 <        } catch (IllegalArgumentException e) {}
448 >        } catch (IllegalArgumentException success) {}
449      }
450  
451      /**
# Line 361 | Line 455 | public class ConcurrentHashMapTest exten
455          try {
456              new ConcurrentHashMap(1,0,-1);
457              shouldThrow();
458 <        } catch (IllegalArgumentException e) {}
458 >        } catch (IllegalArgumentException success) {}
459      }
460  
461      /**
# Line 371 | Line 465 | public class ConcurrentHashMapTest exten
465          try {
466              new ConcurrentHashMap(-1);
467              shouldThrow();
468 <        } catch (IllegalArgumentException e) {}
468 >        } catch (IllegalArgumentException success) {}
469      }
470  
471      /**
# Line 382 | Line 476 | public class ConcurrentHashMapTest exten
476              ConcurrentHashMap c = new ConcurrentHashMap(5);
477              c.get(null);
478              shouldThrow();
479 <        } catch (NullPointerException e) {}
479 >        } catch (NullPointerException success) {}
480      }
481  
482      /**
# Line 393 | Line 487 | public class ConcurrentHashMapTest exten
487              ConcurrentHashMap c = new ConcurrentHashMap(5);
488              c.containsKey(null);
489              shouldThrow();
490 <        } catch (NullPointerException e) {}
490 >        } catch (NullPointerException success) {}
491      }
492  
493      /**
# Line 404 | Line 498 | public class ConcurrentHashMapTest exten
498              ConcurrentHashMap c = new ConcurrentHashMap(5);
499              c.containsValue(null);
500              shouldThrow();
501 <        } catch (NullPointerException e) {}
501 >        } catch (NullPointerException success) {}
502      }
503  
504      /**
# Line 415 | Line 509 | public class ConcurrentHashMapTest exten
509              ConcurrentHashMap c = new ConcurrentHashMap(5);
510              c.contains(null);
511              shouldThrow();
512 <        } catch (NullPointerException e) {}
512 >        } catch (NullPointerException success) {}
513      }
514  
515      /**
# Line 426 | Line 520 | public class ConcurrentHashMapTest exten
520              ConcurrentHashMap c = new ConcurrentHashMap(5);
521              c.put(null, "whatever");
522              shouldThrow();
523 <        } catch (NullPointerException e) {}
523 >        } catch (NullPointerException success) {}
524      }
525  
526      /**
# Line 437 | Line 531 | public class ConcurrentHashMapTest exten
531              ConcurrentHashMap c = new ConcurrentHashMap(5);
532              c.put("whatever", null);
533              shouldThrow();
534 <        } catch (NullPointerException e) {}
534 >        } catch (NullPointerException success) {}
535      }
536  
537      /**
# Line 448 | Line 542 | public class ConcurrentHashMapTest exten
542              ConcurrentHashMap c = new ConcurrentHashMap(5);
543              c.putIfAbsent(null, "whatever");
544              shouldThrow();
545 <        } catch (NullPointerException e) {}
545 >        } catch (NullPointerException success) {}
546      }
547  
548      /**
# Line 459 | Line 553 | public class ConcurrentHashMapTest exten
553              ConcurrentHashMap c = new ConcurrentHashMap(5);
554              c.replace(null, "whatever");
555              shouldThrow();
556 <        } catch (NullPointerException e) {}
556 >        } catch (NullPointerException success) {}
557      }
558  
559      /**
# Line 470 | Line 564 | public class ConcurrentHashMapTest exten
564              ConcurrentHashMap c = new ConcurrentHashMap(5);
565              c.replace(null, one, "whatever");
566              shouldThrow();
567 <        } catch (NullPointerException e) {}
567 >        } catch (NullPointerException success) {}
568      }
569  
570      /**
# Line 481 | Line 575 | public class ConcurrentHashMapTest exten
575              ConcurrentHashMap c = new ConcurrentHashMap(5);
576              c.putIfAbsent("whatever", null);
577              shouldThrow();
578 <        } catch (NullPointerException e) {}
578 >        } catch (NullPointerException success) {}
579      }
580  
487
581      /**
582       * replace(x, null) throws NPE
583       */
# Line 493 | Line 586 | public class ConcurrentHashMapTest exten
586              ConcurrentHashMap c = new ConcurrentHashMap(5);
587              c.replace("whatever", null);
588              shouldThrow();
589 <        } catch (NullPointerException e) {}
589 >        } catch (NullPointerException success) {}
590      }
591  
592      /**
# Line 504 | Line 597 | public class ConcurrentHashMapTest exten
597              ConcurrentHashMap c = new ConcurrentHashMap(5);
598              c.replace("whatever", null, "A");
599              shouldThrow();
600 <        } catch (NullPointerException e) {}
600 >        } catch (NullPointerException success) {}
601      }
602  
603      /**
# Line 515 | Line 608 | public class ConcurrentHashMapTest exten
608              ConcurrentHashMap c = new ConcurrentHashMap(5);
609              c.replace("whatever", one, null);
610              shouldThrow();
611 <        } catch (NullPointerException e) {}
611 >        } catch (NullPointerException success) {}
612      }
613  
521
614      /**
615       * remove(null) throws NPE
616       */
# Line 528 | Line 620 | public class ConcurrentHashMapTest exten
620              c.put("sadsdf", "asdads");
621              c.remove(null);
622              shouldThrow();
623 <        } catch (NullPointerException e) {}
623 >        } catch (NullPointerException success) {}
624      }
625  
626      /**
# Line 540 | Line 632 | public class ConcurrentHashMapTest exten
632              c.put("sadsdf", "asdads");
633              c.remove(null, "whatever");
634              shouldThrow();
635 <        } catch (NullPointerException e) {}
635 >        } catch (NullPointerException success) {}
636      }
637  
638      /**
639       * remove(x, null) returns false
640       */
641      public void testRemove3() {
642 <        try {
643 <            ConcurrentHashMap c = new ConcurrentHashMap(5);
644 <            c.put("sadsdf", "asdads");
553 <            assertFalse(c.remove("sadsdf", null));
554 <        } catch (NullPointerException e) {
555 <            fail();
556 <        }
642 >        ConcurrentHashMap c = new ConcurrentHashMap(5);
643 >        c.put("sadsdf", "asdads");
644 >        assertFalse(c.remove("sadsdf", null));
645      }
646  
647      /**
648       * A deserialized map equals original
649       */
650      public void testSerialization() throws Exception {
651 <        ConcurrentHashMap q = map5();
651 >        Map x = map5();
652 >        Map y = serialClone(x);
653  
654 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
655 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
656 <        out.writeObject(q);
657 <        out.close();
569 <
570 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
571 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
572 <        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
573 <        assertEquals(q.size(), r.size());
574 <        assertTrue(q.equals(r));
575 <        assertTrue(r.equals(q));
654 >        assertTrue(x != y);
655 >        assertEquals(x.size(), y.size());
656 >        assertEquals(x, y);
657 >        assertEquals(y, x);
658      }
659  
578
660      /**
661       * SetValue of an EntrySet entry sets value in the map.
662       */
# Line 587 | Line 668 | public class ConcurrentHashMapTest exten
668              map.put(new Integer(i), new Integer(i));
669          assertFalse(map.isEmpty());
670          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
671 <
672 <        // assert that entry1 is not 16
673 <        assertTrue("entry is 16, test not valid",
674 <                   !entry1.getKey().equals(new Integer(16)));
675 <
676 <        // remove 16 (a different key) from map
677 <        // which just happens to cause entry1 to be cloned in map
678 <        map.remove(new Integer(16));
679 <        entry1.setValue("XYZ");
599 <        assertTrue(map.containsValue("XYZ")); // fails
671 >        // Unless it happens to be first (in which case remainder of
672 >        // test is skipped), remove a possibly-colliding key from map
673 >        // which, under some implementations, may cause entry1 to be
674 >        // cloned in map
675 >        if (!entry1.getKey().equals(new Integer(16))) {
676 >            map.remove(new Integer(16));
677 >            entry1.setValue("XYZ");
678 >            assertTrue(map.containsValue("XYZ")); // fails if write-through broken
679 >        }
680      }
681  
682   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines