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.12 by dl, Sun Jul 21 22:24:18 2013 UTC vs.
Revision 1.23 by jsr166, Fri Feb 27 19:47:57 2015 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.concurrent.ConcurrentHashMap;
21 < import java.util.concurrent.ConcurrentHashMap.KeySetView;
21 > import java.util.concurrent.atomic.LongAdder;
22 > import java.util.function.BiFunction;
23 >
24 > import junit.framework.Test;
25 > import junit.framework.TestSuite;
26  
27   public class ConcurrentHashMap8Test extends JSR166TestCase {
28      public static void main(String[] args) {
# Line 54 | Line 67 | public class ConcurrentHashMap8Test exte
67      }
68  
69      /**
70 <     * computeIfAbsent does not replace  if the key is already present
70 >     * computeIfAbsent does not replace if the key is already present
71       */
72      public void testComputeIfAbsent2() {
73          ConcurrentHashMap map = map5();
# Line 71 | Line 84 | public class ConcurrentHashMap8Test exte
84      }
85  
86      /**
87 <     * computeIfPresent does not replace  if the key is already present
87 >     * computeIfPresent does not replace if the key is already present
88       */
89      public void testComputeIfPresent() {
90          ConcurrentHashMap map = map5();
# Line 88 | Line 101 | public class ConcurrentHashMap8Test exte
101      }
102  
103      /**
104 <     * compute does not replace  if the function returns null
104 >     * compute does not replace if the function returns null
105       */
106      public void testCompute() {
107          ConcurrentHashMap map = map5();
# Line 151 | Line 164 | public class ConcurrentHashMap8Test exte
164          assertTrue(a.isEmpty());
165          for (int i = 0; i < n; i++)
166              a.add(i);
167 <        assertFalse(a.isEmpty());
167 >        assertEquals(n == 0, a.isEmpty());
168          assertEquals(n, a.size());
169          return a;
170      }
# Line 166 | Line 179 | public class ConcurrentHashMap8Test exte
179          return a;
180      }
181  
182 <    /*
182 >    /**
183       * replaceAll replaces all matching values.
184       */
185      public void testReplaceAll() {
186          ConcurrentHashMap<Integer, String> map = map5();
187 <        map.replaceAll((x, y) -> {return x > 3 ? "Z" : y;});
187 >        map.replaceAll((x, y) -> { return x > 3 ? "Z" : y; });
188          assertEquals("A", map.get(one));
189          assertEquals("B", map.get(two));
190          assertEquals("C", map.get(three));
# Line 196 | Line 209 | public class ConcurrentHashMap8Test exte
209          Set set1 = map.keySet();
210          Set set2 = map.keySet(true);
211          set2.add(six);
212 <        assertTrue(((KeySetView)set2).getMap() == map);
213 <        assertTrue(((KeySetView)set1).getMap() == map);
212 >        assertTrue(((ConcurrentHashMap.KeySetView)set2).getMap() == map);
213 >        assertTrue(((ConcurrentHashMap.KeySetView)set1).getMap() == map);
214          assertEquals(set2.size(), map.size());
215          assertEquals(set1.size(), map.size());
216          assertTrue((Boolean)map.get(six));
# Line 209 | Line 222 | public class ConcurrentHashMap8Test exte
222          assertFalse(set2.contains(six));
223      }
224  
212
225      /**
226       * keySet.addAll adds each element from the given collection
227       */
228      public void testAddAll() {
229          Set full = populatedSet(3);
230 <        Vector v = new Vector();
231 <        v.add(three);
232 <        v.add(four);
221 <        v.add(five);
222 <        full.addAll(v);
230 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
231 >        assertEquals(6, full.size());
232 >        assertFalse(full.addAll(Arrays.asList(three, four, five)));
233          assertEquals(6, full.size());
234      }
235  
# Line 229 | Line 239 | public class ConcurrentHashMap8Test exte
239       */
240      public void testAddAll2() {
241          Set full = populatedSet(3);
242 <        Vector v = new Vector();
243 <        v.add(three);
244 <        v.add(four);
245 <        v.add(one); // will not add this element
236 <        full.addAll(v);
242 >        // "one" is duplicate and will not be added
243 >        assertTrue(full.addAll(Arrays.asList(three, four, one)));
244 >        assertEquals(5, full.size());
245 >        assertFalse(full.addAll(Arrays.asList(three, four, one)));
246          assertEquals(5, full.size());
247      }
248  
# Line 255 | Line 264 | public class ConcurrentHashMap8Test exte
264          assertTrue(full.contains(three));
265      }
266  
267 +    /**
268 +     * keySet.add throws UnsupportedOperationException if no default
269 +     * mapped value
270 +     */
271 +    public void testAdd4() {
272 +        Set full = map5().keySet();
273 +        try {
274 +            full.add(three);
275 +            shouldThrow();
276 +        } catch (UnsupportedOperationException e){}
277 +    }
278 +
279 +    /**
280 +     * keySet.add throws NullPointerException if the specified key is
281 +     * null
282 +     */
283 +    public void testAdd5() {
284 +        Set full = populatedSet(3);
285 +        try {
286 +            full.add(null);
287 +            shouldThrow();
288 +        } catch (NullPointerException e){}
289 +    }
290 +
291 +    /**
292 +     * KeySetView.getMappedValue returns the map's mapped value
293 +     */
294 +    public void testGetMappedValue() {
295 +        ConcurrentHashMap map = map5();
296 +        assertNull(map.keySet().getMappedValue());
297 +        try {
298 +            map.keySet(null);
299 +            shouldThrow();
300 +        } catch (NullPointerException e) {}
301 +        ConcurrentHashMap.KeySetView set = map.keySet(one);
302 +        set.add(one);
303 +        set.add(six);
304 +        set.add(seven);
305 +        assertTrue(set.getMappedValue() == one);
306 +        assertTrue(map.get(one) != one);
307 +        assertTrue(map.get(six) == one);
308 +        assertTrue(map.get(seven) == one);
309 +    }
310 +
311 +    void checkSpliteratorCharacteristics(Spliterator<?> sp,
312 +                                         int requiredCharacteristics) {
313 +        assertEquals(requiredCharacteristics,
314 +                     requiredCharacteristics & sp.characteristics());
315 +    }
316  
317 <      /**
318 <      * keySet.add throws UnsupportedOperationException if no default
319 <      * mapped value
320 <      */
321 <     public void testAdd4() {
322 <         Set full = map5().keySet();
323 <         try {
324 <             full.add(three);
325 <             shouldThrow();
326 <         } catch (UnsupportedOperationException e){}
327 <     }
328 <    
329 <     /**
330 <      * keySet.add throws NullPointerException if the specified key is
331 <      * null
332 <      */
333 <     public void testAdd5() {
276 <         Set full = populatedSet(3);
277 <         try {
278 <             full.add(null);
279 <             shouldThrow();
280 <         } catch (NullPointerException e){}
281 <     }
282 <    
283 <     /**
284 <      * KeySetView.getMappedValue returns the map's mapped value
285 <      */
286 <     public void testGetMappedValue() {
287 <         ConcurrentHashMap map = map5();
288 <         assertNull(map.keySet().getMappedValue());
289 <         try {
290 <             map.keySet(null);
291 <             shouldThrow();
292 <         } catch (NullPointerException e) {}
293 <         KeySetView set = map.keySet(one);
294 <         set.add(one);
295 <         set.add(six);
296 <         set.add(seven);
297 <         assertTrue(set.getMappedValue() == one);
298 <         assertTrue(map.get(one) != one);
299 <         assertTrue(map.get(six) == one);
300 <         assertTrue(map.get(seven) == one);
301 <     }
302 <    
303 <     /**
304 <      * KeySetView.spliterator returns spliterator over the elements in this set
305 <      */
306 <     public void testKeySetSpliterator() {
307 <         LongAdder adder = new LongAdder();
308 <         ConcurrentHashMap map = map5();
309 <         Set set = map.keySet();
310 <         Spliterator<Integer> sp = set.spliterator();
311 <         assertEquals(sp.estimateSize(), map.size());
312 <         Spliterator<Integer> sp2 = sp.trySplit();
313 <         sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
314 <         long v = adder.sumThenReset();
315 <         sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
316 <         long v2 = adder.sum();
317 <         assertEquals(v + v2, 15);
318 <     }
319 <
317 >    /**
318 >     * KeySetView.spliterator returns spliterator over the elements in this set
319 >     */
320 >    public void testKeySetSpliterator() {
321 >        LongAdder adder = new LongAdder();
322 >        ConcurrentHashMap map = map5();
323 >        Set set = map.keySet();
324 >        Spliterator<Integer> sp = set.spliterator();
325 >        checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
326 >        assertEquals(sp.estimateSize(), map.size());
327 >        Spliterator<Integer> sp2 = sp.trySplit();
328 >        sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
329 >        long v = adder.sumThenReset();
330 >        sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
331 >        long v2 = adder.sum();
332 >        assertEquals(v + v2, 15);
333 >    }
334  
335      /**
336       * keyset.clear removes all elements from the set
# Line 358 | Line 372 | public class ConcurrentHashMap8Test exte
372       * KeySet.containsAll returns true for collections with subset of elements
373       */
374      public void testContainsAll() {
375 <        Set full = populatedSet(3);
376 <        Vector v = new Vector();
377 <        v.add(one);
378 <        v.add(two);
379 <        assertTrue(full.containsAll(v));
380 <        v.add(six);
367 <        assertFalse(full.containsAll(v));
375 >        Collection full = populatedSet(3);
376 >        assertTrue(full.containsAll(Arrays.asList()));
377 >        assertTrue(full.containsAll(Arrays.asList(one)));
378 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
379 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
380 >        assertFalse(full.containsAll(Arrays.asList(six)));
381      }
382  
383      /**
384       * KeySet.isEmpty is true when empty, else false
385       */
386      public void testIsEmpty() {
387 <        Set empty = ConcurrentHashMap.newKeySet();
388 <        Set full = populatedSet(3);
376 <        assertTrue(empty.isEmpty());
377 <        assertFalse(full.isEmpty());
387 >        assertTrue(populatedSet(0).isEmpty());
388 >        assertFalse(populatedSet(3).isEmpty());
389      }
390  
391      /**
# Line 401 | Line 412 | public class ConcurrentHashMap8Test exte
412              assertTrue(it.hasNext());
413              it.next();
414          }
415 <        assertFalse(it.hasNext());
416 <        try {
417 <            it.next();
418 <            shouldThrow();
419 <        } catch (NoSuchElementException success) {}
415 >        assertIteratorExhausted(it);
416 >    }
417 >
418 >    /**
419 >     * iterator of empty collections has no elements
420 >     */
421 >    public void testEmptyIterator() {
422 >        assertIteratorExhausted(ConcurrentHashMap.newKeySet().iterator());
423 >        assertIteratorExhausted(new ConcurrentHashMap().entrySet().iterator());
424 >        assertIteratorExhausted(new ConcurrentHashMap().values().iterator());
425 >        assertIteratorExhausted(new ConcurrentHashMap().keySet().iterator());
426      }
427  
428      /**
# Line 439 | Line 456 | public class ConcurrentHashMap8Test exte
456       */
457      public void testRemoveAll() {
458          Set full = populatedSet(3);
459 <        Vector v = new Vector();
460 <        v.add(one);
461 <        v.add(two);
445 <        full.removeAll(v);
459 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
460 >        assertEquals(1, full.size());
461 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
462          assertEquals(1, full.size());
463      }
464  
# Line 533 | Line 549 | public class ConcurrentHashMap8Test exte
549  
550          assertNotSame(x, y);
551          assertEquals(x.size(), y.size());
536        assertEquals(x.toString(), y.toString());
537        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
552          assertEquals(x, y);
553          assertEquals(y, x);
554      }
# Line 986 | Line 1000 | public class ConcurrentHashMap8Test exte
1000      public void testSearchValuesSequentially() {
1001          ConcurrentHashMap<Long, Long> m = longMap();
1002          Long r;
1003 <        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null);
1003 >        r = m.searchValues(Long.MAX_VALUE,
1004 >            (Long x) -> (x.longValue() == (long)(SIZE/2)) ? x : null);
1005          assertEquals((long)r, (long)(SIZE/2));
1006 <        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
1006 >        r = m.searchValues(Long.MAX_VALUE,
1007 >            (Long x) -> (x.longValue() < 0L) ? x : null);
1008          assertNull(r);
1009      }
1010  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines