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.42 by jsr166, Sun Feb 22 04:34:44 2015 UTC vs.
Revision 1.53 by jsr166, Sat Mar 11 17:33:32 2017 UTC

# Line 22 | Line 22 | import junit.framework.TestSuite;
22  
23   public class ConcurrentHashMapTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28          return new TestSuite(ConcurrentHashMapTest.class);
# Line 32 | Line 32 | public class ConcurrentHashMapTest exten
32       * Returns a new map from Integers 1-5 to Strings "A"-"E".
33       */
34      private static ConcurrentHashMap<Integer, String> map5() {
35 <        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
35 >        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
36          assertTrue(map.isEmpty());
37          map.put(one, "A");
38          map.put(two, "B");
# Line 45 | Line 45 | public class ConcurrentHashMapTest exten
45      }
46  
47      /** Re-implement Integer.compare for old java versions */
48 <    static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
48 >    static int compare(int x, int y) {
49 >        return (x < y) ? -1 : (x > y) ? 1 : 0;
50 >    }
51  
52      // classes for testing Comparable fallbacks
53      static class BI implements Comparable<BI> {
# Line 329 | Line 331 | public class ConcurrentHashMapTest exten
331      }
332  
333      /**
334 +     * Test keySet().removeAll on empty map
335 +     */
336 +    public void testKeySet_empty_removeAll() {
337 +        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
338 +        Set<Integer> set = map.keySet();
339 +        set.removeAll(Collections.emptyList());
340 +        assertTrue(map.isEmpty());
341 +        assertTrue(set.isEmpty());
342 +        // following is test for JDK-8163353
343 +        set.removeAll(Collections.emptySet());
344 +        assertTrue(map.isEmpty());
345 +        assertTrue(set.isEmpty());
346 +    }
347 +
348 +    /**
349       * keySet.toArray returns contains all keys
350       */
351      public void testKeySetToArray() {
# Line 533 | Line 550 | public class ConcurrentHashMapTest exten
550      /**
551       * Constructor (initialCapacity, loadFactor) throws
552       * IllegalArgumentException if either argument is negative
553 <      */
553 >     */
554      public void testConstructor2() {
555          try {
556              new ConcurrentHashMap(-1, .75f);
557              shouldThrow();
558 <        } catch (IllegalArgumentException e) {}
558 >        } catch (IllegalArgumentException success) {}
559  
560          try {
561              new ConcurrentHashMap(16, -1);
562              shouldThrow();
563 <        } catch (IllegalArgumentException e) {}
563 >        } catch (IllegalArgumentException success) {}
564      }
565  
566      /**
# Line 554 | Line 571 | public class ConcurrentHashMapTest exten
571          try {
572              new ConcurrentHashMap(-1, .75f, 1);
573              shouldThrow();
574 <        } catch (IllegalArgumentException e) {}
574 >        } catch (IllegalArgumentException success) {}
575  
576          try {
577              new ConcurrentHashMap(16, -1, 1);
578              shouldThrow();
579 <        } catch (IllegalArgumentException e) {}
579 >        } catch (IllegalArgumentException success) {}
580  
581          try {
582              new ConcurrentHashMap(16, .75f, -1);
583              shouldThrow();
584 <        } catch (IllegalArgumentException e) {}
584 >        } catch (IllegalArgumentException success) {}
585      }
586  
587      /**
# Line 575 | Line 592 | public class ConcurrentHashMapTest exten
592          try {
593              new ConcurrentHashMap(null);
594              shouldThrow();
595 <        } catch (NullPointerException e) {}
595 >        } catch (NullPointerException success) {}
596      }
597  
598      /**
# Line 594 | Line 611 | public class ConcurrentHashMapTest exten
611       * get(null) throws NPE
612       */
613      public void testGet_NullPointerException() {
614 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
615          try {
598            ConcurrentHashMap c = new ConcurrentHashMap(5);
616              c.get(null);
617              shouldThrow();
618          } catch (NullPointerException success) {}
# Line 605 | Line 622 | public class ConcurrentHashMapTest exten
622       * containsKey(null) throws NPE
623       */
624      public void testContainsKey_NullPointerException() {
625 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
626          try {
609            ConcurrentHashMap c = new ConcurrentHashMap(5);
627              c.containsKey(null);
628              shouldThrow();
629          } catch (NullPointerException success) {}
# Line 616 | Line 633 | public class ConcurrentHashMapTest exten
633       * containsValue(null) throws NPE
634       */
635      public void testContainsValue_NullPointerException() {
636 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
637          try {
620            ConcurrentHashMap c = new ConcurrentHashMap(5);
638              c.containsValue(null);
639              shouldThrow();
640          } catch (NullPointerException success) {}
# Line 627 | Line 644 | public class ConcurrentHashMapTest exten
644       * contains(null) throws NPE
645       */
646      public void testContains_NullPointerException() {
647 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
648          try {
631            ConcurrentHashMap c = new ConcurrentHashMap(5);
649              c.contains(null);
650              shouldThrow();
651          } catch (NullPointerException success) {}
# Line 638 | Line 655 | public class ConcurrentHashMapTest exten
655       * put(null,x) throws NPE
656       */
657      public void testPut1_NullPointerException() {
658 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
659          try {
642            ConcurrentHashMap c = new ConcurrentHashMap(5);
660              c.put(null, "whatever");
661              shouldThrow();
662          } catch (NullPointerException success) {}
# Line 649 | Line 666 | public class ConcurrentHashMapTest exten
666       * put(x, null) throws NPE
667       */
668      public void testPut2_NullPointerException() {
669 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
670          try {
653            ConcurrentHashMap c = new ConcurrentHashMap(5);
671              c.put("whatever", null);
672              shouldThrow();
673          } catch (NullPointerException success) {}
# Line 660 | Line 677 | public class ConcurrentHashMapTest exten
677       * putIfAbsent(null, x) throws NPE
678       */
679      public void testPutIfAbsent1_NullPointerException() {
680 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
681          try {
664            ConcurrentHashMap c = new ConcurrentHashMap(5);
682              c.putIfAbsent(null, "whatever");
683              shouldThrow();
684          } catch (NullPointerException success) {}
# Line 671 | Line 688 | public class ConcurrentHashMapTest exten
688       * replace(null, x) throws NPE
689       */
690      public void testReplace_NullPointerException() {
691 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
692          try {
675            ConcurrentHashMap c = new ConcurrentHashMap(5);
693              c.replace(null, "whatever");
694              shouldThrow();
695          } catch (NullPointerException success) {}
# Line 682 | Line 699 | public class ConcurrentHashMapTest exten
699       * replace(null, x, y) throws NPE
700       */
701      public void testReplaceValue_NullPointerException() {
702 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
703          try {
686            ConcurrentHashMap c = new ConcurrentHashMap(5);
704              c.replace(null, one, "whatever");
705              shouldThrow();
706          } catch (NullPointerException success) {}
# Line 693 | Line 710 | public class ConcurrentHashMapTest exten
710       * putIfAbsent(x, null) throws NPE
711       */
712      public void testPutIfAbsent2_NullPointerException() {
713 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
714          try {
697            ConcurrentHashMap c = new ConcurrentHashMap(5);
715              c.putIfAbsent("whatever", null);
716              shouldThrow();
717          } catch (NullPointerException success) {}
# Line 704 | Line 721 | public class ConcurrentHashMapTest exten
721       * replace(x, null) throws NPE
722       */
723      public void testReplace2_NullPointerException() {
724 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
725          try {
708            ConcurrentHashMap c = new ConcurrentHashMap(5);
726              c.replace("whatever", null);
727              shouldThrow();
728          } catch (NullPointerException success) {}
# Line 715 | Line 732 | public class ConcurrentHashMapTest exten
732       * replace(x, null, y) throws NPE
733       */
734      public void testReplaceValue2_NullPointerException() {
735 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
736          try {
719            ConcurrentHashMap c = new ConcurrentHashMap(5);
737              c.replace("whatever", null, "A");
738              shouldThrow();
739          } catch (NullPointerException success) {}
# Line 726 | Line 743 | public class ConcurrentHashMapTest exten
743       * replace(x, y, null) throws NPE
744       */
745      public void testReplaceValue3_NullPointerException() {
746 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
747          try {
730            ConcurrentHashMap c = new ConcurrentHashMap(5);
748              c.replace("whatever", one, null);
749              shouldThrow();
750          } catch (NullPointerException success) {}
# Line 737 | Line 754 | public class ConcurrentHashMapTest exten
754       * remove(null) throws NPE
755       */
756      public void testRemove1_NullPointerException() {
757 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
758 +        c.put("sadsdf", "asdads");
759          try {
741            ConcurrentHashMap c = new ConcurrentHashMap(5);
742            c.put("sadsdf", "asdads");
760              c.remove(null);
761              shouldThrow();
762          } catch (NullPointerException success) {}
# Line 749 | Line 766 | public class ConcurrentHashMapTest exten
766       * remove(null, x) throws NPE
767       */
768      public void testRemove2_NullPointerException() {
769 +        ConcurrentHashMap c = new ConcurrentHashMap(5);
770 +        c.put("sadsdf", "asdads");
771          try {
753            ConcurrentHashMap c = new ConcurrentHashMap(5);
754            c.put("sadsdf", "asdads");
772              c.remove(null, "whatever");
773              shouldThrow();
774          } catch (NullPointerException success) {}
# Line 801 | Line 818 | public class ConcurrentHashMapTest exten
818          }
819      }
820  
821 +    /**
822 +     * Tests performance of removeAll when the other collection is much smaller.
823 +     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
824 +     */
825 +    public void testRemoveAll_performance() {
826 +        final int mapSize = expensiveTests ? 1_000_000 : 100;
827 +        final int iterations = expensiveTests ? 500 : 2;
828 +        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
829 +        for (int i = 0; i < mapSize; i++)
830 +            map.put(i, i);
831 +        Set<Integer> keySet = map.keySet();
832 +        Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
833 +        for (int i = 0; i < iterations; i++)
834 +            assertFalse(keySet.removeAll(removeMe));
835 +        assertEquals(mapSize, map.size());
836 +    }
837 +
838   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines