1 |
jsr166 |
1.1 |
/* |
2 |
jsr166 |
1.2 |
* Copyright (c) 2001, 2005, 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.2 |
* 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 |
|
|
/* |
25 |
|
|
* @test |
26 |
|
|
* @bug 4485486 6197726 6232484 |
27 |
|
|
* @summary IdentityHashMap's entrySet toArray tests |
28 |
|
|
* @author Josh Bloch, Martin Buchholz |
29 |
|
|
*/ |
30 |
|
|
|
31 |
|
|
import java.util.*; |
32 |
|
|
|
33 |
|
|
public class ToArray { |
34 |
|
|
public static void main(String[] args) { |
35 |
|
|
//---------------------------------------------------------------- |
36 |
|
|
// new ArrayList(IdentityHashMap.entrySet()) |
37 |
|
|
// used to return bogus entries. |
38 |
|
|
//---------------------------------------------------------------- |
39 |
jsr166 |
1.3 |
Map<String,String> mm = new IdentityHashMap<>(); |
40 |
jsr166 |
1.1 |
mm.put("foo", "bar"); |
41 |
|
|
mm.put("baz", "quux"); |
42 |
jsr166 |
1.3 |
List<Map.Entry<String,String>> lm = new ArrayList<>(mm.entrySet()); |
43 |
jsr166 |
1.1 |
String s = lm.toString(); |
44 |
|
|
if (! (s.equals("[foo=bar, baz=quux]") || |
45 |
|
|
s.equals("[baz=quux, foo=bar]"))) |
46 |
|
|
throw new Error("bad string"); |
47 |
|
|
|
48 |
|
|
//---------------------------------------------------------------- |
49 |
|
|
// IdentityHashMap's lack of internal entry objects caused |
50 |
|
|
// the inherited (AbstractMap) version of toArray to |
51 |
|
|
// return garbage when called on the entrySet view. |
52 |
|
|
//---------------------------------------------------------------- |
53 |
|
|
Map m = new IdentityHashMap(); |
54 |
|
|
m.put("french", "connection"); |
55 |
|
|
m.put("polish", "sausage"); |
56 |
|
|
Object[] mArray = m.entrySet().toArray(); |
57 |
|
|
if (mArray[0] == mArray[1]) |
58 |
|
|
throw new RuntimeException("Broken"); |
59 |
|
|
|
60 |
|
|
mArray[0].toString(); |
61 |
|
|
mArray[1].toString(); |
62 |
|
|
|
63 |
|
|
//---------------------------------------------------------------- |
64 |
|
|
// IdentityHashMap.entrySet().toArray(T[] a) used to simply |
65 |
|
|
// return toArray() ! |
66 |
|
|
//---------------------------------------------------------------- |
67 |
jsr166 |
1.3 |
IdentityHashMap<Integer,Integer> map = new IdentityHashMap<>(); |
68 |
jsr166 |
1.1 |
Set<Map.Entry<Integer,Integer>> es = map.entrySet(); |
69 |
|
|
if (es.toArray().length != 0) |
70 |
|
|
throw new Error("non-empty"); |
71 |
|
|
if (es.toArray(new Object[]{Boolean.TRUE})[0] != null) |
72 |
|
|
throw new Error("non-null"); |
73 |
|
|
map.put(7,49); |
74 |
|
|
if (es.toArray().length != 1) |
75 |
|
|
throw new Error("length"); |
76 |
|
|
Object[] x = es.toArray(new Object[]{Boolean.TRUE, Boolean.TRUE}); |
77 |
|
|
if (x[1] != null) |
78 |
|
|
throw new Error("non-null"); |
79 |
|
|
Map.Entry e = (Map.Entry) x[0]; |
80 |
|
|
if (! e.getKey().equals(new Integer(7))) |
81 |
|
|
throw new Error("bad key"); |
82 |
|
|
if (! e.getValue().equals(new Integer(49))) |
83 |
|
|
throw new Error("bad value"); |
84 |
|
|
} |
85 |
|
|
} |