Path: blob/master/test/jdk/java/util/HashMap/ToArray.java
41149 views
/*1* Copyright (c) 2019, 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*/2223import java.util.ArrayList;24import java.util.Arrays;25import java.util.Collection;26import java.util.HashMap;27import java.util.HashSet;28import java.util.LinkedHashMap;29import java.util.LinkedHashSet;30import java.util.List;31import java.util.Map;32import java.util.stream.LongStream;3334/*35* @test36* @summary HashMap.toArray() behavior tests37* @author tvaleev38*/39public class ToArray {40public static void main(String[] args) {41checkMap(false);42checkMap(true);43checkSet(false);44checkSet(true);45}4647private static <T extends Comparable<T>> void checkToArray(String message, T[] expected, Collection<T> collection,48boolean ignoreOrder) {49if (ignoreOrder) {50Arrays.sort(expected);51}52checkToObjectArray(message, expected, collection, ignoreOrder);53checkToTypedArray(message, expected, Arrays.copyOf(expected, 0), collection, ignoreOrder);54checkToTypedArray(message, expected, expected.clone(), collection, ignoreOrder);55if (expected.length > 0) {56T[] biggerArray = Arrays.copyOf(expected, expected.length * 2);57System.arraycopy(expected, 0, biggerArray, expected.length, expected.length);58checkToTypedArray(message, expected, biggerArray, collection, ignoreOrder);59}60}6162private static <T extends Comparable<T>> void checkToTypedArray(String message, T[] expected, T[] inputArray,63Collection<T> collection, boolean ignoreOrder) {64T[] res = collection.toArray(inputArray);65if (expected.length <= inputArray.length && res != inputArray) {66throw new AssertionError(message + ": not the same array returned");67}68if (res.getClass() != expected.getClass()) {69throw new AssertionError(message + ": wrong class returned: " + res.getClass());70}71if (res.length < expected.length) {72throw new AssertionError(message + ": length is smaller than expected: " + res.length + " < " + expected.length);73}74if (ignoreOrder) {75Arrays.sort(res, 0, Math.min(res.length, expected.length));76}77if (inputArray.length <= expected.length) {78if (!Arrays.equals(res, expected)) {79throw new AssertionError(message + ": not equal: " + Arrays.toString(expected) + " != " +80Arrays.toString(res));81}82} else {83int mismatch = Arrays.mismatch(expected, res);84if (mismatch != expected.length) {85throw new AssertionError(message + ": mismatch at " + mismatch);86}87if (res[expected.length] != null) {88throw new AssertionError(message + ": no null at position " + expected.length);89}90// The tail of bigger array after expected.length position must be untouched91mismatch = Arrays92.mismatch(expected, 1, expected.length, res, expected.length + 1, res.length);93if (mismatch != -1) {94throw new AssertionError(message + ": mismatch at " + mismatch);95}96}97}9899private static <T extends Comparable<T>> void checkToObjectArray(String message, T[] expected,100Collection<T> collection, boolean ignoreOrder) {101Object[] objects = collection.toArray();102if (objects.getClass() != Object[].class) {103throw new AssertionError(message + ": wrong class returned: " + objects.getClass());104}105if (ignoreOrder) {106Arrays.sort(objects);107}108int mismatch = Arrays.mismatch(expected, objects);109if (mismatch != -1) {110throw new AssertionError(message + ": mismatch at " + mismatch);111}112}113114private static void checkMap(boolean ordered) {115Map<String, String> map = ordered ? new LinkedHashMap<>() : new HashMap<>();116checkToArray("Empty-keys", new String[0], map.keySet(), !ordered);117checkToArray("Empty-values", new String[0], map.values(), !ordered);118119List<String> keys = new ArrayList<>();120List<String> values = new ArrayList<>();121for (int i = 0; i < 100; i++) {122keys.add(String.valueOf(i));123values.add(String.valueOf(i * 2));124map.put(String.valueOf(i), String.valueOf(i * 2));125checkToArray(i + "-keys", keys.toArray(new String[0]), map.keySet(), !ordered);126checkToArray(i + "-values", values.toArray(new String[0]), map.values(), !ordered);127}128map.clear();129checkToArray("Empty-keys", new String[0], map.keySet(), !ordered);130checkToArray("Empty-values", new String[0], map.values(), !ordered);131}132133private static void checkSet(boolean ordered) {134Collection<String> set = ordered ? new LinkedHashSet<>() : new HashSet<>();135checkToArray("Empty", new String[0], set, !ordered);136set.add("foo");137checkToArray("One", new String[]{"foo"}, set, !ordered);138set.add("bar");139checkToArray("Two", new String[]{"foo", "bar"}, set, !ordered);140141Collection<Long> longSet = ordered ? new LinkedHashSet<>() : new HashSet<>();142for (int x = 0; x < 100; x++) {143longSet.add((long) x);144}145checkToArray("100", LongStream.range(0, 100).boxed().toArray(Long[]::new), longSet, !ordered);146longSet.clear();147checkToArray("After clear", new Long[0], longSet, !ordered);148for (int x = 0; x < 100; x++) {149longSet.add(((long) x) | (((long) x) << 32));150}151checkToArray("Collisions", LongStream.range(0, 100).mapToObj(x -> x | (x << 32))152.toArray(Long[]::new), longSet, !ordered);153}154}155156157