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.6 by dl, Thu Apr 11 19:15:20 2013 UTC vs.
Revision 1.8 by jsr166, Thu Apr 11 20:03:44 2013 UTC

# Line 34 | Line 34 | public class ConcurrentHashMap8Test exte
34          return map;
35      }
36  
37    // classes for testing Comparable fallbacks
38    static class BI implements Comparable<BI> {
39        private final int value;
40        BI(int value) { this.value = value; }
41        public int compareTo(BI other) {
42            return Integer.compare(value, other.value);
43        }
44        public boolean equals(Object x) {
45            return (x instanceof BI) && ((BI)x).value == value;
46        }
47        public int hashCode() { return 42; }
48    }
49    static class CI extends BI { CI(int value) { super(value); } }
50    static class DI extends BI { DI(int value) { super(value); } }
51
52    static class BS implements Comparable<BS> {
53        private final String value;
54        BS(String value) { this.value = value; }
55        public int compareTo(BS other) {
56            return value.compareTo(other.value);
57        }
58        public boolean equals(Object x) {
59            return (x instanceof BS) && value.equals(((BS)x).value);
60        }
61        public int hashCode() { return 42; }
62    }
63
64    static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
65        implements Comparable<LexicographicList<E>> {
66        LexicographicList(Collection<E> c) { super(c); }
67        LexicographicList(E e) { super(Collections.singleton(e)); }
68        public int compareTo(LexicographicList<E> other) {
69            int common = Math.min(size(), other.size());
70            int r = 0;
71            for (int i = 0; i < common; i++) {
72                if ((r = get(i).compareTo(other.get(i))) != 0)
73                    break;
74            }
75            if (r == 0)
76                r = Integer.compare(size(), other.size());
77            return r;
78        }
79        private static final long serialVersionUID = 0;
80    }
81
82    /**
83     * Inserted elements that are subclasses of the same Comparable
84     * class are found.
85     */
86    public void testComparableFamily() {
87        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
88        for (int i = 0; i < 1000; i++) {
89            assertTrue(m.put(new CI(i), true) == null);
90        }
91        for (int i = 0; i < 1000; i++) {
92            assertTrue(m.containsKey(new CI(i)));
93            assertTrue(m.containsKey(new DI(i)));
94        }
95    }
96
97    /**
98     * Elements of classes with erased generic type parameters based
99     * on Comparable can be inserted and found.
100     */
101    public void testGenericComparable() {
102        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
103        for (int i = 0; i < 1000; i++) {
104            BI bi = new BI(i);
105            BS bs = new BS(String.valueOf(i));
106            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
107            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
108            assertTrue(m.putIfAbsent(bis, true) == null);
109            assertTrue(m.containsKey(bis));
110            if (m.putIfAbsent(bss, true) == null)
111                assertTrue(m.containsKey(bss));
112            assertTrue(m.containsKey(bis));
113        }
114        for (int i = 0; i < 1000; i++) {
115            assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i)))));
116        }
117    }
118
119    /**
120     * Elements of non-comparable classes equal to those of classes
121     * with erased generic type parameters based on Comparable can be
122     * inserted and found.
123     */
124    public void testGenericComparable2() {
125        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
126        for (int i = 0; i < 1000; i++) {
127            m.put(new ArrayList(Collections.singleton(new BI(i))), true);
128        }
129
130        for (int i = 0; i < 1000; i++) {
131            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
132            assertTrue(m.containsKey(bis));
133        }
134    }
135
37      /**
38       * getOrDefault returns value if present, else default
39       */
# Line 798 | Line 699 | public class ConcurrentHashMap8Test exte
699          assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2);
700      }
701  
702 <    /*
702 >    /**
703       * Mapped reduceKeysSequentially accumulates mapped keys
704       */
705      public void testMapReduceKeysSequentially() {
# Line 808 | Line 709 | public class ConcurrentHashMap8Test exte
709          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
710      }
711  
712 <    /*
712 >    /**
713       * Mapped reduceValuesSequentially accumulates mapped values
714       */
715      public void testMapReduceValuesSequentially() {
# Line 829 | Line 730 | public class ConcurrentHashMap8Test exte
730          assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2);
731      }
732  
733 <    /*
733 >    /**
734       * Mapped reduceKeysInParallel, accumulates mapped keys
735       */
736      public void testMapReduceKeysInParallel() {
# Line 839 | Line 740 | public class ConcurrentHashMap8Test exte
740          assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2);
741      }
742  
743 <    /*
743 >    /**
744       * Mapped reduceValuesInParallel accumulates mapped values
745       */
746      public void testMapReduceValuesInParallel() {
# Line 861 | Line 762 | public class ConcurrentHashMap8Test exte
762      }
763  
764  
765 <    /*
765 >    /**
766       * reduceKeysToLongSequentially accumulates mapped keys
767       */
768      public void testReduceKeysToLongSequentially() {
# Line 870 | Line 771 | public class ConcurrentHashMap8Test exte
771          assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
772      }
773  
774 <    /*
774 >    /**
775       * reduceKeysToIntSequentially accumulates mapped keys
776       */
777      public void testReduceKeysToIntSequentially() {
# Line 879 | Line 780 | public class ConcurrentHashMap8Test exte
780          assertEquals(ir, SIZE * (SIZE - 1) / 2);
781      }
782  
783 <    /*
783 >    /**
784       * reduceKeysToDoubleSequentially accumulates mapped keys
785       */
786      public void testReduceKeysToDoubleSequentially() {
# Line 888 | Line 789 | public class ConcurrentHashMap8Test exte
789          assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
790      }
791  
792 <    /*
792 >    /**
793       * reduceValuesToLongSequentially accumulates mapped values
794       */
795      public void testReduceValuesToLongSequentially() {
# Line 897 | Line 798 | public class ConcurrentHashMap8Test exte
798          assertEquals(lr, (long)SIZE * (SIZE - 1));
799      }
800  
801 <    /*
801 >    /**
802       * reduceValuesToIntSequentially accumulates mapped values
803       */
804      public void testReduceValuesToIntSequentially() {
# Line 906 | Line 807 | public class ConcurrentHashMap8Test exte
807          assertEquals(ir, SIZE * (SIZE - 1));
808      }
809  
810 <    /*
810 >    /**
811       * reduceValuesToDoubleSequentially accumulates mapped values
812       */
813      public void testReduceValuesToDoubleSequentially() {
# Line 915 | Line 816 | public class ConcurrentHashMap8Test exte
816          assertEquals(dr, (double)SIZE * (SIZE - 1));
817      }
818  
819 <    /*
819 >    /**
820       * reduceKeysToLongInParallel accumulates mapped keys
821       */
822      public void testReduceKeysToLongInParallel() {
# Line 924 | Line 825 | public class ConcurrentHashMap8Test exte
825          assertEquals(lr, (long)SIZE * (SIZE - 1) / 2);
826      }
827  
828 <    /*
828 >    /**
829       * reduceKeysToIntInParallel accumulates mapped keys
830       */
831      public void testReduceKeysToIntInParallel() {
# Line 933 | Line 834 | public class ConcurrentHashMap8Test exte
834          assertEquals(ir, SIZE * (SIZE - 1) / 2);
835      }
836  
837 <    /*
837 >    /**
838       * reduceKeysToDoubleInParallel accumulates mapped values
839       */
840      public void testReduceKeysToDoubleInParallel() {
# Line 942 | Line 843 | public class ConcurrentHashMap8Test exte
843          assertEquals(dr, (double)SIZE * (SIZE - 1) / 2);
844      }
845  
846 <    /*
846 >    /**
847       * reduceValuesToLongInParallel accumulates mapped values
848       */
849      public void testReduceValuesToLongInParallel() {
# Line 951 | Line 852 | public class ConcurrentHashMap8Test exte
852          assertEquals(lr, (long)SIZE * (SIZE - 1));
853      }
854  
855 <    /*
855 >    /**
856       * reduceValuesToIntInParallel accumulates mapped values
857       */
858      public void testReduceValuesToIntInParallel() {
# Line 960 | Line 861 | public class ConcurrentHashMap8Test exte
861          assertEquals(ir, SIZE * (SIZE - 1));
862      }
863  
864 <    /*
864 >    /**
865       * reduceValuesToDoubleInParallel accumulates mapped values
866       */
867      public void testReduceValuesToDoubleInParallel() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines