1 |
jsr166 |
1.1 |
/* |
2 |
jsr166 |
1.6 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
3 |
jsr166 |
1.1 |
* 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 |
jsr166 |
1.3 |
* 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 |
jsr166 |
1.1 |
*/ |
23 |
|
|
|
24 |
jsr166 |
1.7 |
/* |
25 |
jsr166 |
1.1 |
* @test |
26 |
jsr166 |
1.6 |
* @bug 4245809 8029795 |
27 |
jsr166 |
1.1 |
* @summary Basic test for LinkedHashMap. (Based on MapBash) |
28 |
|
|
*/ |
29 |
|
|
|
30 |
jsr166 |
1.8 |
import java.io.ByteArrayInputStream; |
31 |
|
|
import java.io.ByteArrayOutputStream; |
32 |
|
|
import java.io.ObjectInputStream; |
33 |
|
|
import java.io.ObjectOutputStream; |
34 |
|
|
import java.util.ArrayList; |
35 |
|
|
import java.util.Collections; |
36 |
|
|
import java.util.HashMap; |
37 |
|
|
import java.util.Iterator; |
38 |
|
|
import java.util.LinkedHashMap; |
39 |
|
|
import java.util.List; |
40 |
|
|
import java.util.Map; |
41 |
|
|
import java.util.Objects; |
42 |
|
|
import java.util.Random; |
43 |
|
|
import java.util.Set; |
44 |
|
|
import java.util.function.BiFunction; |
45 |
jsr166 |
1.1 |
|
46 |
|
|
public class Basic { |
47 |
jsr166 |
1.6 |
static final Random rnd = new Random(666); |
48 |
|
|
static final Integer nil = new Integer(0); |
49 |
jsr166 |
1.1 |
|
50 |
jsr166 |
1.6 |
public static void main(String[] args) throws Exception { |
51 |
jsr166 |
1.1 |
int numItr = 500; |
52 |
|
|
int mapSize = 500; |
53 |
|
|
|
54 |
jsr166 |
1.6 |
// Linked List testk |
55 |
jsr166 |
1.1 |
for (int i=0; i<numItr; i++) { |
56 |
jsr166 |
1.6 |
Map<Integer,Integer> m = new LinkedHashMap(); |
57 |
|
|
Integer head = nil; |
58 |
jsr166 |
1.1 |
|
59 |
|
|
for (int j=0; j<mapSize; j++) { |
60 |
jsr166 |
1.6 |
Integer newHead; |
61 |
jsr166 |
1.1 |
do { |
62 |
|
|
newHead = new Integer(rnd.nextInt()); |
63 |
|
|
} while (m.containsKey(newHead)); |
64 |
|
|
m.put(newHead, head); |
65 |
|
|
head = newHead; |
66 |
|
|
} |
67 |
|
|
if (m.size() != mapSize) |
68 |
|
|
throw new Exception("Size not as expected."); |
69 |
|
|
|
70 |
|
|
if (new HashMap(m).hashCode() != m.hashCode()) |
71 |
|
|
throw new Exception("Incorrect hashCode computation."); |
72 |
|
|
|
73 |
jsr166 |
1.6 |
Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m); |
74 |
jsr166 |
1.1 |
m2.values().removeAll(m.keySet()); |
75 |
|
|
if (m2.size()!= 1 || !m2.containsValue(nil)) |
76 |
|
|
throw new Exception("Collection views test failed."); |
77 |
|
|
|
78 |
|
|
int j=0; |
79 |
|
|
while (head != nil) { |
80 |
|
|
if (!m.containsKey(head)) |
81 |
|
|
throw new Exception("Linked list doesn't contain a link."); |
82 |
jsr166 |
1.6 |
Integer newHead = m.get(head); |
83 |
jsr166 |
1.1 |
if (newHead == null) |
84 |
|
|
throw new Exception("Could not retrieve a link."); |
85 |
|
|
m.remove(head); |
86 |
|
|
head = newHead; |
87 |
|
|
j++; |
88 |
|
|
} |
89 |
|
|
if (!m.isEmpty()) |
90 |
|
|
throw new Exception("Map nonempty after removing all links."); |
91 |
|
|
if (j != mapSize) |
92 |
|
|
throw new Exception("Linked list size not as expected."); |
93 |
|
|
} |
94 |
|
|
|
95 |
jsr166 |
1.6 |
Map<Integer,Integer> m = new LinkedHashMap(); |
96 |
jsr166 |
1.1 |
for (int i=0; i<mapSize; i++) |
97 |
|
|
if (m.put(new Integer(i), new Integer(2*i)) != null) |
98 |
jsr166 |
1.4 |
throw new Exception("put returns non-null value erroneously."); |
99 |
jsr166 |
1.1 |
for (int i=0; i<2*mapSize; i++) |
100 |
|
|
if (m.containsValue(new Integer(i)) != (i%2==0)) |
101 |
|
|
throw new Exception("contains value "+i); |
102 |
|
|
if (m.put(nil, nil) == null) |
103 |
jsr166 |
1.4 |
throw new Exception("put returns a null value erroneously."); |
104 |
jsr166 |
1.6 |
Map<Integer,Integer> m2 = new LinkedHashMap(); m2.putAll(m); |
105 |
jsr166 |
1.1 |
if (!m.equals(m2)) |
106 |
|
|
throw new Exception("Clone not equal to original. (1)"); |
107 |
|
|
if (!m2.equals(m)) |
108 |
|
|
throw new Exception("Clone not equal to original. (2)"); |
109 |
jsr166 |
1.6 |
Set<Map.Entry<Integer,Integer>> s = m.entrySet(), s2 = m2.entrySet(); |
110 |
jsr166 |
1.1 |
if (!s.equals(s2)) |
111 |
|
|
throw new Exception("Clone not equal to original. (3)"); |
112 |
|
|
if (!s2.equals(s)) |
113 |
|
|
throw new Exception("Clone not equal to original. (4)"); |
114 |
|
|
if (!s.containsAll(s2)) |
115 |
|
|
throw new Exception("Original doesn't contain clone!"); |
116 |
|
|
if (!s2.containsAll(s)) |
117 |
|
|
throw new Exception("Clone doesn't contain original!"); |
118 |
|
|
|
119 |
|
|
m2 = serClone(m); |
120 |
|
|
if (!m.equals(m2)) |
121 |
|
|
throw new Exception("Serialize Clone not equal to original. (1)"); |
122 |
|
|
if (!m2.equals(m)) |
123 |
|
|
throw new Exception("Serialize Clone not equal to original. (2)"); |
124 |
|
|
s = m.entrySet(); s2 = m2.entrySet(); |
125 |
|
|
if (!s.equals(s2)) |
126 |
|
|
throw new Exception("Serialize Clone not equal to original. (3)"); |
127 |
|
|
if (!s2.equals(s)) |
128 |
|
|
throw new Exception("Serialize Clone not equal to original. (4)"); |
129 |
|
|
if (!s.containsAll(s2)) |
130 |
|
|
throw new Exception("Original doesn't contain Serialize clone!"); |
131 |
|
|
if (!s2.containsAll(s)) |
132 |
|
|
throw new Exception("Serialize Clone doesn't contain original!"); |
133 |
|
|
|
134 |
|
|
s2.removeAll(s); |
135 |
|
|
if (!m2.isEmpty()) |
136 |
|
|
throw new Exception("entrySet().removeAll failed."); |
137 |
|
|
|
138 |
|
|
m2.putAll(m); |
139 |
|
|
m2.clear(); |
140 |
|
|
if (!m2.isEmpty()) |
141 |
|
|
throw new Exception("clear failed."); |
142 |
|
|
|
143 |
|
|
Iterator it = m.entrySet().iterator(); |
144 |
jsr166 |
1.2 |
while (it.hasNext()) { |
145 |
jsr166 |
1.1 |
it.next(); |
146 |
|
|
it.remove(); |
147 |
|
|
} |
148 |
|
|
if (!m.isEmpty()) |
149 |
|
|
throw new Exception("Iterator.remove() failed"); |
150 |
|
|
|
151 |
|
|
// Test ordering properties with insert order |
152 |
|
|
m = new LinkedHashMap(); |
153 |
jsr166 |
1.6 |
List<Integer> l = new ArrayList(mapSize); |
154 |
jsr166 |
1.1 |
for (int i=0; i<mapSize; i++) { |
155 |
|
|
Integer x = new Integer(i); |
156 |
|
|
m.put(x, x); |
157 |
|
|
l.add(x); |
158 |
|
|
} |
159 |
|
|
if (!new ArrayList(m.keySet()).equals(l)) |
160 |
|
|
throw new Exception("Insertion order not preserved."); |
161 |
|
|
for (int i=mapSize-1; i>=0; i--) { |
162 |
|
|
Integer x = (Integer) l.get(i); |
163 |
|
|
if (!m.get(x).equals(x)) |
164 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
165 |
|
|
} |
166 |
|
|
if (!new ArrayList(m.keySet()).equals(l)) |
167 |
|
|
throw new Exception("Insertion order not preserved after read."); |
168 |
|
|
|
169 |
|
|
for (int i=mapSize-1; i>=0; i--) { |
170 |
|
|
Integer x = (Integer) l.get(i); |
171 |
|
|
m.put(x, x); |
172 |
|
|
} |
173 |
|
|
if (!new ArrayList(m.keySet()).equals(l)) |
174 |
|
|
throw new Exception("Insert order not preserved after reinsert."); |
175 |
|
|
|
176 |
|
|
m2 = (Map) ((LinkedHashMap)m).clone(); |
177 |
|
|
if (!m.equals(m2)) |
178 |
|
|
throw new Exception("Insert-order Map != clone."); |
179 |
|
|
|
180 |
jsr166 |
1.6 |
List<Integer> l2 = new ArrayList(l); |
181 |
jsr166 |
1.1 |
Collections.shuffle(l2); |
182 |
|
|
for (int i=0; i<mapSize; i++) { |
183 |
|
|
Integer x = (Integer) l2.get(i); |
184 |
|
|
if (!m2.get(x).equals(x)) |
185 |
|
|
throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x); |
186 |
|
|
} |
187 |
|
|
if (!new ArrayList(m2.keySet()).equals(l)) |
188 |
|
|
throw new Exception("Clone: altered by read."); |
189 |
|
|
|
190 |
|
|
// Test ordering properties with access order |
191 |
jsr166 |
1.6 |
m = new LinkedHashMap(2*mapSize, .75f, true); |
192 |
jsr166 |
1.1 |
for (int i=0; i<mapSize; i++) { |
193 |
|
|
Integer x = new Integer(i); |
194 |
|
|
m.put(x, x); |
195 |
|
|
} |
196 |
|
|
if (!new ArrayList(m.keySet()).equals(l)) |
197 |
|
|
throw new Exception("Insertion order not properly preserved."); |
198 |
|
|
|
199 |
|
|
for (int i=0; i<mapSize; i++) { |
200 |
|
|
Integer x = (Integer) l2.get(i); |
201 |
|
|
if (!m.get(x).equals(x)) |
202 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
203 |
|
|
} |
204 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
205 |
|
|
throw new Exception("Insert order not properly altered by read."); |
206 |
|
|
|
207 |
|
|
for (int i=0; i<mapSize; i++) { |
208 |
jsr166 |
1.6 |
Integer x = (Integer) l2.get(i); |
209 |
|
|
if (!m.getOrDefault(x, new Integer(i + 1000)).equals(x)) |
210 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
211 |
|
|
} |
212 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
213 |
|
|
throw new Exception("Insert order not properly altered by read."); |
214 |
|
|
|
215 |
|
|
for (int i=0; i<mapSize; i++) { |
216 |
|
|
Integer x = (Integer) l2.get(i); |
217 |
|
|
if (!m.replace(x, x).equals(x)) |
218 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
219 |
|
|
} |
220 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
221 |
|
|
throw new Exception("Insert order not properly altered by replace."); |
222 |
|
|
|
223 |
|
|
for (int i=0; i<mapSize; i++) { |
224 |
|
|
Integer x = (Integer) l2.get(i); |
225 |
|
|
if (!m.replace(x, x, x)) |
226 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
227 |
|
|
} |
228 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
229 |
|
|
throw new Exception("Insert order not properly altered by replace."); |
230 |
|
|
|
231 |
|
|
BiFunction<Integer,Integer,Integer> f = (Integer y, Integer z) -> { |
232 |
|
|
if (!Objects.equals(y,z)) |
233 |
|
|
throw new RuntimeException("unequal " + y + "," + z); |
234 |
|
|
return new Integer(z); |
235 |
|
|
}; |
236 |
|
|
|
237 |
|
|
for (int i=0; i<mapSize; i++) { |
238 |
|
|
Integer x = (Integer) l2.get(i); |
239 |
|
|
if (!x.equals(m.merge(x, x, f))) |
240 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
241 |
|
|
} |
242 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
243 |
|
|
throw new Exception("Insert order not properly altered by replace."); |
244 |
|
|
|
245 |
|
|
for (int i=0; i<mapSize; i++) { |
246 |
|
|
Integer x = (Integer) l2.get(i); |
247 |
|
|
if (!x.equals(m.compute(x, f))) |
248 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
249 |
|
|
} |
250 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
251 |
|
|
throw new Exception("Insert order not properly altered by replace."); |
252 |
|
|
|
253 |
|
|
for (int i=0; i<mapSize; i++) { |
254 |
|
|
Integer x = (Integer) l2.get(i); |
255 |
|
|
if (!x.equals(m.remove(x))) |
256 |
|
|
throw new Exception("Missing key: "+i+", "+x); |
257 |
|
|
if (!x.equals(m.computeIfAbsent(x, Integer::valueOf))) |
258 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
259 |
|
|
} |
260 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
261 |
|
|
throw new Exception("Insert order not properly altered by replace."); |
262 |
|
|
|
263 |
|
|
for (int i=0; i<mapSize; i++) { |
264 |
|
|
Integer x = (Integer) l2.get(i); |
265 |
|
|
if (!x.equals(m.computeIfPresent(x, f))) |
266 |
|
|
throw new Exception("Wrong value: "+i+", "+m.get(x)+", "+x); |
267 |
|
|
} |
268 |
|
|
if (!new ArrayList(m.keySet()).equals(l2)) |
269 |
|
|
throw new Exception("Insert order not properly altered by replace."); |
270 |
|
|
|
271 |
|
|
for (int i=0; i<mapSize; i++) { |
272 |
jsr166 |
1.1 |
Integer x = new Integer(i); |
273 |
|
|
m.put(x, x); |
274 |
|
|
} |
275 |
|
|
if (!new ArrayList(m.keySet()).equals(l)) |
276 |
|
|
throw new Exception("Insertion order not altered by reinsert."); |
277 |
|
|
|
278 |
|
|
m2 = (Map) ((LinkedHashMap)m).clone(); |
279 |
|
|
if (!m.equals(m2)) |
280 |
|
|
throw new Exception("Access-order Map != clone."); |
281 |
|
|
for (int i=0; i<mapSize; i++) { |
282 |
|
|
Integer x = (Integer) l.get(i); |
283 |
|
|
if (!m2.get(x).equals(x)) |
284 |
|
|
throw new Exception("Clone: Wrong val: "+i+", "+m.get(x)+", "+x); |
285 |
|
|
} |
286 |
|
|
if (!new ArrayList(m2.keySet()).equals(l)) |
287 |
|
|
throw new Exception("Clone: order not properly altered by read."); |
288 |
|
|
|
289 |
|
|
System.err.println("Success."); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
private static Map serClone(Map m) { |
293 |
|
|
Map result = null; |
294 |
|
|
try { |
295 |
|
|
// Serialize |
296 |
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
297 |
|
|
ObjectOutputStream out = new ObjectOutputStream(bos); |
298 |
|
|
out.writeObject(m); |
299 |
|
|
out.flush(); |
300 |
|
|
|
301 |
|
|
// Deserialize |
302 |
|
|
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
303 |
|
|
out.close(); |
304 |
|
|
ObjectInputStream in = new ObjectInputStream(bis); |
305 |
|
|
result = (Map)in.readObject(); |
306 |
|
|
in.close(); |
307 |
jsr166 |
1.2 |
} catch (Exception e) { |
308 |
jsr166 |
1.1 |
e.printStackTrace(); |
309 |
|
|
} |
310 |
|
|
return result; |
311 |
|
|
} |
312 |
|
|
} |