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