6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 4904067 5023830
27 * @summary Unit test for Collections.checkedMap
28 * @author Josh Bloch
29 */
30
31 import java.util.*;
32
33 public class CheckedMapBash {
34 static Random rnd = new Random();
35 static Object nil = new Integer(0);
36
37 public static void main(String[] args) {
38 int numItr = 100;
39 int mapSize = 100;
40
41 // Linked List test
42 for (int i=0; i<numItr; i++) {
43 Map m = newMap();
44 Object head = nil;
45
46 for (int j=0; j<mapSize; j++) {
47 Object newHead;
48 do {
49 newHead = new Integer(rnd.nextInt());
50 } while (m.containsKey(newHead));
51 m.put(newHead, head);
52 head = newHead;
53 }
54 if (m.size() != mapSize)
55 fail("Size not as expected.");
56
57 {
58 HashMap hm = new HashMap(m);
59 if (! (hm.hashCode() == m.hashCode() &&
60 hm.entrySet().hashCode() == m.entrySet().hashCode() &&
61 hm.keySet().hashCode() == m.keySet().hashCode()))
62 fail("Incorrect hashCode computation.");
63
64 if (! (hm.equals(m) &&
65 hm.entrySet().equals(m.entrySet()) &&
66 hm.keySet().equals(m.keySet()) &&
67 m.equals(hm) &&
68 m.entrySet().equals(hm.entrySet()) &&
69 m.keySet().equals(hm.keySet())))
70 fail("Incorrect equals computation.");
71 }
72
73 Map m2 = newMap(); m2.putAll(m);
74 m2.values().removeAll(m.keySet());
75 if (m2.size()!= 1 || !m2.containsValue(nil))
76 fail("Collection views test failed.");
77
78 int j=0;
79 while (head != nil) {
80 if (!m.containsKey(head))
81 fail("Linked list doesn't contain a link.");
82 Object newHead = m.get(head);
83 if (newHead == null)
84 fail("Could not retrieve a link.");
85 m.remove(head);
86 head = newHead;
87 j++;
88 }
89 if (!m.isEmpty())
90 fail("Map nonempty after removing all links.");
91 if (j != mapSize)
92 fail("Linked list size not as expected.");
93 }
94
95 Map m = newMap();
96 for (int i=0; i<mapSize; i++)
97 if (m.put(new Integer(i), new Integer(2*i)) != null)
98 fail("put returns a non-null value erroenously.");
99 for (int i=0; i<2*mapSize; i++)
100 if (m.containsValue(new Integer(i)) != (i%2==0))
101 fail("contains value "+i);
102 if (m.put(nil, nil) == null)
103 fail("put returns a null value erroenously.");
104 Map m2 = newMap(); m2.putAll(m);
105 if (!m.equals(m2))
106 fail("Clone not equal to original. (1)");
107 if (!m2.equals(m))
108 fail("Clone not equal to original. (2)");
109 Set s = m.entrySet(), s2 = m2.entrySet();
110 if (!s.equals(s2))
111 fail("Clone not equal to original. (3)");
112 if (!s2.equals(s))
113 fail("Clone not equal to original. (4)");
114 if (!s.containsAll(s2))
115 fail("Original doesn't contain clone!");
116 if (!s2.containsAll(s))
117 fail("Clone doesn't contain original!");
118
119 s2.removeAll(s);
120 if (!m2.isEmpty())
121 fail("entrySet().removeAll failed.");
122
123 m2.putAll(m);
124 m2.clear();
125 if (!m2.isEmpty())
126 fail("clear failed.");
127
128 Iterator i = m.entrySet().iterator();
129 while(i.hasNext()) {
130 i.next();
131 i.remove();
132 }
133 if (!m.isEmpty())
134 fail("Iterator.remove() failed");
135 }
136
137 static Map newMap() {
138 Map m = Collections.checkedMap(new HashMap(),
139 Integer.class, Integer.class);
140
141 if (!m.isEmpty())
142 fail("New instance non empty.");
143 return m;
144 }
145
146 static void fail(String s) {
147 throw new RuntimeException(s);
148 }
149 }
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 4904067 5023830 7129185
27 * @summary Unit test for Collections.checkedMap
28 * @author Josh Bloch
29 * @run testng CheckedMapBash
30 */
31
32 import java.util.*;
33 import java.util.function.Supplier;
34 import org.testng.annotations.Test;
35 import org.testng.annotations.DataProvider;
36
37 import static org.testng.Assert.fail;
38 import static org.testng.Assert.assertTrue;
39
40 public class CheckedMapBash {
41 static final Random rnd = new Random();
42 static final Object nil = new Integer(0);
43 static final int numItr = 100;
44 static final int mapSize = 100;
45
46 @Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>")
47 public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) {
48 Map m = supplier.get();
49 Object head = nil;
50
51 for (int j=0; j<mapSize; j++) {
52 Object newHead;
53 do {
54 newHead = new Integer(rnd.nextInt());
55 } while (m.containsKey(newHead));
56 m.put(newHead, head);
57 head = newHead;
58 }
59 if (m.size() != mapSize)
60 fail("Size not as expected.");
61
62 {
63 HashMap hm = new HashMap(m);
64 if (! (hm.hashCode() == m.hashCode() &&
65 hm.entrySet().hashCode() == m.entrySet().hashCode() &&
66 hm.keySet().hashCode() == m.keySet().hashCode()))
67 fail("Incorrect hashCode computation.");
68
69 if (! (hm.equals(m) &&
70 hm.entrySet().equals(m.entrySet()) &&
71 hm.keySet().equals(m.keySet()) &&
72 m.equals(hm) &&
73 m.entrySet().equals(hm.entrySet()) &&
74 m.keySet().equals(hm.keySet())))
75 fail("Incorrect equals computation.");
76 }
77
78 Map m2 = supplier.get(); m2.putAll(m);
79 m2.values().removeAll(m.keySet());
80 if (m2.size()!= 1 || !m2.containsValue(nil))
81 fail("Collection views test failed.");
82
83 int j=0;
84 while (head != nil) {
85 if (!m.containsKey(head))
86 fail("Linked list doesn't contain a link.");
87 Object newHead = m.get(head);
88 if (newHead == null)
89 fail("Could not retrieve a link.");
90 m.remove(head);
91 head = newHead;
92 j++;
93 }
94 if (!m.isEmpty())
95 fail("Map nonempty after removing all links.");
96 if (j != mapSize)
97 fail("Linked list size not as expected.");
98 }
99
100 @Test(dataProvider = "Supplier<Map<Integer,Integer>>")
101 public static void testCheckeMap2(String description, Supplier<Map<Integer,Integer>> supplier) {
102 Map m = supplier.get();
103 for (int i=0; i<mapSize; i++)
104 if (m.put(new Integer(i), new Integer(2*i)) != null)
105 fail("put returns a non-null value erroenously.");
106 for (int i=0; i<2*mapSize; i++)
107 if (m.containsValue(new Integer(i)) != (i%2==0))
108 fail("contains value "+i);
109 if (m.put(nil, nil) == null)
110 fail("put returns a null value erroenously.");
111 Map m2 = supplier.get(); m2.putAll(m);
112 if (!m.equals(m2))
113 fail("Clone not equal to original. (1)");
114 if (!m2.equals(m))
115 fail("Clone not equal to original. (2)");
116 Set s = m.entrySet(), s2 = m2.entrySet();
117 if (!s.equals(s2))
118 fail("Clone not equal to original. (3)");
119 if (!s2.equals(s))
120 fail("Clone not equal to original. (4)");
121 if (!s.containsAll(s2))
122 fail("Original doesn't contain clone!");
123 if (!s2.containsAll(s))
124 fail("Clone doesn't contain original!");
125
126 s2.removeAll(s);
127 if (!m2.isEmpty())
128 fail("entrySet().removeAll failed.");
129
130 m2.putAll(m);
131 m2.clear();
132 if (!m2.isEmpty())
133 fail("clear failed.");
134
135 Iterator i = m.entrySet().iterator();
136 while(i.hasNext()) {
137 i.next();
138 i.remove();
139 }
140 if (!m.isEmpty())
141 fail("Iterator.remove() failed");
142 }
143
144
145 @DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)
146 public static Iterator<Object[]> bashNavigableMapProvider() {
147 ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());
148 iters.ensureCapacity(numItr * iters.size());
149 for(int each=1; each < numItr; each++) {
150 iters.addAll( makeCheckedMaps());
151 }
152 return iters.iterator();
153 }
154
155 @DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)
156 public static Iterator<Object[]> navigableMapProvider() {
157 return makeCheckedMaps().iterator();
158 }
159
160 public static Collection<Object[]> makeCheckedMaps() {
161 return Arrays.asList(
162 new Object[]{"Collections.checkedMap(HashMap)",
163 (Supplier) () -> {return Collections.checkedMap(new HashMap(), Integer.class, Integer.class);}},
164 new Object[]{"Collections.checkedMap(TreeSet(reverseOrder)",
165 (Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
166 new Object[]{"Collections.checkedMap(TreeSet).descendingSet()",
167 (Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}},
168 new Object[]{"Collections.checkedNavigableMap(TreeSet)",
169 (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}},
170 new Object[]{"Collections.checkedNavigableMap(TreeSet(reverseOrder)",
171 (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
172 new Object[]{"Collections.checkedNavigableMap().descendingSet()",
173 (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}
174 );
175 }
176 }
|