Path: blob/master/test/jdk/java/util/EnumMap/ToArray.java
41149 views
/*1* Copyright (c) 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 631085826* @summary Tests for toArray27* @author Martin Buchholz28*/2930import java.util.*;3132public class ToArray {33enum Country { FRENCH, POLISH }34public static void main(String[] args) throws Throwable {35Map<Country, String> m = new EnumMap<Country, String>(Country.class);36m.put(Country.FRENCH, "connection");37m.put(Country.POLISH, "sausage");3839Object[] z = m.entrySet().toArray();40System.out.println(Arrays.toString(z));41if (! (z.getClass() == Object[].class &&42z.length == 2 &&43((Map.Entry)z[0]).getKey() == Country.FRENCH &&44((Map.Entry)z[1]).getKey() == Country.POLISH))45throw new AssertionError();4647Map.Entry[] x1 = new Map.Entry[3];48x1[2] = m.entrySet().iterator().next();49Map.Entry[] x2 = m.entrySet().toArray(x1);50System.out.println(Arrays.toString(x2));51if (! (x1 == x2 &&52x2[0].getKey() == Country.FRENCH &&53x2[1].getKey() == Country.POLISH &&54x2[2] == null))55throw new AssertionError();5657Map.Entry[] y1 = new Map.Entry[1];58Map.Entry[] y2 = m.entrySet().toArray(y1);59System.out.println(Arrays.toString(y2));60if (! (y1 != y2 &&61y2.length == 2 &&62y2[0].getKey() == Country.FRENCH &&63y2[1].getKey() == Country.POLISH))64throw new AssertionError();65}66}676869