ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMap8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMap8Test.java (file contents):
Revision 1.9 by dl, Tue May 21 19:11:16 2013 UTC vs.
Revision 1.19 by jsr166, Wed Dec 31 21:35:01 2014 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.*;
9 < import java.util.function.*;
10 < import java.util.concurrent.atomic.LongAdder;
7 > import static java.util.Spliterator.CONCURRENT;
8 > import static java.util.Spliterator.DISTINCT;
9 > import static java.util.Spliterator.NONNULL;
10 >
11 > import java.util.AbstractMap;
12 > import java.util.Arrays;
13 > import java.util.Collection;
14 > import java.util.Collections;
15 > import java.util.Iterator;
16 > import java.util.Map;
17 > import java.util.NoSuchElementException;
18 > import java.util.Set;
19 > import java.util.Spliterator;
20 > import java.util.Vector;
21   import java.util.concurrent.ConcurrentHashMap;
22 + import java.util.concurrent.atomic.LongAdder;
23 + import java.util.function.BiFunction;
24 +
25 + import junit.framework.Test;
26 + import junit.framework.TestSuite;
27  
28   public class ConcurrentHashMap8Test extends JSR166TestCase {
29      public static void main(String[] args) {
# Line 53 | Line 68 | public class ConcurrentHashMap8Test exte
68      }
69  
70      /**
71 <     * computeIfAbsent does not replace  if the key is already present
71 >     * computeIfAbsent does not replace if the key is already present
72       */
73      public void testComputeIfAbsent2() {
74          ConcurrentHashMap map = map5();
# Line 70 | Line 85 | public class ConcurrentHashMap8Test exte
85      }
86  
87      /**
88 <     * computeIfPresent does not replace  if the key is already present
88 >     * computeIfPresent does not replace if the key is already present
89       */
90      public void testComputeIfPresent() {
91          ConcurrentHashMap map = map5();
# Line 87 | Line 102 | public class ConcurrentHashMap8Test exte
102      }
103  
104      /**
105 <     * compute does not replace  if the function returns null
105 >     * compute does not replace if the function returns null
106       */
107      public void testCompute() {
108          ConcurrentHashMap map = map5();
# Line 166 | Line 181 | public class ConcurrentHashMap8Test exte
181      }
182  
183      /**
184 +     * replaceAll replaces all matching values.
185 +     */
186 +    public void testReplaceAll() {
187 +        ConcurrentHashMap<Integer, String> map = map5();
188 +        map.replaceAll((x, y) -> { return x > 3 ? "Z" : y; });
189 +        assertEquals("A", map.get(one));
190 +        assertEquals("B", map.get(two));
191 +        assertEquals("C", map.get(three));
192 +        assertEquals("Z", map.get(four));
193 +        assertEquals("Z", map.get(five));
194 +    }
195 +
196 +    /**
197       * Default-constructed set is empty
198       */
199      public void testNewKeySet() {
# Line 174 | Line 202 | public class ConcurrentHashMap8Test exte
202      }
203  
204      /**
205 +     * keySet.add adds the key with the established value to the map;
206 +     * remove removes it.
207 +     */
208 +    public void testKeySetAddRemove() {
209 +        ConcurrentHashMap map = map5();
210 +        Set set1 = map.keySet();
211 +        Set set2 = map.keySet(true);
212 +        set2.add(six);
213 +        assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map);
214 +        assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map);
215 +        assertEquals(set2.size(), map.size());
216 +        assertEquals(set1.size(), map.size());
217 +        assertTrue((Boolean)map.get(six));
218 +        assertTrue(set1.contains(six));
219 +        assertTrue(set2.contains(six));
220 +        set2.remove(six);
221 +        assertNull(map.get(six));
222 +        assertFalse(set1.contains(six));
223 +        assertFalse(set2.contains(six));
224 +    }
225 +
226 +
227 +    /**
228       * keySet.addAll adds each element from the given collection
229       */
230      public void testAddAll() {
# Line 219 | Line 270 | public class ConcurrentHashMap8Test exte
270      }
271  
272      /**
273 +     * keySet.add throws UnsupportedOperationException if no default
274 +     * mapped value
275 +     */
276 +    public void testAdd4() {
277 +        Set full = map5().keySet();
278 +        try {
279 +            full.add(three);
280 +            shouldThrow();
281 +        } catch (UnsupportedOperationException e){}
282 +    }
283 +
284 +    /**
285 +     * keySet.add throws NullPointerException if the specified key is
286 +     * null
287 +     */
288 +    public void testAdd5() {
289 +        Set full = populatedSet(3);
290 +        try {
291 +            full.add(null);
292 +            shouldThrow();
293 +        } catch (NullPointerException e){}
294 +    }
295 +
296 +    /**
297 +     * KeySetView.getMappedValue returns the map's mapped value
298 +     */
299 +    public void testGetMappedValue() {
300 +        ConcurrentHashMap map = map5();
301 +        assertNull(map.keySet().getMappedValue());
302 +        try {
303 +            map.keySet(null);
304 +            shouldThrow();
305 +        } catch (NullPointerException e) {}
306 +        ConcurrentHashMap.KeySetView set = map.keySet(one);
307 +        set.add(one);
308 +        set.add(six);
309 +        set.add(seven);
310 +        assertTrue(set.getMappedValue() == one);
311 +        assertTrue(map.get(one) != one);
312 +        assertTrue(map.get(six) == one);
313 +        assertTrue(map.get(seven) == one);
314 +    }
315 +
316 +    void checkSpliteratorCharacteristics(Spliterator<?> sp,
317 +                                         int requiredCharacteristics) {
318 +        assertEquals(requiredCharacteristics,
319 +                     requiredCharacteristics & sp.characteristics());
320 +    }
321 +
322 +    /**
323 +     * KeySetView.spliterator returns spliterator over the elements in this set
324 +     */
325 +    public void testKeySetSpliterator() {
326 +        LongAdder adder = new LongAdder();
327 +        ConcurrentHashMap map = map5();
328 +        Set set = map.keySet();
329 +        Spliterator<Integer> sp = set.spliterator();
330 +        checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
331 +        assertEquals(sp.estimateSize(), map.size());
332 +        Spliterator<Integer> sp2 = sp.trySplit();
333 +        sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
334 +        long v = adder.sumThenReset();
335 +        sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
336 +        long v2 = adder.sum();
337 +        assertEquals(v + v2, 15);
338 +    }
339 +
340 +    /**
341       * keyset.clear removes all elements from the set
342       */
343      public void testClear() {
# Line 431 | Line 550 | public class ConcurrentHashMap8Test exte
550          Set x = populatedSet(size);
551          Set y = serialClone(x);
552  
553 <        assertTrue(x != y);
553 >        assertNotSame(x, y);
554          assertEquals(x.size(), y.size());
436        assertEquals(x.toString(), y.toString());
437        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
555          assertEquals(x, y);
556          assertEquals(y, x);
557      }
558  
442
559      static final int SIZE = 10000;
560      static ConcurrentHashMap<Long, Long> longMap;
561  
# Line 637 | Line 753 | public class ConcurrentHashMap8Test exte
753          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
754      }
755  
640
756      /**
757       * reduceKeysSequentially accumulates across all keys,
758       */
# Line 658 | Line 773 | public class ConcurrentHashMap8Test exte
773          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
774      }
775  
661
776      /**
777       * reduceEntriesSequentially accumulates across all entries
778       */
# Line 761 | Line 875 | public class ConcurrentHashMap8Test exte
875          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
876      }
877  
764
878      /**
879       * reduceKeysToLongSequentially accumulates mapped keys
880       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines