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.28 by jsr166, Sat Apr 25 04:55:30 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.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) {
29 <        junit.textui.TestRunner.run(suite());
29 >        main(suite(), args);
30      }
31      public static Test suite() {
32          return new TestSuite(ConcurrentHashMap8Test.class);
# Line 53 | 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 70 | 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 87 | 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 149 | Line 163 | public class ConcurrentHashMap8Test exte
163          Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet();
164          assertTrue(a.isEmpty());
165          for (int i = 0; i < n; i++)
166 <            a.add(i);
167 <        assertFalse(a.isEmpty());
166 >            assertTrue(a.add(i));
167 >        assertEquals(n == 0, a.isEmpty());
168          assertEquals(n, a.size());
169          return a;
170      }
# Line 159 | Line 173 | public class ConcurrentHashMap8Test exte
173          Set<Integer> a = ConcurrentHashMap.<Integer>newKeySet();
174          assertTrue(a.isEmpty());
175          for (int i = 0; i < elements.length; i++)
176 <            a.add(elements[i]);
176 >            assertTrue(a.add(elements[i]));
177          assertFalse(a.isEmpty());
178          assertEquals(elements.length, a.size());
179          return a;
180      }
181  
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; });
188 +        assertEquals("A", map.get(one));
189 +        assertEquals("B", map.get(two));
190 +        assertEquals("C", map.get(three));
191 +        assertEquals("Z", map.get(four));
192 +        assertEquals("Z", map.get(five));
193 +    }
194 +
195 +    /**
196       * Default-constructed set is empty
197       */
198      public void testNewKeySet() {
# Line 174 | Line 201 | public class ConcurrentHashMap8Test exte
201      }
202  
203      /**
204 +     * keySet.add adds the key with the established value to the map;
205 +     * remove removes it.
206 +     */
207 +    public void testKeySetAddRemove() {
208 +        ConcurrentHashMap map = map5();
209 +        Set set1 = map.keySet();
210 +        Set set2 = map.keySet(true);
211 +        set2.add(six);
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));
217 +        assertTrue(set1.contains(six));
218 +        assertTrue(set2.contains(six));
219 +        set2.remove(six);
220 +        assertNull(map.get(six));
221 +        assertFalse(set1.contains(six));
222 +        assertFalse(set2.contains(six));
223 +    }
224 +
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);
184 <        v.add(five);
185 <        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 192 | 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
199 <        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 205 | Line 251 | public class ConcurrentHashMap8Test exte
251       */
252      public void testAdd2() {
253          Set full = populatedSet(3);
254 <        full.add(one);
254 >        assertFalse(full.add(one));
255          assertEquals(3, full.size());
256      }
257  
# Line 214 | Line 260 | public class ConcurrentHashMap8Test exte
260       */
261      public void testAdd3() {
262          Set full = populatedSet(3);
263 <        full.add(three);
263 >        assertTrue(full.add(three));
264 >        assertTrue(full.contains(three));
265 >        assertFalse(full.add(three));
266          assertTrue(full.contains(three));
267      }
268  
269      /**
270 +     * keySet.add throws UnsupportedOperationException if no default
271 +     * mapped value
272 +     */
273 +    public void testAdd4() {
274 +        Set full = map5().keySet();
275 +        try {
276 +            full.add(three);
277 +            shouldThrow();
278 +        } catch (UnsupportedOperationException success) {}
279 +    }
280 +
281 +    /**
282 +     * keySet.add throws NullPointerException if the specified key is
283 +     * null
284 +     */
285 +    public void testAdd5() {
286 +        Set full = populatedSet(3);
287 +        try {
288 +            full.add(null);
289 +            shouldThrow();
290 +        } catch (NullPointerException success) {}
291 +    }
292 +
293 +    /**
294 +     * KeySetView.getMappedValue returns the map's mapped value
295 +     */
296 +    public void testGetMappedValue() {
297 +        ConcurrentHashMap map = map5();
298 +        assertNull(map.keySet().getMappedValue());
299 +        try {
300 +            map.keySet(null);
301 +            shouldThrow();
302 +        } catch (NullPointerException success) {}
303 +        ConcurrentHashMap.KeySetView set = map.keySet(one);
304 +        assertFalse(set.add(one));
305 +        assertTrue(set.add(six));
306 +        assertTrue(set.add(seven));
307 +        assertTrue(set.getMappedValue() == one);
308 +        assertTrue(map.get(one) != one);
309 +        assertTrue(map.get(six) == one);
310 +        assertTrue(map.get(seven) == one);
311 +    }
312 +
313 +    void checkSpliteratorCharacteristics(Spliterator<?> sp,
314 +                                         int requiredCharacteristics) {
315 +        assertEquals(requiredCharacteristics,
316 +                     requiredCharacteristics & sp.characteristics());
317 +    }
318 +
319 +    /**
320 +     * KeySetView.spliterator returns spliterator over the elements in this set
321 +     */
322 +    public void testKeySetSpliterator() {
323 +        LongAdder adder = new LongAdder();
324 +        ConcurrentHashMap map = map5();
325 +        Set set = map.keySet();
326 +        Spliterator<Integer> sp = set.spliterator();
327 +        checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
328 +        assertEquals(sp.estimateSize(), map.size());
329 +        Spliterator<Integer> sp2 = sp.trySplit();
330 +        sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
331 +        long v = adder.sumThenReset();
332 +        sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
333 +        long v2 = adder.sum();
334 +        assertEquals(v + v2, 15);
335 +    }
336 +
337 +    /**
338       * keyset.clear removes all elements from the set
339       */
340      public void testClear() {
# Line 258 | Line 374 | public class ConcurrentHashMap8Test exte
374       * KeySet.containsAll returns true for collections with subset of elements
375       */
376      public void testContainsAll() {
377 <        Set full = populatedSet(3);
378 <        Vector v = new Vector();
379 <        v.add(one);
380 <        v.add(two);
381 <        assertTrue(full.containsAll(v));
382 <        v.add(six);
267 <        assertFalse(full.containsAll(v));
377 >        Collection full = populatedSet(3);
378 >        assertTrue(full.containsAll(Arrays.asList()));
379 >        assertTrue(full.containsAll(Arrays.asList(one)));
380 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
381 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
382 >        assertFalse(full.containsAll(Arrays.asList(six)));
383      }
384  
385      /**
386       * KeySet.isEmpty is true when empty, else false
387       */
388      public void testIsEmpty() {
389 <        Set empty = ConcurrentHashMap.newKeySet();
390 <        Set full = populatedSet(3);
276 <        assertTrue(empty.isEmpty());
277 <        assertFalse(full.isEmpty());
389 >        assertTrue(populatedSet(0).isEmpty());
390 >        assertFalse(populatedSet(3).isEmpty());
391      }
392  
393      /**
# Line 301 | Line 414 | public class ConcurrentHashMap8Test exte
414              assertTrue(it.hasNext());
415              it.next();
416          }
417 <        assertFalse(it.hasNext());
418 <        try {
419 <            it.next();
420 <            shouldThrow();
421 <        } catch (NoSuchElementException success) {}
417 >        assertIteratorExhausted(it);
418 >    }
419 >
420 >    /**
421 >     * iterator of empty collections has no elements
422 >     */
423 >    public void testEmptyIterator() {
424 >        assertIteratorExhausted(ConcurrentHashMap.newKeySet().iterator());
425 >        assertIteratorExhausted(new ConcurrentHashMap().entrySet().iterator());
426 >        assertIteratorExhausted(new ConcurrentHashMap().values().iterator());
427 >        assertIteratorExhausted(new ConcurrentHashMap().keySet().iterator());
428      }
429  
430      /**
# Line 339 | Line 458 | public class ConcurrentHashMap8Test exte
458       */
459      public void testRemoveAll() {
460          Set full = populatedSet(3);
461 <        Vector v = new Vector();
462 <        v.add(one);
463 <        v.add(two);
345 <        full.removeAll(v);
461 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
462 >        assertEquals(1, full.size());
463 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
464          assertEquals(1, full.size());
465      }
466  
# Line 431 | Line 549 | public class ConcurrentHashMap8Test exte
549          Set x = populatedSet(size);
550          Set y = serialClone(x);
551  
552 <        assertTrue(x != y);
552 >        assertNotSame(x, y);
553          assertEquals(x.size(), y.size());
436        assertEquals(x.toString(), y.toString());
437        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
554          assertEquals(x, y);
555          assertEquals(y, x);
556      }
557  
442
558      static final int SIZE = 10000;
559      static ConcurrentHashMap<Long, Long> longMap;
560  
# Line 637 | Line 752 | public class ConcurrentHashMap8Test exte
752          assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
753      }
754  
640
755      /**
756       * reduceKeysSequentially accumulates across all keys,
757       */
# Line 658 | Line 772 | public class ConcurrentHashMap8Test exte
772          assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2);
773      }
774  
661
775      /**
776       * reduceEntriesSequentially accumulates across all entries
777       */
# Line 761 | Line 874 | public class ConcurrentHashMap8Test exte
874          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
875      }
876  
764
877      /**
878       * reduceKeysToLongSequentially accumulates mapped keys
879       */
# Line 890 | Line 1002 | public class ConcurrentHashMap8Test exte
1002      public void testSearchValuesSequentially() {
1003          ConcurrentHashMap<Long, Long> m = longMap();
1004          Long r;
1005 <        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null);
1005 >        r = m.searchValues(Long.MAX_VALUE,
1006 >            (Long x) -> (x.longValue() == (long)(SIZE/2)) ? x : null);
1007          assertEquals((long)r, (long)(SIZE/2));
1008 <        r = m.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null);
1008 >        r = m.searchValues(Long.MAX_VALUE,
1009 >            (Long x) -> (x.longValue() < 0L) ? x : null);
1010          assertNull(r);
1011      }
1012  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines