Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Map/ToArray.java
41149 views
1
/*
2
* Copyright (c) 2013, 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
24
/*
25
* @test
26
* @bug 8008785
27
* @summary Ensure toArray() implementations return correct results.
28
* @author Mike Duigou
29
*/
30
import java.util.*;
31
import java.util.concurrent.ConcurrentHashMap;
32
import java.util.concurrent.ConcurrentSkipListMap;
33
34
public class ToArray {
35
36
/**
37
* Number of elements per map.
38
*/
39
private static final int TEST_SIZE = 5000;
40
41
private static void realMain(String[] args) throws Throwable {
42
Map<Integer, Long>[] maps = (Map<Integer, Long>[]) new Map[]{
43
new HashMap<>(),
44
new Hashtable<>(),
45
new IdentityHashMap<>(),
46
new LinkedHashMap<>(),
47
new TreeMap<>(),
48
new WeakHashMap<>(),
49
new ConcurrentHashMap<>(),
50
new ConcurrentSkipListMap<>()
51
};
52
53
// for each map type.
54
for (Map<Integer, Long> map : maps) {
55
try {
56
testMap(map);
57
} catch(Exception all) {
58
unexpected("Failed for " + map.getClass().getName(), all);
59
}
60
}
61
}
62
63
private static final Integer[] KEYS = new Integer[TEST_SIZE];
64
65
private static final Long[] VALUES = new Long[TEST_SIZE];
66
67
static {
68
for (int each = 0; each < TEST_SIZE; each++) {
69
KEYS[each] = Integer.valueOf(each);
70
VALUES[each] = Long.valueOf(each + TEST_SIZE);
71
}
72
}
73
74
75
private static void testMap(Map<Integer, Long> map) {
76
System.out.println("Testing " + map.getClass());
77
System.out.flush();
78
79
// Fill the map
80
for (int each = 0; each < TEST_SIZE; each++) {
81
map.put(KEYS[each], VALUES[each]);
82
}
83
84
// check the keys
85
Object[] keys = map.keySet().toArray();
86
Arrays.sort(keys);
87
88
for(int each = 0; each < TEST_SIZE; each++) {
89
check( "unexpected key", keys[each] == KEYS[each]);
90
}
91
92
// check the values
93
Object[] values = map.values().toArray();
94
Arrays.sort(values);
95
96
for(int each = 0; each < TEST_SIZE; each++) {
97
check( "unexpected value", values[each] == VALUES[each]);
98
}
99
100
// check the entries
101
Map.Entry<Integer,Long>[] entries = map.entrySet().toArray(new Map.Entry[TEST_SIZE]);
102
Arrays.sort( entries,new Comparator<Map.Entry<Integer,Long>>() {
103
public int compare(Map.Entry<Integer,Long> o1, Map.Entry<Integer,Long> o2) {
104
return o1.getKey().compareTo(o2.getKey());
105
}});
106
107
for(int each = 0; each < TEST_SIZE; each++) {
108
check( "unexpected entry", entries[each].getKey() == KEYS[each] && entries[each].getValue() == VALUES[each]);
109
}
110
}
111
112
//--------------------- Infrastructure ---------------------------
113
static volatile int passed = 0, failed = 0;
114
115
static void pass() {
116
passed++;
117
}
118
119
static void fail() {
120
failed++;
121
(new Error("Failure")).printStackTrace(System.err);
122
}
123
124
static void fail(String msg) {
125
failed++;
126
(new Error("Failure: " + msg)).printStackTrace(System.err);
127
}
128
129
static void abort() {
130
fail();
131
System.exit(1);
132
}
133
134
static void abort(String msg) {
135
fail(msg);
136
System.exit(1);
137
}
138
139
static void unexpected(String msg, Throwable t) {
140
System.err.println("Unexpected: " + msg);
141
unexpected(t);
142
}
143
144
static void unexpected(Throwable t) {
145
failed++;
146
t.printStackTrace(System.err);
147
}
148
149
static void check(boolean cond) {
150
if (cond) {
151
pass();
152
} else {
153
fail();
154
}
155
}
156
157
static void check(String desc, boolean cond) {
158
if (cond) {
159
pass();
160
} else {
161
fail(desc);
162
}
163
}
164
165
static void equal(Object x, Object y) {
166
if (Objects.equals(x, y)) {
167
pass();
168
} else {
169
fail(x + " not equal to " + y);
170
}
171
}
172
173
public static void main(String[] args) throws Throwable {
174
Thread.currentThread().setName(ToArray.class.getName());
175
// Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
176
try {
177
realMain(args);
178
} catch (Throwable t) {
179
unexpected(t);
180
}
181
182
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
183
if (failed > 0) {
184
throw new Error("Some tests failed");
185
}
186
}
187
}
188
189