Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PriorityQueue/NoNulls.java
38812 views
1
/*
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 it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation.
7
*
8
* This code is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11
* version 2 for more details (a copy is included in the LICENSE file that
12
* accompanied this code).
13
*
14
* You should have received a copy of the GNU General Public License version
15
* 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 USA
19
* or visit www.oracle.com if you need additional information or have any
20
* questions.
21
*/
22
23
/*
24
* This file is available under and governed by the GNU General Public
25
* License version 2 only, as published by the Free Software Foundation.
26
* However, the following notice accompanied the original version of this
27
* file:
28
*
29
* Written by Martin Buchholz with assistance from members of JCP JSR-166
30
* Expert Group and released to the public domain, as explained at
31
* http://creativecommons.org/publicdomain/zero/1.0/
32
*/
33
34
/*
35
* @test
36
* @bug 6950540
37
* @summary Attempt to add a null throws NullPointerException
38
*/
39
40
import java.util.ArrayList;
41
import java.util.Arrays;
42
import java.util.Comparator;
43
import java.util.Collection;
44
import java.util.Collections;
45
import java.util.PriorityQueue;
46
import java.util.SortedSet;
47
import java.util.TreeSet;
48
import java.util.concurrent.ArrayBlockingQueue;
49
import java.util.concurrent.LinkedBlockingDeque;
50
import java.util.concurrent.LinkedBlockingQueue;
51
import java.util.concurrent.PriorityBlockingQueue;
52
53
public class NoNulls {
54
void test(String[] args) throws Throwable {
55
final Comparator<String> nullTolerantComparator
56
= new Comparator<String>() {
57
public int compare(String x, String y) {
58
return (x == null ? -1 :
59
y == null ? 1 :
60
x.compareTo(y));
61
}};
62
63
final SortedSet<String> nullSortedSet
64
= new TreeSet<>(nullTolerantComparator);
65
nullSortedSet.add(null);
66
67
final PriorityQueue<String> nullPriorityQueue
68
= new PriorityQueue<String>() {
69
public Object[] toArray() { return new Object[] { null };}};
70
71
final Collection<String> nullCollection = new ArrayList<>();
72
nullCollection.add(null);
73
74
THROWS(NullPointerException.class,
75
new F() { void f() {
76
new PriorityQueue<String>(nullCollection);
77
}},
78
new F() { void f() {
79
new PriorityBlockingQueue<String>(nullCollection);
80
}},
81
new F() { void f() {
82
new ArrayBlockingQueue<String>(10, false, nullCollection);
83
}},
84
new F() { void f() {
85
new ArrayBlockingQueue<String>(10, true, nullCollection);
86
}},
87
new F() { void f() {
88
new LinkedBlockingQueue<String>(nullCollection);
89
}},
90
new F() { void f() {
91
new LinkedBlockingDeque<String>(nullCollection);
92
}},
93
94
new F() { void f() {
95
new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
96
}},
97
new F() { void f() {
98
new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
99
}},
100
101
new F() { void f() {
102
new PriorityQueue<String>(nullSortedSet);
103
}},
104
new F() { void f() {
105
new PriorityBlockingQueue<String>(nullSortedSet);
106
}},
107
108
new F() { void f() {
109
new PriorityQueue<String>((Collection<String>) nullSortedSet);
110
}},
111
new F() { void f() {
112
new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
113
}},
114
115
new F() { void f() {
116
new PriorityQueue<String>(nullPriorityQueue);
117
}},
118
new F() { void f() {
119
new PriorityBlockingQueue<String>(nullPriorityQueue);
120
}},
121
122
new F() { void f() {
123
new PriorityQueue<String>().add(null);
124
}},
125
new F() { void f() {
126
new PriorityBlockingQueue<String>().add(null);
127
}},
128
new F() { void f() {
129
new ArrayBlockingQueue<String>(10, false).add(null);
130
}},
131
new F() { void f() {
132
new ArrayBlockingQueue<String>(10, true).add(null);
133
}},
134
new F() { void f() {
135
new LinkedBlockingQueue<String>().add(null);
136
}},
137
new F() { void f() {
138
new LinkedBlockingDeque<String>().add(null);
139
}},
140
141
new F() { void f() {
142
new PriorityQueue<String>().offer(null);
143
}},
144
new F() { void f() {
145
new PriorityBlockingQueue<String>().offer(null);
146
}});
147
148
nullSortedSet.add("foo");
149
nullCollection.add("foo");
150
THROWS(NullPointerException.class,
151
new F() { void f() {
152
new PriorityQueue<String>(nullCollection);
153
}},
154
new F() { void f() {
155
new PriorityBlockingQueue<String>(nullCollection);
156
}},
157
158
new F() { void f() {
159
new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
160
}},
161
new F() { void f() {
162
new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
163
}},
164
165
new F() { void f() {
166
new PriorityQueue<String>(nullSortedSet);
167
}},
168
new F() { void f() {
169
new PriorityBlockingQueue<String>(nullSortedSet);
170
}},
171
172
new F() { void f() {
173
new PriorityQueue<String>((Collection<String>) nullSortedSet);
174
}},
175
new F() { void f() {
176
new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
177
}});
178
179
}
180
181
//--------------------- Infrastructure ---------------------------
182
volatile int passed = 0, failed = 0;
183
void pass() {passed++;}
184
void fail() {failed++; Thread.dumpStack();}
185
void fail(String msg) {System.err.println(msg); fail();}
186
void unexpected(Throwable t) {failed++; t.printStackTrace();}
187
void check(boolean cond) {if (cond) pass(); else fail();}
188
void equal(Object x, Object y) {
189
if (x == null ? y == null : x.equals(y)) pass();
190
else fail(x + " not equal to " + y);}
191
public static void main(String[] args) throws Throwable {
192
new NoNulls().instanceMain(args);}
193
public void instanceMain(String[] args) throws Throwable {
194
try {test(args);} catch (Throwable t) {unexpected(t);}
195
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
196
if (failed > 0) throw new AssertionError("Some tests failed");}
197
abstract class F {abstract void f() throws Throwable;}
198
void THROWS(Class<? extends Throwable> k, F... fs) {
199
for (F f : fs)
200
try {f.f(); fail("Expected " + k.getName() + " not thrown");}
201
catch (Throwable t) {
202
if (k.isAssignableFrom(t.getClass())) pass();
203
else unexpected(t);}}
204
}
205
206