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