Path: blob/master/test/jdk/java/util/Map/ToArray.java
41149 views
/*1* Copyright (c) 2013, 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 800878526* @summary Ensure toArray() implementations return correct results.27* @author Mike Duigou28*/29import java.util.*;30import java.util.concurrent.ConcurrentHashMap;31import java.util.concurrent.ConcurrentSkipListMap;3233public class ToArray {3435/**36* Number of elements per map.37*/38private static final int TEST_SIZE = 5000;3940private static void realMain(String[] args) throws Throwable {41Map<Integer, Long>[] maps = (Map<Integer, Long>[]) new Map[]{42new HashMap<>(),43new Hashtable<>(),44new IdentityHashMap<>(),45new LinkedHashMap<>(),46new TreeMap<>(),47new WeakHashMap<>(),48new ConcurrentHashMap<>(),49new ConcurrentSkipListMap<>()50};5152// for each map type.53for (Map<Integer, Long> map : maps) {54try {55testMap(map);56} catch(Exception all) {57unexpected("Failed for " + map.getClass().getName(), all);58}59}60}6162private static final Integer[] KEYS = new Integer[TEST_SIZE];6364private static final Long[] VALUES = new Long[TEST_SIZE];6566static {67for (int each = 0; each < TEST_SIZE; each++) {68KEYS[each] = Integer.valueOf(each);69VALUES[each] = Long.valueOf(each + TEST_SIZE);70}71}727374private static void testMap(Map<Integer, Long> map) {75System.out.println("Testing " + map.getClass());76System.out.flush();7778// Fill the map79for (int each = 0; each < TEST_SIZE; each++) {80map.put(KEYS[each], VALUES[each]);81}8283// check the keys84Object[] keys = map.keySet().toArray();85Arrays.sort(keys);8687for(int each = 0; each < TEST_SIZE; each++) {88check( "unexpected key", keys[each] == KEYS[each]);89}9091// check the values92Object[] values = map.values().toArray();93Arrays.sort(values);9495for(int each = 0; each < TEST_SIZE; each++) {96check( "unexpected value", values[each] == VALUES[each]);97}9899// check the entries100Map.Entry<Integer,Long>[] entries = map.entrySet().toArray(new Map.Entry[TEST_SIZE]);101Arrays.sort( entries,new Comparator<Map.Entry<Integer,Long>>() {102public int compare(Map.Entry<Integer,Long> o1, Map.Entry<Integer,Long> o2) {103return o1.getKey().compareTo(o2.getKey());104}});105106for(int each = 0; each < TEST_SIZE; each++) {107check( "unexpected entry", entries[each].getKey() == KEYS[each] && entries[each].getValue() == VALUES[each]);108}109}110111//--------------------- Infrastructure ---------------------------112static volatile int passed = 0, failed = 0;113114static void pass() {115passed++;116}117118static void fail() {119failed++;120(new Error("Failure")).printStackTrace(System.err);121}122123static void fail(String msg) {124failed++;125(new Error("Failure: " + msg)).printStackTrace(System.err);126}127128static void abort() {129fail();130System.exit(1);131}132133static void abort(String msg) {134fail(msg);135System.exit(1);136}137138static void unexpected(String msg, Throwable t) {139System.err.println("Unexpected: " + msg);140unexpected(t);141}142143static void unexpected(Throwable t) {144failed++;145t.printStackTrace(System.err);146}147148static void check(boolean cond) {149if (cond) {150pass();151} else {152fail();153}154}155156static void check(String desc, boolean cond) {157if (cond) {158pass();159} else {160fail(desc);161}162}163164static void equal(Object x, Object y) {165if (Objects.equals(x, y)) {166pass();167} else {168fail(x + " not equal to " + y);169}170}171172public static void main(String[] args) throws Throwable {173Thread.currentThread().setName(ToArray.class.getName());174// Thread.currentThread().setPriority(Thread.MAX_PRIORITY);175try {176realMain(args);177} catch (Throwable t) {178unexpected(t);179}180181System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);182if (failed > 0) {183throw new Error("Some tests failed");184}185}186}187188189