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.19 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.32 by jsr166, Wed Jun 12 18:09:00 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 =
88 >            new ConcurrentHashMap<BI, Boolean>();
89 >        for (int i = 0; i < 1000; i++) {
90 >            assertTrue(m.put(new CI(i), true) == null);
91 >        }
92 >        for (int i = 0; i < 1000; i++) {
93 >            assertTrue(m.containsKey(new CI(i)));
94 >            assertTrue(m.containsKey(new DI(i)));
95 >        }
96 >    }
97 >
98 >    /**
99 >     * Elements of classes with erased generic type parameters based
100 >     * on Comparable can be inserted and found.
101 >     */
102 >    public void testGenericComparable() {
103 >        ConcurrentHashMap<Object, Boolean> m =
104 >            new ConcurrentHashMap<Object, Boolean>();
105 >        for (int i = 0; i < 1000; i++) {
106 >            BI bi = new BI(i);
107 >            BS bs = new BS(String.valueOf(i));
108 >            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
109 >            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
110 >            assertTrue(m.putIfAbsent(bis, true) == null);
111 >            assertTrue(m.containsKey(bis));
112 >            if (m.putIfAbsent(bss, true) == null)
113 >                assertTrue(m.containsKey(bss));
114 >            assertTrue(m.containsKey(bis));
115 >        }
116 >        for (int i = 0; i < 1000; i++) {
117 >            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
118 >        }
119 >    }
120 >
121 >    /**
122 >     * Elements of non-comparable classes equal to those of classes
123 >     * with erased generic type parameters based on Comparable can be
124 >     * inserted and found.
125 >     */
126 >    public void testGenericComparable2() {
127 >        ConcurrentHashMap<Object, Boolean> m =
128 >            new ConcurrentHashMap<Object, Boolean>();
129 >        for (int i = 0; i < 1000; i++) {
130 >            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
131 >        }
132 >
133 >        for (int i = 0; i < 1000; i++) {
134 >            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
135 >            assertTrue(m.containsKey(bis));
136 >        }
137 >    }
138 >
139 >    /**
140 >     * clear removes all pairs
141       */
142      public void testClear() {
143          ConcurrentHashMap map = map5();
144          map.clear();
145 <        assertEquals(map.size(), 0);
145 >        assertEquals(0, map.size());
146      }
147  
148      /**
149 <     *  Maps with same contents are equal
149 >     * Maps with same contents are equal
150       */
151      public void testEquals() {
152          ConcurrentHashMap map1 = map5();
# Line 59 | Line 159 | public class ConcurrentHashMapTest exten
159      }
160  
161      /**
162 <     *  contains returns true for contained value
162 >     * contains returns true for contained value
163       */
164      public void testContains() {
165          ConcurrentHashMap map = map5();
# Line 68 | Line 168 | public class ConcurrentHashMapTest exten
168      }
169  
170      /**
171 <     *  containsKey returns true for contained key
171 >     * containsKey returns true for contained key
172       */
173      public void testContainsKey() {
174          ConcurrentHashMap map = map5();
# Line 77 | Line 177 | public class ConcurrentHashMapTest exten
177      }
178  
179      /**
180 <     *  containsValue returns true for held values
180 >     * containsValue returns true for held values
181       */
182      public void testContainsValue() {
183          ConcurrentHashMap map = map5();
# Line 86 | Line 186 | public class ConcurrentHashMapTest exten
186      }
187  
188      /**
189 <     *   enumeration returns an enumeration containing the correct
190 <     *   elements
189 >     * enumeration returns an enumeration containing the correct
190 >     * elements
191       */
192      public void testEnumeration() {
193          ConcurrentHashMap map = map5();
# Line 101 | Line 201 | public class ConcurrentHashMapTest exten
201      }
202  
203      /**
204 <     *  get returns the correct element at the given key,
205 <     *  or null if not present
204 >     * get returns the correct element at the given key,
205 >     * or null if not present
206       */
207      public void testGet() {
208          ConcurrentHashMap map = map5();
# Line 112 | Line 212 | public class ConcurrentHashMapTest exten
212      }
213  
214      /**
215 <     *  isEmpty is true of empty map and false for non-empty
215 >     * isEmpty is true of empty map and false for non-empty
216       */
217      public void testIsEmpty() {
218          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 122 | Line 222 | public class ConcurrentHashMapTest exten
222      }
223  
224      /**
225 <     *   keys returns an enumeration containing all the keys from the map
225 >     * keys returns an enumeration containing all the keys from the map
226       */
227      public void testKeys() {
228          ConcurrentHashMap map = map5();
# Line 136 | Line 236 | public class ConcurrentHashMapTest exten
236      }
237  
238      /**
239 <     *   keySet returns a Set containing all the keys
239 >     * keySet returns a Set containing all the keys
240       */
241      public void testKeySet() {
242          ConcurrentHashMap map = map5();
# Line 150 | Line 250 | public class ConcurrentHashMapTest exten
250      }
251  
252      /**
253 <     *  keySet.toArray returns contains all keys
253 >     * keySet.toArray returns contains all keys
254       */
255      public void testKeySetToArray() {
256          ConcurrentHashMap map = map5();
# Line 163 | Line 263 | public class ConcurrentHashMapTest exten
263      }
264  
265      /**
266 <     *  Values.toArray contains all values
266 >     * Values.toArray contains all values
267       */
268      public void testValuesToArray() {
269          ConcurrentHashMap map = map5();
# Line 179 | Line 279 | public class ConcurrentHashMapTest exten
279      }
280  
281      /**
282 <     *  entrySet.toArray contains all entries
282 >     * entrySet.toArray contains all entries
283       */
284      public void testEntrySetToArray() {
285          ConcurrentHashMap map = map5();
# Line 226 | Line 326 | public class ConcurrentHashMapTest exten
326      }
327  
328      /**
329 <     *   putAll  adds all key-value pairs from the given map
329 >     * putAll adds all key-value pairs from the given map
330       */
331      public void testPutAll() {
332          ConcurrentHashMap empty = new ConcurrentHashMap();
# Line 241 | Line 341 | public class ConcurrentHashMapTest exten
341      }
342  
343      /**
344 <     *   putIfAbsent works when the given key is not present
344 >     * putIfAbsent works when the given key is not present
345       */
346      public void testPutIfAbsent() {
347          ConcurrentHashMap map = map5();
# Line 250 | Line 350 | public class ConcurrentHashMapTest exten
350      }
351  
352      /**
353 <     *   putIfAbsent does not add the pair if the key is already present
353 >     * putIfAbsent does not add the pair if the key is already present
354       */
355      public void testPutIfAbsent2() {
356          ConcurrentHashMap map = map5();
# Line 258 | Line 358 | public class ConcurrentHashMapTest exten
358      }
359  
360      /**
361 <     *   replace fails when the given key is not present
361 >     * replace fails when the given key is not present
362       */
363      public void testReplace() {
364          ConcurrentHashMap map = map5();
# Line 267 | Line 367 | public class ConcurrentHashMapTest exten
367      }
368  
369      /**
370 <     *   replace succeeds if the key is already present
370 >     * replace succeeds if the key is already present
371       */
372      public void testReplace2() {
373          ConcurrentHashMap map = map5();
# Line 275 | Line 375 | public class ConcurrentHashMapTest exten
375          assertEquals("Z", map.get(one));
376      }
377  
278
378      /**
379       * replace value fails when the given key not mapped to expected value
380       */
# Line 296 | Line 395 | public class ConcurrentHashMapTest exten
395          assertEquals("Z", map.get(one));
396      }
397  
299
398      /**
399 <     *   remove removes the correct key-value pair from the map
399 >     * remove removes the correct key-value pair from the map
400       */
401      public void testRemove() {
402          ConcurrentHashMap map = map5();
# Line 318 | Line 416 | public class ConcurrentHashMapTest exten
416          map.remove(four, "A");
417          assertEquals(4, map.size());
418          assertTrue(map.containsKey(four));
321
419      }
420  
421      /**
422 <     *   size returns the correct values
422 >     * size returns the correct values
423       */
424      public void testSize() {
425          ConcurrentHashMap map = map5();
# Line 338 | Line 435 | public class ConcurrentHashMapTest exten
435          ConcurrentHashMap map = map5();
436          String s = map.toString();
437          for (int i = 1; i <= 5; ++i) {
438 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
438 >            assertTrue(s.contains(String.valueOf(i)));
439          }
440      }
441  
# Line 484 | Line 581 | public class ConcurrentHashMapTest exten
581          } catch (NullPointerException success) {}
582      }
583  
487
584      /**
585       * replace(x, null) throws NPE
586       */
# Line 518 | Line 614 | public class ConcurrentHashMapTest exten
614          } catch (NullPointerException success) {}
615      }
616  
521
617      /**
618       * remove(null) throws NPE
619       */
# Line 556 | Line 651 | public class ConcurrentHashMapTest exten
651       * A deserialized map equals original
652       */
653      public void testSerialization() throws Exception {
654 <        ConcurrentHashMap q = map5();
654 >        Map x = map5();
655 >        Map y = serialClone(x);
656  
657 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
658 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
659 <        out.writeObject(q);
660 <        out.close();
565 <
566 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
567 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
568 <        ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
569 <        assertEquals(q.size(), r.size());
570 <        assertTrue(q.equals(r));
571 <        assertTrue(r.equals(q));
657 >        assertNotSame(x, y);
658 >        assertEquals(x.size(), y.size());
659 >        assertEquals(x, y);
660 >        assertEquals(y, x);
661      }
662  
574
663      /**
664       * SetValue of an EntrySet entry sets value in the map.
665       */
# Line 583 | Line 671 | public class ConcurrentHashMapTest exten
671              map.put(new Integer(i), new Integer(i));
672          assertFalse(map.isEmpty());
673          Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
674 <
675 <        // assert that entry1 is not 16
676 <        assertTrue("entry is 16, test not valid",
677 <                   !entry1.getKey().equals(new Integer(16)));
678 <
679 <        // remove 16 (a different key) from map
680 <        // which just happens to cause entry1 to be cloned in map
681 <        map.remove(new Integer(16));
682 <        entry1.setValue("XYZ");
595 <        assertTrue(map.containsValue("XYZ")); // fails
674 >        // Unless it happens to be first (in which case remainder of
675 >        // test is skipped), remove a possibly-colliding key from map
676 >        // which, under some implementations, may cause entry1 to be
677 >        // cloned in map
678 >        if (!entry1.getKey().equals(new Integer(16))) {
679 >            map.remove(new Integer(16));
680 >            entry1.setValue("XYZ");
681 >            assertTrue(map.containsValue("XYZ")); // fails if write-through broken
682 >        }
683      }
684  
685   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines