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