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); |
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> { |
535 |
|
/** |
536 |
|
* Constructor (initialCapacity, loadFactor) throws |
537 |
|
* IllegalArgumentException if either argument is negative |
538 |
< |
*/ |
538 |
> |
*/ |
539 |
|
public void testConstructor2() { |
540 |
|
try { |
541 |
|
new ConcurrentHashMap(-1, .75f); |
542 |
|
shouldThrow(); |
543 |
< |
} catch (IllegalArgumentException e) {} |
543 |
> |
} catch (IllegalArgumentException success) {} |
544 |
|
|
545 |
|
try { |
546 |
|
new ConcurrentHashMap(16, -1); |
547 |
|
shouldThrow(); |
548 |
< |
} catch (IllegalArgumentException e) {} |
548 |
> |
} catch (IllegalArgumentException success) {} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
556 |
|
try { |
557 |
|
new ConcurrentHashMap(-1, .75f, 1); |
558 |
|
shouldThrow(); |
559 |
< |
} catch (IllegalArgumentException e) {} |
559 |
> |
} catch (IllegalArgumentException success) {} |
560 |
|
|
561 |
|
try { |
562 |
|
new ConcurrentHashMap(16, -1, 1); |
563 |
|
shouldThrow(); |
564 |
< |
} catch (IllegalArgumentException e) {} |
564 |
> |
} catch (IllegalArgumentException success) {} |
565 |
|
|
566 |
|
try { |
567 |
|
new ConcurrentHashMap(16, .75f, -1); |
568 |
|
shouldThrow(); |
569 |
< |
} catch (IllegalArgumentException e) {} |
569 |
> |
} catch (IllegalArgumentException success) {} |
570 |
|
} |
571 |
|
|
572 |
|
/** |
577 |
|
try { |
578 |
|
new ConcurrentHashMap(null); |
579 |
|
shouldThrow(); |
580 |
< |
} catch (NullPointerException e) {} |
580 |
> |
} catch (NullPointerException success) {} |
581 |
|
} |
582 |
|
|
583 |
|
/** |
596 |
|
* get(null) throws NPE |
597 |
|
*/ |
598 |
|
public void testGet_NullPointerException() { |
599 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
600 |
|
try { |
598 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
601 |
|
c.get(null); |
602 |
|
shouldThrow(); |
603 |
|
} catch (NullPointerException success) {} |
607 |
|
* containsKey(null) throws NPE |
608 |
|
*/ |
609 |
|
public void testContainsKey_NullPointerException() { |
610 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
611 |
|
try { |
609 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
612 |
|
c.containsKey(null); |
613 |
|
shouldThrow(); |
614 |
|
} catch (NullPointerException success) {} |
618 |
|
* containsValue(null) throws NPE |
619 |
|
*/ |
620 |
|
public void testContainsValue_NullPointerException() { |
621 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
622 |
|
try { |
620 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
623 |
|
c.containsValue(null); |
624 |
|
shouldThrow(); |
625 |
|
} catch (NullPointerException success) {} |
629 |
|
* contains(null) throws NPE |
630 |
|
*/ |
631 |
|
public void testContains_NullPointerException() { |
632 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
633 |
|
try { |
631 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
634 |
|
c.contains(null); |
635 |
|
shouldThrow(); |
636 |
|
} catch (NullPointerException success) {} |
640 |
|
* put(null,x) throws NPE |
641 |
|
*/ |
642 |
|
public void testPut1_NullPointerException() { |
643 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
644 |
|
try { |
642 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
645 |
|
c.put(null, "whatever"); |
646 |
|
shouldThrow(); |
647 |
|
} catch (NullPointerException success) {} |
651 |
|
* put(x, null) throws NPE |
652 |
|
*/ |
653 |
|
public void testPut2_NullPointerException() { |
654 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
655 |
|
try { |
653 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
656 |
|
c.put("whatever", null); |
657 |
|
shouldThrow(); |
658 |
|
} catch (NullPointerException success) {} |
662 |
|
* putIfAbsent(null, x) throws NPE |
663 |
|
*/ |
664 |
|
public void testPutIfAbsent1_NullPointerException() { |
665 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
666 |
|
try { |
664 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
667 |
|
c.putIfAbsent(null, "whatever"); |
668 |
|
shouldThrow(); |
669 |
|
} catch (NullPointerException success) {} |
673 |
|
* replace(null, x) throws NPE |
674 |
|
*/ |
675 |
|
public void testReplace_NullPointerException() { |
676 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
677 |
|
try { |
675 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
678 |
|
c.replace(null, "whatever"); |
679 |
|
shouldThrow(); |
680 |
|
} catch (NullPointerException success) {} |
684 |
|
* replace(null, x, y) throws NPE |
685 |
|
*/ |
686 |
|
public void testReplaceValue_NullPointerException() { |
687 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
688 |
|
try { |
686 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
689 |
|
c.replace(null, one, "whatever"); |
690 |
|
shouldThrow(); |
691 |
|
} catch (NullPointerException success) {} |
695 |
|
* putIfAbsent(x, null) throws NPE |
696 |
|
*/ |
697 |
|
public void testPutIfAbsent2_NullPointerException() { |
698 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
699 |
|
try { |
697 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
700 |
|
c.putIfAbsent("whatever", null); |
701 |
|
shouldThrow(); |
702 |
|
} catch (NullPointerException success) {} |
706 |
|
* replace(x, null) throws NPE |
707 |
|
*/ |
708 |
|
public void testReplace2_NullPointerException() { |
709 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
710 |
|
try { |
708 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
711 |
|
c.replace("whatever", null); |
712 |
|
shouldThrow(); |
713 |
|
} catch (NullPointerException success) {} |
717 |
|
* replace(x, null, y) throws NPE |
718 |
|
*/ |
719 |
|
public void testReplaceValue2_NullPointerException() { |
720 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
721 |
|
try { |
719 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
722 |
|
c.replace("whatever", null, "A"); |
723 |
|
shouldThrow(); |
724 |
|
} catch (NullPointerException success) {} |
728 |
|
* replace(x, y, null) throws NPE |
729 |
|
*/ |
730 |
|
public void testReplaceValue3_NullPointerException() { |
731 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
732 |
|
try { |
730 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
733 |
|
c.replace("whatever", one, null); |
734 |
|
shouldThrow(); |
735 |
|
} catch (NullPointerException success) {} |
739 |
|
* remove(null) throws NPE |
740 |
|
*/ |
741 |
|
public void testRemove1_NullPointerException() { |
742 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
743 |
+ |
c.put("sadsdf", "asdads"); |
744 |
|
try { |
741 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
742 |
– |
c.put("sadsdf", "asdads"); |
745 |
|
c.remove(null); |
746 |
|
shouldThrow(); |
747 |
|
} catch (NullPointerException success) {} |
751 |
|
* remove(null, x) throws NPE |
752 |
|
*/ |
753 |
|
public void testRemove2_NullPointerException() { |
754 |
+ |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
755 |
+ |
c.put("sadsdf", "asdads"); |
756 |
|
try { |
753 |
– |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
754 |
– |
c.put("sadsdf", "asdads"); |
757 |
|
c.remove(null, "whatever"); |
758 |
|
shouldThrow(); |
759 |
|
} catch (NullPointerException success) {} |
803 |
|
} |
804 |
|
} |
805 |
|
|
806 |
+ |
/** |
807 |
+ |
* Tests performance of removeAll when the other collection is much smaller. |
808 |
+ |
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck |
809 |
+ |
*/ |
810 |
+ |
public void testRemoveAll_performance() { |
811 |
+ |
int mapSize = expensiveTests ? 1_000_000 : 100; |
812 |
+ |
int iterations = expensiveTests ? 500 : 2; |
813 |
+ |
ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(); |
814 |
+ |
for (int i = 0; i < mapSize; i++) |
815 |
+ |
map.put(i, i); |
816 |
+ |
Set<Integer> keySet = map.keySet(); |
817 |
+ |
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 }); |
818 |
+ |
for (int i = 0; i < iterations; i++) |
819 |
+ |
assertFalse(keySet.removeAll(removeMe)); |
820 |
+ |
assertEquals(mapSize, map.size()); |
821 |
+ |
} |
822 |
|
} |