Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collections/EmptyNavigableMap.java
41149 views
1
/*
2
* Copyright (c) 2011, 2017, 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 4533691 7129185
27
* @summary Unit test for Collections.emptyNavigableMap
28
* @run testng EmptyNavigableMap
29
*/
30
31
import org.testng.Assert;
32
import org.testng.Assert.ThrowingRunnable;
33
import org.testng.annotations.DataProvider;
34
import org.testng.annotations.Test;
35
36
import java.math.BigInteger;
37
import java.util.Arrays;
38
import java.util.Collection;
39
import java.util.Collections;
40
import java.util.Comparator;
41
import java.util.Iterator;
42
import java.util.NavigableMap;
43
import java.util.SortedMap;
44
import java.util.TreeMap;
45
46
import static org.testng.Assert.assertFalse;
47
import static org.testng.Assert.assertTrue;
48
49
public class EmptyNavigableMap {
50
51
public static <T> void assertInstance(T actual, Class<? extends T> expected) {
52
assertInstance(actual, expected, null);
53
}
54
55
public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
56
assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
57
+ " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
58
}
59
60
public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) {
61
assertInstance(obj, NavigableMap.class);
62
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0));
63
}
64
65
public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) {
66
assertInstance(obj, NavigableMap.class, message);
67
assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0),
68
((null != message) ? message : "") + " Not empty. ");
69
}
70
71
private <T extends Throwable> void assertThrows(Class<T> throwableClass,
72
ThrowingRunnable runnable,
73
String message) {
74
try {
75
Assert.assertThrows(throwableClass, runnable);
76
} catch (AssertionError e) {
77
throw new AssertionError(String.format("%s%n%s",
78
((null != message) ? message : ""), e.getMessage()), e);
79
}
80
}
81
82
private void assertThrowsCCE(ThrowingRunnable r, String s) {
83
assertThrows(ClassCastException.class, r, s);
84
}
85
86
private void assertThrowsNPE(ThrowingRunnable r, String s) {
87
assertThrows(NullPointerException.class, r, s);
88
}
89
90
private void assertThrowsIAE(ThrowingRunnable r, String s) {
91
assertThrows(IllegalArgumentException.class, r, s);
92
}
93
94
public static final boolean isDescending(SortedMap<?,?> set) {
95
if (null == set.comparator()) {
96
// natural order
97
return false;
98
}
99
100
if (Collections.reverseOrder() == set.comparator()) {
101
// reverse natural order.
102
return true;
103
}
104
105
if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
106
// it's a Collections.reverseOrder(Comparator).
107
return true;
108
}
109
110
throw new IllegalStateException("can't determine ordering for " + set);
111
}
112
113
/**
114
* Tests that the comparator is {@code null}.
115
*/
116
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
117
public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) {
118
Comparator comparator = navigableMap.comparator();
119
120
assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
121
}
122
123
/**
124
* Tests that contains requires Comparable
125
*/
126
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
127
public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {
128
assertThrowsCCE(
129
() -> navigableMap.containsKey(new Object()),
130
description + ": Comparable should be required");
131
}
132
133
/**
134
* Tests that the contains method returns {@code false}.
135
*/
136
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
137
public void testContains(String description, NavigableMap<?,?> navigableMap) {
138
assertFalse(navigableMap.containsKey(new Integer(1)),
139
description + ": Should not contain any elements.");
140
assertFalse(navigableMap.containsValue(new Integer(1)),
141
description + ": Should not contain any elements.");
142
}
143
144
/**
145
* Tests that the containsAll method returns {@code false}.
146
*/
147
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
148
public void testContainsAll(String description, NavigableMap<?,?> navigableMap) {
149
TreeMap treeMap = new TreeMap();
150
treeMap.put("1", 1);
151
treeMap.put("2", 2);
152
treeMap.put("3", 3);
153
154
assertFalse(navigableMap.equals(treeMap), "Should not contain any elements.");
155
}
156
157
/**
158
* Tests that the iterator is empty.
159
*/
160
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
161
public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) {
162
assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty.");
163
assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty.");
164
assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty.");
165
}
166
167
/**
168
* Tests that the set is empty.
169
*/
170
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
171
public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) {
172
assertTrue(navigableMap.isEmpty(), "The set is not empty.");
173
}
174
175
/**
176
* Tests the headMap() method.
177
*/
178
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
179
public void testHeadMap(String description, NavigableMap navigableMap) {
180
assertThrowsNPE(
181
() -> { NavigableMap ss = navigableMap.headMap(null, false); },
182
description + ": Must throw NullPointerException for null element");
183
184
assertThrowsCCE(
185
() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },
186
description + ": Must throw ClassCastException for non-Comparable element");
187
188
NavigableMap ss = navigableMap.headMap("1", false);
189
190
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
191
}
192
193
/**
194
* Tests that the size is 0.
195
*/
196
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
197
public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) {
198
assertTrue(0 == navigableMap.size(), "The size of the set is not 0.");
199
}
200
201
/**
202
* Tests the subMap() method.
203
*/
204
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
205
public void testSubMap(String description, NavigableMap navigableMap) {
206
assertThrowsNPE(
207
() -> {
208
SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);
209
},
210
description + ": Must throw NullPointerException for null element");
211
212
assertThrowsNPE(
213
() -> {
214
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);
215
},
216
description + ": Must throw NullPointerException for null element");
217
218
assertThrowsNPE(
219
() -> {
220
SortedMap ss = navigableMap.subMap(null, null);
221
},
222
description + ": Must throw NullPointerException for null element");
223
224
Object obj1 = new Object();
225
Object obj2 = new Object();
226
227
assertThrowsCCE(
228
() -> {
229
SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN);
230
},
231
description + ": Must throw ClassCastException for parameter which is not Comparable.");
232
233
assertThrowsCCE(
234
() -> {
235
SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2);
236
},
237
description + ": Must throw ClassCastException for parameter which is not Comparable.");
238
239
assertThrowsCCE(
240
() -> {
241
SortedMap ss = navigableMap.subMap(obj1, obj2);
242
},
243
description + ": Must throw ClassCastException for parameter which is not Comparable.");
244
245
// minimal range
246
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false);
247
navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true);
248
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false);
249
navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true);
250
251
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
252
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
253
254
assertThrowsIAE(
255
() -> navigableMap.subMap(last, true, first, false),
256
description + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
257
258
navigableMap.subMap(first, true, last, false);
259
}
260
261
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
262
public void testSubMapRanges(String description, NavigableMap navigableMap) {
263
Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
264
Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
265
266
NavigableMap subMap = navigableMap.subMap(first, true, last, true);
267
268
// same subset
269
subMap.subMap(first, true, last, true);
270
271
// slightly smaller
272
NavigableMap ns = subMap.subMap(first, false, last, false);
273
// slight expansion
274
assertThrowsIAE(
275
() -> ns.subMap(first, true, last, true),
276
description + ": Expansion should not be allowed");
277
278
// much smaller
279
subMap.subMap(first, false, BigInteger.ONE, false);
280
}
281
282
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
283
public void testheadMapRanges(String description, NavigableMap navigableMap) {
284
NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);
285
286
// same subset
287
subMap.headMap(BigInteger.ONE, true);
288
289
// slightly smaller
290
NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
291
292
// slight expansion
293
assertThrowsIAE(
294
() -> ns.headMap(BigInteger.ONE, true),
295
description + ": Expansion should not be allowed");
296
297
// much smaller
298
subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
299
}
300
301
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
302
public void testTailMapRanges(String description, NavigableMap navigableMap) {
303
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
304
305
// same subset
306
subMap.tailMap(BigInteger.ONE, true);
307
308
// slightly smaller
309
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
310
311
// slight expansion
312
assertThrowsIAE(
313
() -> ns.tailMap(BigInteger.ONE, true),
314
description + ": Expansion should not be allowed");
315
316
// much smaller
317
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
318
}
319
320
/**
321
* Tests the tailMap() method.
322
*/
323
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
324
public void testTailMap(String description, NavigableMap navigableMap) {
325
assertThrowsNPE(
326
() -> navigableMap.tailMap(null),
327
description + ": Must throw NullPointerException for null element");
328
329
assertThrowsCCE(
330
() -> navigableMap.tailMap(new Object()),
331
description);
332
333
NavigableMap ss = navigableMap.tailMap("1", true);
334
335
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
336
}
337
338
@DataProvider(name = "NavigableMap<?,?>", parallel = true)
339
public static Iterator<Object[]> navigableMapsProvider() {
340
return makeNavigableMaps().iterator();
341
}
342
343
public static Collection<Object[]> makeNavigableMaps() {
344
return Arrays.asList(
345
new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())},
346
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())},
347
new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())},
348
new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()},
349
new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()},
350
new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()}
351
);
352
}
353
}
354
355