Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Objects/BasicObjectsTest.java
41149 views
1
/*
2
* Copyright (c) 2009, 2015, 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 6797535 6889858 6891113 8013712 8011800 8014365
27
* @summary Basic tests for methods in java.util.Objects
28
* @author Joseph D. Darcy
29
*/
30
31
import java.util.*;
32
import java.util.function.*;
33
34
public class BasicObjectsTest {
35
public static void main(String... args) {
36
int errors = 0;
37
errors += testEquals();
38
errors += testDeepEquals();
39
errors += testHashCode();
40
errors += testHash();
41
errors += testToString();
42
errors += testToString2();
43
errors += testCompare();
44
errors += testRequireNonNull();
45
errors += testIsNull();
46
errors += testNonNull();
47
errors += testNonNullOf();
48
if (errors > 0 )
49
throw new RuntimeException();
50
}
51
52
private static int testEquals() {
53
int errors = 0;
54
Object[] values = {null, "42", 42};
55
for(int i = 0; i < values.length; i++)
56
for(int j = 0; j < values.length; j++) {
57
boolean expected = (i == j);
58
Object a = values[i];
59
Object b = values[j];
60
boolean result = Objects.equals(a, b);
61
if (result != expected) {
62
errors++;
63
System.err.printf("When equating %s to %s, got %b instead of %b%n.",
64
a, b, result, expected);
65
}
66
}
67
return errors;
68
}
69
70
private static int testDeepEquals() {
71
int errors = 0;
72
Object[] values = {null,
73
null, // Change to values later
74
new byte[] {(byte)1},
75
new short[] {(short)1},
76
new int[] {1},
77
new long[] {1L},
78
new char[] {(char)1},
79
new float[] {1.0f},
80
new double[]{1.0d},
81
new String[]{"one"}};
82
values[1] = values;
83
84
for(int i = 0; i < values.length; i++)
85
for(int j = 0; j < values.length; j++) {
86
boolean expected = (i == j);
87
Object a = values[i];
88
Object b = values[j];
89
boolean result = Objects.deepEquals(a, b);
90
if (result != expected) {
91
errors++;
92
System.err.printf("When equating %s to %s, got %b instead of %b%n.",
93
a, b, result, expected);
94
}
95
}
96
97
return errors;
98
}
99
100
private static int testHashCode() {
101
int errors = 0;
102
errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
103
String s = "42";
104
errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
105
return errors;
106
}
107
108
private static int testHash() {
109
int errors = 0;
110
111
Object[] data = new String[]{"perfect", "ham", "THC"};
112
113
errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1);
114
115
errors += (Objects.hash("perfect", "ham", "THC") ==
116
Arrays.hashCode(data)) ? 0 : 1;
117
118
return errors;
119
}
120
121
private static int testToString() {
122
int errors = 0;
123
errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
124
String s = "Some string";
125
errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
126
return errors;
127
}
128
129
private static int testToString2() {
130
int errors = 0;
131
String s = "not the default";
132
errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1;
133
errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1;
134
return errors;
135
}
136
137
private static int testCompare() {
138
int errors = 0;
139
String[] values = {"e. e. cummings", "zzz"};
140
String[] VALUES = {"E. E. Cummings", "ZZZ"};
141
errors += compareTest(null, null, 0);
142
for(int i = 0; i < values.length; i++) {
143
String a = values[i];
144
errors += compareTest(a, a, 0);
145
for(int j = 0; j < VALUES.length; j++) {
146
int expected = Integer.compare(i, j);
147
String b = VALUES[j];
148
errors += compareTest(a, b, expected);
149
}
150
}
151
return errors;
152
}
153
154
private static int compareTest(String a, String b, int expected) {
155
int errors = 0;
156
int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
157
if (Integer.signum(result) != Integer.signum(expected)) {
158
errors++;
159
System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
160
a, b, result, expected);
161
}
162
return errors;
163
}
164
165
private static int testRequireNonNull() {
166
int errors = 0;
167
168
final String RNN_1 = "1-arg requireNonNull";
169
final String RNN_2 = "2-arg requireNonNull";
170
final String RNN_3 = "Supplier requireNonNull";
171
172
Function<String, String> rnn1 = s -> Objects.requireNonNull(s);
173
Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers");
174
Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers");
175
176
errors += testRNN_NonNull(rnn1, RNN_1);
177
errors += testRNN_NonNull(rnn2, RNN_2);
178
errors += testRNN_NonNull(rnn3, RNN_3);
179
180
errors += testRNN_Null(rnn1, RNN_1, null);
181
errors += testRNN_Null(rnn2, RNN_2, "trousers");
182
errors += testRNN_Null(rnn3, RNN_3, "trousers");
183
return errors;
184
}
185
186
private static int testRNN_NonNull(Function<String, String> testFunc,
187
String testFuncName) {
188
int errors = 0;
189
try {
190
String s = testFunc.apply("pants");
191
if (s != "pants") {
192
System.err.printf(testFuncName + " failed to return its arg");
193
errors++;
194
}
195
} catch (NullPointerException e) {
196
System.err.printf(testFuncName + " threw unexpected NPE");
197
errors++;
198
}
199
return errors;
200
}
201
202
private static int testRNN_Null(Function<String, String> testFunc,
203
String testFuncName,
204
String expectedMessage) {
205
int errors = 0;
206
try {
207
String s = testFunc.apply(null);
208
System.err.printf(testFuncName + " failed to throw NPE");
209
errors++;
210
} catch (NullPointerException e) {
211
if (e.getMessage() != expectedMessage) {
212
System.err.printf(testFuncName + " threw NPE w/ bad detail msg");
213
errors++;
214
}
215
}
216
return errors;
217
}
218
219
private static int testIsNull() {
220
int errors = 0;
221
222
errors += Objects.isNull(null) ? 0 : 1;
223
errors += Objects.isNull(Objects.class) ? 1 : 0;
224
225
return errors;
226
}
227
228
private static int testNonNull() {
229
int errors = 0;
230
231
errors += Objects.nonNull(null) ? 1 : 0;
232
errors += Objects.nonNull(Objects.class) ? 0 : 1;
233
234
return errors;
235
}
236
237
private static int testNonNullOf() {
238
int errors = 0;
239
String defString = new String("default");
240
String nullString = null;
241
String nonNullString = "non-null";
242
243
// Confirm the compile time return type matches
244
String result = Objects.requireNonNullElse(nullString, defString);
245
errors += (result == defString) ? 0 : 1;
246
errors += (Objects.requireNonNullElse(nonNullString, defString) == nonNullString) ? 0 : 1;
247
errors += (Objects.requireNonNullElse(nonNullString, null) == nonNullString) ? 0 : 1;
248
try {
249
Objects.requireNonNullElse(null, null);
250
errors += 1;
251
} catch (NullPointerException npe) {
252
// expected
253
errors += npe.getMessage().equals("defaultObj") ? 0 : 1;
254
}
255
256
257
// Test requireNonNullElseGet with a supplier
258
errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
259
errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
260
errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
261
262
try {
263
Objects.requireNonNullElseGet(null, () -> null);
264
errors += 1;
265
} catch (NullPointerException npe) {
266
// expected
267
errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;
268
}
269
try { // supplier is null
270
Objects.requireNonNullElseGet(null, null);
271
errors += 1;
272
} catch (NullPointerException npe) {
273
// expected
274
errors += npe.getMessage().equals("supplier") ? 0 : 1;
275
}
276
return errors;
277
}
278
}
279
280