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.58 by jsr166, Mon Jan 8 03:37:59 2018 UTC vs.
Revision 1.66 by jsr166, Wed Jan 27 02:55:18 2021 UTC

# Line 28 | Line 28 | public class ConcurrentHashMapTest exten
28          class Implementation implements MapImplementation {
29              public Class<?> klazz() { return ConcurrentHashMap.class; }
30              public Map emptyMap() { return new ConcurrentHashMap(); }
31            public Object makeKey(int i) { return i; }
32            public Object makeValue(int i) { return i; }
31              public boolean isConcurrent() { return true; }
32              public boolean permitsNullKeys() { return false; }
33              public boolean permitsNullValues() { return false; }
# Line 41 | Line 39 | public class ConcurrentHashMapTest exten
39      }
40  
41      /**
42 <     * Returns a new map from Integers 1-5 to Strings "A"-"E".
42 >     * Returns a new map from Items 1-5 to Strings "A"-"E".
43       */
44 <    private static ConcurrentHashMap<Integer, String> map5() {
45 <        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
44 >    private static ConcurrentHashMap<Item, String> map5() {
45 >        ConcurrentHashMap<Item, String> map = new ConcurrentHashMap<>(5);
46          assertTrue(map.isEmpty());
47          map.put(one, "A");
48          map.put(two, "B");
# Line 52 | Line 50 | public class ConcurrentHashMapTest exten
50          map.put(four, "D");
51          map.put(five, "E");
52          assertFalse(map.isEmpty());
53 <        assertEquals(5, map.size());
53 >        mustEqual(5, map.size());
54          return map;
55      }
56  
# Line 198 | Line 196 | public class ConcurrentHashMapTest exten
196          }
197          int count = 0;
198          for (Object k : map.keySet()) {
199 <            assertEquals(map.get(k), k);
199 >            mustEqual(map.get(k), k);
200              ++count;
201          }
202 <        assertEquals(count, size);
203 <        assertEquals(map.size(), size);
202 >        mustEqual(count, size);
203 >        mustEqual(map.size(), size);
204          for (Object k : map.keySet()) {
205 <            assertEquals(map.put(k, k), k);
205 >            mustEqual(map.put(k, k), k);
206          }
207      }
208  
# Line 212 | Line 210 | public class ConcurrentHashMapTest exten
210       * clear removes all pairs
211       */
212      public void testClear() {
213 <        ConcurrentHashMap map = map5();
213 >        ConcurrentHashMap<Item,String> map = map5();
214          map.clear();
215 <        assertEquals(0, map.size());
215 >        mustEqual(0, map.size());
216      }
217  
218      /**
219       * Maps with same contents are equal
220       */
221      public void testEquals() {
222 <        ConcurrentHashMap map1 = map5();
223 <        ConcurrentHashMap map2 = map5();
224 <        assertEquals(map1, map2);
225 <        assertEquals(map2, map1);
222 >        ConcurrentHashMap<Item,String> map1 = map5();
223 >        ConcurrentHashMap<Item,String> map2 = map5();
224 >        mustEqual(map1, map2);
225 >        mustEqual(map2, map1);
226          map1.clear();
227          assertFalse(map1.equals(map2));
228          assertFalse(map2.equals(map1));
# Line 234 | Line 232 | public class ConcurrentHashMapTest exten
232       * hashCode() equals sum of each key.hashCode ^ value.hashCode
233       */
234      public void testHashCode() {
235 <        ConcurrentHashMap<Integer,String> map = map5();
235 >        ConcurrentHashMap<Item,String> map = map5();
236          int sum = 0;
237 <        for (Map.Entry<Integer,String> e : map.entrySet())
237 >        for (Map.Entry<Item,String> e : map.entrySet())
238              sum += e.getKey().hashCode() ^ e.getValue().hashCode();
239 <        assertEquals(sum, map.hashCode());
239 >        mustEqual(sum, map.hashCode());
240      }
241  
242      /**
243       * contains returns true for contained value
244       */
245      public void testContains() {
246 <        ConcurrentHashMap map = map5();
246 >        ConcurrentHashMap<Item,String> map = map5();
247          assertTrue(map.contains("A"));
248          assertFalse(map.contains("Z"));
249      }
# Line 254 | Line 252 | public class ConcurrentHashMapTest exten
252       * containsKey returns true for contained key
253       */
254      public void testContainsKey() {
255 <        ConcurrentHashMap map = map5();
255 >        ConcurrentHashMap<Item,String> map = map5();
256          assertTrue(map.containsKey(one));
257          assertFalse(map.containsKey(zero));
258      }
# Line 263 | Line 261 | public class ConcurrentHashMapTest exten
261       * containsValue returns true for held values
262       */
263      public void testContainsValue() {
264 <        ConcurrentHashMap map = map5();
264 >        ConcurrentHashMap<Item,String> map = map5();
265          assertTrue(map.containsValue("A"));
266          assertFalse(map.containsValue("Z"));
267      }
# Line 273 | Line 271 | public class ConcurrentHashMapTest exten
271       * elements
272       */
273      public void testEnumeration() {
274 <        ConcurrentHashMap map = map5();
275 <        Enumeration e = map.elements();
274 >        ConcurrentHashMap<Item,String> map = map5();
275 >        Enumeration<String> e = map.elements();
276          int count = 0;
277          while (e.hasMoreElements()) {
278              count++;
279              e.nextElement();
280          }
281 <        assertEquals(5, count);
281 >        mustEqual(5, count);
282      }
283  
284      /**
285       * get returns the correct element at the given key,
286       * or null if not present
287       */
288 +    @SuppressWarnings("CollectionIncompatibleType")
289      public void testGet() {
290 <        ConcurrentHashMap map = map5();
291 <        assertEquals("A", (String)map.get(one));
292 <        ConcurrentHashMap empty = new ConcurrentHashMap();
290 >        ConcurrentHashMap<Item,String> map = map5();
291 >        mustEqual("A", map.get(one));
292 >        ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<>();
293          assertNull(map.get("anything"));
294          assertNull(empty.get("anything"));
295      }
# Line 299 | Line 298 | public class ConcurrentHashMapTest exten
298       * isEmpty is true of empty map and false for non-empty
299       */
300      public void testIsEmpty() {
301 <        ConcurrentHashMap empty = new ConcurrentHashMap();
302 <        ConcurrentHashMap map = map5();
301 >        ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<>();
302 >        ConcurrentHashMap<Item,String> map = map5();
303          assertTrue(empty.isEmpty());
304          assertFalse(map.isEmpty());
305      }
# Line 309 | Line 308 | public class ConcurrentHashMapTest exten
308       * keys returns an enumeration containing all the keys from the map
309       */
310      public void testKeys() {
311 <        ConcurrentHashMap map = map5();
312 <        Enumeration e = map.keys();
311 >        ConcurrentHashMap<Item,String> map = map5();
312 >        Enumeration<Item> e = map.keys();
313          int count = 0;
314          while (e.hasMoreElements()) {
315              count++;
316              e.nextElement();
317          }
318 <        assertEquals(5, count);
318 >        mustEqual(5, count);
319      }
320  
321      /**
322       * keySet returns a Set containing all the keys
323       */
324      public void testKeySet() {
325 <        ConcurrentHashMap map = map5();
326 <        Set s = map.keySet();
327 <        assertEquals(5, s.size());
328 <        assertTrue(s.contains(one));
329 <        assertTrue(s.contains(two));
330 <        assertTrue(s.contains(three));
331 <        assertTrue(s.contains(four));
332 <        assertTrue(s.contains(five));
325 >        ConcurrentHashMap<Item,String> map = map5();
326 >        Set<Item> s = map.keySet();
327 >        mustEqual(5, s.size());
328 >        mustContain(s, one);
329 >        mustContain(s, two);
330 >        mustContain(s, three);
331 >        mustContain(s, four);
332 >        mustContain(s, five);
333      }
334  
335      /**
336       * Test keySet().removeAll on empty map
337       */
338      public void testKeySet_empty_removeAll() {
339 <        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
340 <        Set<Integer> set = map.keySet();
339 >        ConcurrentHashMap<Item, String> map = new ConcurrentHashMap<>();
340 >        Set<Item> set = map.keySet();
341          set.removeAll(Collections.emptyList());
342          assertTrue(map.isEmpty());
343          assertTrue(set.isEmpty());
# Line 352 | Line 351 | public class ConcurrentHashMapTest exten
351       * keySet.toArray returns contains all keys
352       */
353      public void testKeySetToArray() {
354 <        ConcurrentHashMap map = map5();
355 <        Set s = map.keySet();
354 >        ConcurrentHashMap<Item,String> map = map5();
355 >        Set<Item> s = map.keySet();
356          Object[] ar = s.toArray();
357          assertTrue(s.containsAll(Arrays.asList(ar)));
358 <        assertEquals(5, ar.length);
359 <        ar[0] = m10;
358 >        mustEqual(5, ar.length);
359 >        ar[0] = minusTen;
360          assertFalse(s.containsAll(Arrays.asList(ar)));
361      }
362  
# Line 365 | Line 364 | public class ConcurrentHashMapTest exten
364       * Values.toArray contains all values
365       */
366      public void testValuesToArray() {
367 <        ConcurrentHashMap map = map5();
368 <        Collection v = map.values();
369 <        Object[] ar = v.toArray();
370 <        ArrayList s = new ArrayList(Arrays.asList(ar));
371 <        assertEquals(5, ar.length);
367 >        ConcurrentHashMap<Item,String> map = map5();
368 >        Collection<String> v = map.values();
369 >        String[] ar = v.toArray(new String[0]);
370 >        ArrayList<String> s = new ArrayList<>(Arrays.asList(ar));
371 >        mustEqual(5, ar.length);
372          assertTrue(s.contains("A"));
373          assertTrue(s.contains("B"));
374          assertTrue(s.contains("C"));
# Line 381 | Line 380 | public class ConcurrentHashMapTest exten
380       * entrySet.toArray contains all entries
381       */
382      public void testEntrySetToArray() {
383 <        ConcurrentHashMap map = map5();
384 <        Set s = map.entrySet();
383 >        ConcurrentHashMap<Item,String> map = map5();
384 >        Set<Map.Entry<Item,String>> s = map.entrySet();
385          Object[] ar = s.toArray();
386 <        assertEquals(5, ar.length);
386 >        mustEqual(5, ar.length);
387          for (int i = 0; i < 5; ++i) {
388              assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
389              assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
# Line 395 | Line 394 | public class ConcurrentHashMapTest exten
394       * values collection contains all values
395       */
396      public void testValues() {
397 <        ConcurrentHashMap map = map5();
398 <        Collection s = map.values();
399 <        assertEquals(5, s.size());
397 >        ConcurrentHashMap<Item,String> map = map5();
398 >        Collection<String> s = map.values();
399 >        mustEqual(5, s.size());
400          assertTrue(s.contains("A"));
401          assertTrue(s.contains("B"));
402          assertTrue(s.contains("C"));
# Line 409 | Line 408 | public class ConcurrentHashMapTest exten
408       * entrySet contains all pairs
409       */
410      public void testEntrySet() {
411 <        ConcurrentHashMap map = map5();
412 <        Set s = map.entrySet();
413 <        assertEquals(5, s.size());
414 <        Iterator it = s.iterator();
411 >        ConcurrentHashMap<Item, String> map = map5();
412 >        Set<Map.Entry<Item,String>> s = map.entrySet();
413 >        mustEqual(5, s.size());
414 >        Iterator<Map.Entry<Item,String>> it = s.iterator();
415          while (it.hasNext()) {
416 <            Map.Entry e = (Map.Entry) it.next();
416 >            Map.Entry<Item,String> e = it.next();
417              assertTrue(
418                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
419                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
# Line 428 | Line 427 | public class ConcurrentHashMapTest exten
427       * putAll adds all key-value pairs from the given map
428       */
429      public void testPutAll() {
430 <        ConcurrentHashMap empty = new ConcurrentHashMap();
431 <        ConcurrentHashMap map = map5();
432 <        empty.putAll(map);
433 <        assertEquals(5, empty.size());
434 <        assertTrue(empty.containsKey(one));
435 <        assertTrue(empty.containsKey(two));
436 <        assertTrue(empty.containsKey(three));
437 <        assertTrue(empty.containsKey(four));
438 <        assertTrue(empty.containsKey(five));
430 >        ConcurrentHashMap<Item,String> p = new ConcurrentHashMap<>();
431 >        ConcurrentHashMap<Item,String> map = map5();
432 >        p.putAll(map);
433 >        mustEqual(5, p.size());
434 >        assertTrue(p.containsKey(one));
435 >        assertTrue(p.containsKey(two));
436 >        assertTrue(p.containsKey(three));
437 >        assertTrue(p.containsKey(four));
438 >        assertTrue(p.containsKey(five));
439      }
440  
441      /**
442       * putIfAbsent works when the given key is not present
443       */
444      public void testPutIfAbsent() {
445 <        ConcurrentHashMap map = map5();
445 >        ConcurrentHashMap<Item,String> map = map5();
446          map.putIfAbsent(six, "Z");
447          assertTrue(map.containsKey(six));
448      }
# Line 452 | Line 451 | public class ConcurrentHashMapTest exten
451       * putIfAbsent does not add the pair if the key is already present
452       */
453      public void testPutIfAbsent2() {
454 <        ConcurrentHashMap map = map5();
455 <        assertEquals("A", map.putIfAbsent(one, "Z"));
454 >        ConcurrentHashMap<Item,String> map = map5();
455 >        mustEqual("A", map.putIfAbsent(one, "Z"));
456      }
457  
458      /**
459       * replace fails when the given key is not present
460       */
461      public void testReplace() {
462 <        ConcurrentHashMap map = map5();
462 >        ConcurrentHashMap<Item,String> map = map5();
463          assertNull(map.replace(six, "Z"));
464          assertFalse(map.containsKey(six));
465      }
# Line 469 | Line 468 | public class ConcurrentHashMapTest exten
468       * replace succeeds if the key is already present
469       */
470      public void testReplace2() {
471 <        ConcurrentHashMap map = map5();
471 >        ConcurrentHashMap<Item,String> map = map5();
472          assertNotNull(map.replace(one, "Z"));
473 <        assertEquals("Z", map.get(one));
473 >        mustEqual("Z", map.get(one));
474      }
475  
476      /**
477       * replace value fails when the given key not mapped to expected value
478       */
479      public void testReplaceValue() {
480 <        ConcurrentHashMap map = map5();
481 <        assertEquals("A", map.get(one));
480 >        ConcurrentHashMap<Item,String> map = map5();
481 >        mustEqual("A", map.get(one));
482          assertFalse(map.replace(one, "Z", "Z"));
483 <        assertEquals("A", map.get(one));
483 >        mustEqual("A", map.get(one));
484      }
485  
486      /**
487       * replace value succeeds when the given key mapped to expected value
488       */
489      public void testReplaceValue2() {
490 <        ConcurrentHashMap map = map5();
491 <        assertEquals("A", map.get(one));
490 >        ConcurrentHashMap<Item,String> map = map5();
491 >        mustEqual("A", map.get(one));
492          assertTrue(map.replace(one, "A", "Z"));
493 <        assertEquals("Z", map.get(one));
493 >        mustEqual("Z", map.get(one));
494      }
495  
496      /**
497       * remove removes the correct key-value pair from the map
498       */
499      public void testRemove() {
500 <        ConcurrentHashMap map = map5();
500 >        ConcurrentHashMap<Item,String> map = map5();
501          map.remove(five);
502 <        assertEquals(4, map.size());
502 >        mustEqual(4, map.size());
503          assertFalse(map.containsKey(five));
504      }
505  
# Line 508 | Line 507 | public class ConcurrentHashMapTest exten
507       * remove(key,value) removes only if pair present
508       */
509      public void testRemove2() {
510 <        ConcurrentHashMap map = map5();
510 >        ConcurrentHashMap<Item,String> map = map5();
511          map.remove(five, "E");
512 <        assertEquals(4, map.size());
512 >        mustEqual(4, map.size());
513          assertFalse(map.containsKey(five));
514          map.remove(four, "A");
515 <        assertEquals(4, map.size());
515 >        mustEqual(4, map.size());
516          assertTrue(map.containsKey(four));
517      }
518  
# Line 521 | Line 520 | public class ConcurrentHashMapTest exten
520       * size returns the correct values
521       */
522      public void testSize() {
523 <        ConcurrentHashMap map = map5();
524 <        ConcurrentHashMap empty = new ConcurrentHashMap();
525 <        assertEquals(0, empty.size());
526 <        assertEquals(5, map.size());
523 >        ConcurrentHashMap<Item,String> map = map5();
524 >        ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<>();
525 >        mustEqual(0, empty.size());
526 >        mustEqual(5, map.size());
527      }
528  
529      /**
530       * toString contains toString of elements
531       */
532      public void testToString() {
533 <        ConcurrentHashMap map = map5();
533 >        ConcurrentHashMap<Item,String> map = map5();
534          String s = map.toString();
535          for (int i = 1; i <= 5; ++i) {
536              assertTrue(s.contains(String.valueOf(i)));
# Line 545 | Line 544 | public class ConcurrentHashMapTest exten
544       */
545      public void testConstructor1() {
546          try {
547 <            new ConcurrentHashMap(-1);
547 >            new ConcurrentHashMap<Item,String>(-1);
548              shouldThrow();
549          } catch (IllegalArgumentException success) {}
550      }
# Line 556 | Line 555 | public class ConcurrentHashMapTest exten
555       */
556      public void testConstructor2() {
557          try {
558 <            new ConcurrentHashMap(-1, .75f);
558 >            new ConcurrentHashMap<Item,String>(-1, .75f);
559              shouldThrow();
560          } catch (IllegalArgumentException success) {}
561  
562          try {
563 <            new ConcurrentHashMap(16, -1);
563 >            new ConcurrentHashMap<Item,String>(16, -1);
564              shouldThrow();
565          } catch (IllegalArgumentException success) {}
566      }
# Line 572 | Line 571 | public class ConcurrentHashMapTest exten
571       */
572      public void testConstructor3() {
573          try {
574 <            new ConcurrentHashMap(-1, .75f, 1);
574 >            new ConcurrentHashMap<Item,String>(-1, .75f, 1);
575              shouldThrow();
576          } catch (IllegalArgumentException success) {}
577  
578          try {
579 <            new ConcurrentHashMap(16, -1, 1);
579 >            new ConcurrentHashMap<Item,String>(16, -1, 1);
580              shouldThrow();
581          } catch (IllegalArgumentException success) {}
582  
583          try {
584 <            new ConcurrentHashMap(16, .75f, -1);
584 >            new ConcurrentHashMap<Item,String>(16, .75f, -1);
585              shouldThrow();
586          } catch (IllegalArgumentException success) {}
587      }
# Line 593 | Line 592 | public class ConcurrentHashMapTest exten
592       */
593      public void testConstructor4() {
594          try {
595 <            new ConcurrentHashMap(null);
595 >            new ConcurrentHashMap<Item,String>(null);
596              shouldThrow();
597          } catch (NullPointerException success) {}
598      }
# Line 603 | Line 602 | public class ConcurrentHashMapTest exten
602       * as the given map
603       */
604      public void testConstructor5() {
605 <        ConcurrentHashMap map1 = map5();
606 <        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
605 >        ConcurrentHashMap<Item,String> map1 = map5();
606 >        ConcurrentHashMap<Item,String> map2 = new ConcurrentHashMap<>(map1);
607          assertTrue(map2.equals(map1));
608          map2.put(one, "F");
609          assertFalse(map2.equals(map1));
# Line 614 | Line 613 | public class ConcurrentHashMapTest exten
613       * get(null) throws NPE
614       */
615      public void testGet_NullPointerException() {
616 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
616 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
617          try {
618              c.get(null);
619              shouldThrow();
# Line 625 | Line 624 | public class ConcurrentHashMapTest exten
624       * containsKey(null) throws NPE
625       */
626      public void testContainsKey_NullPointerException() {
627 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
627 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
628          try {
629              c.containsKey(null);
630              shouldThrow();
# Line 636 | Line 635 | public class ConcurrentHashMapTest exten
635       * containsValue(null) throws NPE
636       */
637      public void testContainsValue_NullPointerException() {
638 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
638 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
639          try {
640              c.containsValue(null);
641              shouldThrow();
# Line 647 | Line 646 | public class ConcurrentHashMapTest exten
646       * contains(null) throws NPE
647       */
648      public void testContains_NullPointerException() {
649 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
649 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
650          try {
651              c.contains(null);
652              shouldThrow();
# Line 658 | Line 657 | public class ConcurrentHashMapTest exten
657       * put(null,x) throws NPE
658       */
659      public void testPut1_NullPointerException() {
660 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
660 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
661          try {
662              c.put(null, "whatever");
663              shouldThrow();
# Line 669 | Line 668 | public class ConcurrentHashMapTest exten
668       * put(x, null) throws NPE
669       */
670      public void testPut2_NullPointerException() {
671 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
671 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
672          try {
673 <            c.put("whatever", null);
673 >            c.put(zero, null);
674              shouldThrow();
675          } catch (NullPointerException success) {}
676      }
# Line 680 | Line 679 | public class ConcurrentHashMapTest exten
679       * putIfAbsent(null, x) throws NPE
680       */
681      public void testPutIfAbsent1_NullPointerException() {
682 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
682 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
683          try {
684              c.putIfAbsent(null, "whatever");
685              shouldThrow();
# Line 691 | Line 690 | public class ConcurrentHashMapTest exten
690       * replace(null, x) throws NPE
691       */
692      public void testReplace_NullPointerException() {
693 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
693 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
694          try {
695              c.replace(null, "whatever");
696              shouldThrow();
# Line 702 | Line 701 | public class ConcurrentHashMapTest exten
701       * replace(null, x, y) throws NPE
702       */
703      public void testReplaceValue_NullPointerException() {
704 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
704 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
705          try {
706 <            c.replace(null, one, "whatever");
706 >            c.replace(null, "A", "B");
707              shouldThrow();
708          } catch (NullPointerException success) {}
709      }
# Line 713 | Line 712 | public class ConcurrentHashMapTest exten
712       * putIfAbsent(x, null) throws NPE
713       */
714      public void testPutIfAbsent2_NullPointerException() {
715 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
715 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
716          try {
717 <            c.putIfAbsent("whatever", null);
717 >            c.putIfAbsent(zero, null);
718              shouldThrow();
719          } catch (NullPointerException success) {}
720      }
# Line 724 | Line 723 | public class ConcurrentHashMapTest exten
723       * replace(x, null) throws NPE
724       */
725      public void testReplace2_NullPointerException() {
726 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
726 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
727          try {
728 <            c.replace("whatever", null);
728 >            c.replace(one, null);
729              shouldThrow();
730          } catch (NullPointerException success) {}
731      }
# Line 735 | Line 734 | public class ConcurrentHashMapTest exten
734       * replace(x, null, y) throws NPE
735       */
736      public void testReplaceValue2_NullPointerException() {
737 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
737 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
738          try {
739 <            c.replace("whatever", null, "A");
739 >            c.replace(one, null, "A");
740              shouldThrow();
741          } catch (NullPointerException success) {}
742      }
# Line 746 | Line 745 | public class ConcurrentHashMapTest exten
745       * replace(x, y, null) throws NPE
746       */
747      public void testReplaceValue3_NullPointerException() {
748 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
748 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
749          try {
750 <            c.replace("whatever", one, null);
750 >            c.replace(zero, "A", null);
751              shouldThrow();
752          } catch (NullPointerException success) {}
753      }
# Line 757 | Line 756 | public class ConcurrentHashMapTest exten
756       * remove(null) throws NPE
757       */
758      public void testRemove1_NullPointerException() {
759 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
760 <        c.put("sadsdf", "asdads");
759 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
760 >        c.put(one, "asdads");
761          try {
762              c.remove(null);
763              shouldThrow();
# Line 769 | Line 768 | public class ConcurrentHashMapTest exten
768       * remove(null, x) throws NPE
769       */
770      public void testRemove2_NullPointerException() {
771 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
772 <        c.put("sadsdf", "asdads");
771 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
772 >        c.put(one, "asdads");
773          try {
774              c.remove(null, "whatever");
775              shouldThrow();
# Line 781 | Line 780 | public class ConcurrentHashMapTest exten
780       * remove(x, null) returns false
781       */
782      public void testRemove3() {
783 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
784 <        c.put("sadsdf", "asdads");
785 <        assertFalse(c.remove("sadsdf", null));
783 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<>(5);
784 >        c.put(one, "asdads");
785 >        assertFalse(c.remove(one, null));
786      }
787  
788      /**
789       * A deserialized/reserialized map equals original
790       */
791      public void testSerialization() throws Exception {
792 <        Map x = map5();
793 <        Map y = serialClone(x);
792 >        Map<Item,String> x = map5();
793 >        Map<Item,String> y = serialClone(x);
794  
795          assertNotSame(x, y);
796 <        assertEquals(x.size(), y.size());
797 <        assertEquals(x, y);
798 <        assertEquals(y, x);
796 >        mustEqual(x.size(), y.size());
797 >        mustEqual(x, y);
798 >        mustEqual(y, x);
799      }
800  
801      /**
802       * SetValue of an EntrySet entry sets value in the map.
803       */
804 +    @SuppressWarnings("unchecked")
805      public void testSetValueWriteThrough() {
806          // Adapted from a bug report by Eric Zoerner
807 <        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
807 >        ConcurrentHashMap<Object,Object> map = new ConcurrentHashMap<>(2, 5.0f, 1);
808          assertTrue(map.isEmpty());
809          for (int i = 0; i < 20; i++)
810 <            map.put(new Integer(i), new Integer(i));
810 >            map.put(itemFor(i), itemFor(i));
811          assertFalse(map.isEmpty());
812 <        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
812 >        Item key = itemFor(16);
813 >        Map.Entry<Object,Object> entry1 = map.entrySet().iterator().next();
814          // Unless it happens to be first (in which case remainder of
815          // test is skipped), remove a possibly-colliding key from map
816          // which, under some implementations, may cause entry1 to be
817          // cloned in map
818 <        if (!entry1.getKey().equals(new Integer(16))) {
819 <            map.remove(new Integer(16));
818 >        if (!entry1.getKey().equals(key)) {
819 >            map.remove(key);
820              entry1.setValue("XYZ");
821              assertTrue(map.containsValue("XYZ")); // fails if write-through broken
822          }
# Line 828 | Line 829 | public class ConcurrentHashMapTest exten
829      public void testRemoveAll_performance() {
830          final int mapSize = expensiveTests ? 1_000_000 : 100;
831          final int iterations = expensiveTests ? 500 : 2;
832 <        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
833 <        for (int i = 0; i < mapSize; i++)
834 <            map.put(i, i);
835 <        Set<Integer> keySet = map.keySet();
836 <        Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
832 >        final ConcurrentHashMap<Item, Item> map = new ConcurrentHashMap<>();
833 >        for (int i = 0; i < mapSize; i++) {
834 >            Item I = itemFor(i);
835 >            map.put(I, I);
836 >        }
837 >        Set<Item> keySet = map.keySet();
838 >        Collection<Item> removeMe = Arrays.asList(new Item[] { minusOne, minusTwo });
839          for (int i = 0; i < iterations; i++)
840              assertFalse(keySet.removeAll(removeMe));
841 <        assertEquals(mapSize, map.size());
841 >        mustEqual(mapSize, map.size());
842 >    }
843 >
844 >    public void testReentrantComputeIfAbsent() {
845 >        ConcurrentHashMap<Item, Item> map = new ConcurrentHashMap<>(16);
846 >        try {
847 >            for (int i = 0; i < 100; i++) { // force a resize
848 >                map.computeIfAbsent(new Item(i), key -> new Item(findValue(map, key)));
849 >            }
850 >            fail("recursive computeIfAbsent should throw IllegalStateException");
851 >        } catch (IllegalStateException success) {}
852      }
853  
854 +    private static Item findValue(ConcurrentHashMap<Item, Item> map,
855 +                                  Item key) {
856 +        return (key.value % 5 == 0) ?  key :
857 +            map.computeIfAbsent(new Item(key.value + 1), k -> new Item(findValue(map, k)));
858 +    }
859   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines