Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/IdentityHashMap/ToArray.java
38811 views
/*1* Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4485486 6197726 623248426* @summary IdentityHashMap's entrySet toArray tests27* @author Josh Bloch, Martin Buchholz28*/2930import java.util.*;3132public class ToArray {33public static void main(String[] args) {34//----------------------------------------------------------------35// new ArrayList(IdentityHashMap.entrySet())36// used to return bogus entries.37//----------------------------------------------------------------38Map<String,String> mm = new IdentityHashMap<String,String>();39mm.put("foo", "bar");40mm.put("baz", "quux");41List<Map.Entry<String,String>> lm42= new ArrayList<Map.Entry<String,String>>(mm.entrySet());43String s = lm.toString();44if (! (s.equals("[foo=bar, baz=quux]") ||45s.equals("[baz=quux, foo=bar]")))46throw new Error("bad string");4748//----------------------------------------------------------------49// IdentityHashMap's lack of internal entry objects caused50// the inherited (AbstractMap) version of toArray to51// return garbage when called on the entrySet view.52//----------------------------------------------------------------53Map m = new IdentityHashMap();54m.put("french", "connection");55m.put("polish", "sausage");56Object[] mArray = m.entrySet().toArray();57if (mArray[0] == mArray[1])58throw new RuntimeException("Broken");5960mArray[0].toString();61mArray[1].toString();6263//----------------------------------------------------------------64// IdentityHashMap.entrySet().toArray(T[] a) used to simply65// return toArray() !66//----------------------------------------------------------------67IdentityHashMap<Integer,Integer> map68= new IdentityHashMap<Integer,Integer>();69Set<Map.Entry<Integer,Integer>> es = map.entrySet();70if (es.toArray().length != 0)71throw new Error("non-empty");72if (es.toArray(new Object[]{Boolean.TRUE})[0] != null)73throw new Error("non-null");74map.put(7,49);75if (es.toArray().length != 1)76throw new Error("length");77Object[] x = es.toArray(new Object[]{Boolean.TRUE, Boolean.TRUE});78if (x[1] != null)79throw new Error("non-null");80Map.Entry e = (Map.Entry) x[0];81if (! e.getKey().equals(new Integer(7)))82throw new Error("bad key");83if (! e.getValue().equals(new Integer(49)))84throw new Error("bad value");85}86}878889