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