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.62 by jsr166, Sun Sep 29 20:40:48 2019 UTC vs.
Revision 1.63 by dl, Tue Jan 26 13:33:05 2021 UTC

# Line 39 | 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 50 | 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 196 | 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 210 | 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 232 | 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 252 | 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 261 | 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 271 | 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      /**
# Line 286 | Line 286 | public class ConcurrentHashMapTest exten
286       * or null if not present
287       */
288      public void testGet() {
289 <        ConcurrentHashMap map = map5();
290 <        assertEquals("A", (String)map.get(one));
291 <        ConcurrentHashMap empty = new ConcurrentHashMap();
289 >        ConcurrentHashMap<Item,String> map = map5();
290 >        mustEqual("A", map.get(one));
291 >        ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<Item,String>();
292          assertNull(map.get("anything"));
293          assertNull(empty.get("anything"));
294      }
# Line 297 | Line 297 | public class ConcurrentHashMapTest exten
297       * isEmpty is true of empty map and false for non-empty
298       */
299      public void testIsEmpty() {
300 <        ConcurrentHashMap empty = new ConcurrentHashMap();
301 <        ConcurrentHashMap map = map5();
300 >        ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<Item,String>();
301 >        ConcurrentHashMap<Item,String> map = map5();
302          assertTrue(empty.isEmpty());
303          assertFalse(map.isEmpty());
304      }
# Line 307 | Line 307 | public class ConcurrentHashMapTest exten
307       * keys returns an enumeration containing all the keys from the map
308       */
309      public void testKeys() {
310 <        ConcurrentHashMap map = map5();
311 <        Enumeration e = map.keys();
310 >        ConcurrentHashMap<Item,String> map = map5();
311 >        Enumeration<Item> e = map.keys();
312          int count = 0;
313          while (e.hasMoreElements()) {
314              count++;
315              e.nextElement();
316          }
317 <        assertEquals(5, count);
317 >        mustEqual(5, count);
318      }
319  
320      /**
321       * keySet returns a Set containing all the keys
322       */
323      public void testKeySet() {
324 <        ConcurrentHashMap map = map5();
325 <        Set s = map.keySet();
326 <        assertEquals(5, s.size());
327 <        assertTrue(s.contains(one));
328 <        assertTrue(s.contains(two));
329 <        assertTrue(s.contains(three));
330 <        assertTrue(s.contains(four));
331 <        assertTrue(s.contains(five));
324 >        ConcurrentHashMap<Item,String> map = map5();
325 >        Set<Item> s = map.keySet();
326 >        mustEqual(5, s.size());
327 >        mustContain(s, one);
328 >        mustContain(s, two);
329 >        mustContain(s, three);
330 >        mustContain(s, four);
331 >        mustContain(s, five);
332      }
333  
334      /**
335       * Test keySet().removeAll on empty map
336       */
337      public void testKeySet_empty_removeAll() {
338 <        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
339 <        Set<Integer> set = map.keySet();
338 >        ConcurrentHashMap<Item, String> map = new ConcurrentHashMap<>();
339 >        Set<Item> set = map.keySet();
340          set.removeAll(Collections.emptyList());
341          assertTrue(map.isEmpty());
342          assertTrue(set.isEmpty());
# Line 350 | Line 350 | public class ConcurrentHashMapTest exten
350       * keySet.toArray returns contains all keys
351       */
352      public void testKeySetToArray() {
353 <        ConcurrentHashMap map = map5();
354 <        Set s = map.keySet();
353 >        ConcurrentHashMap<Item,String> map = map5();
354 >        Set<Item> s = map.keySet();
355          Object[] ar = s.toArray();
356          assertTrue(s.containsAll(Arrays.asList(ar)));
357 <        assertEquals(5, ar.length);
358 <        ar[0] = m10;
357 >        mustEqual(5, ar.length);
358 >        ar[0] = minusTen;
359          assertFalse(s.containsAll(Arrays.asList(ar)));
360      }
361  
# Line 363 | Line 363 | public class ConcurrentHashMapTest exten
363       * Values.toArray contains all values
364       */
365      public void testValuesToArray() {
366 <        ConcurrentHashMap map = map5();
367 <        Collection v = map.values();
368 <        Object[] ar = v.toArray();
369 <        ArrayList s = new ArrayList(Arrays.asList(ar));
370 <        assertEquals(5, ar.length);
366 >        ConcurrentHashMap<Item,String> map = map5();
367 >        Collection<String> v = map.values();
368 >        String[] ar = v.toArray(new String[0]);
369 >        ArrayList<String> s = new ArrayList<String>(Arrays.asList(ar));
370 >        mustEqual(5, ar.length);
371          assertTrue(s.contains("A"));
372          assertTrue(s.contains("B"));
373          assertTrue(s.contains("C"));
# Line 379 | Line 379 | public class ConcurrentHashMapTest exten
379       * entrySet.toArray contains all entries
380       */
381      public void testEntrySetToArray() {
382 <        ConcurrentHashMap map = map5();
383 <        Set s = map.entrySet();
382 >        ConcurrentHashMap<Item,String> map = map5();
383 >        Set<Map.Entry<Item,String>> s = map.entrySet();
384          Object[] ar = s.toArray();
385 <        assertEquals(5, ar.length);
385 >        mustEqual(5, ar.length);
386          for (int i = 0; i < 5; ++i) {
387              assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
388              assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
# Line 393 | Line 393 | public class ConcurrentHashMapTest exten
393       * values collection contains all values
394       */
395      public void testValues() {
396 <        ConcurrentHashMap map = map5();
397 <        Collection s = map.values();
398 <        assertEquals(5, s.size());
396 >        ConcurrentHashMap<Item,String> map = map5();
397 >        Collection<String> s = map.values();
398 >        mustEqual(5, s.size());
399          assertTrue(s.contains("A"));
400          assertTrue(s.contains("B"));
401          assertTrue(s.contains("C"));
# Line 407 | Line 407 | public class ConcurrentHashMapTest exten
407       * entrySet contains all pairs
408       */
409      public void testEntrySet() {
410 <        ConcurrentHashMap map = map5();
411 <        Set s = map.entrySet();
412 <        assertEquals(5, s.size());
413 <        Iterator it = s.iterator();
410 >        ConcurrentHashMap<Item, String> map = map5();
411 >        Set<Map.Entry<Item,String>> s = map.entrySet();
412 >        mustEqual(5, s.size());
413 >        Iterator<Map.Entry<Item,String>> it = s.iterator();
414          while (it.hasNext()) {
415 <            Map.Entry e = (Map.Entry) it.next();
415 >            Map.Entry<Item,String> e = it.next();
416              assertTrue(
417                         (e.getKey().equals(one) && e.getValue().equals("A")) ||
418                         (e.getKey().equals(two) && e.getValue().equals("B")) ||
# Line 426 | Line 426 | public class ConcurrentHashMapTest exten
426       * putAll adds all key-value pairs from the given map
427       */
428      public void testPutAll() {
429 <        ConcurrentHashMap empty = new ConcurrentHashMap();
430 <        ConcurrentHashMap map = map5();
431 <        empty.putAll(map);
432 <        assertEquals(5, empty.size());
433 <        assertTrue(empty.containsKey(one));
434 <        assertTrue(empty.containsKey(two));
435 <        assertTrue(empty.containsKey(three));
436 <        assertTrue(empty.containsKey(four));
437 <        assertTrue(empty.containsKey(five));
429 >        ConcurrentHashMap<Item,String> p = new ConcurrentHashMap<Item,String>();
430 >        ConcurrentHashMap<Item,String> map = map5();
431 >        p.putAll(map);
432 >        mustEqual(5, p.size());
433 >        assertTrue(p.containsKey(one));
434 >        assertTrue(p.containsKey(two));
435 >        assertTrue(p.containsKey(three));
436 >        assertTrue(p.containsKey(four));
437 >        assertTrue(p.containsKey(five));
438      }
439  
440      /**
441       * putIfAbsent works when the given key is not present
442       */
443      public void testPutIfAbsent() {
444 <        ConcurrentHashMap map = map5();
444 >        ConcurrentHashMap<Item,String> map = map5();
445          map.putIfAbsent(six, "Z");
446          assertTrue(map.containsKey(six));
447      }
# Line 450 | Line 450 | public class ConcurrentHashMapTest exten
450       * putIfAbsent does not add the pair if the key is already present
451       */
452      public void testPutIfAbsent2() {
453 <        ConcurrentHashMap map = map5();
454 <        assertEquals("A", map.putIfAbsent(one, "Z"));
453 >        ConcurrentHashMap<Item,String> map = map5();
454 >        mustEqual("A", map.putIfAbsent(one, "Z"));
455      }
456  
457      /**
458       * replace fails when the given key is not present
459       */
460      public void testReplace() {
461 <        ConcurrentHashMap map = map5();
461 >        ConcurrentHashMap<Item,String> map = map5();
462          assertNull(map.replace(six, "Z"));
463          assertFalse(map.containsKey(six));
464      }
# Line 467 | Line 467 | public class ConcurrentHashMapTest exten
467       * replace succeeds if the key is already present
468       */
469      public void testReplace2() {
470 <        ConcurrentHashMap map = map5();
470 >        ConcurrentHashMap<Item,String> map = map5();
471          assertNotNull(map.replace(one, "Z"));
472 <        assertEquals("Z", map.get(one));
472 >        mustEqual("Z", map.get(one));
473      }
474  
475      /**
476       * replace value fails when the given key not mapped to expected value
477       */
478      public void testReplaceValue() {
479 <        ConcurrentHashMap map = map5();
480 <        assertEquals("A", map.get(one));
479 >        ConcurrentHashMap<Item,String> map = map5();
480 >        mustEqual("A", map.get(one));
481          assertFalse(map.replace(one, "Z", "Z"));
482 <        assertEquals("A", map.get(one));
482 >        mustEqual("A", map.get(one));
483      }
484  
485      /**
486       * replace value succeeds when the given key mapped to expected value
487       */
488      public void testReplaceValue2() {
489 <        ConcurrentHashMap map = map5();
490 <        assertEquals("A", map.get(one));
489 >        ConcurrentHashMap<Item,String> map = map5();
490 >        mustEqual("A", map.get(one));
491          assertTrue(map.replace(one, "A", "Z"));
492 <        assertEquals("Z", map.get(one));
492 >        mustEqual("Z", map.get(one));
493      }
494  
495      /**
496       * remove removes the correct key-value pair from the map
497       */
498      public void testRemove() {
499 <        ConcurrentHashMap map = map5();
499 >        ConcurrentHashMap<Item,String> map = map5();
500          map.remove(five);
501 <        assertEquals(4, map.size());
501 >        mustEqual(4, map.size());
502          assertFalse(map.containsKey(five));
503      }
504  
# Line 506 | Line 506 | public class ConcurrentHashMapTest exten
506       * remove(key,value) removes only if pair present
507       */
508      public void testRemove2() {
509 <        ConcurrentHashMap map = map5();
509 >        ConcurrentHashMap<Item,String> map = map5();
510          map.remove(five, "E");
511 <        assertEquals(4, map.size());
511 >        mustEqual(4, map.size());
512          assertFalse(map.containsKey(five));
513          map.remove(four, "A");
514 <        assertEquals(4, map.size());
514 >        mustEqual(4, map.size());
515          assertTrue(map.containsKey(four));
516      }
517  
# Line 519 | Line 519 | public class ConcurrentHashMapTest exten
519       * size returns the correct values
520       */
521      public void testSize() {
522 <        ConcurrentHashMap map = map5();
523 <        ConcurrentHashMap empty = new ConcurrentHashMap();
524 <        assertEquals(0, empty.size());
525 <        assertEquals(5, map.size());
522 >        ConcurrentHashMap<Item,String> map = map5();
523 >        ConcurrentHashMap<Item,String> empty = new ConcurrentHashMap<Item,String>();
524 >        mustEqual(0, empty.size());
525 >        mustEqual(5, map.size());
526      }
527  
528      /**
529       * toString contains toString of elements
530       */
531      public void testToString() {
532 <        ConcurrentHashMap map = map5();
532 >        ConcurrentHashMap<Item,String> map = map5();
533          String s = map.toString();
534          for (int i = 1; i <= 5; ++i) {
535              assertTrue(s.contains(String.valueOf(i)));
# Line 543 | Line 543 | public class ConcurrentHashMapTest exten
543       */
544      public void testConstructor1() {
545          try {
546 <            new ConcurrentHashMap(-1);
546 >            new ConcurrentHashMap<Item,String>(-1);
547              shouldThrow();
548          } catch (IllegalArgumentException success) {}
549      }
# Line 554 | Line 554 | public class ConcurrentHashMapTest exten
554       */
555      public void testConstructor2() {
556          try {
557 <            new ConcurrentHashMap(-1, .75f);
557 >            new ConcurrentHashMap<Item,String>(-1, .75f);
558              shouldThrow();
559          } catch (IllegalArgumentException success) {}
560  
561          try {
562 <            new ConcurrentHashMap(16, -1);
562 >            new ConcurrentHashMap<Item,String>(16, -1);
563              shouldThrow();
564          } catch (IllegalArgumentException success) {}
565      }
# Line 570 | Line 570 | public class ConcurrentHashMapTest exten
570       */
571      public void testConstructor3() {
572          try {
573 <            new ConcurrentHashMap(-1, .75f, 1);
573 >            new ConcurrentHashMap<Item,String>(-1, .75f, 1);
574              shouldThrow();
575          } catch (IllegalArgumentException success) {}
576  
577          try {
578 <            new ConcurrentHashMap(16, -1, 1);
578 >            new ConcurrentHashMap<Item,String>(16, -1, 1);
579              shouldThrow();
580          } catch (IllegalArgumentException success) {}
581  
582          try {
583 <            new ConcurrentHashMap(16, .75f, -1);
583 >            new ConcurrentHashMap<Item,String>(16, .75f, -1);
584              shouldThrow();
585          } catch (IllegalArgumentException success) {}
586      }
# Line 591 | Line 591 | public class ConcurrentHashMapTest exten
591       */
592      public void testConstructor4() {
593          try {
594 <            new ConcurrentHashMap(null);
594 >            new ConcurrentHashMap<Item,String>(null);
595              shouldThrow();
596          } catch (NullPointerException success) {}
597      }
# Line 601 | Line 601 | public class ConcurrentHashMapTest exten
601       * as the given map
602       */
603      public void testConstructor5() {
604 <        ConcurrentHashMap map1 = map5();
605 <        ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
604 >        ConcurrentHashMap<Item,String> map1 = map5();
605 >        ConcurrentHashMap<Item,String> map2 = new ConcurrentHashMap<Item,String>(map1);
606          assertTrue(map2.equals(map1));
607          map2.put(one, "F");
608          assertFalse(map2.equals(map1));
# Line 612 | Line 612 | public class ConcurrentHashMapTest exten
612       * get(null) throws NPE
613       */
614      public void testGet_NullPointerException() {
615 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
615 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
616          try {
617              c.get(null);
618              shouldThrow();
# Line 623 | Line 623 | public class ConcurrentHashMapTest exten
623       * containsKey(null) throws NPE
624       */
625      public void testContainsKey_NullPointerException() {
626 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
626 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
627          try {
628              c.containsKey(null);
629              shouldThrow();
# Line 634 | Line 634 | public class ConcurrentHashMapTest exten
634       * containsValue(null) throws NPE
635       */
636      public void testContainsValue_NullPointerException() {
637 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
637 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
638          try {
639              c.containsValue(null);
640              shouldThrow();
# Line 645 | Line 645 | public class ConcurrentHashMapTest exten
645       * contains(null) throws NPE
646       */
647      public void testContains_NullPointerException() {
648 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
648 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
649          try {
650              c.contains(null);
651              shouldThrow();
# Line 656 | Line 656 | public class ConcurrentHashMapTest exten
656       * put(null,x) throws NPE
657       */
658      public void testPut1_NullPointerException() {
659 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
659 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
660          try {
661              c.put(null, "whatever");
662              shouldThrow();
# Line 667 | Line 667 | public class ConcurrentHashMapTest exten
667       * put(x, null) throws NPE
668       */
669      public void testPut2_NullPointerException() {
670 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
670 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
671          try {
672 <            c.put("whatever", null);
672 >            c.put(zero, null);
673              shouldThrow();
674          } catch (NullPointerException success) {}
675      }
# Line 678 | Line 678 | public class ConcurrentHashMapTest exten
678       * putIfAbsent(null, x) throws NPE
679       */
680      public void testPutIfAbsent1_NullPointerException() {
681 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
681 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
682          try {
683              c.putIfAbsent(null, "whatever");
684              shouldThrow();
# Line 689 | Line 689 | public class ConcurrentHashMapTest exten
689       * replace(null, x) throws NPE
690       */
691      public void testReplace_NullPointerException() {
692 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
692 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
693          try {
694              c.replace(null, "whatever");
695              shouldThrow();
# Line 700 | Line 700 | public class ConcurrentHashMapTest exten
700       * replace(null, x, y) throws NPE
701       */
702      public void testReplaceValue_NullPointerException() {
703 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
703 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
704          try {
705 <            c.replace(null, one, "whatever");
705 >            c.replace(null, "A", "B");
706              shouldThrow();
707          } catch (NullPointerException success) {}
708      }
# Line 711 | Line 711 | public class ConcurrentHashMapTest exten
711       * putIfAbsent(x, null) throws NPE
712       */
713      public void testPutIfAbsent2_NullPointerException() {
714 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
714 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
715          try {
716 <            c.putIfAbsent("whatever", null);
716 >            c.putIfAbsent(zero, null);
717              shouldThrow();
718          } catch (NullPointerException success) {}
719      }
# Line 722 | Line 722 | public class ConcurrentHashMapTest exten
722       * replace(x, null) throws NPE
723       */
724      public void testReplace2_NullPointerException() {
725 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
725 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
726          try {
727 <            c.replace("whatever", null);
727 >            c.replace(one, null);
728              shouldThrow();
729          } catch (NullPointerException success) {}
730      }
# Line 733 | Line 733 | public class ConcurrentHashMapTest exten
733       * replace(x, null, y) throws NPE
734       */
735      public void testReplaceValue2_NullPointerException() {
736 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
736 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
737          try {
738 <            c.replace("whatever", null, "A");
738 >            c.replace(one, null, "A");
739              shouldThrow();
740          } catch (NullPointerException success) {}
741      }
# Line 744 | Line 744 | public class ConcurrentHashMapTest exten
744       * replace(x, y, null) throws NPE
745       */
746      public void testReplaceValue3_NullPointerException() {
747 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
747 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
748          try {
749 <            c.replace("whatever", one, null);
749 >            c.replace(zero, "A", null);
750              shouldThrow();
751          } catch (NullPointerException success) {}
752      }
# Line 755 | Line 755 | public class ConcurrentHashMapTest exten
755       * remove(null) throws NPE
756       */
757      public void testRemove1_NullPointerException() {
758 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
759 <        c.put("sadsdf", "asdads");
758 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
759 >        c.put(one, "asdads");
760          try {
761              c.remove(null);
762              shouldThrow();
# Line 767 | Line 767 | public class ConcurrentHashMapTest exten
767       * remove(null, x) throws NPE
768       */
769      public void testRemove2_NullPointerException() {
770 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
771 <        c.put("sadsdf", "asdads");
770 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
771 >        c.put(one, "asdads");
772          try {
773              c.remove(null, "whatever");
774              shouldThrow();
# Line 779 | Line 779 | public class ConcurrentHashMapTest exten
779       * remove(x, null) returns false
780       */
781      public void testRemove3() {
782 <        ConcurrentHashMap c = new ConcurrentHashMap(5);
783 <        c.put("sadsdf", "asdads");
784 <        assertFalse(c.remove("sadsdf", null));
782 >        ConcurrentHashMap<Item,String> c = new ConcurrentHashMap<Item,String>(5);
783 >        c.put(one, "asdads");
784 >        assertFalse(c.remove(one, null));
785      }
786  
787      /**
788       * A deserialized/reserialized map equals original
789       */
790      public void testSerialization() throws Exception {
791 <        Map x = map5();
792 <        Map y = serialClone(x);
791 >        Map<Item,String> x = map5();
792 >        Map<Item,String> y = serialClone(x);
793  
794          assertNotSame(x, y);
795 <        assertEquals(x.size(), y.size());
796 <        assertEquals(x, y);
797 <        assertEquals(y, x);
795 >        mustEqual(x.size(), y.size());
796 >        mustEqual(x, y);
797 >        mustEqual(y, x);
798      }
799  
800      /**
801       * SetValue of an EntrySet entry sets value in the map.
802       */
803 +    @SuppressWarnings("unchecked")
804      public void testSetValueWriteThrough() {
805          // Adapted from a bug report by Eric Zoerner
806 <        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
806 >        ConcurrentHashMap<Object,Object> map = new ConcurrentHashMap<Object,Object>(2, 5.0f, 1);
807          assertTrue(map.isEmpty());
808          for (int i = 0; i < 20; i++)
809 <            map.put(new Integer(i), new Integer(i));
809 >            map.put(itemFor(i), itemFor(i));
810          assertFalse(map.isEmpty());
811 <        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
811 >        Item key = itemFor(16);
812 >        Map.Entry<Object,Object> entry1 = map.entrySet().iterator().next();
813          // Unless it happens to be first (in which case remainder of
814          // test is skipped), remove a possibly-colliding key from map
815          // which, under some implementations, may cause entry1 to be
816          // cloned in map
817 <        if (!entry1.getKey().equals(new Integer(16))) {
818 <            map.remove(new Integer(16));
817 >        if (!entry1.getKey().equals(key)) {
818 >            map.remove(key);
819              entry1.setValue("XYZ");
820              assertTrue(map.containsValue("XYZ")); // fails if write-through broken
821          }
# Line 826 | Line 828 | public class ConcurrentHashMapTest exten
828      public void testRemoveAll_performance() {
829          final int mapSize = expensiveTests ? 1_000_000 : 100;
830          final int iterations = expensiveTests ? 500 : 2;
831 <        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
832 <        for (int i = 0; i < mapSize; i++)
833 <            map.put(i, i);
834 <        Set<Integer> keySet = map.keySet();
835 <        Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
831 >        final ConcurrentHashMap<Item, Item> map = new ConcurrentHashMap<>();
832 >        for (int i = 0; i < mapSize; i++) {
833 >            Item I = itemFor(i);
834 >            map.put(I, I);
835 >        }
836 >        Set<Item> keySet = map.keySet();
837 >        Collection<Item> removeMe = Arrays.asList(new Item[] { minusOne, minusTwo });
838          for (int i = 0; i < iterations; i++)
839              assertFalse(keySet.removeAll(removeMe));
840 <        assertEquals(mapSize, map.size());
840 >        mustEqual(mapSize, map.size());
841      }
842  
843      public void testReentrantComputeIfAbsent() {
844 <        ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16);
844 >        ConcurrentHashMap<Item, Item> map = new ConcurrentHashMap<>(16);
845          try {
846              for (int i = 0; i < 100; i++) { // force a resize
847 <                map.computeIfAbsent(i, key -> findValue(map, key));
847 >                map.computeIfAbsent(new Item(i), key -> new Item(findValue(map, key)));
848              }
849              fail("recursive computeIfAbsent should throw IllegalStateException");
850          } catch (IllegalStateException success) {}
851      }
852  
853 <    private Integer findValue(ConcurrentHashMap<Integer, Integer> map,
854 <                              Integer key) {
855 <        return (key % 5 == 0) ?  key :
856 <            map.computeIfAbsent(key + 1, k -> findValue(map, k));
853 >    static private Item findValue(ConcurrentHashMap<Item, Item> map,
854 >                                  Item key) {
855 >        return (key.value % 5 == 0) ?  key :
856 >            map.computeIfAbsent(new Item(key.value + 1), k -> new Item(findValue(map, k)));
857      }
854
858   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines