1 /*
2 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
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 8072015 8292955
27 * @summary Unit test for Collections.checkedMap
28 * @author Josh Bloch
29 * @run testng CheckedMapBash
30 * @key randomness
31 */
32
33 import org.testng.Assert;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
44 import java.util.Random;
45 import java.util.Set;
46 import java.util.TreeMap;
47 import java.util.function.Supplier;
48
49 import static org.testng.Assert.fail;
50
51 public class CheckedMapBash {
52 static final Random rnd = new Random();
53 static final Object nil = new Integer(0);
54 static final int numItr = 100;
55 static final int mapSize = 100;
56
57 @Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>")
58 public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) {
59 Map m = supplier.get();
60 Object head = nil;
61
62 for (int j=0; j<mapSize; j++) {
63 Object newHead;
64 do {
65 newHead = new Integer(rnd.nextInt());
66 } while (m.containsKey(newHead) || newHead.equals(nil));
67 m.put(newHead, head);
68 head = newHead;
69 }
70 if (m.size() != mapSize)
71 fail("Size not as expected.");
72
73 {
74 HashMap hm = new HashMap(m);
75 if (! (hm.hashCode() == m.hashCode() &&
76 hm.entrySet().hashCode() == m.entrySet().hashCode() &&
77 hm.keySet().hashCode() == m.keySet().hashCode()))
78 fail("Incorrect hashCode computation.");
79
80 if (! (hm.equals(m) &&
81 hm.entrySet().equals(m.entrySet()) &&
82 hm.keySet().equals(m.keySet()) &&
83 m.equals(hm) &&
84 m.entrySet().equals(hm.entrySet()) &&
85 m.keySet().equals(hm.keySet())))
86 fail("Incorrect equals computation.");
87 }
88
89 Map m2 = supplier.get(); m2.putAll(m);
90 m2.values().removeAll(m.keySet());
91 if (m2.size()!= 1 || !m2.containsValue(nil))
92 fail("Collection views test failed.");
93
94 int j=0;
95 while (head != nil) {
96 if (!m.containsKey(head))
97 fail("Linked list doesn't contain a link.");
98 Object newHead = m.get(head);
99 if (newHead == null)
100 fail("Could not retrieve a link.");
101 m.remove(head);
102 head = newHead;
103 j++;
104 }
105 if (!m.isEmpty())
106 fail("Map nonempty after removing all links.");
107 if (j != mapSize)
108 fail("Linked list size not as expected.");
109 }
110
111 @Test(dataProvider = "Supplier<Map<Integer,Integer>>")
112 public static void testCheckedMap2(String description, Supplier<Map<Integer,Integer>> supplier) {
113 Map m = supplier.get();
114 for (int i=0; i<mapSize; i++)
115 if (m.put(new Integer(i), new Integer(2*i)) != null)
116 fail("put returns a non-null value erroneously.");
117 for (int i=0; i<2*mapSize; i++)
118 if (m.containsValue(new Integer(i)) != (i%2==0))
119 fail("contains value "+i);
120 if (m.put(nil, nil) == null)
121 fail("put returns a null value erroneously.");
122 Map m2 = supplier.get(); m2.putAll(m);
123 if (!m.equals(m2))
124 fail("Clone not equal to original. (1)");
125 if (!m2.equals(m))
126 fail("Clone not equal to original. (2)");
127 Set s = m.entrySet(), s2 = m2.entrySet();
128 if (!s.equals(s2))
129 fail("Clone not equal to original. (3)");
130 if (!s2.equals(s))
131 fail("Clone not equal to original. (4)");
132 if (!s.containsAll(s2))
133 fail("Original doesn't contain clone!");
134 if (!s2.containsAll(s))
135 fail("Clone doesn't contain original!");
136
137 s2.removeAll(s);
138 if (!m2.isEmpty())
139 fail("entrySet().removeAll failed.");
140
141 m2.putAll(m);
142 m2.clear();
143 if (!m2.isEmpty())
144 fail("clear failed.");
145
146 Iterator i = m.entrySet().iterator();
147 while (i.hasNext()) {
148 i.next();
149 i.remove();
150 }
151 if (!m.isEmpty())
152 fail("Iterator.remove() failed");
153 }
154
155 @DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)
156 public static Iterator<Object[]> bashNavigableMapProvider() {
157 ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());
158 iters.ensureCapacity(numItr * iters.size());
159 for (int each=1; each < numItr; each++) {
160 iters.addAll(makeCheckedMaps());
161 }
162 return iters.iterator();
163 }
164
165 @DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)
166 public static Iterator<Object[]> navigableMapProvider() {
167 return makeCheckedMaps().iterator();
168 }
169
170 public static Collection<Object[]> makeCheckedMaps() {
171 Object[][] params = {
172 {"Collections.checkedMap(HashMap)",
173 (Supplier) () -> Collections.checkedMap(new HashMap(), Integer.class, Integer.class)},
174 {"Collections.checkedMap(TreeMap(reverseOrder))",
175 (Supplier) () -> Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},
176 {"Collections.checkedMap(TreeMap.descendingMap())",
177 (Supplier) () -> Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},
178 {"Collections.checkedNavigableMap(TreeMap)",
179 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class)},
180 {"Collections.checkedNavigableMap(TreeMap(reverseOrder))",
181 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)},
182 {"Collections.checkedNavigableMap(TreeMap.descendingMap())",
183 (Supplier) () -> Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class)},
184 };
185 return Arrays.asList(params);
186 }
187
188 @Test(groups = "type_check")
189 public static void testCheckedMapMerge() {
190 Map m = Collections.checkedMap(new HashMap<>(), Integer.class, Integer.class);
191 Assert.assertThrows(ClassCastException.class, () -> m.merge("key", "value", (v1, v2) -> null));
192 Assert.assertThrows(ClassCastException.class, () -> m.merge("key", 3, (v1, v2) -> v2));
193 }
194 }