1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.LongStream;
/*
* @test
* @summary HashMap.toArray() behavior tests
* @author tvaleev
*/
public class ToArray {
public static void main(String[] args) {
checkMap(false);
checkMap(true);
checkSet(false);
checkSet(true);
}
private static <T extends Comparable<T>> void checkToArray(String message, T[] expected, Collection<T> collection,
boolean ignoreOrder) {
if (ignoreOrder) {
Arrays.sort(expected);
}
checkToObjectArray(message, expected, collection, ignoreOrder);
checkToTypedArray(message, expected, Arrays.copyOf(expected, 0), collection, ignoreOrder);
checkToTypedArray(message, expected, expected.clone(), collection, ignoreOrder);
if (expected.length > 0) {
T[] biggerArray = Arrays.copyOf(expected, expected.length * 2);
System.arraycopy(expected, 0, biggerArray, expected.length, expected.length);
checkToTypedArray(message, expected, biggerArray, collection, ignoreOrder);
}
}
private static <T extends Comparable<T>> void checkToTypedArray(String message, T[] expected, T[] inputArray,
Collection<T> collection, boolean ignoreOrder) {
T[] res = collection.toArray(inputArray);
if (expected.length <= inputArray.length && res != inputArray) {
throw new AssertionError(message + ": not the same array returned");
}
if (res.getClass() != expected.getClass()) {
throw new AssertionError(message + ": wrong class returned: " + res.getClass());
}
if (res.length < expected.length) {
throw new AssertionError(message + ": length is smaller than expected: " + res.length + " < " + expected.length);
}
if (ignoreOrder) {
Arrays.sort(res, 0, Math.min(res.length, expected.length));
}
if (inputArray.length <= expected.length) {
if (!Arrays.equals(res, expected)) {
throw new AssertionError(message + ": not equal: " + Arrays.toString(expected) + " != " +
Arrays.toString(res));
}
} else {
int mismatch = Arrays.mismatch(expected, res);
if (mismatch != expected.length) {
throw new AssertionError(message + ": mismatch at " + mismatch);
}
if (res[expected.length] != null) {
throw new AssertionError(message + ": no null at position " + expected.length);
}
// The tail of bigger array after expected.length position must be untouched
mismatch = Arrays
.mismatch(expected, 1, expected.length, res, expected.length + 1, res.length);
if (mismatch != -1) {
throw new AssertionError(message + ": mismatch at " + mismatch);
}
}
}
private static <T extends Comparable<T>> void checkToObjectArray(String message, T[] expected,
Collection<T> collection, boolean ignoreOrder) {
Object[] objects = collection.toArray();
if (objects.getClass() != Object[].class) {
throw new AssertionError(message + ": wrong class returned: " + objects.getClass());
}
if (ignoreOrder) {
Arrays.sort(objects);
}
int mismatch = Arrays.mismatch(expected, objects);
if (mismatch != -1) {
throw new AssertionError(message + ": mismatch at " + mismatch);
}
}
private static void checkMap(boolean ordered) {
Map<String, String> map = ordered ? new LinkedHashMap<>() : new HashMap<>();
checkToArray("Empty-keys", new String[0], map.keySet(), !ordered);
checkToArray("Empty-values", new String[0], map.values(), !ordered);
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
for (int i = 0; i < 100; i++) {
keys.add(String.valueOf(i));
values.add(String.valueOf(i * 2));
map.put(String.valueOf(i), String.valueOf(i * 2));
checkToArray(i + "-keys", keys.toArray(new String[0]), map.keySet(), !ordered);
checkToArray(i + "-values", values.toArray(new String[0]), map.values(), !ordered);
}
map.clear();
checkToArray("Empty-keys", new String[0], map.keySet(), !ordered);
checkToArray("Empty-values", new String[0], map.values(), !ordered);
}
private static void checkSet(boolean ordered) {
Collection<String> set = ordered ? new LinkedHashSet<>() : new HashSet<>();
checkToArray("Empty", new String[0], set, !ordered);
set.add("foo");
checkToArray("One", new String[]{"foo"}, set, !ordered);
set.add("bar");
checkToArray("Two", new String[]{"foo", "bar"}, set, !ordered);
Collection<Long> longSet = ordered ? new LinkedHashSet<>() : new HashSet<>();
for (int x = 0; x < 100; x++) {
longSet.add((long) x);
}
checkToArray("100", LongStream.range(0, 100).boxed().toArray(Long[]::new), longSet, !ordered);
longSet.clear();
checkToArray("After clear", new Long[0], longSet, !ordered);
for (int x = 0; x < 100; x++) {
longSet.add(((long) x) | (((long) x) << 32));
}
checkToArray("Collisions", LongStream.range(0, 100).mapToObj(x -> x | (x << 32))
.toArray(Long[]::new), longSet, !ordered);
}
}
|